Java program in many cases as a service program, under the Un*x platform can be used after the command "&" to run the program as a background service, but under Windows as the Console window on the desktop, you have been worried about other at the same time you The console window is closed? Do you miss the Win32 service program written by VC?
Turn on JBoss, Tomcat's release package and discover that they all use an open Source--java Service Wrapper. With the Java Service Wrapper, we can easily solve our needs and make our service program become WIN32 system service.
Of course, you can also use the Java service Wrapper under Un*x to avoid the rude way of adding "&", resulting in a heap of mail being received every day and viewing the running information through the log mode provided by the Java Service Wrapper.
Java Service Wrapper is powerful, supports both the Windows and Un*x platforms, and provides three ways to package your Java program into a system service, which only describes the simplest way, since there is no need to make any changes to the existing service program, Just add a few script, and the configuration file will be able to change your Java service program into a system service program.
Of course, you need to download the Java Service Wrapper release package to http://sourceforge.net/project/showfiles.php?group_id=39428 before using it.
Here is a brief introduction to the specific use steps:
1. Extract the downloaded Java Service WRAPPER package to local directory {wrapper_home};
2. The service application name is Myservapp, set up the bin, conf, logs, and Lib directories under directory d:\MyServApp, and copy your existing applications such as Nioblockingserver.class to the directory;
3. Copy the following files under {wrapper_home}\src\bin\ to the Myservapp directory and rename them.
{Wrapper_home}\bin\wrapper.exe? C + Myservapp \bin\wrapper.exe
{wrapper_home}\src\bin\app.bat.in? C + Myservapp\bin\myapp.bat
{wrapper_home}\src\bin\installapp-nt.bat.in? C + Myservapp\bin\installmyapp-nt.bat
{wrapper_home}\src\bin\uninstallapp-nt.bat.in? C + Myservapp\bin\uninstallmyapp-nt.bat
4. Copy the following files under {Wrapper_home}\lib to C: \ Myservapp \lib Directory
{Wrapper_home}\lib\wrapper.dll
{Wrapper_home}\lib\wrapper.jar
5. Copy {wrapper_home}\src\conf\wrapper.conf.in to C: \ Myservapp the \conf directory and name Wrapper.conf, and modify the wrapper.conf file where your app service is configured.
The following items can be modified mainly:
#你的JVM位置:
Wrapper.java.command=d:\sun\j2sdk1.4.0_03\bin\java
#运行参数: such as:
Wrapper.java.additional.1=-dprogram.name=run.bat
#classpath:
wrapper.java.classpath.1=. /lib/wrapper.jar
Wrapper.java.classpath.2=. /bin/.
# Java Library Path (location of Wrapper.DLL or libwrapper.so)
wrapper.java.library.path.1=. /lib
#MAIN CLASS here determines how Java service wrapper is used
Wrapper.java.mainclass=org.tanukisoftware.wrapper.wrappersimpleapp
#你的Java应用类
wrapper.app.parameter.1= Nonblockingserver
# Service Name
Wrapper.ntservice.name=nb
# Display Name of the service
Wrapper.ntservice.displayname=nio nonblocking Server
# Service Description
Wrapper.ntservice.description=nio nonblocking Server
Other configurations can be changed according to your needs.
6. Test the Myapp.bat configured above and run the Myapp.bat as if you were running Tomcat under the console window;
7. Test the above configured services and run the C: \ Myservapp\bin\installmyapp-nt.bat will install your application (here, nioblockingserver) into the WIN32 system service.
8. Open Control Panel-hypervisor-services, see NiO nonblocking server is already in system service, and other usage is the same as the familiar Windows service.
Tomcat uses Java service wrapper mode two, which requires minor changes to existing programs, but can control the startup of the service program core through the socket port, making it more flexible. The Java Service Wrapper provides a more complex pattern that requires more coding, which I did not study.
Adopt mode one, can be simple and effective packaging of our service program into a system service program, and enhance the log function, we can myservapp several files into a template, each time you modify the file name, configuration file on it, have the energy of friends can be made Eclipse plugin, The mouse point to the application into a service.
The attachment is a template that can be modified directly by modifying the file name and configuration files. Java Service Wrapper(hereinafter referred to as JSW) is a program that can encapsulate a Java application as a Windows service or a UNIX daemon (daemon) and is free of charge. It can run on:
- Aix-aix
- Freebsd-freebsd
- Hpux, Hpux64-hp-ux, and 64-bit versions.
- Irix-sgi Irix
- Linux-linux Kernels; 2.2.x 2.4.x, 2.6.x. Known to work with Debian and Red Hat, but should work with any distribution.
- Macosx-macintosh OS X.
- Osf1-dec OSF1.
- Solaris-sun OS, Solaris 7, 8.
- Win32-windows NT, +, XP, and 2003.
There are three ways to use JSW:
-
Launches the application with Wrappersimpleapp class. This is the simplest way to use JSW, and it is also the recommended way. There is a problem with this approach, however, because JSW exits the JVM directly using System.exit (), which is the same as when the consumer exits the program directly by CTRL-C while the application is running. If the application needs to execute clean up before exiting, you need to register the shutdown hook yourself, or do not use this method. Configure jboss in this manner (win32 , linux/unix ).
-
The second way is to use the Wrapperstartstopapp class. This approach is for applications such as Tomcat (that is, applications that start in one class but end in another). Typically, this type of application opens a server socket at startup, waiting for a connection that requires the program to end, and the "End" class is executed when a request to end is received. The JSW is the "End" class that is executed directly when the request to stop running is received. In this way configure tomcat example (win32 , linux/unix ). The
-
Is the third, the most complex, the most flexible, and the only way to program. This way you write a class that implements the Wrapperlistener interface. Some features are unique in this way, such as receiving and responding to system control events (such as start and end) directly in the program. But it also adds to the complexity of the program. If these features are not required, it is convenient to use the first method. The .
Note: Use the first method preferably do not use jre1.3.x or above, because 1.2.x no shutdown hook technology. If the program can only use jre1.2.x, you need to use the second or third method of JSW.
Today, while browsing all JSRs, I saw a withdrawed JSR, Daemon Java, that developed a Java Daemon Framework (container) to run each platform. Go to Google to turn over some information, link to an interesting East Java Service Wrapper (http://wrapper.tanukisoftware.org/doc/english/introduction.html) Major features including:
- On the Windows platform with the service running, on the UNIX platform after the station daemon run;
- Provides high availability of the application, implemented by monitoring the JVM process, discovers that the JVM process crash or hangs, restart it;
- Provide consistent application startup scripts for each platform;
- Provides the ability to apply Restart to its own JVM (Restart On-demand);
- Easy to install and manage with JMX support
Personal feelings, features 1 and 2 are more valuable.
Unfortunately, under the siege of the multinational force led by Apache, the creative JSR 96 was killed.
registering a Java app as a background service
There is a Java application in the project, after the delivery of the user requirements to make the program into a background service program, namely: The system starts after the program can start automatically, and in the foreground do not appear to run Windows, maintenance personnel as long as in "service Management" (Windows) select Start or stop.
The solution is as follows:
Linux
It is relatively easy to register a backend service program in Linux, just edit the/etc/init.d/boot.local file and add the following script to the Boot.local file:
/iapappserver/messageserver2. 1/run.sh
Where Run.sh is a running script for Java applications
Windows
The situation under Windows is more complex and requires the use of two tools provided by Windows:instsrv.exe( download ) and Srvany.exe( download ).
INSTSRV: This tool turns the Win32 program into a system service.
Basic usage:
Because of the need of the application, the Java app needs to be run as NT service, so I found the Java Service wrapper this tool to help. Official website: http://wrapper.tanukisoftware.org/doc/english/download.jsp.
Java Service Wrapper provides 4 ways to register the Java app as a service run (integration Methods).
- Use the Wrappersimpleapp help class to launch the app. This method is the simplest method. Using this approach, it is important to note that Java Service wrapper stops the Java app without invoking the appropriate receive method for the Java app, but instead calls System.exit () directly to end the Java app.
- Use the Wrapperstartstopapp help class to launch the app. This approach assumes that the Java app has a corresponding startup, stop class. CLASSX is responsible for starting the Java App,classy to stop the Java App. Of course, you can use the same class to start or stop the Java App when you are using it, as long as you initialize the different start or stop parameters of the class.
- Use the Wrappermanager class to start the app. This approach is the most flexible way to start, and requires the Java app's startup class to implement the Wrapperlistener interface. The Wrapperlistener interface has the start (string[] arg0) and the Stop (int arg0) methods, which need to be implemented by the Java app's startup class. This allows you to use the Wrappermanager class to manage the main class of the Java app.
- Use the Wrapperjarapp help class to launch the app. This approach is similar to the Wrappersimpleapp helper class, except that the Java app requires packaging as an executable jar file when using this method.
I use the second way to start my own Java App. After downloading the Java Service Wrapper, first copy some files to your Java app's corresponding directory. The structure of the directory looks like this:
Src
|
|--bin
| |--wrapper.exe
| |--app.bat
| |--installapp-nt.bat
| |--uninstallapp-nt.bat
|
|--conf
| |--wrapper.conf
|
|--lib
| |--wrapper.dll
| |--wrapper.jar
|
|--logs
| |--wrapper.log
|
|--<your own Source Classes here>
Of course, if your application is already written, you can't change the name of an existing directory, then modify the Java Service Wrapper configuration file. For example, the original name of the Conf file directory is the configuration, then you can modify the bin directory to refer to the Wrapper.conf bat file in the appropriate place.
Then there is the modification of the configuration file wrapper.conf.
wrapper. java. command=java: specifies java. If you have set the environment variables for Java, you can not modify them here; if not, you can use the absolute path to point to bin directory.
Wrapper. java. mainclass=org.tanukisoftware.wrapper.wrapperstartstopapp: Specifies the Help class to run, which is one of the startup classes in the 4 mode described above. Java Service Wrapper starts the application with its own class and passes in the main class of the Java application that is actually to be launched as the first parameter of the class.
wrapper. java.classpath.1=. /lib/wrapper.jar: Configure java the classpath, here will wrapper.jar is also included, where you can set the position of the parameter, and this position must be from 1 Start, cannot skip, must be specified in order, specify the Classpath, and then arrange the classnotfoundexception error, here support absolute path and relative path, also support wildcard character "*", such as wrapper. java.classpath.1=. /lib/wrapper* /span>
Wrapper. java.library.path.1=. /lib: Specifies the Wrapper library file folder, such as Wrapper.DLL file, as long as the corresponding parent directory name is specified, support wildcard characters.
wrapper.app.parameter.1=: refers to the startup class, as said above CLASSX.
wrapper.app.parameter.2=: refers to the number of arguments required to start the class Main method.
wrapper.app.parameter.3=: refers to the actual parameters of the startup class Main method, listed in sequence.
wrapper.app.parameter.x=: actual parameters.
Wrapper.app.parameter.y=: refers to the stop class, as said above classy. Of course, it can be as classx as the startup class, but requires parameters to differentiate the operation.
Wrapper.console.title=java App: the console window displays the title,
wrapper.ntservice.name=service Name: names of system services ,
wrapper.ntservice.displayname= Service Name: Names that are displayed in Services management
wrapper.ntservice.description= service Name introduction information :
Wrapper.ntservice.starttype=auto_start: Configure the service startup mode, you can select auto_start (Auto ) and Demand_start (manual ) Two different ways. The default is automatic.
Java programs run as a service program on Windows systems