Java joins Windows System Service (Java service wrapper)

Source: Internet
Author: User
Tags syslog

It is necessary to develop a distributed system for work, but there is a problem in server setup. Because Java has never been related to the operating system. It is not so easy to add to the system service. If a user is logged out, the Java W process will also be shut down accordingly. There are many ways to add tomcat or IIS to the backend service of the server. After trying, we foundJava service wrapperTo achieve relevant results.

First download the latest versionJava service wrapper http://wrapper.tanukisoftware.org/doc/english/download.jsp.

After decompression, not every file must be used. First, we create a folder to store the system process.

The approximate structure is as follows:

 

 

The main files in the bin directory are: installnb-nt.batw.nb.batw.uninstallnb-nt.batw.wrapper.exe

The main files in the conf directory are: wrapper. conf

Main files in the lib directory are: wrapper. dll, wrapper. jar, and wrappertest. jar.

Main files in the logs directory are: Nb. Log (you can change the name)

All these files can be found in the ZIP file you downloaded. They are required.

 

Then paste the class of your entire project to the bin directory (I am not storing jar files, for the sake of convenience of updating). Some XML files used as the configuration must also be placed in this location, then the class can be read directly.

 

 

The main method of my program is in server. dbdaoserver. Class.

 

Then pay attention to the content in/CONF/wrapper. conf.

 

 

#*************************************** * ************************** <Br/> # wrapper properties <br/> #*************************************** * ************************** <br/> # Java application <br/> wrapper. java. command = .. /jre1.7.0/bin/Java </P> <p> # Java main class. this class must implement the wrapperlistener interface <br/> # or guarantee that the wrappermanager class is initialized. helper <br/> # classes are provided to do this for you. see the Integration Section <br/> # Of the documentation for details. <br/> wrapper. java. mainclass = org. tanukisoftware. wrapper. wrappersimpleapp </P> <p> # Java classpath (include wrapper. jar) add class path elements as <br/> # needed starting from 1 <br/> wrapper. java. classpath.1 = .. /lib/wrapper. jar <br/> wrapper. java. classpath.2 = .. /bin /. </P> <p> # Java library path (location of wrapper. DLL or libwrapper. so) <br/> wrapper. java. library. path.1 = .. /lib </P> <p> # Java additional parameters <br/> wrapper. java. additional.1 =-dprogram. name = Nb. bat </P> <p> # initial Java heap size (in MB) <br/> # wrapper. java. initmemory = 3 </P> <p> # maximum Java heap size (in MB) <br/> # wrapper. java. maxmemory = 64 </P> <p> # application parameters. add parameters as needed starting from 1 <br/> wrapper. app. parameter.1 = server. dbdaoserver </P> <p> #******************************* *********************************** <br/> # wrapper logging properties <br/> #****************************** ************************************ <br /> # format of output for the console. (See docs for formats) <br/> wrapper. console. format = PM </P> <p> # log level for console output. (See docs for log levels) <br/> wrapper. console. loglevel = info </P> <p> # log file to use for wrapper output logging. <br/> wrapper. logfile = .. /logs/Nb. log </P> <p> # format of output for the log file. (See docs for formats) <br/> wrapper. logfile. format = lptm </P> <p> # log level for log file output. (See docs for log levels) <br/> wrapper. logfile. loglevel = info </P> <p> # maximum size that the log file will be allowed to grow to before <br/> # the log is rolled. size is specified in bytes. the default value <br/> # of 0, disables log rolling. may abbreviate with the 'K' (Kb) or <br/> # 'M' (MB) suffix. for example: 10 m = 10 megabytes. <br/> wrapper. logfile. maxsize = 0 </P> <p> # maximum number of rolled log files which will be allowed before old <br/> # files are deleted. the default value of 0 implies no limit. <br/> wrapper. logfile. maxfiles = 0 </P> <p> # log level for sys/event log output. (See docs for log levels) <br/> wrapper. syslog. loglevel = none </P> <p> #***************************** *********************************** <br/> # wrapper NT Service Properties <br/> #*************************** **************************************** * <br/> # warning-do not modify any of these properties when an application <br/> # using this configuration file has been installed as a service. <br/> # please uninstall the service before modifying this section. the <br/> # service can be reinstalled. </P> <p> # Name of the Service <br/> wrapper. ntservice. name = rmiservice </P> <p> # display name of the Service <br/> wrapper. ntservice. displayname = rmiservice </P> <p> # description of the Service <br/> wrapper. ntservice. description = rmiservice </P> <p> # service dependencies. add dependencies as needed starting from 1 <br/> wrapper. ntservice. dependency.1 = </P> <p> # mode in which the service is installed. auto_start or demand_start <br/> wrapper. ntservice. starttype = auto_start </P> <p> # allow the Service to interact with the desktop. <br/> wrapper. ntservice. interactive = false

 

Configure the JRE location:

# Java application
Wrapper. java. Command = ../jre1.7.0/bin/Java

 

Configure the class Space Location

Wrapper. java. classpath.1 = ../lib/wrapper. Jar
Wrapper. java. classpath.2 =.../bin /.

 

Configure the Lib path

Wrapper. java. Library. path.1 = ../lib

 

Configure the BAT file to be started (for testing)

Wrapper. java. additional.1 =-dprogram. Name = Nb. bat

 

Configure initial memory and maximum memory

# Initial Java heap size (in MB)
# Wrapper. java. initmemory = 3

# Maximum Java heap size (in MB)
# Wrapper. java. maxmemory = 64

 

Configure the main class of the service (key)

# Application parameters. Add parameters as needed starting from 1
Wrapper. App. parameter.1 = server. dbdaoserver

 

Configuration Console (no modification required)

# Format of output for the console. (See docs for formats)
Wrapper. Console. format = PM

# Log level for console output. (See docs for log levels)
Wrapper. Console. loglevel = info

Configure the running record file

# Log file to use for wrapper output logging.
Wrapper. logfile = ../logs/Nb. Log

# Format of output for the log file. (See docs for formats)
Wrapper. logfile. format = lptm

# Log level for log file output. (See docs for log levels)
Wrapper. logfile. loglevel = info

 

# Maximum size that the log file will be allowed to grow to before
# The log is rolled. size is specified in bytes. The default value
# Of 0, disables log rolling. May abbreviate with the 'K' (Kb) or
# 'M' (MB) Suffix. For example: 10 m = 10 megabytes.
Wrapper. logfile. maxsize = 0

# Maximum number of rolled log files which will be allowed before old
# Files are deleted. The default value of 0 implies no limit.
Wrapper. logfile. maxfiles = 0

# Log level for sys/event log output. (See docs for log levels)
Wrapper. syslog. loglevel = none

 

Configure System Service name

# Name of the service
Wrapper. ntservice. Name = rmiservice

 

Configure the name displayed by the System Service

# Display name of the service
Wrapper. ntservice. displayname = rmiservice

 

Configure System Service Description

# Description of the service
Wrapper. ntservice. Description = rmiservice

Configure the System Service Startup Mode

# Mode in which the service is installed. auto_start or demand_start
Wrapper. ntservice. starttype = auto_start

 

 

The source code of the Nb. BAT file is attached:

@ Echo off <br/> setlocal </P> <p> REM <br/> REM Java service wrapper General Startup Script <br/> REM </P> <p> Rem <br/> REM resolve the real path of the wrapper.exe <br/> REM for non NT systems, the _ realpath and _ wrapper_conf values <br/> REM can be hard-coded below and the following test removed. <br/> REM <br/> If "% OS %" = "windows_nt" Goto nt <br/> echo this script only works with NT-based versions of Windows. <Br/> Goto: EOF </P> <p>: NT <br/> REM find the application home. <br/> REM % ~ Dp0 is location of current script under NT <br/> SET _ realpath = % ~ Dp0 <br/> set _wrapper_exe1_1__realpath1_wrapper.exe </P> <p> REM <br/> REM find the wrapper. conf <br/> REM <br/>: conf <br/> SET _ wrapper_conf = "% ~ F1 "<br/> if not % _ wrapper_conf % =" "Goto startup <br/> SET _ wrapper_conf =" % _ realpath % .. /CONF/wrapper. conf "</P> <p> REM <br/> REM start the wrapper <br/> REM <br/>: startup <br/> "% _ wrapper_exe %"-C % _ wrapper_conf % <br/> if not errorlevel 1 Goto: EOF <br/> pause

 

 

If any of the above processes are incorrect, I hope to correct them.

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.