[英]When to shutdown ScheduledExecutorService?
I have a singleton that needs to start a scheduled execution. 我有一个需要开始计划执行的单例。 This is the code: 这是代码:
public enum Service{
INSTANCE;
private Service() {
startAutomaticUpdate();
}
private void startAutomaticUpdate() {
try {
ScheduledExecutorService executor = Executors.newScheduledThreadPool(1);
executor.scheduleAtFixedRate(new AutomaticUpdate(), 0, 15, TimeUnit.MINUTES);
} catch (Exception e) {
LOG.error(e.getMessage() + "Automatic update not working: ");
}
}
//Makes a call to a webservice that updates a static variable.
private void getTemplateNames(){...}
private class AutomaticUpdate implements Runnable {
public AutomaticUpdate() {
}
@Override
public void run(){
try{
getTemplateNames();
}catch(Exception e){
LOG.error("Error in automatic update: "+e.getMessage());
}
}
}
I am not sure when or if I should call the shutdown method of the executor. 我不确定何时或是否应该调用执行程序的shutdown方法。 I'm using JEE5, so I'm not sure if simply undeploying the app will automatically execute the shutdown, or if I am messing up big time and creating a ridiculous amount of threads and not killing them. 我使用的是JEE5,因此不确定是否简单地取消部署应用程序即可自动执行关闭操作,或者是否浪费大量时间并创建了大量的线程而不杀死它们。
-EDIT- -编辑-
I'll add a bit more info, just in case. 为了防万一,我将添加更多信息。
The whole app is a RESTful web app using Jersey as a ServletContainer. 整个应用程序都是一个RESTful Web应用程序,使用Jersey作为ServletContainer。
You said JEE5? 你说JEE5? Why you're reeinventing the wheel? 为什么要重新发明轮子?
Just create a EJB with @Schedule
and @Startup
只需使用 @Schedule
和@Startup
创建EJB
@Singleton @Startup public class TaskSingleton { @Schedule(second = "0", minute = "*/15", hour = "*")//This mean each 15:00 minutes public void getTemplateNames() { // YOUR TASK IMPLEMENTATION HERE } }
No you don't mean JEE5 complaint server. 不,您不是说JEE5投诉服务器。 :( :(
Go for the implementation with a ServletContextListener. 使用ServletContextListener进行实现。 I wrote some answer like that here , It's the same idea, it does applies here. 我在这里写了这样的答案,这是相同的想法,它确实适用于这里。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.