Linux 如何编写 Java 守护进程
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7814222/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to write a Java daemon
提问by Aaron Bruce
This will be a network application that will always (or near as always as I can manage) be listening on a given port.
这将是一个网络应用程序,它将始终(或几乎像我所能管理的那样)一直侦听给定端口。
I'm fairly new to Java, and very new to non-web server side programming, so I'd like to get feedback from the community on my assumptions and preliminary plans.
我对 Java 还很陌生,对非 Web 服务器端编程也很陌生,所以我想从社区获得关于我的假设和初步计划的反馈。
I've read about jsvc ( http://commons.apache.org/daemon/jsvc.html) and am currently operating on the assumption that this is the "best" way to write a daemon in java for a linux box (likely running centOS).
我已经阅读了有关 jsvc ( http://commons.apache.org/daemon/jsvc.html) 的内容,并且目前我认为这是在 java 中为 linux box 编写守护程序的“最佳”方式(可能是运行centOS)。
Can nagios be configured to monitor whether or not my daemon is running, and to alert me or the sys admin when it isn't? (I assume yes, but I'm not a very talented sys admin type)
nagios 是否可以配置为监视我的守护程序是否正在运行,并在未运行时提醒我或系统管理员?(我假设是的,但我不是一个非常有才华的系统管理员类型)
This will be an SMPP client app (or ESME app I guess) which is why I've chosen Java as it seems to be a very mature platform for SMPP. However, I know that it's more "traditional" to write a daemon in C/C++. With modern Java, performing fairly uncomplicated tasks, am I likely to run into any major disadvantages?
这将是一个 SMPP 客户端应用程序(或者我猜是 ESME 应用程序),这就是我选择 Java 的原因,因为它似乎是一个非常成熟的 SMPP 平台。但是,我知道用 C/C++ 编写守护进程更“传统”。使用现代 Java 执行相当简单的任务,我可能会遇到任何重大缺点吗?
What's the best way to manage deployment of new builds? Just stop the daemon and replace the binary as quickly as possible and restart?
管理新构建部署的最佳方法是什么?只是停止守护进程并尽快替换二进制文件并重新启动?
Any other input would be greatly appreciated.
任何其他输入将不胜感激。
采纳答案by Gray
How to write a Java daemon that has 24/7 uptime...
如何编写具有 24/7 正常运行时间的 Java 守护进程...
We run a number of 24/365 applications on our Linux servers that just call the Java like the following -- no need for any C wrappers:
我们在 Linux 服务器上运行许多 24/365 应用程序,它们只调用 Java,如下所示——不需要任何 C 包装器:
nohup java -D... -X... -jar something.jar ... < /dev/null > output.log 2>&1 &
That will put the jar running in the background (nohup ... &
) with no input (< /dev/null
) and the output (stdout and stderr) redirected to a logfile (> output.log 2>&1
). We have distributed logging infrastructure but some console output (such as thread dumps) is still expected. These applications can run for months until we upgrade them.
这将使 jar 在nohup ... &
没有输入 ( < /dev/null
)的情况下在后台 ( ) 中运行,并且输出 (stdout 和 stderr) 会重定向到日志文件 ( > output.log 2>&1
)。我们有分布式日志基础设施,但仍然需要一些控制台输出(例如线程转储)。这些应用程序可以运行数月,直到我们升级它们。
Can nagios be configured to monitor whether or not my daemon is running, and to alert me or the sys admin when it isn't?
nagios 是否可以配置为监视我的守护程序是否正在运行,并在未运行时提醒我或系统管理员?
In terms of monitoring, there is much you can do. Nagios looks to have a JMX pluginfor testing the information that jconsole
displays. There are also a lot of native JMX logging and monitoring utilities out there. We have internal green/yellow/red indicators that can be pulled up using JMX and easily checked. I also have exported a simple JMX/HTTP servicefrom each application to provide status information making it easy for 3rd party monitoring tools to detect failures.
在监控方面,您可以做很多事情。Nagios 看起来有一个JMX 插件来测试jconsole
显示的信息。还有很多本机 JMX 日志记录和监控实用程序。我们有内部的绿色/黄色/红色指示器,可以使用 JMX 调出并轻松检查。我还从每个应用程序导出了一个简单的 JMX/HTTP 服务,以提供状态信息,使 3rd 方监控工具可以轻松检测故障。
This will be an SMPP client app (or ESME app I guess) which is why I've chosen Java as it seems to be a very mature platform for SMPP.
这将是一个 SMPP 客户端应用程序(或者我猜是 ESME 应用程序),这就是我选择 Java 的原因,因为它似乎是一个非常成熟的 SMPP 平台。
I assume you mean SMPP? If so then I see no reason why Java couldn't do a good job. Our applications do a wide variety of HTTP, UDP, SMTP, JDBC, LDAP, and other protocols in real time. We use Jgroupsat lot which accomplishes a complete authenticated, encrypted, network stack in Java.
我想你的意思是SMPP?如果是这样,那么我就没有理由认为 Java 不能做好。我们的应用程序实时执行各种 HTTP、UDP、SMTP、JDBC、LDAP 和其他协议。我们大量使用Jgroups,它在 Java 中完成了一个完整的经过身份验证的、加密的网络堆栈。
What's the best way to manage deployment of new builds? Just stop the daemon and replace the binary as quickly as possible and restart?
管理新构建部署的最佳方法是什么?只是停止守护进程并尽快替换二进制文件并重新启动?
In terms of replacing a running binary on the fly, that it more complicated. We have VIPsup front and replace the binaries at our leisure. Our internal protocols are designed to failover. If you do not have a VIP then one thing to consider would be an orderly handoff. You boot the new jar and it talks to the application running the old jar when it is ready to bind to the new port. Then the old application unbinds and the new one binds immediately afterwards. Something like that.
就动态替换正在运行的二进制文件而言,它更复杂。我们预先设置了VIP,并在闲暇时替换二进制文件。我们的内部协议旨在进行故障转移。如果您没有 VIP,那么需要考虑的一件事是有序交接。您启动新 jar,当它准备好绑定到新端口时,它会与运行旧 jar 的应用程序对话。然后旧应用程序解除绑定,新应用程序随即绑定。类似的东西。
Hope this helps.
希望这可以帮助。
回答by krico
If you really want to have something running non-stop on *nix, I recommend you have a look at daemontools.
如果你真的想让一些东西在 *nix 上不停地运行,我建议你看看daemontools。
There are some examples on how to do this hereand here.
Basically svscan will spawn a process that monitors your java process from init, and every time it crashes, it gets restarted.
基本上 svscan 会产生一个进程,从 init 监视你的 java 进程,每次它崩溃时,它都会重新启动。