我们有时候可能需要将java程序运行成为后台service,我们可以使用apache daemon来实现,借助里面的函数,编写我们的bat脚本,然后运行该脚本,即可将我们的程序运行为一个service
apache daemon下载地址和文档参考:http://commons.apache.org/proper/commons-daemon/
也可以到我的资源库下载程序和daemon工具:http://download.csdn.net/download/harderxin/10225669
下面我们来做一个小demo:
例如,我们需要将下面的测试程序运行成为我们的service,我们编写两个方法,一个方法用于启动service后需要执行的逻辑,一个方法用于停止service后需要执行的逻辑,因为是做测试,所以程序比较简单,我做的都是获取连接数据库的驱动对象:
package com.harderxin.test;
import java.sql.Connection;
import java.sql.DriverManager;
public class DaemonTest {
public static Connection getDBConnection() throws Exception {
String driverName = "net.sourceforge.jtds.jdbc.Driver";
String dbURL = "jdbc:jtds:sqlserver://T-SQL01-W2K12/Report_data;instance=INT01";
Class.forName(driverName);
Connection dbConn = DriverManager.getConnection(dbURL);
return dbConn;
}
/**
* the main method
* @param args
* @throws Exception
*/
public static void main(String[] args)throws Exception {
System.out.println(getDBConnection());
}
/**
* start method
* @param args
* @throws Exception
*/
public static void start(String[] args)throws Exception{
System.out.println(getDBConnection());
}
/**
* stop method
* @param args
* @throws Exception
*/
public static void stop(String[] args)throws Exception{
System.out.println(getDBConnection());
}
}
这里面需要一个依赖包:jtds-1.3.1.jar,使用这种方式来获取数据库连接的Connection对象。编写我们的bat脚本:
"C:\test\daemon\amd64\prunsrv.exe" //TS//TestService --DisplayName="Test Service" ^
--Install="C:\test\daemon\amd64\prunsrv.exe" --Jvm=auto --StartMode=jvm --StopMode=jvm ^
--Classpath="C:\test\test.jar" ^
--LibraryPath="%PATH%;C:\x64\SSO" ^
--StartClass=com.harderxin.test.DaemonTest --StartMethod=start ^
--StopClass=com.harderxin.test.DaemonTest --StopMethod=stop
下面针对上面的Daemon参数一一解释一下:
"C:\test\daemon\amd64\prunsrv.exe" 表示我们下载的Daemon中的prunsrv.exe文件,我们就是根据这个程序,然后执行里面的参数,来将java程序运行成为后台服务的,它后面接了很多参数:
--DisplayName:显示为后台服务的名称,在windows系统的service服务中显示出来
--Install:prunsrv.exe运行的具体路径,根据操作系统的不同,下载后会对应不同的版本:adm64、adm32以及ia64,我们需要根据不同操作系统来决定
--Jvm:jvm的设置,我们可以设置为auto,也可以设置为jre或者jdk的jvm
JVM=%MY_JAVA_HOME%\jre\bin\server\jvm.dll
--StartMode、--StopMode:启动和停止模式,我们可以选择jvm
--Classpath:需要运行jar包的位置,可以用正则表达式指定:set "CLASSPATH=%CURRENT_DIR%\lib\*"
--LibraryPath:这个参数是可选的,因为我上面的程序运行需要依赖一个dll文件:ntlmauth.dll,这个文件表示我们在连接数据库驱动的时候可以不使用用户名+密码的方式,而是采用同一个domain下实例的方式进行的:jdbc:jtds:sqlserver://T-SQL01-W2K12/Report_data;instance=INT01,这就涉及到授权验证,所以我们需要设置这个dll文件的环境变量,当然如果我们在本地做测试,可以直接将它拷贝到系统的环境变量中即可
--StartClass:启动类 --StartMethod:启动方法
--StopClass:停止类 --StopMethod:停止方法
根据ClassPath+StartClass/StopClass+StartMethod/StopMethod这种jar包+类名+方法名即可运行一个具体的方法
还有很多的启动参数,我们可以在文档中了解:http://commons.apache.org/proper/commons-daemon/procrun.html
eg:"%EXECUTABLE%" //IS//%SERVICE_NAME% ^
--Description "%SERVICE_DISPLAY_DESC%" ^
--DisplayName "%SERVICE_DISPLAY_NAME%" ^
--ServiceUser "%SERVICE_USER%" ^
--ServicePassword "%SERVICE_PASSWORD%" ^
--Install "%EXECUTABLE%" ^
--LogPath "%LOG_PATH%" ^
--PidFile "%PID_FILE%" ^
--Classpath "%CLASSPATH%" ^
--Jvm "%JVM%" ^
--StartMode jvm ^
--StopMode jvm ^
--StartPath "%CURRENT_DIR%" ^
--StopPath "%CURRENT_DIR%" ^
--StartClass %START_CLASS% ^
--StopClass %STOP_CLASS% ^
--StartMethod %START_METHOD% ^
--StopMethod %STOP_METHOD% ^
--StartParams %START_PARAMS% ^
--StopParams %STOP_PARAMS% ^
--Startup "%SERVICE_STARTUP_MODE%" ^
--JavaHome "%MY_JAVA_HOME%" ^
--LibraryPath "%PATH%;%JTDS_NTLMAUTH_PATH%" ^
--JvmMs "%JvmMs%" ^
--JvmMx "%JvmMx%"
当我们准备完脚本后,在cmd命令执行该bat脚本,即可将我们的程序运行为后台服务。
下面是我这个程序运行的目录及执行结果:
在后台service中即可看到我们的Test Service服务: