2
我遇到問題ManagedScheduledExecutorService。如果我禁用或取消部署我的應用程序,調度程序仍在運行。它僅在重新啓動應用程序服務器時停止。計劃在禁用或取消部署應用程序後仍然運行
我使用JEE,應用服務器Wildfly 9和Java8。
以下以下代碼:
package br.com.decarli;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Date;
import javax.annotation.Resource;
import javax.enterprise.concurrent.LastExecution;
import javax.enterprise.concurrent.ManagedScheduledExecutorService;
import javax.enterprise.concurrent.Trigger;
import javax.enterprise.context.ApplicationScoped;
import javax.enterprise.context.Destroyed;
import javax.enterprise.context.Initialized;
import javax.enterprise.event.Observes;
@ApplicationScoped
public class ApplicationSchedulerX {
@Resource(lookup = "java:jboss/ee/concurrency/scheduler/MyScheduler")
private ManagedScheduledExecutorService timerService;
public void init(@Observes @Initialized(ApplicationScoped.class) Object o) {
timerService.schedule(() -> {
System.out.println(" Run scheduler... " );
}, new Trigger() {
@Override
public Date getNextRunTime(LastExecution le, Date date) {
return getNextDateBySeconds(60);
}
@Override
public boolean skipRun(LastExecution le, Date date) {
return false;
}
});
}
private Date getNextDateBySeconds(long seconds) {
LocalDateTime ldt = LocalDateTime.now().plusSeconds(seconds);
return Date.from(ldt.atZone(ZoneId.systemDefault()).toInstant());
}
public void destroy(@Observes @Destroyed(ApplicationScoped.class) Object o) {
//TODO error: Lifecycle operation not supported
//timerService.shutdown();
}
}
Wildfly 9調度配置:
<managed-scheduled-executor-service name="MyScheduler" jndi-name="java:jboss/ee/concurrency/scheduler/MyScheduler" hung-task-threshold="50000" long-running-tasks="true" core-threads="4" keepalive-time="500" reject-policy="ABORT"/>
可以使用[的ServletContextListener(http://docs.oracle.com/javaee/7/api/javax/servlet/ServletContextListener.html)? [示例](https://www.mkyong.com/servlet/what-is-listener-servletcontextlistener-example/) – ebanouz
它啓動時正常工作,當它結束時,方法執行,但timerService仍在運行。我還沒有找到一種方法來清除timerService的所有時間表。 –