Java 如何在Tomcat中安排任务
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18226126/
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 schedule a task in Tomcat
提问by dileepVikram
I have a web application deployed in Tomcat. I have a set of code in it, that checks database for certain data and then sends a mail to users depending on that data. Can somebody suggest how to schedule this in Tomcat.
我在 Tomcat 中部署了一个 Web 应用程序。我有一组代码,用于检查数据库中的某些数据,然后根据该数据向用户发送邮件。有人可以建议如何在Tomcat中安排这个。
回答by madhead
It depends on libraries you use. Several libraries can do that:
这取决于您使用的库。有几个库可以做到这一点:
- Quartz/ Example for Tomcat.
- Spring.
- A class from Java SE.
- If you run on GAE take a look at this.
- Quartz/ Tomcat 示例。
- 春天。
- 来自 Java SE 的类。
- 如果你在 GAE 上运行,看看这个。
回答by Pablo Gallego Falcón
You can use a listener and cron4j:
您可以使用侦听器和cron4j:
@WebListener
public class StartListener implements ServletContextListener {
@Override
public void contextInitialized(final ServletContextEvent servletContextEvent) {
Scheduler scheduler = new Scheduler();
scheduler.schedule("0 * * * *", new Task());
scheduler.start();
servletContextEvent.getServletContext().setAttribute("SCHEDULER", scheduler);
}
回答by Benjamin Leconte
Actually, the best way to schedule task in Tomcat is to use ScheduledExecutorService. TimeTask should not be used in J2E applications, this is not a good practice.
实际上,Tomcat 中调度任务的最佳方式是使用 ScheduledExecutorService。不应在 J2E 应用程序中使用 TimeTask,这不是一个好的做法。
Example with the right way :
正确方法的例子:
create a package different that you controller one (servlet package), and create a new java class on this new package as example :
创建一个与您控制的包不同的包(servlet 包),并在这个新包上创建一个新的 java 类作为示例:
// your package
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;
@WebListener
public class BackgroundJobManager implements ServletContextListener {
private ScheduledExecutorService scheduler;
@Override
public void contextInitialized(ServletContextEvent event) {
scheduler = Executors.newSingleThreadScheduledExecutor();
// scheduler.scheduleAtFixedRate(new DailyJob(), 0, 1, TimeUnit.DAYS);
scheduler.scheduleAtFixedRate(new HourlyJob(), 0, 1, TimeUnit.HOURS);
//scheduler.scheduleAtFixedRate(new MinJob(), 0, 1, TimeUnit.MINUTES);
// scheduler.scheduleAtFixedRate(new SecJob(), 0, 15, TimeUnit.SECONDS);
}
@Override
public void contextDestroyed(ServletContextEvent event) {
scheduler.shutdownNow();
}
}
After that you can create other java class (one per schedule) as follow :
之后,您可以创建其他 Java 类(每个计划一个),如下所示:
public class HourlyJob implements Runnable {
@Override
public void run() {
// Do your hourly job here.
System.out.println("Job trigged by scheduler");
}
}
Enjoy :)
享受 :)