Java 守护进程的更简单的初始化脚本?
我编写了一个小型库,用于用 Java 创建服务/守护进程。这个想法很简单。当您启动应用程序时,您将传递命令和端口号。如果该命令是启动命令,则应用程序的新实例将在指定端口上启动。否则,该命令将被发送到可能在该端口上运行的任何实例。
简而言之,该库提供了一个启动守护程序控制器线程的 daemonize 方法。它使用套接字使您的应用程序与其自身的实例进行通信(正如您可能已经发现的那样)。
为了清楚起见,这里有一个如何使用它的示例:
public class MyApp extends Daemon
{
public static void main(String[] args)
{
if (daemonize(MyApp.class, args))
{
// normal main body
startMyServerOrWhatever();
}
else
{
// failed to start or send command to daemon
// probably wrong syntax or unknown command
printUsageInfoAndExit();
}
}
@Command(start = true)
public static int start()
{
// executed on "start" command, e.g. java -jar MyApp.jar start 8899
doSomeInitializing();
return 0; // return 0 or void to detach from console
}
@Command
public static void mycmd()
{
// executed on "mycmd" command, i.e. java -jar MyApp.jar mycmd 8899
doSomethingCool();
}
@Command(stop = true)
public static int stop()
{
// executed on "stop" command, i.e. java -jar MyApp.jar stop 8899
doSomeCleanup();
return 0; // used as application exit code
}
}
该库运行得非常好,我已经使用它创建了几个将在 Linux 服务器上运行的守护程序。现在缺少的是一些脚本,可以让管理员控制这些守护进程,就像控制服务器上的其他守护进程一样(例如在启动时启动)。
不幸的是,我的 *nix 技能,特别是在脚本编写方面,并不是顶级的。我对 BSD 风格的 init 过程 (rc.d) 有基本的了解,但查看 这个我感觉有点失落。
所以我的问题是,对于我的情况,是否有更简单的方法来做到这一点?我的意思是,我的守护进程已经理解这些命令,并且它们自己应该对任何操作负责(除非守护进程没有响应停止的情况 - 它应该在超时后被杀死)。
I've written a small library for creating services/daemons in Java. The idea is simple. When you start your application you pass a command and a port number. If the command is a start command, a new instance of your application will be started on the specified port. Otherwise, the command will be sent to any instance that might be running on that port.
In short, the library provides a daemonize method that starts a daemon controller thread. It uses sockets to make your application communicate with instances of itself (as you've probably already figured out).
For clarity, here's an example of how you would use it:
public class MyApp extends Daemon
{
public static void main(String[] args)
{
if (daemonize(MyApp.class, args))
{
// normal main body
startMyServerOrWhatever();
}
else
{
// failed to start or send command to daemon
// probably wrong syntax or unknown command
printUsageInfoAndExit();
}
}
@Command(start = true)
public static int start()
{
// executed on "start" command, e.g. java -jar MyApp.jar start 8899
doSomeInitializing();
return 0; // return 0 or void to detach from console
}
@Command
public static void mycmd()
{
// executed on "mycmd" command, i.e. java -jar MyApp.jar mycmd 8899
doSomethingCool();
}
@Command(stop = true)
public static int stop()
{
// executed on "stop" command, i.e. java -jar MyApp.jar stop 8899
doSomeCleanup();
return 0; // used as application exit code
}
}
The library works really well and I've used it to create a couple of daemons that will run on a Linux server. What's missing now is some scripts to let the admins control these daemons like they control other daemons on the server (e.g. start at boot).
Unfortunately my *nix skills, especially when it comes to scripting, are not top level. I have a basic understanding of the BSD-style init procedure (rc.d), but looking at sample scripts like this one I feel a little lost.
So my question is, isn't there a simpler way to do this in my case? I mean, my daemons already understand the commands and should themselves be responsible for any actions (except in the case where a daemon doesn't respond to stop - it should then be killed after some timeout).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您确实应该看看 tanuki 软件的 java 服务包装器。
请参阅 http://wrapper.tanukisoftware.com/
我喜欢他们方法的一点是他们有标准化的守护程序和使用单一工具和通用脚本进行 Windows 服务进程。
我注意到这个工具在一些备受瞩目的项目中得到了很好的采用,例如 Nexus、ServiceMix 等。
当我遇到一个采用Java Service Wrapper来管理守护进程的项目时,那么命令集和配置对我来说已经很熟悉了,这降低了学习曲线。
也许您可以将套接字控制器机制融入到这个现有框架中。
You should really have a look at the java service wrapper by tanuki software.
See http://wrapper.tanukisoftware.com/
What I like about their approach is that they have standardized deamon and windows service processes with a single tool, and common scripts.
I have noticed a good level of adoption of this tool across some high profile projects such as nexus, servicemix and others.
And when I encounter a project that has adopted the Java Service Wrapper for managing deamon processes, then the command set and configuration is already familiar to me, which lowers the learning curve.
Perhaps you could fit your socket controller mechanism into this existing framework.
我曾经编写过 守护进程脚本 来通过 SSH 启动我们的 java 应用程序, ?它们是简约的 - 没有强制终止或 rc.d/SMF 集成,只有使用 TERM 信号的守护进程启动和关闭。
I wrote once daemon scripts to start our java application over SSH. They are minimalistic - no force kill or rc.d/SMF integration, only daemon startup and shutdown using TERM signal.