Wednesday, 14 October 2015

Spock Framework with spring , spring boot , hsql in memory db

To Start with disclaimer , I am new to Groovy so the syntax I used in this might not be the best practice but that shouldn't be problem as that is the not the problem statement.

Sample Project


The sample project is available on git.

Code is self explanatory . Basically I have tried to test a repository ( inserts data in in memory hsql db which is injected through Spring boot )  and stub a LDAP service which is normally called as part of authentication.

Version of frameworks used 


The best way to explain this section is to specify the pom I used



Integration with spring boot


It was smooth . The only hurdle was we can't use @SpringApplicationConfiguration  but need to use @ContextConfiguration directly as shown in the below code . This is the groovy  specification which is used for testing.

I have tried to cover


  • Simple Test case of null check
  • Data Driven Test (DDT)
  • And usage of mock 
A small hack which was needed to be done in case of DDT specially when using DataTables . It didn't allow for cases in which exceptions do occur . Its again self explanatory in the code.  



Conclusion


I personally found using spock with spring seamless . It was not only easy but due to its groovy influence it was much easier to write .

It incorporates the ease of mockito and brings in new features specially when it comes to mocking like the use of regular expression .

Loved the verbose nature of the failure scenarios which was pretty self explanatory.

Reference



At the end of my work I also found another really nice ebook :




Wednesday, 4 March 2015

SUBSCRIBE ISSUE WITH GEMFIRE (CONNECTION CONNECTS AND DISCONNECTS )




                                           SUBSCRIBE ISSUE WITH GEMFIRE (CONNECTION CONNECTS                                                     AND DISCONNECTS )


Lately this evening I got stuck with an issue with one of or Gemfire Clients not able to subscribe to our gemfire cache.

We are using CQ Services of Gemfire to give our external client to query our cache .

Now one of our client informed us that post their overnight release they are not been able to  get any updates from our gemfire cache.

On looking deep into the logs of our Gemfire it seemed like their system was connecting and disconnecting again and again , this process of connection creation and disconnection was continuously happening but there weren't any more errors on our side.

With having no clue on our side , we tried to replicate the scenario in our lower ENV's

Here we asked the same client to connect their lower ENV to one of our lower ENV , increase their logs to level of trace , increase Gemfire logs to 'all' .

Once they connected and our cache attempted to send them the data as part of subscription notification , we were able to replicate the problem of connecting and disconnecting system .

But as the logs were at their deepest level , we noticed an exception on their side .

Now this exception was due to their own wrong code ( it was a business level exception ) , which they weren't catching properly.

Now once they fixed this issue , and reconnected to our server , they were able to get updates from our server and the issue of rigorous connection-disconnection also stopped .

So what I Assume  : If a client level application exception occurs , gemfire subscription process stops the subscription and tries to connect again and again try to publish the same message . This was causing the connecting-disconnecting problem to appear .

So if someone is too facing the same problem , they can increase the log levels on client side and analyze that log for more info. 

Wednesday, 18 February 2015

View Contents of keystore Java without knowing keystore password - stolen idea!!

                                 
                                                            Contents of Key Store


This evening at work , my jenkins  release stopped working .

Reason : It was building properly but was not able to release to artifactory . Upon investigation and beating my head around , found that the certificate used for the connection b/w jenkins and artifactory had expired.

So I had to import new certificate to my JDK cacerts . But before doing it I wanted to view the contents of my cacerts.

Viewing was easy ,

                          keytool -list -keystore <keystore-location>

but then it asked me for the keystore password and as most of the times i didnt remember it.

So falling back to the basic option : GOOGLE

3rd link pointed me to a wonderful code at

                https://gist.github.com/zach-klippenstein/4631307

The code helped me to create a new keystore with a new password and after this I ran the keytool code again and woooosh everything was there.


Little bit of hack but helps in dire needs.


Tuesday, 5 November 2013

Dynamic Evaluation of expression in java



                                 Dynamically Evaluate expression in java


I am sure many people had already stumbled on this and recently I too became one of them . I was working on an assignment which needed some run time evaluation of expression. These expressions were not very complex of nature. They were pretty much like assign a variable with value 0 if that variable was null

something on the lines of :   abc=abc==null?0:abc

or something like  abc=abc=='D'?abc+'B':abc+'C'

now there was the way to build a kind of parser which parses these equations and provide the output , but every new requirement might need a enhancement to the parser.

Then going back to the basics I started googling around for solutions and stumbled across

http://docs.oracle.com/javase/6/docs/technotes/guides/scripting/programmer_guide/

This is basically a script engine (what the browsers internally might be using ) to do evaluation of javascript method like Eval.

And to my surprise it works pretty well . I understand there are pretty limitations to this engine ( as I read in of the blogs that it has issues with some regular expressions , not exactly sure which case was he mentioning ) , but it satisfies all the basic expressions.

This allowed me to store these expressions in some external properties files and invoke it dynamically and calculate the expressions.

May be worth trying if someone is looking for some utility under java.

Sunday, 27 October 2013

Flex-Java SWFLoader issue


                                    SWF Loader issue with  clustered env


Last week i got stuck in a strange issue:

We had a application (which under normal development work ) was being built on a single machine system  with no clustered env.

It had a Web application built using Flex 4.6 framework integrated with spring and blaze-ds to contact back end layer built in java. This looked good all on dev system but started troubling on integration system which mimicked more like production having multiple boxes and each box having multiple servers clustered together.

In the application , we had complex situation in which a swf application was loading another swf application within a popup (when I say swf application , it was APPLICATION not the MODULES). So when the primary application tried to open the secondary swf within for the first time , there was no response from the server . The secondary swf bytes were fetched but nothing  was shown on the screen. But subsequent hits to open the pop up with secondary swf resulted in success . So the question was the reason of first time failure.

On deeper analysis showed that , when the first time secondary swf was loaded from the server , there was no session ID passed as cookies to the load balancer , but still swf bytes were loaded. On the first invocation of blaze ds request within the secondary SWF , resulted in the create of session id . due to this the first invocation failed but subsequent worked well. This phenomenon was not noticed in single server env (like local dev systems) but were only found in clustered system with load balancer in place.

So the solution was to somehow create the session id even before first request of blaze ds was made .

The easiest way to do this was make a dummy HTTP request from the secondary swf immediate after its loaded (basically just after creation complete ) event. As this dummy HTTP request to any servlet on server would surely create a session ( until or unless there is request.getSession() method in servlet ).

So any subsequent request of balze ds would pick this session id and continue its work as normal.

This worked for us , hope it helps some one else too.






Thursday, 21 March 2013

Real To Java for fun


I had this weird observation yesterday thought of sharing with you:

I was on the way from my home station (Journal Square) to office (33d Street NY) in train  and as always I had taken the  corner most seat of the wagon and was busy reading some newspaper article . After couple of stops I realized a super hot blonde girl with the minimal of formal outfit you could think of  was sitting opposite to me . As most of us do , I too tried avoiding a stare towards her but again as always couldn’t avoid and somehow or the other kept on looking at her ( for some reason , there was this James blunt’s song , You’r beautiful  started playing in my head  )  . Anyways 23rd street arrived and again as always she left the train without even noticing me and the respect I had grown for her beauty J . So with heavy heart my stop arrived and I got down thinking of her and felt I LOVE BLONDE GIRLS.

Then as every day morning I proceeded towards a star bucks coffee shop next to my office to pick my latte ( as I have realized that if you want to be noticed in NY , you need to carry star bucks coffee even if you end up dumping it in your restroom, but somehow I like their latte). And as I entered I saw Christine again , a dark eyed ,dark haired beautiful Mexican girl, smiling and asked me do I need latte again ? ( as she suggests me many times to try some other coffee’s too at that place , but I don’t end up taking risk) . I picked my coffee , paid my bill and came out of the shop thinking I LOVE MEXICAN GIRLS.

At this moment , I realized what happened to my love for blonde girls ? Why did it change suddenly?

The first thing that came to my mind was , BLONDE and MEXICAN are 2 classes in my brains which are loaded and with every girl of that type and instance is created for them and if they are beautiful they are persisted else made available for garbage collection.  So when I saw the girl in train , my JVM loaded a  class called Blonde and quickly made an instance and filled all the variables in it J . Then when I went to coffee shop , it loaded a class called MEXICAN , again made a instance  of it with features of
Christine . But what happened to my class and object of blonde , it should have been still live. How does brain takes care of so many different classes of girls ?

The only reason I thought was if classes were getting unloaded back to main storage from my RAM . So does it even happen in JAVA . But I have never faced it in JAVA. neither I do take care of it while coding .

So I searched JVM doc to find the answer and I found it on a short note up which says A class or interface may be unloaded if and only if its defining class loader may be reclaimed by the garbage collector’

So if we write a custom class loader without much care and this gets unloaded so all the classes it loaded will get unloaded.

The dangers could be : If any class which was loaded before get unloaded ( due to classloader) but gets loaded again , all the static blocks would run again . And if you had some DB calls or Business logic present , this might be even more dangerous .

I understand it’s not a daily activity to write a classloader but thought of sharing with you if you some day end up finding a issue like this.


Tuesday, 12 June 2012

Debugging 'Out of Memory' in Java


This document takes one through an example code deliberately executed in order to obtain the “out Of Memory” Exception. The below is the sequence


  1. Execute the program using the JVM option HeapDumpOnOutOfMemoryError. This ensures the JVM to dump data in file something like java_pidpid.hprof  in Java working directory 
  2. Execute jhat <data file name>
  3. The above step will start a local server, accessible via web browsers. This step will read the data file obtained from step #1 and then analyze the information, resolves the references and be ready for the further steps
  4. Open the IE (or Mozilla) with the below default URL
http://localhost:7000
  1. The IE will list out all the references. This has the capability of drilling down from one wrapper object (or container object) to the contained objects. Example: On the landing page:
    1. Click on class WrapperClass”. This will show all the reference to the objects held by WrapperClass instance. Similarly, the step can be repeated to drill down on the references held to reveal the references held by the clicked reference. This is a top-down drilling.
    2. Click on “Show instance counts for all classes (including platform)This will show ALL the instances that were held at the moment the system crashed
                                          i.    Our example deliberately pushes Integer objects onto an arraylist contained as a member variable inside the WrapperClass.
                                         ii.    We should see a large number of instances of Integers (because, we have deliberately did that). Click on the “instances of Integer”.
                                        iii.    This should reveal ALL individual Integer Object instances
                                        iv.    Click on any of the object instance shown
                                         v.    That should show the “references to this object
                                        vi.    Repeat the step until the container object, WrapperClass, in our case is revealed 
                          This is the bottom-up drilling 

                   Hence, jhat comes extremely handy while debugging above scenarios.

         Main Program

                                
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;


public class MemorCrashTest {

                public static void main(String args[])
                {
                                for(;;)
                                {
                                                WrapperClass w=new WrapperClass();
                                                File f=new File("D:\\Workflow.zip");
                                                try {
                                                                FileInputStream fis=new FileInputStream(f);
                                                               
                                                                while(fis.read()!=-1)
                                                                {
                                                                                w.a.add(fis.read());
                                                                }
                                                                System.out.println(w.a);
                                                } catch (FileNotFoundException e) {
                                                                // TODO Auto-generated catch block
                                                                e.printStackTrace();
                                                }catch (IOException e) {
                                                                // TODO Auto-generated catch block
                                                                e.printStackTrace();
                                                }
                                }
                }
               
}


WrapperClass.java -> This contains the ArrayList

 
import java.util.ArrayList;


public class WrapperClass {

                public ArrayList a =new ArrayList();
               
}




Compile and execute using the JVM parameters HeapDumpOnOutOfMemoryError


The program has crashed with “Out of Memory” Error. And, has produced the data file in c:\tmp\java_pid4072.hprof

  
Run jhat and pass the generated profile data for analysis



 Jhat has now analyzed the data and is ready for reports. Open the browser for further steps


  Click on the WrapperClass Instance

  
Bottom Up Drilldown – From Landing Page




Now you can dig into any level of classes to find the memory leakage.