Apache Commons Daemon 1.0.12 發布
Commons Daemon 可以幫你實現將一個普通的 Java 應用變成系統的一個后臺服務。
例如 Tomcat 就是利用這個項目來實現作為 Linux 和 Windows 的服務啟動和停止的
Apache Commons Daemon 1.0.12 發布了,主要是修復了在 1.0.11 中發現的 bug。
/**
* Launch the Engine from a variety of sources, either through a main() or invoked through
* Apache Daemon.
*/
public class EngineLauncher implements Daemon {
private static final Log4J log = Log4J.getLog();
private static Engine engine = null;
private static EngineLauncher engineLauncherInstance = new EngineLauncher();
/**
* The Java entry point.
* @param args Command line arguments, all ignored.
*/
public static void main(String[] args) {
// the main routine is only here so I can also run the app from the command line
engineLauncherInstance.initialize();
Scanner sc = new Scanner(System.in);
// wait until receive stop command from keyboard
System.out.printf("Enter 'stop' to halt: ");
while(!sc.nextLine().toLowerCase().equals("stop"));
if (!engine.isStopped()) {
engineLauncherInstance.terminate();
}
}
/**
* Static methods called by prunsrv to start/stop
* the Windows service. Pass the argument "start"
* to start the service, and pass "stop" to
* stop the service.
*
* Taken lock, stock and barrel from Christopher Pierce's blog at http://blog.platinumsolutions.com/node/234
*
* @param args Arguments from prunsrv command line
**/
public static void windowsService(String args[]) {
String cmd = "start";
if (args.length > 0) {
cmd = args[0];
}
if ("start".equals(cmd)) {
engineLauncherInstance.windowsStart();
}
else {
engineLauncherInstance.windowsStop();
}
}
public void windowsStart() {
log.debug("windowsStart called");
initialize();
while (!engine.isStopped()) {
// don't return until stopped
synchronized(this) {
try {
this.wait(60000); // wait 1 minute and check if stopped
}
catch(InterruptedException ie){}
}
}
}
public void windowsStop() {
log.debug("windowsStop called");
terminate();
synchronized(this) {
// stop the start loop
this.notify();
}
}
// Implementing the Daemon interface is not required for Windows but is for Linux
@Override
public void init(DaemonContext arg0) throws Exception {
log.debug("Daemon init");
}
@Override
public void start() {
log.debug("Daemon start");
initialize();
}
@Override
public void stop() {
log.debug("Daemon stop");
terminate();
}
@Override
public void destroy() {
log.debug("Daemon destroy");
}
/**
* Do the work of starting the engine
*/
private void initialize() {
if (engine == null) {
log.info("Starting the Engine");
... spawn threads etc
}
}
/**
* Cleanly stop the engine.
*/
public void terminate() {
if (engine != null) {
log.info("Stopping the Engine");
engine.stop();
log.info("Engine stopped");
}
}
} 本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!