如何使 servlet 在销毁之前等待当前任务完成?
更新...
@WebListener
public class EmailServlet implements ServletContextListener {
private ScheduledExecutorService executorService;
ScheduledFuture scheduledFuture;
private int delay = 300000;
private int repeat = 300000;
@Override
public void contextInitialized(ServletContextEvent event) {
System.out.println("The email servlet started");
executorService = Executors.newSingleThreadScheduledExecutor();
scheduledFuture = executorService.scheduleWithFixedDelay(new Task3(), delay, repeat, TimeUnit.MILLISECONDS);
}
@Override
public void contextDestroyed(ServletContextEvent event) {
scheduledFuture.cancel(true);
executorService.shutdown();
System.out.println("The email servlet stopped");
}
}