IBM Java tools works only with its own JVM.

When working with any of IBM products like Websphere, Thread Dump Analyzer developed by IBM or those n number of other Java tools, if you run into issues despite getting all obvious things correct one worthy check would be to see if the tool works with the JVMs other than the one developed by IBM.

Because most of the cool and awesome Java tools created by IBM are not compatible with JVMs other than the one developed by it.

This assumption usually wastes lot of time causing frustration.

How to generate Weblogic core dump in Solaris

This is what I did recently to generate core dump of Weblogic server running in Solaris box. Had to spend sometime to figure it out where the dump is being generated.

Here are the steps I followed:

[1] Run ps -ef | grep java command.
[2] Identify the process id of the Weblogic process [ Make sure only one Weblogic process is running so that it's easy to identify ].
[3] Run the command kill -3 process_id
[4] Assuming that the Weblogic server is started using nohup ./startWeblogic.sh & command, the core dump will be appended to nohup.out file.
[5] You can check this by doing less and Shift + F on nohup.out file.

How to pre-complie JSPs in Weblogic

This is one of the techniques I implemented in my last project, among others, to improve response time of JSPs in a Struts 2 based web application.

We know that, in any J2EE web application, the page load time is relatively high when we access the page for the first time because the container has to compile the JSP before presenting to the user. This is a performance bottleneck and gives the perception to the users that the system is slow.

To prevent this, we can precompile JSPs before deploying them in the server. WebLogic server offers two ways to pre-compile JSPs.

[1] Declarative Method
[2] Programmatic Method


[1] Declarative Method.

How to implement:

Add the following configuration to WEB-INF/weblogic.xml

<weblogic-web-app>

       ……….

 <jsp-descriptor>
       <jsp-param>
              <param-name>precompile</param-name>
              <param-value>true</param-value>
       </jsp-param>
 </jsp-descriptor>

       ……….

</weblogic-web-app>

Pros:

Easy to implement

Cons:

Precompilation happens at the time of deployment. This is a major disadvantage because:

(a) If any compilation error occurs, deployment will be halted at the point where the error occurred.
(b)The time taken for deployment will be very high if an application has more number of JSPs.
(c) Pre-compilation occurs every time the application is deployed, increasing the time of deployment.

[2] Programmatic Method.

Declarative method is not used for practical purposes for the disadvantages it has compared to its advantages.

Most efficient method for JSP precompilation is the programmatic method. This method eliminates the biggest disadvantage of declarative method – precompiling at deployment phase- and moves the precompilation process to development phase.

How to implement:

WebLogic provides a tool called weblogic.appc for precompiling JSPs.

Add the following ant target to the build.xml file

<target name="jspcompile">

<java classname="weblogic.appc" fork="yes">

<classpath path="${env.WL_HOME}\server\lib\weblogic.jar;${env.JAVA_HOME}\lib\tools.jar"/>

<arg line="-verbose -classpath '${dir.build}\requiredLibs.jar' '${dir.build}\demoApplication.war'" />

</java>

</target>

Some points to note:

(a) This task should execute after the task which creates WAR file because weblogic.appc expects a WAR file containing JSPs.
(b) Environment variables WL_HOME and JAVA_HOME should be set for ant to access environment variables using env.WL_HOME and env.JAVA_HOME
(c) dir.build is the project’s intermediate build directory where WAR file and any other JAR files etc are created.
(d) requiredLibs.jar is the library containing any classes used by JSPs. Multiple library files can be added as comma separated list.
(e) demoApplication.war is the WAR file where application’s JSPs reside.
(f) If you have spaces in dir.build , then use single quote(‘) as shown above. Otherwise ‘ are not required.

Pros:

(a) This method moves the precompilation to development phase so that any compilation errors are identified and corrected much before deployment.

(b) This method allows us to compile once deploy as many times as we want.

(c) This method reduced the deployment time significantly because precompilation is moved to development phase.

References:

1. http://download.oracle.com/docs/cd/E13222_01/wls/docs100/webapp/reference.html#wp57794

2. http://m-button.blogspot.com/2008/09/using-jsp-precompilation-in-weblogic.html

How to read files from CLASSPATH in Java

This is one learning from my previous project in 2009.

The most platform-independent and flexible way in Java to load resources such as files is to read the resource from CLASSPATH. This way, as long as the resource’s location is added to CLASSPATH, the resource can be anywhere on the disk.

Java provides a method named getResourceAsStream() for this purpose. This method is present in both Class and ClassLoader classes. There are very subtle differences between these two methods, which if not understood properly, will lead to confusion.

Using ClassLoader.getResourceAsStream(),

While using ClassLoader.getResourceAsStream() method, the name of the resource should not be preceded with leading slash (/) in any circumstances. This is because from ClassLoader all paths are already absolute and there is no context from which they could be relative. Therefore the question of whether or not to use leading slash (/) should not arise. If you add leading slash in front of the resource name, InputStream will always be null.

To use ClassLoader.getResourceAsStream(),

[1] In CLASSPATH add the directory where someFile.txt resides.

[2] Use the following code to read the resource.

InputStream in = this.getClass().getClassLoader().getResourceAsStream(“SomeFile.txt”);

Using Class.getResourceAsStream(),

(a) If the resource and the Java class which is reading the resource are in the same directory then there is no need to use a leading slash (/). You can directly use the following line of code to read the resource.

InputStream in = this.getClass().getResourceAsStream(“SomeFile.txt”);

(b) If the resource and the Java class which is reading the resource are in different directories then you have to use a leading slash (/).

[1] In CLASSPATH add the directory where someFile.txt resides.

[2] Use the following code to read the resource.

InputStream in = this.getClass().getResourceAsStream(“/SomeFile.txt”);

Important Note:

When we are using a managed environment i.e. in case of application servers, CLASSPATH entry has to be made in the server’s start up script. For example in WebLogic, CLASSPATH entry has to be appended to the one already present in startWebLogic.cmd (startWebLogic.sh in Unix environments) in DOMAIN_HOME\bin directory.

Reference:

1.http://stackoverflow.com/questions/1464291/how-to-really-read-text-file-from-classpath-in-java

How to pause cmd window incase of any exceptions.

Last year I had to write a small piece of java code to launch another application using one of the available ports between certain range. I wrote the code and saved the command to launch the java class in batch file so that when the user double clicks it, java class will launch the application.

There is a small catch here. The cmd window does not pause irrespective of how the program runs. If the program runs successfully, then fine. But how will the user know in case of any exceptions? So we need to add one line in the batch script at the end to pause the cmd window in case of any exceptions so that the user will get the chance to see what the exception is. The following line is what needs to be added so that the cmd window pauses in case of exceptions:

IF %ERRORLEVEL% NEQ 0 pause

How to induce delay in Loadrunner Java Vuser script

I had to send two xml messages to a JMS queue with slight delay between them. Since I am writing a Java Vuser script, I wasted no time in choosing Thread.sleep( milli sec) function. This worked fine in Vugen so I was sure. But during the execution of Scenario in Load Controller, I observed that the expected output of the transaction is broken and the transaction is failed.

Upon investigation I found that the two messages are not going to the queue in the order we expected. The second message is going early causing the transaction to fail. This is because the Controller is NOT inducing the delay I mentioned in Thread.sleep function. So the correct function to use is lr.think_time(sec). This function causes the Controller to actually wait for the specified number of seconds.

Just this 1 line of code resulted in wastage of 2 person days! But again, this can be learned only by experience.

The code which made me sleepless last night.

I wrote a small piece of code which basically listens to JMS queue and receives messages. The following 3 code snips shows what I wanted to do.
The snip on left is the improved and refactored code and the one on the right is the bad code.

In this following snip I tried to create a queue and receive messages in it.

[ Click to enlarge the image ]

The mistake I did here, as can be seen in the right hand side is, I create the queue and also receive messages in it, both in the same method.

In the following snip, I am calling the method to receive messags in the queue but what I overlooked is that I am also creating the queue in it. This is the where the disaster is lurking.

[ Click to enlarge the image ]

So even if I close session and connection in the following snip, it just closes the last connection.

[ Click to enlarge the image ]

Whereas I have created hell lot of connections in snip-2, ( Notice the while loop, which was churning umpteen number of queue connections )
I am only closing the last connection in snip-3 which leaves thousands of connections opened in memory. This triggered garbage collection which took about 12-13% of CPU consistently even if nothing was processing in the system.

This demonstrates the side effects of trying to do too many things in a single method. Also it is very important to handle connections, whether databse / JMS / sockets very very carefully. This made me sleepless for a night.

Create a Macro to draw a horizontal line in Textpad

Tip Courtesy: Veera Sundar ( http://www.veerasundar.com )

1. Press Ctrl + Shift + R (Record Macro), which starts a macro recording session.
2. Continuously press the ‘_’ (underscore) character for about 100 chars length (which now looks like a horizontal line).
3. Then again press Ctrl + Shift + R, which stops the current macro recording session and asks the file name and macro name to save the recorded macro. Give ‘Draw Line’ as the macro name.
4. After this a new menu option ‘Draw Line’ will be added to ‘Macros‘ menu.

Now whenever you want to insert a horizontal line, you just need to choose the menu option ‘Macros -> Draw Line‘ which will do the rest for you.

The created macro will be saved to C:\Documents and Settings\user_name\Application Data\TextPad folder with .tpm file extension. In case you want to delete the macro simply delete the .tpm file from the above location.

Ubuntu

At last, after keeping CDs idle for about 1 year, I installed Ubuntu in my laptop. It is cool and simple. I could finish it in second installation attempt. First attempt failed because the partition which I selected to install was in FAT32 format. Installation failed when it was about to complete.GRUB couldn’t get installed.Then I again formatted the partition to NTFS and this time I could install it.

Slowly getting used to Ubuntu.Saw Python and Pidgin IM in action.Still have to learn how to install other various softwares.

And one more thing to learn is how to set and use root(/) account.I am not able to login due to incorrect password.Will learn it soon and experiment in (U)Linux.