ITeye博客

将标准的java程序封装为Windows下面的服务

lanmh · 2010-10-19 14:02 · JAVA · 编程语言 · Markdown

1、首先到请下载最新版本的Java Service Wrapper,
可以到 http://wrapper.tanukisoftware.org/doc/english/introduction.html 原始站点下载最新版。
也可以使用本站使用中的一个版本 点我下载 。
2、准备如下文件,复制到test目录,这些文件可以从下载包中找到:
wrapper.dll
wrapper.exe
wrapper.jar
3、编写wrapper.conf,也放在同一级目录下面,内容如下

 

Java代码 复制代码

  1. # Java Application完整路径
  2. wrapper.java.command=java
  3. wrapper.java.mainclass=org.tanukisoftware.wrapper.WrapperSimpleApp
  4. #定义程序运行的classpath
  5. ## 注意你需要的jar都写上,包括wrapper.jar和当前目录(点)
  6. wrapper.java.classpath.1=wrapper.jar
  7. wrapper.java.classpath.2=d:/tomcat/shared/lib/commons-logging-1.1.jar
  8. wrapper.java.classpath.3 = .
  9. # Java Library Path (location of Wrapper.DLL or libwrapper.so)
  10. wrapper.java.library.path.1=./
  11. # Java Additional Parameters
  12. #wrapper.java.additional.1=
  13. # Initial Java Heap Size (in MB)
  14. # 初始化的内存使用数量
  15. #wrapper.java.initmemory=16
  16. # Maximum Java Heap Size (in MB)
  17. # 最大的内存使用数量
  18. #wrapper.java.maxmemory=64
  19. # 运行的Main Class
  20. # 请携带路径,写上完整的运行主类的全名
  21. wrapper.app.parameter.1= mypackage.MyMainService
  22. # Port which the native wrapper code will attempt to connect to
  23. # 占用的端口,不要可存在的有冲突即可。用于与服务的通信。
  24. wrapper.port=15013
  25. wrapper.console.format=PM
  26. wrapper.console.loglevel=INFO
  27. wrapper.logfile=Beanskt.log
  28. wrapper.logfile.format=LPTM
  29. wrapper.logfile.loglevel=INFO
  30. wrapper.logfile.maxsize=0
  31. wrapper.logfile.maxfiles=0
  32. wrapper.syslog.loglevel=NONE
  33. #window服务配置
  34. #在控制台运行时的标题
  35. wrapper.console.title=My Service
  36. #服务名
  37. wrapper.ntservice.name=My Service
  38. # 显示名称
  39. wrapper.ntservice.displayname= My Service
  40. # 描述
  41. wrapper.ntservice.description=sample Service
  42. # 依赖项
  43. wrapper.ntservice.dependency.1=
  44. # 启动模式: AUTO_START or DEMAND_START
  45. wrapper.ntservice.starttype=AUTO_START
  46. # 是否交互.
  47. wrapper.ntservice.interactive=false
 
# 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){
// 异常处理,防止循环退出
}
}

附属一个我的完整版的配置文件

 

Java代码 复制代码

  1. #********************************************************************
  2. # Wrapper Properties
  3. #********************************************************************
  4. # Java Application
  5. wrapper.java.command=java
  6. # Java Main class.  This class must implement the WrapperListener interface
  7. #  or guarantee that the WrapperManager class is initialized.  Helper
  8. #  classes are provided to do this for you.  See the Integration section
  9. #  of the documentation for details.
  10. wrapper.java.mainclass=org.tanukisoftware.wrapper.WrapperSimpleApp
  11. # Java Classpath (include wrapper.jar)  Add class path elements as
  12. #  needed starting from 1
  13. wrapper.java.classpath.1=d:/tomcat/shared/lib/java2000.jar
  14. wrapper.java.classpath.2=d:/tomcat/shared/lib/commons-logging-1.1.jar
  15. wrapper.java.classpath.3=wrapper.jar
  16. wrapper.java.classpath.4=.
  17. # Java Library Path (location of Wrapper.DLL or libwrapper.so)
  18. wrapper.java.library.path=./
  19. # Java Additional Parameters
  20. # wrapper.java.additional.1=-Djava.ext.dirs=../lib;../tools/lib
  21. # wrapper.java.additional.2=-Dphoenix.home=..
  22. # wrapper.java.additional.3=-Djava.security.policy=jar:file:phoenix-loader.jar!/META-INF/java.policy
  23. # wrapper.java.additional.4=-Djava.security.manager
  24. # Initial Java Heap Size (in MB)
  25. wrapper.java.initmemory=16
  26. # Maximum Java Heap Size (in MB)
  27. wrapper.java.maxmemory=64
  28. # Application parameters.  Add parameters as needed starting from 1
  29. wrapper.app.parameter.1=com.triuse.tools.Downloador
  30. #********************************************************************
  31. # Wrapper Advanced Properties
  32. #********************************************************************
  33. # Port which the native wrapper code will attempt to connect to
  34. wrapper.port=15013
  35. # Number of seconds to allow for the JVM to be launched and contact the wrapper before the
  36. #  wrapper should assume that the JVM is hung and terminate the JVM process.  0 means never
  37. #  time out.  Defaults to 30 seconds.
  38. wrapper.startup.timeout=30
  39. # Number of seconds to allow between the wrapper pinging the JVM and the response.  0 means
  40. #  never time out.  Defaults to 30 seconds.
  41. wrapper.ping.timeout=30
  42. # Number of seconds to allow for the JVM to shutdown before the wrapper should assume that
  43. #  the JVM is hung and terminate the JVM process.  0 means never time out.  Defaults to 30
  44. #  seconds.
  45. wrapper.shutdown.timeout=30
  46. # The Wrapper detects when an application calls System.exit() and treats this as a request
  47. #  to stop the server by default.
  48. #wrapper.disable_shutdown_hook=TRUE
  49. # Tell the Wrapper to request a JVM thread dump if the JVM fails to exit when requested.
  50. #wrapper.request_thread_dump_on_failed_jvm_exit=TRUE
  51. #********************************************************************
  52. # Wrapper Logging parameters
  53. #********************************************************************
  54. # Format of output for the console.  (See docs for formats)
  55. wrapper.console.format=PM
  56. # Log Level for console output.  (See docs for log levels)
  57. wrapper.console.loglevel=INFO
  58. # Log file to use for wrapper output logging.
  59. wrapper.logfile=./logs/wrapper.log
  60. # Format of output for the log file.  (See docs for formats)
  61. wrapper.logfile.format=LPTM
  62. # Log Level for log file output.  (See docs for log levels)
  63. wrapper.logfile.loglevel=INFO
  64. # Maximum size that the log file will be allowed to grow to before
  65. #  the log is rolled. Size is specified in bytes.  The default value
  66. #  of 0, disables log rolling.  May abbreviate with the 'k' (kb) or
  67. #  'm' (mb) suffix.  For example: 10m = 10 megabytes.
  68. wrapper.logfile.maxsize=5m
  69. # Maximum number of rolled log files which will be allowed before old
  70. #  files are deleted.  The default value of 0 implies no limit.
  71. wrapper.logfile.maxfiles=0
  72. # Log Level for sys/event log output.  (See docs for log levels)
  73. wrapper.syslog.loglevel=NONE
  74. #********************************************************************
  75. # Wrapper Unix daemon parameters
  76. #********************************************************************
  77. # File to write process ID to
  78. #wrapper.pidfile=/var/run/phoenix.pid
  79. #********************************************************************
  80. # Wrapper Windows Properties
  81. #********************************************************************
  82. # Title to use when running as a console
  83. wrapper.console.title=Triuse Downloador 1.0
  84. #********************************************************************
  85. # Wrapper Windows NT/2000/XP Service Properties
  86. #********************************************************************
  87. # WARNING - Do not modify any of these properties when an application
  88. #  using this configuration file has been installed as a service.
  89. #  Please uninstall the service before modifying this section.  The
  90. #  service can then be reinstalled.
  91. # Name of the service
  92. wrapper.ntservice.name=Triuse Downloador
  93. # Display name of the service
  94. wrapper.ntservice.displayname=Triuse Downloador
  95. # Description of the service
  96. wrapper.ntservice.description=Triuse Downloador 1.0
  97. # Service dependencies.  Add dependencies as needed starting from 1
  98. wrapper.ntservice.dependency.1=
  99. # Mode in which the service is installed.  AUTO_START or DEMAND_START
  100. wrapper.ntservice.starttype=AUTO_START
  101. # Priority at which the service is run.  NORMAL, LOW, HIGH, or REALTIME
  102. wrapper.ntservice.process_priority=NORMAL
  103. # Allow the service to interact with the desktop.
  104. wrapper.ntservice.interactive=false
 
#********************************************************************
# 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

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。