Java Daemon 撰寫與佈署

這篇不是真正教如何設計Java Daemon,考量如何利用別人寫好的Java Daemon能夠比較容易整合於不同環境中。 如通常不會自行開發工作排程系統,利用已成熟Quarts為基礎往上堆應用程式,雖然是已成熟Java Daemon要如何整合於不同的作業系統(Linux, Windows, Mac) ?

這個問題說難不難說簡單不簡單,解決方案也五花八門。傳統方式就是撰寫 public static void main(String[]) 統一由這一支Java程式啟動服務,然後撰寫一堆shell呼叫, 雖然問題也是可以解決但是往後系統維護管理不易也沒有統一。

Apache Commons Daemon 專案滿不錯解決方案之一

...Most multi-user operating systems already have a way in which server applications are started and stopped. Under Unix based operating systems non interactive server applications are called daemons and are controlled by the operating system with a set of specified signals. Under Windows such programs are called services and are controlled by appropriate calls to specific functions defined in the application binary, but although the ways of dealing with the problem are different, in both cases the operating system can notify a server application of its imminent shutdown, and the application has the ability to perform certain tasks before its process of execution is destroyed.

配合 Jsvc bootstrap

Jsvc is a set of libraries and applications for making Java applications run on UNIX more easily. Jsvc allows the application (e.g. Tomcat) to perform some privileged operations as root (e.g. bind to a port < 1024), and then switch identity to a non-privileged user.

簡易撰寫 Java Daemon 程式

[code lang="java"] import org.apache.commons.daemon.Daemon; import org.apache.commons.daemon.DaemonContext; public class MyDaemon implements Daemon { private boolean isShutdown = false; // void init(String[] arguments): Here open configuration files, create a trace file, create ServerSockets, Threads public void init(DaemonContext context) throws Exception { } // void start(): Start the Thread, accept incoming connections public void start() throws Exception { while (!isShutdown) { System.out.println("do something..."); } } // void stop(): Inform the Thread to terminate the run(), close the ServerSockets public void stop() throws Exception { isShutdown = true; } //void destroy(): Destroy any object created in init() public void destroy() { } } [/code]


[code lang="bash"] sudo ./jsvc -debug -jvm server -cp crawler-1.0.jar:commons-daemon-1.0.3.jar com.abc.daemon.MyDaemon sudo ./jsvc -stop -debug -jvm server -cp crawler-1.0.jar:commons-daemon-1.0.3.jar com.abc.daemon.MyDaemon [/code]


[code] $ Using specific JVM in /System/Library/Frameworks/JavaVM.framework/Home/../Libraries/libserver.dylib Attemtping to load library /System/Library/Frameworks/JavaVM.framework/Home/../Libraries/libserver.dylib JVM library /System/Library/Frameworks/JavaVM.framework/Home/../Libraries/libserver.dylib loaded Attemtping to load library /System/Library/Frameworks/JavaVM.framework/Home/../Libraries/libverify.dylib Shell library /System/Library/Frameworks/JavaVM.framework/Home/../Libraries/libverify.dylib loaded JVM library entry point found (0x00090B80) +-- DUMPING JAVA VM CREATION ARGUMENTS ----------------- | Version: 0x010004 | Ignore Unrecognized Arguments: False | Extra options: 1 | "-Djava.class.path=crawler-1.0.jar:commons-daemon-1.0.3.jar" (0x00000000) +------------------------------------------------------- | Internal options: 4 | "-Dcommons.daemon.process.id=25010" (0x00000000) | "-Dcommons.daemon.process.parent=25009" (0x00000000) | "-Dcommons.daemon.version=1.0.5" (0x00000000) | "abort" (0x00005e9e) +------------------------------------------------------- Java VM created successfully Class org/apache/commons/daemon/support/DaemonLoader found Native methods registered java_init done Daemon loading... init... Daemon loaded successfully java_load done do something... [/code]

可參考/etc/init.d範本撰寫Shell掛在開機啟動,在windows 環境可用Procrun 視為服務來啟動。 利用bootstrap觀念讓Java Application能夠簡易佈署在不同的作業環境中,同時統一撰寫Daemon方式及想法後續維護管理比較一致。


No comments:

Post a Comment