Java programs run as a service program on Windows systems

Source: Internet
Author: User
Tags app service win32 jboss

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:

    1. Start 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. Example of configuring JBoss in this Way (Win32, Linux/unix).

    2. 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. The example of configuring Tomcat in this Way (Win32, Linux/unix).

    3. The third way, the most complex, the most flexible way, is also the only one that needs to be programmed. 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 example configured in this way.

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:
    1. On the Windows platform with the service running, on the UNIX platform after the station daemon run;
    2. Provides high availability of the application, implemented by monitoring the JVM process, discovers that the JVM process crash or hangs, restart it;
    3. Provide consistent application startup scripts for each platform;
    4. Provides the ability to apply Restart to its own JVM (Restart On-demand);
    5. 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).

    1. 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.
    2. 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.
    3. 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.
    4. 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 the Java to run . If you have set the environment variables for Java, you can not modify them here, and if not, you can use absolute paths to point to Java in the JDK 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: Configures the Java classpath, where the wrapper. Jar is also included, where the parameter can be set, and the position must start at 1, cannot be skipped, must be specified sequentially, When specifying the CLASSPATH, there is a dependency on the front, otherwise there will be classnotfoundexception error, the absolute path and relative path are supported, and the wildcard "*", such as wrapper, is supported . java. classpath.1=. /lib/Wrapper *, but this wildcard can only be used to match file names and cannot be used to match folder names. Of course, you must also add the current directory or the top level directory, depending on the directory where your main class resides.

Wrapper. java. library.path.1=. /lib: Specifies the class library file storage folder Wrapper comes with, such as Wrapper. DLL files, and so on, as long as you specify the corresponding parent directory name on the line, support wildcard characters.

wrapper. app.parameter.1=: Refers to the startup class, as described 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 turn.

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 displayed in service management

wrapper. ntservice.description= Service Name: Description of the service displayed in Service Manager

Wrapper. Ntservice.starttype=auto_start: Configure how the service starts, either Auto_start (Automatic) and Demand_start (manual). The default is automatic.

Java programs run as a service program on Windows systems

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.