天天看點

java windows系統服務_Java實作Windows系統服務

Windows系統服務(NT服務)相對于普通應用程式最直接的一個優點是系統啟動後就可直接運作而無需使用者登入系統。事實上,作為伺服器來使用的系統,通常也并不需要登入系統,這樣不隻是友善,也提升了系統的安全性。不過,通常情況下,Windows系統服務使用C或C++實作,而有些時候基于某些因素的考慮,我們期望使用Java來實作系統服務,可以借助開源的JavaService達到此目的。

以下示範其實作過程。

首先編寫實作NT服務的Java類,以下的示例代碼通過兩個類來達到實作NT服務的目的。類TestService提供了NT服務啟動及停止的控制方法,而類Service則實作了NT服務真正要完成的工作。類TestService及類Service的完整代碼如下:

package com.yanzhijun;

import java.util.Calendar;

public class TestService

{

private static Thread thread=null;

private static Service service = null;

public static void StopService(String[] args)

{

System.out.println("停止服務");

service.setRunFlag(false);

}

public static void StartService(String[] args)

{

System.out.println("啟動服務");

// 産生服務線程

service = new Service();

thread=new Thread(service);

try

{

// 将服務線程設定為使用者線程,以避免StartService方法結束後線程退出

thread.setDaemon(false);

if(!thread.isDaemon())

{

System.out.println("成功設定線程為使用者線程!");

}

//啟動服務線程

thread.start();

}

catch(SecurityException se)

{

System.out.println("線程類型設定失敗!");

}

}

}

class Service implements Runnable

{

private boolean runFlag = true;

public synchronized void setRunFlag(boolean runFlag)

{

this.runFlag = runFlag;

}

private synchronized boolean getRunFlag()

{

return runFlag;

}

@Override

public void run()

{

System.out.println("服務線程開始運作");

while(getRunFlag())

{

Calendar cal = Calendar.getInstance();

long mis = cal.getTimeInMillis();

System.out.println("目前時間:" + mis);

try

{

Thread.sleep(1000*10);

}

catch (InterruptedException e)

{

e.printStackTrace();

}

}

System.out.println("服務線程結束運作");

}

}

類TestService中的方法StartService用于啟動NT服務,而StopService方法則用于停止NT服務。從代碼中可以看出方法StartService與StopService實際上是分别實作了啟動線程與終止線程的功能,而這裡的線程就是實作NT服務的線程。這裡有兩點要特别注意:

1.方法StartService與StopService都擁有參數String[] args,在安裝NT服務時,可以指定啟動或停止服務的參數,這些指定的參數将可以通過String[] args傳遞給方法StartService與StopService。在本例中由于示範的功能較為簡單,故而未曾使到該參數。

2.在StartService方法中啟動線程之前,有必要将線程設定為使用者線程。其原因在于如果線程是一個背景線程,則當主程式結束後,JVM會自動退出,背景線程當然也就終止執行,而如果主程式結束時,還有使用者線程在運作,則JVM不會自動退出,而是要等使用者線程結束後才退出。是以,要保證NT服務正常運作,這一點也是要特别注意的。

類Service是實作NT服務的線程類,NT服務要完成的功能在方法run中完成,在示例中它僅僅輸出了目前時間,在實際應用中應根據需要在其中完成更為複雜的功能。方法setRunFlag與getRunFlag分别用于設定線程是否運作的标志和取得該标志值。setRunFlag方法需要在類外被調用并通過設定不同的參數通知線程是否需要繼續執行;getRunFlag方法則取得該标志,它隻在方法run的循環中被調用,是以聲明為私有方法。此外,由于在不同線程中對線程運作标志進行設定與讀取,是以方法setRunFlag與getRunFlag被設定為同步方法。

代碼編寫完成後,執行編譯過程,必要時還可以打包成jar檔案。

随後的工作就是利用JavaService注冊NT服務,JavaService是一個開源項目,其項目位址為,打開該位址後下載下傳JavaService壓縮包,并将解壓之後的JavaService.exe置于上述代碼編譯之後包所在的目錄,或者将JavaService.exe所在目錄添加到環境變量PATH之中。

JavaService一共提供了8個參數可供選擇,其中我們隻需要關心安裝NT服務的-install參數和解除安裝NT服務的-uninstall參數。

使用-install參數安裝NT服務時還需要提供與服務相關的其它一些參數,其指令格式如下:

JavaService -install service_name jvm_library [jvm_options]

-start start_class [-method start_method] [-params (start_parameters)]

[-stop start_class [-method stop_method] [-params (stop_parameters)]]

[-out out_log_file] [-err err_log_file]

[-current current_dir]

[-path extra_path]

[-depends other_service]

[-auto | -manual]

[-shutdown seconds]

[-user user_name -password password]

[-append | -overwrite]

[-startup seconds]

[-description service_desc]

相關參數的作用說明如下:

service_name: The name of the service.

jvm_library:The location of the JVM DLL used to run the service.

jvm_option:An option to use when starting the JVM, such as:

"-Djava.class.path=c:/classes" or "-Xmx128m".

start_class:The class to load when starting the service.

start_method: The method to call in the start_class. default: main

start_parameters:Parameter(s) to pass in to the start_method.

stop_class:The class to load when stopping the service.

stop_method:The method to call in the stop_class. default: main

stop_parameters:Parameter(s) to pass in to the stop_method.

out_log_file: A file to redirect System.out into. (gets overwritten)

err_log_file: A file to redirect System.err into. (gets overwritten)

current_dir:The current working directory for the service.

Relative paths will be relative to this directory.

extra_path:Path additions, for native DLLs etc. (no spaces)

other_service:Single service name dependency, must start first.

auto / manual:Startup automatic (default) or manual mode.

seconds:Java method processing time (startup:sleep, shutdown:timeout).

user_name:User specified to execute the service ([email protected]).

password:Password applicable if user specified to run the service.

append / overwrite:Log file output mode, append (default) or overwrite.

service_desc: Text describing installed service (quoted string, max 1024).

要安裝前面我們用Java編寫的NT服務,可以用以下指令完成:

JavaService.exe -install TJS "%JAVA_HOME%/jre/bin/server/jvm.dll" -Xmx128m -Djava.class.path=%CLASSPATH% -start com.yanzhijun.TestService -method StartService -stop com.yanzhijun.TestService -method StopService -out "%CD%/out.log" -err "%CD%/err.log" -current "%CD%" –auto

上述指令中用“%”包含的是環境變量,其中JAVA_HOME是JDK的安裝目錄,CLASSPATH是為類的查找路徑,這兩者與正常配置JDK保持一緻即可。CD則是目前目錄。

成功安裝服務以後,可以用以下指令啟動服務:

net start TJS

由于在安裝服務時指定了auto參數,是以服務被設定為自動啟動,是以機器重新開機後無需使用上述指令這個NT服務就會啟動。

如果需要停止服務,可以使用下列指令:

net stop TJS

這樣就可以正常地使用Java所編寫的NT服務了,如果不需要該服務,則應将該服務解除安裝,解除安裝服務的指令相比安裝要簡單地多,其格式如下:

JavaService -uninstall service_name

例如要解除安裝剛才安裝的名稱為TJS的服務,可以執行以下指令:

JavaService -uninstall TJS

注意:Java代碼所生成的NT服務類所存儲的目錄路徑中不能包含中文,否則啟動服務時會失敗。