1、首先到请下载最新版本的Java Service Wrapper,
可以到 http://wrapper.tanukisoftware.org/doc/english/introduction.html 原始站点下载最新版。
也可以使用本站使用中的一个版本 点我下载 。
2、准备如下文件,复制到test目录,这些文件可以从下载包中找到:
wrapper.dll
wrapper.exe
wrapper.jar
3、编写wrapper.conf,也放在同一级目录下面,内容如下
# Java Application完整路径
wrapper.java.command=java
wrapper.java.mainclass=org.tanukisoftware.wrapper.WrapperSimpleApp
#定义程序运行的classpath
## 注意你需要的jar都写上,包括wrapper.jar和当前目录(点)
wrapper.java.classpath.1=wrapper.jar
wrapper.java.classpath.2=d:/tomcat/shared/lib/commons-logging-1.1.jar
wrapper.java.classpath.3 = .
# Java Library Path (location of Wrapper.DLL or libwrapper.so)
wrapper.java.library.path.1=./
# Java Additional Parameters
#wrapper.java.additional.1=
# Initial Java Heap Size (in MB)
# 初始化的内存使用数量
#wrapper.java.initmemory=16
# Maximum Java Heap Size (in MB)
# 最大的内存使用数量
#wrapper.java.maxmemory=64
# 运行的Main Class
# 请携带路径,写上完整的运行主类的全名
wrapper.app.parameter.1= mypackage.MyMainService
# Port which the native wrapper code will attempt to connect to
# 占用的端口,不要可存在的有冲突即可。用于与服务的通信。
wrapper.port=15013
wrapper.console.format=PM
wrapper.console.loglevel=INFO
wrapper.logfile=Beanskt.log
wrapper.logfile.format=LPTM
wrapper.logfile.loglevel=INFO
wrapper.logfile.maxsize=0
wrapper.logfile.maxfiles=0
wrapper.syslog.loglevel=NONE
#window服务配置
#在控制台运行时的标题
wrapper.console.title=My Service
#服务名
wrapper.ntservice.name=My Service
# 显示名称
wrapper.ntservice.displayname= My Service
# 描述
wrapper.ntservice.description=sample Service
# 依赖项
wrapper.ntservice.dependency.1=
# 启动模式: AUTO_START or DEMAND_START
wrapper.ntservice.starttype=AUTO_START
# 是否交互.
wrapper.ntservice.interactive=false
4、运行测试,安装服务
》测试运行是否正常
wrapper.exe -c wrapper.conf
》安装服务
wrapper.exe -i wrapper.conf
》卸载服务
wrapper.exe -r wrapper.conf
》启动
wrapper.exe -t wrapper.conf
》停止
wrapper.exe -p wrapper.conf
提示:
服务一般都是连续运行的,所以你的代码一般都是包含在一个死循环里面的。比如
while(true){
if(forceStop){
break;
}
try{
// 代码的主体
}catch(Exception ex){
// 异常处理,防止循环退出
}
}
附属一个我的完整版的配置文件
#********************************************************************
# Wrapper Properties
#********************************************************************
# Java Application
wrapper.java.command=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 1
wrapper.java.classpath.1=d:/tomcat/shared/lib/java2000.jar
wrapper.java.classpath.2=d:/tomcat/shared/lib/commons-logging-1.1.jar
wrapper.java.classpath.3=wrapper.jar
wrapper.java.classpath.4=.
# Java Library Path (location of Wrapper.DLL or libwrapper.so)
wrapper.java.library.path=./
# Java Additional Parameters
# wrapper.java.additional.1=-Djava.ext.dirs=../lib;../tools/lib
# wrapper.java.additional.2=-Dphoenix.home=..
# wrapper.java.additional.3=-Djava.security.policy=jar:file:phoenix-loader.jar!/META-INF/java.policy
# wrapper.java.additional.4=-Djava.security.manager
# Initial Java Heap Size (in MB)
wrapper.java.initmemory=16
# Maximum Java Heap Size (in MB)
wrapper.java.maxmemory=64
# Application parameters. Add parameters as needed starting from 1
wrapper.app.parameter.1=com.triuse.tools.Downloador
#********************************************************************
# Wrapper Advanced Properties
#********************************************************************
# Port which the native wrapper code will attempt to connect to
wrapper.port=15013
# Number of seconds to allow for the JVM to be launched and contact the wrapper before the
# wrapper should assume that the JVM is hung and terminate the JVM process. 0 means never
# time out. Defaults to 30 seconds.
wrapper.startup.timeout=30
# Number of seconds to allow between the wrapper pinging the JVM and the response. 0 means
# never time out. Defaults to 30 seconds.
wrapper.ping.timeout=30
# Number of seconds to allow for the JVM to shutdown before the wrapper should assume that
# the JVM is hung and terminate the JVM process. 0 means never time out. Defaults to 30
# seconds.
wrapper.shutdown.timeout=30
# The Wrapper detects when an application calls System.exit() and treats this as a request
# to stop the server by default.
#wrapper.disable_shutdown_hook=TRUE
# Tell the Wrapper to request a JVM thread dump if the JVM fails to exit when requested.
#wrapper.request_thread_dump_on_failed_jvm_exit=TRUE
#********************************************************************
# Wrapper Logging parameters
#********************************************************************
# 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/wrapper.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=5m
# 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 Unix daemon parameters
#********************************************************************
# File to write process ID to
#wrapper.pidfile=/var/run/phoenix.pid
#********************************************************************
# Wrapper Windows Properties
#********************************************************************
# Title to use when running as a console
wrapper.console.title=Triuse Downloador 1.0
#********************************************************************
# Wrapper Windows NT/2000/XP 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 service
wrapper.ntservice.name=Triuse Downloador
# Display name of the service
wrapper.ntservice.displayname=Triuse Downloador
# Description of the service
wrapper.ntservice.description=Triuse Downloador 1.0
# Service dependencies. Add dependencies as needed starting from 1
wrapper.ntservice.dependency.1=
# Mode in which the service is installed. AUTO_START or DEMAND_START
wrapper.ntservice.starttype=AUTO_START
# Priority at which the service is run. NORMAL, LOW, HIGH, or REALTIME
wrapper.ntservice.process_priority=NORMAL
# Allow the service to interact with the desktop.
wrapper.ntservice.interactive=false