LendKey

Sunday, August 15, 2010

How to monitor Java app started as Windows Service


If you have a Java server running as Windows Service, such as Tomcat, JBoss, etc., it's not possible to run JConsole or JVisualVM to monitor those JVMs, because Windows doesn't allow those apps to see JVM started as Windows Service.

There're two ways I fund over the internet:
1. You can use Sysinternals PsExec.exe to accomplish this.
psexec.exe -i -s c:\visualvm\bin\visualvm.exe


2. Use interactive Windows Service:

1. Install a service using sc.exe - that will open a command prompt under local service account by executing this on cmd window sc create debugservice binpath= "cmd /K start" type= own type= interact 2. sc start debugservice [on a remote desktop session, this service has to be started only in console session for the command prompt window to pop up] will print message that [SC] StartService FAILED 1053: but will start a command prompt under local system account. 3. now navigate to %JAVA_HOME%/bin on the command prompt that got opened and type in jmap -dump:format=b,file=D:\temp\test.hprof  this will dump the heap to D:\temp folder. Be careful dumping heap on a live system as it will lock up the system for the duration of heap dump. 4. similarly jconsole can be started using that command prompt and connecting to the pid of the tomcat. Technical explanation: a. as tomcat service is installed under local system account, jconsole / jmap cannot connect to the service when these are started under the windows logged in user. b. So the command prompt interactive service is installed as a local system service and when that service is started, it opens the command prompt under local system account. c. And hence any process started from that command prompt will be started under local system account and hence be able to access any local system services.

Wednesday, April 21, 2010

Run Terracotta Server as Windows Service

Terracotta is a JVM-level Java clustering solution. Lots of Java frameworks and libraries, such as Ehcache, Quartz, Web Session, have been offering clustered version over this nice platform.

This document describes steps to install Terracotta Server as a Windows Service that can be managed and executed just as any other standard Windows services, including auto startup when Windows is started and management through standard service management UI and command.

Setup Procedure


Preparation

1. Download and install JDK
    Goto http://java.sun.com to download JDK 1.6.
    As of now, JDK 1.6.0_u17 is certified and recommended.
    Install JDK to c:\java\jdk_1.6.0_17

2. Download and install Terracotta
    Open source version can be downloaded from http://www.terracotta.org.
    Enterprise commercial version can be downloaded by contacting sales@terracottatech.com.
    If download is in form of tar.gz file, untar it to c:\terracotta, such as c:\terracotta\terracotta-3.2.1_1.
    If download is in jar form, double click the file to start installation. Follow installation steps to install Terracotta to to c:\terracotta, such as c:\terracotta\terracotta-3.2.1_1.

3. If Terracotta License file is needed, copy it to Terracotta Installation Folder, such as \terracotta\terracotta3.2.1_1.

4. Download Windows Resource Kit Tools
    Go to http://www.microsoft.com to search and download Windows Resource Kit Tools that is suitable to the targeted Windows version.
    Run the installer and install it to the default folder.
    Copy anysrv.exe to c:\terracotta\terracotta-3.2.1_1\bin

5. Make sure sc.exe is included in your Windows installation. Otherwise, download it from Microsoft’s website

Configuration

1. Create tc-config.xml to be used by Terracotta Server, place it to bin folder under Terracotta installation home folder.

2. Create Registration file
    Create a text file with any text editor, copy paste the following text into the file and save it as tcservice.reg.

REGEDIT4
[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Terracotta\Parameters]
"Application"="\"C:\\Java\\jdk1.6.0_17\\bin\\java.exe\\"
"AppParameters"="-cp c:\\terracotta\\terracotta-3.2.1_1\\lib\\tc.jar -server -Xms4G –Xmx4G -XX:+HeapDumpOnOutOfMemoryError -Dtc.install-root=c:\\terracotta\\terracotta-3.2.1_1 com.tc.server.TCServerMain"
"AppDirectory"="c:\\terracotta\\terracotta-3.2.1_1\\"


Note:
If you need to pass more parameters to TC server, yon can do so by appending them to the AppParameters section. For example, if I want to specify a tc-config.xml and a server name, I can change the AppParameters to:
"AppParameters"="-cp c:\\terracotta\\terracotta-3.2.1_1\\lib\\tc.jar -server -Xms4G –Xmx4G -XX:+HeapDumpOnOutOfMemoryError -Dtc.install-root=c:\\terracotta\\terracotta-3.2.1_1 com.tc.server.TCServerMain -f c:\\terracotta\\my-tc-config.xml -n Server1"


Install Service

1. Create Windows Service
    Open a command prompt window as System Administrator.
    Run the following command:
sc.exe create Terracotta binPath= “c:\terracotta\terracotta-3.2.1_1\bin\srvany.exe” type= own start= auto DisplayName= “Terracotta Server 3.2.1_1”
    It will create a new Windows Service named Terracotta which will be started automatically when Windows is started.

2. Modify service parameter, so it will start Terracotta Server
    In the same command prompt box, run:
regedit tcserver.reg
    It will modify the newly created service to run Terracotta Server as a Java application.

Test

1. In Windows Service Management window, start the service. Make sure there’s no error popped up

2. Run dev-console.bat from Terracotta Installation folder and connect to server_name 9520. Developer’s console should be able to login and display the standard Terracotta management interface.

Trouble Shooting

1. Pay attention to quotation signs and garbage characters when copy&paste code snippet from this article


2. To test run the service manually
Since Windows doesn't provide any console based information when starting service, it could be very difficult to trouble shoot problems.
If your service does not start as expected, it is highly recommended to run the service manually just to make sure everything is configured correctly.
To do so, you can follow the following steps:

  1. Open a Commend Shell
  2. Go to folder defined by AppDirectory
  3. Run the Application with AppParameters
In my example, it will be:

CD c:\terracotta\terracotta-3.2.1_1
C:\Java\jdk1.6.0_17\bin\java.exe -cp c:\terracotta\terracotta-3.2.1_1\lib\tc.jar -server -Xms4G –Xmx4G -XX:+HeapDumpOnOutOfMemoryError -Dtc.install-root=c:\terracotta\terracotta-3.2.1_1 com.tc.server.TCServerMain

Friday, March 26, 2010

Install ColdFusion 8 on Snow Leopard

I need to test something in ColdFusion 8 on my MacBook Pro with Snow Leopard. After I downloaded and unzipped the installer, the installer simply did not run. I double click on the ColdFusion 8 Installer.app from Finder, nothing happened. I guess the Install Anywhere script which was used to create installer for CF 8 might have issue with Snow Leopard.

From internet, there're lots of articles about how run CF8 in Snow Leopard, no one really talked about installation part. Hope I'm not the only one that had this problem.

Anyway, I do managed to find a way to do the installation. Here's what I did:
1. Open a Terminal
2. cd ColdFusion\ 8\ Installer.app
3. cd Contents/Resources/Java
4. java -cp ./IAClasses.zip:Execute.zip:installscript.zip:. com.zerog.ia.installer.Main
5. Follow the steps and I was able to finish setup and run CF8

Cheers!

Li