- 浏览: 153099 次
文章分类
最新评论
-
飘零雪:
[b][/b][i][/i][u][/u]引用
自定义Mave archetype的创建 -
fujohnwang:
<div class="quote_title ...
基于iBatis的开源分布式数据访问层 -
gzenzen:
<pre name="code" c ...
基于iBatis的开源分布式数据访问层 -
fujohnwang:
bornwan 写道我就很想知道分布式数据源,水平切分之后排序 ...
基于iBatis的开源分布式数据访问层 -
fujohnwang:
gzenzen 写道什么时候支持mybatis3、spring ...
基于iBatis的开源分布式数据访问层
As to desktop or normal Java applications, we can easily know when we should shutdown the application or not, because users have explicit ways to do this, for example, for a Swing Application, usually a "CLOSE" menu or tool-bar item will be available, or directly click the "X" icon on the left/right top of the window. But for a Java application that will be run as a server process(which don't need interactive behavior), what we do?
Directly “kill -9 ” ? Of course, that's a way, but that's too brutal.
A Java process that will be run as a server process usually will be sent to OS's background to run, that's called “daemon ” on Unix and “service ” on Windows. A Simple way to control the life-cycle of a Java daemon is to start a loop and wait for user input, like this:
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); String line = null; do { line = reader.readLine(); if (line != null && line.equals("quit")) { break; // and exit gracefully } } while (true); // clean up and exit
This solution is better than just start an infinite loop and do nothing, because the latter solution will occupy all of your CPU's power(I have seen such a stupid solution and it does exist). But this is still not a best one, although users can interactive with it, the process itself have no way to notify the process to exit. That's, you can control it from outside, but can't do it from inside.
Another mostly used solution is, start a TCP server socket and listen control requests, when termination control request is received, a loop based on control flag will break and exit. This is similar to above solution, just with another communication channel. [1 ]
A common pattern for both solution can be found,that's, set up a control flag to start a loop with, and then wait for other channels to change the control flag. This can be formulated as:
boolean running = true; while(running) { // do Sth. } // exit
As to how to change the control flag, there are two solutions presented, can you find more?
Old Days Solutions have their pros and cons, today more solutions are available for us.
The first one is Jakarta Commons Daemon . It provides a Java daemon solution with native Jsvc and Procrun support.
The second and third are Java Service Wrapper and yajsw , The former is a commerce solution now, and the latter is an open source one which has some works based on the former(there is a possibility that I misunderstand its introduction).
Other Solutions are Akuma , Start-Stop-Daemon, or Classword , but I don't get patience to read their document. If you are interested in them,follow the links I provide or google them.
I choose to combine shell and “sun.misc.Signal ” and “sun.misc.SignalHandler ” to achieve Java daemon, because they are much simpler to me to understand them and use them. The Shell will take care of running-in-background stuff:
#!/bin/sh java -cp your_class_path com.domain.main_class <&- & pid=$! echo ${pid} > mydaemon.pid
And “sun.misc.Signal ” and “sun.misc.SignalHandler ” will take care of controlling the life-cycle of the program.
About “sun.misc.Signal ” and “sun.misc.SignalHandler ” , you can find more information at 参考文档 , here I just simply introduce how to use them together to achieve asynchronous communication between processes or just internally in a same process.
The concept of “sun.misc.Signal ” and “sun.misc.SignalHandler ” is simple:
-
Signal is the signal that u will send to SignalHandler to process, so you can create a Signal just like instantiate a simple value object:
Signal sig = new Signal("USR1");
The signal names you pass to “Signal ” conform a pattern, that's, remove the prefix "SIG" from the name of the standard signals that's used by JVM [2 ] . For example, if you want to send “SIGINT ” , then you create “Signal ” instance with name of “INT ” ; If you want to send “SIGTERM ” , you create “Signal ” instance with name of “TERM ” :
Signal interactiveSignal = new Signal("INT"); Signal terminationSignal = new Signal("TERM");
Fucking Simple, right?
After you have a Signal, you can send it out by using “Signal ” class's “raise ” method:
Signal.raise(sig); Signal.raise(interactiveSignal); Signal.raise(terminationSignal);
-
As the name indicates, “SignalHandler ” will take the responsibility of handling the “Signal ” s.
You implements your own signal handlers by implementing the “SignalHandler ” interface. It has only one method:
public class MySigHandler implements SignalHandler { public void handle(Signal sig) { // ... } }
It's fucking simple too.
After you get both “Signal ” and “SignalHandler ” of your own, you should link them together to make it work. This is by “Signal ” class's static method “handle ” :
MySigHandler sigHandler = new MySigHandler(); Signal.handle(sig, sigHandler); Signal.handle(interactiveSignal, sigHandler); Signal.handle(terminationSignal, sigHandler);
Now as long as you add them to your java programs and send proper signals to it, the pairs of “Signal ” and “SignalHandler ” will work for you.
You have 2 ways to send signals to your program's process:
-
Use Signal.raise() internally. This can help to coordinate application's internal state and help to control the life-cycle internally. For example, as long as internal worker thread dies, it can send out a signal, when signal hander finds that all other the worker threads die, it can change the control flag of the whole process and terminate it gracefully.
-
Send Signal from other processes. directly send out supported OS signal via shell scripts:
kill -s SIGUSR1 <pid of the process>
combining the pid you get in before shell, this works perfectly.
The only cons to use “Signal ” and “SignalHandler ” is, they are both restricted API which are not guaranteed.
发表评论
-
基于iBatis的开源分布式数据访问层
2011-03-28 11:46 5472http://code.alibabatech.com/wik ... -
分布式数据访问与同步场景浅析
2010-09-06 19:50 2189分布式数据访 ... -
Netty Framework Tips And Gotchas
2010-08-11 18:01 2624王福强(Darren.W ... -
有关Maven编译DeprecatedAPI失败的问题
2010-08-02 10:59 4436在项目代码里用了sun.misc.Signal ... -
Event Driven Style API Design Instead of Old Procedure Style Ones
2010-07-12 19:53 1437王福强(Darren.Wang) <f ... -
HA狭义与广义论
2010-07-09 09:25 1426Author: Darren Wang(fujohnwang) ... -
Why We Need A Global ID Generator?!
2010-05-18 13:01 1585Table of Contents 1. Pai ... -
Gotchas With JUnit's Execution Model
2010-03-26 09:22 1005Maybe you have known it before, ... -
Transaction Management Patterns In Brief
2010-02-09 10:27 1728There are several patte ... -
"扩展Spring的依赖注入行为"两例
2009-12-26 12:59 2687扩展Spring的依赖注入行为两例 ... -
框架API设计相关的碎言
2009-11-17 09:32 1548框架的API设计,应该是 ... -
自定义Mave archetype的创建
2009-10-29 20:12 12277Table of Contents ... -
看来有人已经有要抢先推出这个节目的意思了
2009-10-27 19:29 974这篇blog对java, clojure和scala中的并发处 ... -
Roma Documentation Outline
2009-10-27 17:35 150Roma Docume ... -
Hot Stuff - Lombok
2009-10-22 19:46 982give it a try, it's really cool ... -
ROMA框架潜在改进点思考(Thinking in ROMA improvements)
2009-10-21 19:53 1931. 关于ROMA现有表单 ... -
Valang Validator under the hood
2009-10-19 13:29 1616Table of Contents 1. Va ... -
ThreadSafety, Non-ThreadSafety 与 Stateless, Stateful有必然的对应关系吗?
2009-10-09 09:11 1819“It depends. ” 我们 ... -
A Big Piture On Concurrency
2009-09-12 09:49 11993- Concurrency Share (Concur ... -
尴尬的COC
2009-08-25 11:04 951Convention Over Configurati ...
相关推荐
java daemon 程序,有助于理解守护线程的概念和使用
【IT十八掌徐培成】Java基础第08天-02.多线程-join-daemon-同步.zip
这是一个简单的Java应用程序,它将使用Apache Commons守护程序作为守护程序在Unix / Linux操作系统... 可以将java_daemon_init脚本复制到/etc/init.d/(并重命名为适当的名称),然后以常规的{start | stop}方式使用。
commons-daemon+Weld启动windows 主要为了自己记录而已
ThreadDaemon.java
java中的daemon thread java中有两种类型的thread,user threads 和 daemon threads。 User threads是高优先级的thread,JVM将会等待所有的User Threads运行完毕之后才会结束运行。 daemon threads是低优先级的...
Daemon_Tools Daemon_Tools Daemon_Tools Daemon_Tools
虚拟光驱 daemon_tools_347cn_eric虚拟光驱 daemon_tools_347cn_eric虚拟光驱 daemon_tools_347cn_eric虚拟光驱 daemon_tools_347cn_eric虚拟光驱 daemon_tools_347cn_eric虚拟光驱 daemon_tools_347cn_eric虚拟光驱...
标签:commons、daemon、jar包、java、中文文档; 使用方法:解压翻译后的API文档,用浏览器打开“index.html”文件,即可纵览文档内容。 人性化翻译,文档中的代码和结构保持不变,注释和说明精准翻译,请放心使用...
daemon 程序例子daemon 程序例子daemon 程序例子
Daemon Tools v4.03 V4.03HE 汉化版,相信经常使用虚拟光驱的人都应该记得,这是当年极其经典的一个版本。 现在的虚拟光驱越做越大,功能也越来越多,最新的版本Daemon Tools 安装后要占用硬盘50多M,然尔对一般的...
daemontool4.11中文完美版daemontool4.11中文完美版daemontool4.11中文完美版
daemon.rar 虚拟光驱,安装iso文件的好东西
Daemon Tools 3.46 简体中文版.rar 内有Daemon Tools 3.46 简体中文版.rar; daemon.log
daemon344光驱模拟器
嵌入式开发中如何保障设备稳定、可靠地运行是一个重要的问题。采用PCD(Process Control Daemon)程序可以在不需要对程序做任何修改的情况下,对多个进程进行监控,从而保障系统稳定运行。
虚拟光驱daemon
本资源是打包好的一套demo实例,适用于64位jre。如需32位请自行更换jre包或引用路径,同时更换支持32位的prunsrv.exe,相关32位和64位的工具请下载我的资源 commons Daemon+Procrun
此程序能帮助你实现将命令行程序变成服务运行,比如将"java -jar xxx.jar" 放在后台执行。 ./start-stop-daemon --help start-stop-daemon 1.9.18 for Debian - small and fast C version written by Marek ...
daemon341.exe 虚拟光驱