用Java Service Wrapper 注册一个系统服务

之前要想在系统开机启动一个Java进程,一般都是用脚本写到rc.local中。

还有一种办法是写了个服务脚本把它注册到系统服务,也可以开机启动。并可以通过service命令进行管理。

JavaServiceWrapper就是帮我们来做这个事。

下载地址在这里:

http://wrapper.tanukisoftware.com/doc/english/download.jsp

有三个版本,社区版本是免费的。其他要钱也更高级,可以管理服务进程的一些异常情况。对于注册服务来说,社区版本也够用了。
 

我的原进程就是一个单一个jar包,把所有的依赖都打到一起了。通过下面的脚本启动:

java -jar xxx.jar

这个wrapper怎么用呢。下面就以linux版本做一个例子。

文件复制

下载linux对应版本后解压,从里面找到以下几个文件放到我的程序目录下:

wrapper
wrapper.conf
libwrapper.so
wrapper.jar
demoapp

修改wrapper.conf 

这个是wrapper在启动服务时的一些配置,主要改动如下,其他详细配置可看官方文档。

wrapper.lang.encoding=UTF-8
wrapper.java.command=/usr/local/jdk1.8.0_60/bin/java

wrapper.java.mainclass=org.tanukisoftware.wrapper.WrapperJarApp
wrapper.java.classpath.1=./wrapper.jar
wrapper.java.classpath.2=/usr/local/jdk1.8.0_60/lib/dt.jar
wrapper.java.classpath.3=/usr/local/jdk1.8.0_60/lib/tools.jar
wrapper.java.classpath.4=./xxx.jar
wrapper.java.classpath.5=./log4j2.xml
wrapper.java.classpath.6=META-INF/MANIFEST.MF
wrapper.java.library.path.1=./
wrapper.app.parameter.1=./xxx.jar
wrapper.console.title=@app.long.name@

wrapper.name=@app.name@
wrapper.displayname=@app.long.name@
wrapper.description=@app.description@

修改demoapp

改为当前程序要注册的服务的信息

# Application
APP_NAME="xxx-service"
APP_LONG_NAME="xxx service"

# Wrapper
WRAPPER_CMD="./wrapper"
WRAPPER_CONF="./wrapper.conf"

执行demoapp install

将服务注册到系统

demoapp install

再启动一下系统就OK了

通过jps -lm可以看到启动的进程信息

1464 org.tanukisoftware.wrapper.WrapperJarApp ./xxx.jar

ps -ef | grep java 一下能看到启动了两个进程,有一个是wrapper。

netstat -ntap 还能看到这两个进程之间有一个内部通信连接。

再用service xxx-service status看一下服务状态, xxx-service 是在demoapp脚本里写的名称

xxx service is running: PID:1462, Wrapper:STARTED, Java:STARTED

 

附配置文件详解

Properties added after the initial release are followed by the Wrapper version in which they were implemented.

[A]

[B]

  • wrapper.backend.type (3.5.8)

    Configures the communication between the Wrapper's process and the class running within the JVM.

[C]

[D]

[E]

[F]

[I]

[J]

[L]

[M]

[N]

[O]

  • wrapper.on_exit.<n> (3.1.0)

    Configures to restart the application in the event that it exits with certain exit codes.

[P]

  • wrapper.pausable (3.5.0)

    Enables the PAUSE and RESUME actions related with some properties.

  • wrapper.pausable.stop_jvm (3.5.0)

    Controls what happens when the Wrapper is paused using pausable actions.

  • wrapper.pause_on_startup (3.5.5)

    This will cause the Wrapper to go immediately into a paused state, without ever launching a JVM.

  • wrapper.pidfile (Usage changed in 3.0.5)

    The Wrapper's process ID will be written in this file.

  • wrapper.pidfile.strict (3.5.14)

    Tells the Wrapper to check if the pid file does exists, before it tries to write its pid file.

  • wrapper.pidfile.umask (3.2.0)

    Specifies the umask to use when creating the pid file.

  • wrapper.ping.alert.loglevel (3.5.16)

    Specifies at which log level the ping alert messages will be logged in the log file.

  • wrapper.ping.alert.threshold (3.5.16)

    Specifies that the Wrapper should log a warning in the log file whenever a ping response of the JVM takes longer that a number of seconds.

  • wrapper.ping.interval (3.0.5)

    Configures the number of seconds for the interval between the Wrapper ping requests to the JVM.

  • wrapper.ping.interval.logged (3.2.4)

    Configures the number of seconds for the interval between Wrapper ping requests which will be logged.

  • wrapper.ping.timeout (1.0.0)

    Configures the number of seconds to allow between the Wrapper pinging the JVM and the response from the JVM.

  • wrapper.ping.timeout.action (3.5.16)

    Allows to specify a certain action, which will be performed whenever a ping timeout takes place.

  • wrapper.port (1.0.0)

    Allows the configuration of a port, which the Wrapper uses to communicate with its Java component running inside a JVM.

  • wrapper.port.address (3.5.18)

    Specifies the interface to which the port(s), for communication of the Wrapper with the JVM, should be bound to.

  • wrapper.port.max (3.2.0)
  • wrapper.port.min (3.2.0)

    Controls the min/max range of ports that the Wrapper will use when choosing a port.

  • wrapper.properties.dump.loglevel (3.5.30)

    Controls the log level at which the configuration properties will be logged.

  • wrapper.property_warning.loglevel (3.5.21)

    Controls log level warnings about property values that will be logged.

[R]

[S]

[T]

[U]

  • wrapper.umask (3.2.0)

    Sets the permission used when creating files.

  • wrapper.use_javaio_thread (3.5.8)

    Controls whether the Wrapper uses a dedicated thread to process console output from the JVM.

  • wrapper.use_system_time (3.1.0)

    Controls internally how the Wrapper manages the time and scheduling of events.

[W]

转载于:https://my.oschina.net/piorcn/blog/711865

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值