JAVA加入windows系統服務(Java Service Wrapper)

來源:互聯網
上載者:User

    因為工作需要要開發一個分布式的系統,但是在伺服器架設上就遇到問題。因為一直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才可以直接讀到。

  

    

 

 

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

 

   然後注意配置/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: 10m = 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 then 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

 

  

   配置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 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<br />rem Find the application home.<br />rem<br />rem %~dp0 is location of current script under NT<br />set _REALPATH=%~dp0<br />set _WRAPPER_EXE=%_REALPATH%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

 

 

    以上整個過程,如有錯漏,希望指正

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.