Linux上的Java服务 – 如何确保正常运行时间。 Deamon,Shell脚本还是Wrapper?

我有一个Java工作人员通过Web服务调用轮询外部队列系统的作业。

确保工人在任何特定时间运作的最坚实的方法是什么?

JVM执行与其他程序没有区别。 所以你想要做的就是把一个shell脚本放在一起,放在/etc/init.d中,然后把它合适地链接到/etc/rc.d。 在RedHat中,它将确保系统启动服务。

拧脚本可能会很棘手,但我会复制现有的一个,并改变它调用正确的参数的Java可执行文件。 在这个脚本中,你将不得不捕获java进程ID。 您可以使用它来监视您的过程,并根据需要重新启动。

java.util.concurrent包为这类工作提供了一个简单,可靠的框架。

下面是一些可以工作的代码的简单例子:

 import java.util.concurrent.*; public static void main(String[] args) { ScheduledExecutorService service = Executors.newScheduledThreadPool(1); // Here's an anonymous class, but your should probably create a class for this Runnable poll = new Runnable() { public void run() { // put your polling code here } }; // Have your code called every 5 seconds like this: service.scheduleAtFixedRate(poll, 0, 5, TimeUnit.SECONDS); }