如何编写Java守护进程 [英] How to write a Java daemon

查看:126
本文介绍了如何编写Java守护进程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这将是一个网络应用程序,它总是(或接近我可以管理的)一直在监听给定的端口。

This will be a network application that will always (or near as always as I can manage) be listening on a given port.

我很新Java,非新的非Web服务器端编程,所以我想从我的假设和初步计划中获得社区的反馈。

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.

我读过关于jsvc( http://commons.apache.org/daemon/jsvc.html )并且我目前正在假设这是在java中为linux盒子编写守护进程的最佳方式(可能运行centOS)。

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).

可以配置nagios监视我的守护程序是否正在运行,并在不执行时提醒我或系统管理员? (我假设是的,但我不是一个非常有才华的系统管理员类型)

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)

这将是一个SMPP客户端应用程序(或我猜的ESME应用程序)这就是为什么我选择Java因为它似乎是SMPP非常成熟的平台。但是,我知道在C / C ++中编写守护进程更传统。使用现代Java,执行相当简单的任务,我是否会遇到任何主要缺点?

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?

管理新构建部署的最佳方法是什么?只是停止守护进程并尽快更换二进制文件并重新启动?

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.

推荐答案


如何编写具有24/7正常运行时间的Java守护程序...

How to write a Java daemon that has 24/7 uptime...

我们在Linux服务器上运行了许多24/365应用程序,只需调用Java,如下所示 - 不需要任何C包装器:

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:

nohup java -D... -X... -jar something.jar ... < /dev/null > output.log 2>&1 &

这会让jar在后台运行( nohup ...& ; )没有输入(< / dev / null ),输出(stdout和stderr)重定向到日志文件(> 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.


可以将nagios配置为监视我的守护程序是否正在运行,并发出警报我或者系统管理员不是吗?

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希望有一个 JMX插件测试显示 jconsole 的信息。还有很多本机JMX日志记录和监视实用程序。我们有内部绿色/黄色/红色指示灯,可以使用JMX上拉并轻松检查。我还从每个应用程序中导出了简单的JMX / HTTP服务,以提供状态信息制作第三方监控工具很容易检测到故障。

In terms of monitoring, there is much you can do. Nagios looks to have a JMX plugin for 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 service from each application to provide status information making it easy for 3rd party monitoring tools to detect failures.


这将是一个SMPP客户端应用程序(或我猜的ESME应用程序)这就是为什么我选择了Java,因为它似乎是SMPP非常成熟的平台。

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 ?如果是这样,那么我认为Java没有理由不能做好。我们的应用程序实时执行各种HTTP,UDP,SMTP,JDBC,LDAP和其他协议。我们在批次中使用 Jgroups ,以Java完成经过验证的完整加密网络堆栈。

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 Jgroups at lot which accomplishes a complete authenticated, encrypted, network stack in 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?

在动态替换正在运行的二进制文件方面,它更复杂。我们预先贵宾并在我们闲暇时更换二进制文件。我们的内部协议旨在实现故障转移。如果你没有VIP,那么要考虑的一件事就是有序的交接。你启动新jar,当它准备好绑定到新端口时,它会与运行旧jar的应用程序进行通信。然后旧的应用程序解除绑定,然后新的应用程序立即绑定。类似的东西。

In terms of replacing a running binary on the fly, that it more complicated. We have VIPs up 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.

希望这会有所帮助。

这篇关于如何编写Java守护进程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆