Java Service Wrapper

项目需要把java的application打成jar包单独跑,在linux环境下

存在问题:部署上去之后,java -jar xxx.jar之后 ,因为用ssh进去的,所以这个窗口关掉之后,他的这个进程也就关掉了,这个是不能接收的

目标:开机自动启动,不用人为干预,是守护进程。普遍做法是写shell脚本,网上查了一些,推荐用Java Service Wrapper 

优点:各个系统大部分都支持,可以做成服务,可以开机启动

下面说结合自身和网络说一下:

参考文章地址:http://blog.csdn.net/lyflower/article/details/6098025

下面在linux的/opt下建立tianlong目录,复制tianlong.jar到/home/cotton/applications/data_receive/lib目录下。

1、下载Java Service Wrapper(目前版本wrapper-linux-x86-64-3.5.26), 解压。
http://wrapper.tanukisoftware.org/
复制wrapper/src/bin/sh.script.in到/home/cotton/applications/data_receive/bin目录,重命名为DataReceive

复制wrapper/bin目录下的wrapper文件到/home/cotton/applications/data_receive/bin目录

复制wrapper/src/conf/wrapper.conf.in到/home/cotton/applications/data_receive/conf目录,重命名为wrapper.conf

复制wrapper/lib目录下3个文件到/home/cotton/applications/data_receive/lib目录

libwrapper.so
wrapper.jar
wrappertest.jar

2、修改/home/cotton/applications/data_receive/bin/DataReceive文件
APP_NAME="DataReceive" //注意这里,这里的应用服务名称应该与第1步的/home/cotton/applications/data_receive/bin里面的DataReceive名字相同
APP_LONG_NAME="DataReceive Application Server"
WRAPPER_CMD="./wrapper" //这里的路径要写正确,这里是对应了/home/cotton/applications/data_receive/bin/wrapper这个文件
WRAPPER_CONF="../conf/wrapper.conf" //这里的路径要写正确,这里是对应了/home/cotton/applications/data_receive/conf/wrapper.conf这个文件
赋予执行权限
chmod 775 /home/cotton/applications/data_receive/bin/DataReceive
chmod 775 /home/cotton/applications/data_receive/bin/wrapper

3、修改/home/cotton/applications/data_receive/conf/wrapper.conf文件
wrapper.java.command=/home/cotton/jdk1.7.0_71/bin/java //这里是写java的安装路径
使用WrapperStartStopApp,这样可以通过命令带start/stop来启动/停止程序。
wrapper.java.mainclass=org.tanukisoftware.wrapper.WrapperStartStopApp
#这里添加你用到的jar包文件,从1..n你自己全写上吧,不过wrapper是必须的 ,我这里面就用到了classpath1和2
wrapper.java.classpath.1=../lib/wrapper.jar
wrapper.java.classpath.2=../lib/Data_receiver.jar //这里表示将打包好的tianlong.jar放在/home/cotton/applications/data_receive/lib这个文件夹中
wrapper.java.classpath.3=../lib/asterisk-java-0.3.jar
wrapper.java.classpath.4=../lib/mysql-connector-java-5.1.6-bin.jar
wrapper.java.classpath.5=/home/cotton/jdk1.7.0_71/lib/dt.jar
wrapper.java.classpath.6=/home/cotton/jdk1.7.0_71/lib/tools.jar
wrapper.java.classpath.7=../cofig.properties //这个是应用程序用到的配置文件。如果使用的应用程序需要用到配置,就需要把这个文件包含进来
wrapper.java.library.path.1=../lib //这里是表示wrapper使用到的jar包的路径,一般默认即可,绝对路径是:/home/cotton/applications/data_receive/lib
#这个就是你自己可执行程序的主类(MainClass),一般来说,就是包含public static void main的那个类
wrapper.app.parameter.1=MainMothed_Class
wrapper.app.parameter.2=1
wrapper.app.parameter.3=true
wrapper.app.parameter.4=MainMothed_Class
wrapper.app.parameter.5=true
wrapper.app.parameter.6=1
wrapper.app.parameter.7=stop

4、让DataReceive开机自动运行
ln -s /home/cotton/applications/data_receive/bin/DataReceive /etc/init.d/DataReceive
ln -s /home/cotton/applications/data_receive/bin/DataReceive /etc/rc0.d/DataReceive
ln -s /home/cotton/applications/data_receive/bin/DataReceive /etc/rc1.d/DataReceive
ln -s /home/cotton/applications/data_receive/bin/DataReceive /etc/rc2.d/DataReceive
ln -s /home/cotton/applications/data_receive/bin/DataReceive /etc/rc3.d/DataReceive
ln -s /home/cotton/applications/data_receive/bin/DataReceive /etc/rc4.d/DataReceive
ln -s /home/cotton/applications/data_receive/bin/DataReceive /etc/rc5.d/DataReceive
ln -s /home/cotton/applications/data_receive/bin/DataReceive /etc/rc6.d/DataReceive

5、测试 执行命令:service DataReceive start|stop|restart|status
程序运行时,Java Service Wrapper在/home/cotton/applications/data_receive/bin/目录下产生wrapper.log日志,
STATUS | wrapper| 2009/02/25 17:04:01 | --> Wrapper Started as Daemon
STATUS | wrapper| 2009/02/25 17:04:01 | Java Service Wrapper Community Edition 3.3.2
STATUS | wrapper| 2009/02/25 17:04:01 | Copyright (C) 1999-2009 Tanuki Software, Ltd.All Rights Reserved.
STATUS | wrapper| 2009/02/25 17:04:01 | http://wrapper.tanukisoftware.org
STATUS | wrapper| 2009/02/25 17:04:01 |
STATUS | wrapper| 2009/02/25 17:04:01 | Launching a JVM...
INFO | jvm 1 | 2009/02/25 17:04:02 | WrapperManager: Initializing...

出现上面这个就表示已经配置成功了。如果配置有错误的话,在wrapper.log里面会有提示。
同时,如果程序配置成功的并且启动的话,在/home/cotton/applications/data_receive/bin里面会有一个DataReceive.pid。

附目录结构
/home/cotton/applications/data_receive/

/home/cotton/applications/data_receive/bin
| DataReceive
| DataReceive.java.status(程序运行时自动产生)
| DataReceive.pid(程序运行时自动产生)
| DataReceive.status(程序运行时自动产生)
| wrapper
| wrapper.log(程序运行时自动产生)
/home/cotton/applications/data_receive/conf
| wrapper.conf
/home/cotton/applications/data_receive/lib
| libwrapper.so
| wrapper.jar
| Data_receiver.jar.jar
| wrappertest.jar

原文地址:https://www.cnblogs.com/aishangyizhihu/p/4721856.html