- 浏览: 156219 次
文章分类
最新评论
-
飘零雪:
[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 5533http://code.alibabatech.com/wik ... -
分布式数据访问与同步场景浅析
2010-09-06 19:50 2235分布式数据访 ... -
Netty Framework Tips And Gotchas
2010-08-11 18:01 2662王福强(Darren.W ... -
有关Maven编译DeprecatedAPI失败的问题
2010-08-02 10:59 4467在项目代码里用了sun.misc.Signal ... -
Event Driven Style API Design Instead of Old Procedure Style Ones
2010-07-12 19:53 1470王福强(Darren.Wang) <f ... -
HA狭义与广义论
2010-07-09 09:25 1461Author: Darren Wang(fujohnwang) ... -
Why We Need A Global ID Generator?!
2010-05-18 13:01 1620Table of Contents 1. Pai ... -
Gotchas With JUnit's Execution Model
2010-03-26 09:22 1045Maybe you have known it before, ... -
Transaction Management Patterns In Brief
2010-02-09 10:27 1761There are several patte ... -
"扩展Spring的依赖注入行为"两例
2009-12-26 12:59 2726扩展Spring的依赖注入行为两例 ... -
框架API设计相关的碎言
2009-11-17 09:32 1606框架的API设计,应该是 ... -
自定义Mave archetype的创建
2009-10-29 20:12 12349Table of Contents ... -
看来有人已经有要抢先推出这个节目的意思了
2009-10-27 19:29 1014这篇blog对java, clojure和scala中的并发处 ... -
Roma Documentation Outline
2009-10-27 17:35 150Roma Docume ... -
Hot Stuff - Lombok
2009-10-22 19:46 1024give 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 1657Table of Contents 1. Va ... -
ThreadSafety, Non-ThreadSafety 与 Stateless, Stateful有必然的对应关系吗?
2009-10-09 09:11 1855“It depends. ” 我们 ... -
A Big Piture On Concurrency
2009-09-12 09:49 12353- Concurrency Share (Concur ... -
尴尬的COC
2009-08-25 11:04 992Convention Over Configurati ...
相关推荐
### Java调试指南:详解关键调试工具与方法 #### 概述 本文档旨在提供一份全面且深入的Java应用问题调试指南。随着Java平台标准版(Java Platform, Standard Edition,简称Java SE)的发展与普及,软件开发者们面临...
标签“工具”可能意味着会讨论一些有助于调试和分析线程的工具,如JConsole、VisualVM或Java Mission Control(JMC),这些工具可以帮助开发者监控线程状态、检测死锁、分析CPU和内存使用情况。 "Multi-Threads ...
#### Control 控制是指程序中的逻辑流程管理。在 Java 中,控制结构包括条件语句(如 if、switch)、循环语句(如 for、while)等,用于决定程序的执行路径。 #### Convention 约定是指在软件开发过程中遵循的...
Developers can choose from various destinations such as files, a `java.io.Writer`, or a syslog daemon. This flexibility enables the logging output to be customized according to specific needs. **...
5. **Java编程**:作为Java开发的应用,mpd-control利用了Java的跨平台特性,同时可以利用Android SDK提供的各种服务和组件。 ### 开发技术 - **Android SDK**:mpd-control的开发离不开Android SDK,它提供了开发...
- **Control**:控制,在软件工程中,通常指控制流或控制结构,用于管理程序执行的顺序和方式。 - **Convention**:约定,在软件开发中,为简化协作而制定的一套规则或惯例。 - **CORBA (Common Object Request ...
app_ril_control 一个简单的应用程序,使用root权限来停止/启动ril-daemon。 该存储库已存档,并且是只读的。 请访问 ,以获取此存储库的开放和可编辑版本。 我所有的存储库都已复制到GitLab。 请张贴任何问题,...
* Jace:Java Application Control Engine,指代各种没有界面的,嵌入式设备平台。 * Supervisor:通常指运行在一个工作站或服务器上运行的 station。 * Client:通常的 client 是指那些运行着桌面操作系统,比如 ...
还有一些第三方库,如Apache Commons Daemon,它可以提供Java API来管理Windows服务。你可以通过这些库来简化与Windows服务交互的过程。 4. **在J2EE应用中集成:** 在J2EE应用中,你可以将以上实现包装成一个...
$ ipfs daemon 控制台 我们还有一个Web控制台,可用于检查节点的状态。 在您喜欢的Web浏览器上,转到: http://localhost:5001/webui 这应该调出一个这样的控制台: 代码 // 1. 创建公有网关方式 IPFS ipfs = new ...
6. **安全性**:Tomcat提供了一套安全配置机制,包括 Realm(认证域)用于用户身份验证,Role(角色)定义权限,以及Access Control列表限制访问。用户应根据应用需求配置server.xml和context.xml文件以增强安全性。...
【标题】"sshd.rar_S2S2H3 d_s2s2h3" 提供了一个关于集成开发环境的线索,这里的"sshd"可能指的是Secure Shell Daemon,它是一个广泛用于远程登录服务的安全协议。而"S2S2H3"则暗示了这是一个基于Java技术栈的项目,...
CICS Transaction Gateway(简称CICSTG)是IBM提供的一种中间件技术,它允许Java应用程序通过标准的Java API来访问CICS(Customer Information Control System)环境中的业务逻辑和服务。CICSTG为Java开发人员提供了...
以下是对 MAXIMO 控制中心(Control Center)默认安装路径下各个子目录的详细介绍: 1. **`C:\Program Files\ControlCenter`**: - **`jre`**:Java 运行环境目录,用于支持 MAXIMO 控制中心中的 Java 应用程序...
TCP(Transmission Control Protocol)是一种面向连接、可靠的传输协议,广泛应用于网络通信中,包括移动设备上的应用程序。以下是对这个"安卓TCP服务端代码"主题的详细解析。 1. **TCP基础知识**: - TCP是传输层...