Sunday, March 14, 2010

Thread dump in Tomcat

In a multithreaded environment if the implementation is not perfect, then deadlocks are common scenarios. We can analyse the deadlocks by placing log statement at proper locations in the code and analyzing the log, however to exactly pinpoint a deadlock thread dump would be required. Using the thread dump, we can exactly point out what are the two threads which are fighting for the monitor and prints the stack trace of each thread. With the stack trace we can easily find the use case where the deadlock occurs. There are many ways of getting the thread dump and few of them are listed below.

Unix:
Thread dump can be obtained in the appropriate log by sending the command
kill -3 PID, where PID is the process id of the Tomcat jvm.

Windows:
If your tomcat is running as a windows console, then you can open the console and type <ctrl>+<break>. This would print out the thread dump on the console. Make sure you increase the screen buffer memory size to capture the entire thread dump. However If your tomcat is running as a windows service, then you can obtain the thread dump by using the Tomcat monitor.

More on Tomcat as Windows service:
The Tomcat service monitor when started will bring an icon on the system tray. Right click on the icon and select "Thread Dump" in the menu options. This will trigger the generation of thread dump and the thread dump stack trace will be logged to the file system based on the Tomcat service configuration.

With the default settings, the tomcat log redirection would be set to auto. This can be verified by opening the Logging tab of the Tomcat service editor. With the default configuration, the threaddump would be either in jakarta_service, stdout or stderr logs. Or if you have configured tomcat service to redirect to a specific file then you will find the stack trace in the configured file. However if you not configured the logging properly for the tomcat service, then the stack trace would be lost. You will have to configure the service with proper values for logging and restart the tomcat service.

There are also other hacks of sending the <ctrl>+<break> signal to the tomcat service. For instance there is an application called SendSignal.exe which can send <ctrl>+<break> signal to tomcat service by running the command: sendsignal PID, where PID is the process id of the tomcat service. The thread dump stack trace would be logged to file system based the tomcat service configuration. However it may not work in all scenarios based on the security settings. Atleast I couldn't get it working as it always gave me the error related to insufficient privileges, though both the service and sendsignal were run by the same user.

NOTE: sc queryex <service_name> will give you the process id of the service.

Saturday, March 13, 2010

Tomcat as a Windows service

Tomcat, a very familiar name in J2ee world. An excellent example of how open source is growing so powerful in the server domain.

In Windows, Tomcat can be setup in two ways
1. Install the Tomcat as a standalone server
2. Install the Tomcat as a windows service

The first method is fairly simple, which is nothing but just extracting the tomcat archive and running the startup script to run the Tomcat. Normally in a developers local system he would just extract the archive and run the Tomcat, however for running the Tomcat on the server the second method is preferred. Again there are two ways of installing Tomcat as a Windows service.

1. If you are running the Tomcat installable exe file then follow the instructions to install the Tomcat as a service.
2. If you have just extracted the Tomcat and wish to setup Tomcat as a Windows service or If you have already installed the Tomcat with the default name and would like to install another Tomcat instance with different Windows service name, then there are set of steps one should follow. Below are more details of the same. Lets take a look of it one by one.

Under the Tomcat bin directory you would find two interesting executable files. Assuming that you are using Tomcat with 6.x version, however if you are using different version then the file names would be different accordingly.
Tomcat6.exe - This is a service application for running Tomcat6 as Windows service
Tomcat6w.exe - GUI application for monitoring and configuring Tomcat services.

By invoking these executables with specific parameters we can install, update, edit, run, stop, monitor, delete the Windows service. However as these parameters are more techincal, Tomcat also provides a batch script, service.bat, with two simple commands install/remove. The usage of this batch script is as below.
Usage: service install/remove [service_name]

The service_name is the name of the Windows service and is an optional parameter. If you do not specify the service name then by default it will attempt to install/uninstall the service with name tomcat6.

If you are planning to write a batch script of your own, similar to service.bat, then refer to Windows service HOW-TO in locally installed tomcat documentation or in the internet with link http://tomcat.apache.org/tomcat-6.0-doc/windows-service-howto.html. It would also help you by looking at what all are being configured in the service.bat script provided by Tomcat.

Tomcat6.exe is used to install, edit, run, stop and delete the Tomcat service. However if you are looking for a GUI interface for editing the service configuration then the answer to this would be Tomcat6w.exe. Tomcat6w.exe is used only for configuring and monitoring the Tomcat services, so it is a prerequisite to have the Tomcat service already installed for running Tomcat6w.exe. There are two modes of running this application.
1. Run as a GUI based service editor
2. Run as a GUI based service monitor

By just running this exe without any parameter it would by default attempt to open the service editor with service name as file name without 'w'. Here it would be Tomcat6, assuming that you are running Tomcat6w.exe. If your service name is different, i.e., other than the Tomcat6, then you can open this service configuration by running the command: Tomcat6w //ES//<service_name>. Or much simpler solution is to make a copy of this executable file and rename it as <service_name>w.exe. In this case by just running this new file it would open the editor for windows service with the name as <service_name>.

To run the Tomcat as a monitor, you need to specify the command as: Tomcat6w //MS//<service_name>. It would be beneficial if you write a simple batch script, say monitor.bat, under Tomcat bin directory with just above command for monitoring purpose. Again remember if you have made a copy of Tomcat6w with service name as said above then the command can also be <service_name>w //MS//

In summary, below steps help you in managing the Tomcat as a windows service.
1. Install: In command prompt, cd to Tomcat bin and run the command: service install <service_name>
2. Edit: Make a copy of file tomcat6w.exe and rename it to <service_name>w.exe. Just run this file to open service editor.
3. Monitor: Prepare a batch script with command: tomcat6w //MS//<service_name> (or) <service_name>w //MS//. Run this script to open service monitor.
4. Uninstall: In command prompt, cd to Tomcat bin and run the command: service remove <service_name>

Note: The Windows service name and service display name are different and service name is case insensitive.

Learning programming

    Programming is an interesting world where you apply your skills to build a software program that comes into life when it is run on a com...