java 在服务器端为 servlet JSP MVC 网站运行定期任务
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2248971/
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
running periodic task at server side for servlet JSP MVC website
提问by jsshah
I have developed a web application using using servlet and JSP. I am not using any framework per se, instead using my own home brewed MVC framework. I am using MySQL as a backend.
我已经使用 servlet 和 JSP 开发了一个 Web 应用程序。我本身没有使用任何框架,而是使用我自己自制的 MVC 框架。我使用 MySQL 作为后端。
I want to do the following:
我想做以下事情:
- Clean up some data from the data base every hour
- Generate and store statistics about data every 15 minutes in an XML file somewhere
- 每小时从数据库中清理一些数据
- 每 15 分钟生成一次有关数据的统计信息并将其存储在某个 XML 文件中
The problem is: currently all my code runs as a result of the request received from a client.
问题是:目前我所有的代码都是根据从客户端收到的请求而运行的。
How do I run periodic task(s) at the server side?
如何在服务器端运行周期性任务?
One solution I have right now is to creare a thread in the controller's init function. Are there any other options?
我现在的一个解决方案是在控制器的 init 函数中创建一个线程。还有其他选择吗?
回答by BalusC
You can use ServletContextListenerto execute some initialization on webapp's startup. The standard Java API way to run periodic tasks would be a combination of Timerand TimerTask. Here's a kickoff example:
您可以使用ServletContextListener在 webapp 启动时执行一些初始化。运行周期性任务的标准 Java API 方式是Timer和的组合TimerTask。这是一个启动示例:
public void contextInitialized(ServletContextEvent event) {
Timer timer = new Timer(true);
timer.scheduleAtFixedRate(new CleanDBTask(), 0, oneHourInMillis);
timer.scheduleAtFixedRate(new StatisticsTask(), 0, oneQuartInMillis);
}
where the both tasks can look like:
这两个任务看起来像:
public class CleanDBTask extends TimerTask {
public void run() {
// Implement.
}
}
Using Timeris however not recommended in Java EE. If the task throws an exception, then the entire Timerthread is killed and you'd basically need to restart the whole server to get it to run again. The Timeris also sensitive to changes in system clock.
利用Timer然而,不使用Java EE建议。如果任务抛出异常,则整个Timer线程都会被终止,您基本上需要重新启动整个服务器才能使其再次运行。该Timer也是在系统时钟的变化很敏感。
The newer and more robust java.util.concurrentway would be a combination of ScheduledExecutorServiceand just a Runnable. Here's a kickoff example:
较新的和更强大的java.util.concurrent方式将是一个组合ScheduledExecutorService,只是一个Runnable。这是一个启动示例:
private ScheduledExecutorService scheduler;
public void contextInitialized(ServletContextEvent event) {
scheduler = Executors.newSingleThreadScheduledExecutor();
scheduler.scheduleAtFixedRate(new CleanDBTask(), 0, 1, TimeUnit.HOURS);
scheduler.scheduleAtFixedRate(new StatisticsTask(), 0, 15, TimeUnit.MINUTES);
}
public void contextDestroyed(ServletContextEvent event) {
scheduler.shutdownNow();
}
回答by Puran
you can use any schedular to schedule your process like quartz, spring scheduler
您可以使用任何调度程序来安排您的进程,如石英、弹簧调度程序
http://static.springsource.org/spring/docs/2.5.x/reference/scheduling.htmlhas a good support for these stuffs with any implementation.
http://static.springsource.org/spring/docs/2.5.x/reference/scheduling.html对任何实现的这些东西都有很好的支持。

