Showing posts with label technical. Show all posts
Showing posts with label technical. Show all posts

Wednesday, July 21, 2010

Java in 16 days

Learn Java, Basics of J2EE, Open Source Frameworks etc in under 16 days. A free tutorial with links to good content from Sun Java and with to do exercise.

We (Nagarajan and me) prepared a curriculum for interns fresh from college, freshers and people taking up JAVA as a new programming language.

It is a 16 day learning exercise broken down into smaller chunks of contents to read and understand and a small to do exercise at the end of each learning content.








The link to access the document is here - Java learning Curriculum.

It is similar to a Java tutorial for freshers.

m.m

Tuesday, July 13, 2010

Java File Loading in Web Application

Different applications, different technologies, different designs, different packaging, different servers, different developers... Everything is different in web applications the loading of files becomes really difficult.

So how does one load files (with relative path / with absolute path) in the project structure in their java code within web applications.



1) Get the real path of the file from ServletContext or HttpServletRequest
ServletContext sc = (ServletContext)context;
sc.getRealPath("temp.txt");

or

HttpServletRequest hsrTemp = (HttpServletRequest)context;
hsrTemp.getRealPath("temp.txt");

2) Try with context path
HttpServletRequest hsrTemp = (HttpServletRequest)context;
File fileTemp = new File(hsrTemp.getContextPath()+"/resources/temp.txt");

3) Input stream - file in classes folder
InputStream inpStrTemp = this.getClass().getResourceAsStream("temp.txt");

4) resource url - file in classes folder
URL urlTemp = this.getClass().getResource("temp.txt");

5) Try this one.....................the magic one :) (all four above failed for me, this worked and yes I wrote it on my own ...)

URL currentClassFolder = TestClass.class.getResource(""); //this gives you the package reference of the test class
String pathFromUrl = currentClassFolder.getPath(); //this gives the actual path
String finalPath = pathFromUrl.substring(0,pathFromUrl.indexOf("WEB-INF/classes/com/test/web/")); //navigate to where ever you want to
finalPath = finalPath.replaceAll("% 20", " "); //command prompt doesn't like % 20'

Hope it helps, if it does - do leave a comment!

m.m

Monday, July 12, 2010

Netgear Wifi + Nortel VPN Issues?

I was troubled by the long list of issues that I was facing when I used to connect via VPN through wifi, the connection used to be refused and the office help desk would ask me to seek help from the ISP and vice versa.

Finally one day (a week back), I sat down tried to break my head over it and solved it under 2 hours. Quite pleased with the effort.


Issue: Nortel VPN not connecting via wifi router (netgear in my case) but connects via LAN (directly to the modem)

What I did:

You can access your Modem's application as follows
Link - http://192.168.1.1/
Username / password - admin / password (usually but if it doesn't work search in the net for the right one)

Wifi Router
Netgear router
Link -
http://www.routerlogin.net
or
http://10.0.0.1/
Username / password - admin / password (usually but if it doesn't work search in the net for the right one) [for the virgin media provided wifi router it is virgin/password]

For the VPN to work

Add port triggering for the following ports 50, 80, 500, 4500, 1701, 1723 under both the Wifi and modem's application.
And then Navigate to the wan setup under the wifi router's application and
check the Disable SPI Firewall.

Restart the modem and your system and connect to VPN and voila you connect and if it works drop a comment :)

m.m

Tuesday, January 05, 2010

java.lang.OutOfMemoryError: PermGen space

We encountered the dreaded java.lang.OutOfMemoryError: PermGen space error



What does it means - Permgen Space is the space where the class files are all loaded for the first time and there after they are loaded or referenced from there. This error means there is a hell lot of duplicate classes being loaded and not all are being unloaded. These duplicate class instances are cluttering the permgen space and resulting in this error. One expects only only class instance of a java file. This is basically a memory leak.

Temporary Solution -
'start tag' jvmarg value="-XX:+UseConcMarkSweepGC" 'end tag'
'start tag' jvmarg value="-XX:+CMSPermGenSweepingEnabled" 'end tag'
'start tag' jvmarg value="-XX:+CMSClassUnloadingEnabled" 'end tag'
- These allow perm gen sweeping and allows class unloading, I guess it works really well when there is no memory leak but low perm gen space allocated, so when there is a space deficiency it automatically unloads few classes and then reloads them when needs. Yes there will be a performance issue but am sure not many will notice it.
'start tag' jvmarg value="-XX:PermSize=256M" 'end tag'
'start tag' jvmarg value="-XX:MaxPermSize=512M" 'end tag'
- you can keep increasing this value between 128 - 512 or more but this will only delay the time after which the error is thrown.

Tools - Few tools that I used for analysing this issue
a) MAT - Eclipse Memory Analyser Tool (Also recommended - has some great features to zero in on the suspect / culprit)
b) IBM Heap Analyser
c) JConsole
d) Yourkit - trial version for 14 days (you better solve your issue within 14 days :)) [Highly recommended - it allows monitoring and a hell lot of features]
e) JMAP


Analysis & Solution -
There are two ways of going about this, you would never programmatically create duplicate classes and load them into the permgen, so the only few options are
First Way
1) Are you using different classloaders or custom classloaders to load few classes? If yes there are cases that same classes are loaded often because of different classloader instances. Check you own code.
Second Way
1) Analyse the various frameworks that you are using, check in particular if you in your code or these frameworks use dynamic proxying, which means creating proxy classes at run time for loosely coupling the classes - http://java.sun.com/j2se/1.4.2/docs/guide/reflection/proxy.html
PS: A tip - check if any framework uses CGLIB, in my case it was this.

Approach 1
1) I used jmap on the unix machine and got the heap reproduced in a file called heap.bin.
2) Then open the heap.bin using the free tool IBM Heap Analyser (ha36.jar Let me know if you need it) or MAT or Yourkit.
3) Now you can view different details of the heap, like duplicate class, which objects is being created in large number or objects based on size etc.
4) Now go to class instances and search for duplicate classes or sort the classes by name and then go through the list.
5) You should be able to see at some point few duplicate classes being loaded and they would have different extension
example- Class ABC you can see Class ABC, Class ABC$$EnchancerByCGLIB$$9a6119f_32$$FastClassByCGLIB$$123456, Class ABC$$EnchancerByCGLIB$$9a6119f_33$$FastClassByCGLIB$$123453, Class ABC$$EnchancerByCGLIB$$9a6119f_34$$FastClassByCGLIB$$123454, Class ABC$$EnchancerByCGLIB$$9a6119f_35$$FastClassByCGLIB$$123455 etc etc
6) Now you know this class is being created by CGLIB / dynamic proxying and because they are all different classes all together they will be surely kept in the perm gen adding to the clutter and the error.

Approach 2
1) Yourkit attaches to the live running application - the process is given in their site.
2) Monitor the live application and keep checking the permgen and number of classes loaded and unloaded - if the classes loaded keeps increasing even after a threshold we know we are in trouble (jconsole also can be used to see the classes loaded and unloaded)
3) In your kit keep checking the class instances and search for duplicate classes or sort the classes by name and then go through the list.
4) You should be able to see at some point few duplicate classes being loaded and they would have different extension
example- Class ABC you can see Class ABC, Class ABC$$EnchancerByCGLIB$$9a6119f_32$$FastClassByCGLIB$$123456, Class ABC$$EnchancerByCGLIB$$9a6119f_33$$FastClassByCGLIB$$123453, Class ABC$$EnchancerByCGLIB$$9a6119f_34$$FastClassByCGLIB$$123454, Class ABC$$EnchancerByCGLIB$$9a6119f_35$$FastClassByCGLIB$$123455 etc etc
5) Now you know this class is being created by CGLIB / dynamic proxying and because they are all different classes all together they will be surely kept in the perm gen adding to the clutter and the error.
6) This list will only keep increasing as time flies / more requests are being processed.


Solution -
1) Now check which version of the framework you are using and check any recorded bug against it - I was using cglib-nodep-2.1_3.jar and it seems to have a weak reference to all the classes that it creates causing this clutter. I then had to upgrade Spring to spring 2.5.6.SEC01, Hibernate and CGLIB to their production stable releases.
2) After this fix / upgrade I took regular imprints / monitored it live and found there were no more duplicate classes being created.
2.1) Another way is to take regular heap imprints and you would see that there is pattern say heap1 = 10 MB, heap2 = 50 MB, heap3 = 100 MB, heap4 = 80 MB, Heap5 ~= 80 MB.
In the case of the error prone application / memory leak plagued application the pattern would be heap1 = 10 MB, heap2 = 50 MB, heap3 = 100 MB, heap4 = 120 MB, Heap5 = 140 MB....


PS: Jconsole can be used to see the heap growth, perm gen growth, class loading, class loading etc at run time. All the graphs can have peaks and downs but must even out in due course, if they keep going north then you know for sure you are going to have trouble.

Few More
jmap -permstat 18254 > permstats_2.txt
jmap -histo:live 18254 > histo_live.txt
jmap -histo 18254 > histo.txt
-XX:-TraceClassLoading Trace loading of classes.
-XX:-TraceClassLoadingPreorder Trace all classes loaded in order referenced (not loaded). (Introduced in 1.4.2.)
-XX:-TraceClassResolution Trace constant pool resolutions. (Introduced in 1.4.2.)
-XX:-TraceClassUnloading Trace unloading of classes.

m.m

java.lang.OutOfMemoryError: Java heap space

We encountered the dreaded java.lang.OutOfMemoryError: Java heap space error.
How often do we hit this error and look in bewilderment.



What does it means - Means there is a hell lot of objects being created and not all are being marked for GC. These stray objects are cluttering the heap space and resulting in this error. This is basically a memory leak.

Temporary Solution - start tag jvmarg value="-Xmx892M" end tag- you can keep increasing this value between 256 - 1024 or more but this will only delay the time after which the error is thrown.

Tools - Few tools that I used for analysing this issue
a) jmap -heap:format=b 12345 (unix)
b) jps - for pid in windows
c) jmap -dump:format=b,file=heap.bin 12345 (windows)
d) IBM Heap Analyser
e) JConsole

Analysis & Solution -
1) I used jmap on the unix machine and got the heap reproduced in a file called heap.bin.
2) Then open the heap.bin using the free tool IBM Heap Analyser (ha36.jar). Let me know if you need it.
3) Now you can view different details of the heap, like duplicate class, which objects is being created in large number or objects based on size etc.
4) Now there is a option to get the leak suspects, it would list a few leak suspects - it would surely list few open source frameworks like Active MQ, Hibernate etc which means you will have to upgrade these to the latest production release from the framework providers.
5) Concentrate on any leak suspect either on your classes or your packages or on any java's Class used a lot in your class.
6) I was able to find a wrongly declared and used variable related to threadpoolexecutor which was causing a lot of hanging objects. (key is to find the link between a leak suspect and the way it is related to your code)
7) After fixing this I took regular heap imprints and analysed them and found that the leak suspect was missing.
8) After this fix my application starting running for almost double the time but I soon hit the dreaded java.lang.OutOfMemoryError: PermGen space, about which I shall write in the next post.
8.1) Another way of seeing the success of your fix is to take regular heap imprints and you would see that there is pattern say heap1 = 10 MB, heap2 = 50 MB, heap3 = 100 MB, heap4 = 80 MB, Heap5 ~= 80 MB.
In the case of the error prone application / memory leak plagued application the pattern would be heap1 = 10 MB, heap2 = 50 MB, heap3 = 100 MB, heap4 = 120 MB, Heap5 = 140 MB....

PS: Jconsole can be used to see the heap growth, perm gen growth, class loading, class loading etc at run time. All the graphs can have peaks and downs but must even out in due course, if they keep going north then you know for sure you are going to have trouble.

Few more
jmap -permstat 12345 > permstats_2.txt
jmap -histo:live 12345 > histo_live.txt
jmap -histo 12345 > histo.txt
-XX:-TraceClassLoading Trace loading of classes.
-XX:-TraceClassLoadingPreorder Trace all classes loaded in order referenced (not loaded). (Introduced in 1.4.2.)
-XX:-TraceClassResolution Trace constant pool resolutions. (Introduced in 1.4.2.)
-XX:-TraceClassUnloading Trace unloading of classes.

m.m

Monday, August 31, 2009

The extra mile...

I am still wondering if the service provided by my favorite GSM provider - Airtel was an act of going the extra mile in customer support / satisfaction or going the extra mile in senseless activities.

Call from customer care (CC): Hallo may I speak to M?
M: Yes this is M.
CC: This is regarding your complaint that you have registered at the customer care.
M: (scratching my head), now which complaint was this, tariff change? service activation? DND?...(anyways) YES tell me.
CC: Now is your connection and network coverage issue proper?
M: (I was surprised that they proactively cover everyone's network and in a hurry check my tower signal bar on the phone and see that it is ready to burst out of the phone) Yes it is fine. What was my complaint regarding?
CC: Sir, you have complained about your network coverage in ABC area.
M: uhhhhhhhhh? hmmmm? Are you serious?
CC: Yes sir.
M: Yes the network coverage is long gone, but I want to tell you something, this was a complaint given in the mid of 2007 and you are calling me regarding this a cool 2 years later. (Anyways thanks for remembering)
CC: Sorry for the inconvenience, is there anything else that you would to know from us.
M: No
CC: If you have any query please call 1234455 to get your queries answered, thank you and have a good day sir.
M: Wish you too a good day.
click....

Bright and Shining till retirement


Makes me wonder if they are doing a great job in resolving all issues or are they having some ancient process / systems thats making their good intent to serve people go wrong. Are they losing on money, time, customer good will and above all are they barking on the wrong tree???

Can we apply Business Process Management (BPM) here?
ps: Yes I just finished a training program on BPM.

m.m

Friday, August 07, 2009

Job Schedulers - UNIX


We might have worked on various Job Scheduling Frameworks, one of the famous one that comes to our mind is Quartz. Yes quartz is a cool frameworks which has advanced error handling, job scheduling and various other great feature. Recently while trying to create a scheduling task for a task in UNIX I decided to check out the inbuilt job scheduling component - crontab, instead of setting up a java application and scheduling it using quartz. There is also a command like a timer at which a script can be run - at
chmod u+x /export/home/user/example/DeleteOldTmpFiles.sh

Useful Links
http://www.adminschoice.com/docs/crontab.htm






m
.
m

SVN vs CVS

Why is every one adopting SVN / Subversion over CVS?

Is SVN / Subversion winning over CVS?

Find out more at the following links.

Detailed Notes
http://svnbook.red-bean.com/en/1.5/svn.forcvs.html

Comparison Details
http://www.pushok.com/soft_svn_vscvs.php
http://momentarypause.blogspot.com/2007/01/subversion-vs-cvs.html
http://versioncontrolblog.com/comparison/CVS/Subversion/index.html

m.m