JAVA加入windows系统服务(Java Service Wrapper)

               

    因为工作需要要开发一个分布式的系统,但是在服务器架设上就遇到问题。因为一直JAVA就和操作系统无关性。不是那么容易加入到系统的服务中,如果一旦注销一个用户,JAVAW进程也会相应关闭。想很多的办法也没办法想tomcat或者IIS加入到服务器的后台服务。经过尝试,发现Java Service Wrapper可以达到相关的效果。

   首先下载最新版的Java Service Wrapper http://wrapper.tanukisoftware.org/doc/english/download.jsp

   解压后里面不是每个文件都必须用上的,我们首先建立一个文件夹放这个系统进程的。

   大概结构如下:

  

文件夹

  bin目录下主要的文件有:InstallNB-NT.bat、NB.bat、UninstallNB-NT.bat、wrapper.exe

  conf目录下主要文件有:wrapper.conf

  lib目录下主要文件有:wrapper.dll、wrapper.jar、wrappertest.jar

  logs目录下主要文件有:NB.log(可以配置改名的)

 

  这些文件都可以在你下载的zip那里找到,必须要的。

  然后把你的整个项目的class黏贴到bin的目录下(我不是存放jar文件,为了更新方便),系统的一些做为配置的xml文件也必须放在这个位置,那么class才可以直接读到。

  

   bin 

   我程序的main方法在server.DBdaoServer.class.

   然后注意配置/conf/wrapper.conf里面的内容

   #********************************************************************# Wrapper Properties#********************************************************************# Java Applicationwrapper.java.command=../jre1.7.0/bin/java# Java Main class.  This class must implement the WrapperListener interface#  or guarantee that the WrapperManager class is initialized.  Helper#  classes are provided to do this for you.  See the Integration section#  of the documentation for details.wrapper.java.mainclass=org.tanukisoftware.wrapper.WrapperSimpleApp# Java Classpath (include wrapper.jar)  Add class path elements as#  needed starting from 1wrapper.java.classpath.1=../lib/wrapper.jarwrapper.java.classpath.2=../bin/.# Java Library Path (location of Wrapper.DLL or libwrapper.so)wrapper.java.library.path.1=../lib# Java Additional Parameterswrapper.java.additional.1=-Dprogram.name=NB.bat# Initial Java Heap Size (in MB)#wrapper.java.initmemory=3# Maximum Java Heap Size (in MB)#wrapper.java.maxmemory=64# Application parameters.  Add parameters as needed starting from 1wrapper.app.parameter.1=server.DBdaoServer#********************************************************************# Wrapper Logging Properties#********************************************************************# 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# 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: 10m = 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#********************************************************************# Wrapper NT Service Properties#********************************************************************# WARNING - Do not modify any of these properties when an application#  using this configuration file has been installed as a service.#  Please uninstall the service before modifying this section.  The#  service can then be reinstalled.# Name of the servicewrapper.ntservice.name=RmiService# Display name of the servicewrapper.ntservice.displayname=RmiService# Description of the servicewrapper.ntservice.description=RmiService# Service dependencies.  Add dependencies as needed starting from 1wrapper.ntservice.dependency.1=# Mode in which the service is installed.  AUTO_START or DEMAND_STARTwrapper.ntservice.starttype=AUTO_START# Allow the service to interact with the desktop.wrapper.ntservice.interactive=false

  

   配置jre的位置:

   # Java Application
   wrapper.java.command=../jre1.7.0/bin/java

   配置class空间位置

   wrapper.java.classpath.1=../lib/wrapper.jar
   wrapper.java.classpath.2=../bin/.

   配置lib路径

   wrapper.java.library.path.1=../lib

   配置启动的bat文件(测试使用)

   wrapper.java.additional.1=-Dprogram.name=NB.bat

   配置初始内存和最大内存

   # Initial Java Heap Size (in MB)
   #wrapper.java.initmemory=3

   # Maximum Java Heap Size (in MB)
   #wrapper.java.maxmemory=64

   配置服务的main class(关键)

   # Application parameters.  Add parameters as needed starting from 1
   wrapper.app.parameter.1=server.DBdaoServer

    配置控制台(不需修改)

    # 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

   

    配置运行记录文件

   # 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: 10m = 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

   配置系统服务名称

   # Name of the service
   wrapper.ntservice.name=RmiService

   配置系统服务显示的名称

   # Display name of the service
   wrapper.ntservice.displayname=RmiService

   配置系统服务的描述

   # Description of the service
   wrapper.ntservice.description=RmiService 

  

    配置系统的服务的启动方式

   # Mode in which the service is installed.  AUTO_START or DEMAND_START
   wrapper.ntservice.starttype=AUTO_START

    附上NB.bat的文件源码:

    @echo offsetlocalremrem Java Service Wrapper general startup scriptremremrem Resolve the real path of the Wrapper.exerem  For non NT systems, the _REALPATH and _WRAPPER_CONF valuesrem  can be hard-coded below and the following test removed.remif "%OS%"=="Windows_NT" goto ntecho This script only works with NT-based versions of Windows.goto :eof:ntremrem Find the application home.remrem %~dp0 is location of current script under NTset _REALPATH=%~dp0set _WRAPPER_EXE=%_REALPATH%Wrapper.exeremrem Find the wrapper.confrem:confset _WRAPPER_CONF="%~f1"if not %_WRAPPER_CONF%=="" goto startupset _WRAPPER_CONF="%_REALPATH%../conf/wrapper.conf"remrem Start the Wrapperrem:startup"%_WRAPPER_EXE%" -c %_WRAPPER_CONF%if not errorlevel 1 goto :eofpause

    以上整个过程,如有错漏,希望指正

           

猜你喜欢

转载自blog.csdn.net/qq_44952688/article/details/89522052
今日推荐