我如何制作Java守护进程 [英] How can i make a Java Daemon
问题描述
我需要在我的网络应用程序(tomcat上的jsp)中进行定期操作(调用java方法)。
我该怎么做? Java守护程序或其他解决方案?
I need to do a periodic operation (call a java method) in my web app (jsp on tomcat). How can i do this ? Java daemon or others solutions ?
推荐答案
您可以使用 ScheduledExecutorService
。但是,如果您需要更复杂的类似cron的调度,请查看 Quartz 。特别推荐使用 Quartz与Spring结合使用,如果你沿着这条路走下去,因为它提供了一个更好的API,并允许你在配置中控制你的工作。
You could use a ScheduledExecutorService
for periodic execution of a task. However, if you require more complex cron-like scheduling then take a look at Quartz. In particular I'd recommend using Quartz in conjunction with Spring if you go down this route, as it provides a nicer API and allows you to control your job firing in configuration.
ScheduledExecutorService示例(取自Javadoc)
import static java.util.concurrent.TimeUnit.*;
class BeeperControl {
private final ScheduledExecutorService scheduler =
Executors.newScheduledThreadPool(1);
public void beepForAnHour() {
final Runnable beeper = new Runnable() {
public void run() { System.out.println("beep"); }
};
final ScheduledFuture<?> beeperHandle =
scheduler.scheduleAtFixedRate(beeper, 10, 10, SECONDS);
scheduler.schedule(new Runnable() {
public void run() { beeperHandle.cancel(true); }
}, 60 * 60, SECONDS);
}
}
这篇关于我如何制作Java守护进程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!