我创建了一个包含EJB的测试应用程序,该应用程序应该每秒打印一次当前的时间戳。现在,在将Web应用程序部署到我的wildfly上之后,我每秒获得约5条日志消息...看起来好像正在运行多个调度程序,这会触发输出...
您有什么想法,如何解决?如果我只是用@Schedule注释方法,它将每秒输出结果,但是通过以编程方式设置计时器会出现问题:
@Singleton
@Startup
public class Scheduler {
@Resource
TimerService timerService;
@Inject
Logger logger;
@Timeout
public void doStuff() {
logger.info("Programmatic Scheduler: " + System.currentTimeMillis());
}
@PostConstruct
public void startScheduler() {
TimerConfig timerConfig = new TimerConfig();
timerConfig.setPersistent(false);
ScheduleExpression expression = new ScheduleExpression();
expression.second("*/1").minute("*").hour("*");
timerService.createCalendarTimer(expression, timerConfig);
}
}
...这是您所看到的输出,每秒可打印多张照片:
16:26:46,574 INFO [de.hays.liferay.schedulerprogrammatically.Scheduler] (EJB default - 10) Programmatic Scheduler: 143455120657
16:26:46,849 INFO [de.hays.liferay.schedulerprogrammatically.Scheduler] (EJB default - 8) Programmatic Scheduler: 1434551206849
16:26:46,869 INFO [de.hays.liferay.schedulerprogrammatically.Scheduler] (EJB default - 7) Programmatic Scheduler: 1434551206869
16:26:46,890 INFO [de.hays.liferay.schedulerprogrammatically.Scheduler] (EJB default - 4) Programmatic Scheduler: 1434551206890
16:26:47,083 INFO [de.hays.liferay.schedulerprogrammatically.Scheduler] (EJB default - 3) Programmatic Scheduler: 1434551207083
16:26:47,145 INFO [de.hays.liferay.schedulerprogrammatically.Scheduler] (EJB default - 1) Programmatic Scheduler: 1434551207145
16:26:47,185 INFO [de.hays.liferay.schedulerprogrammatically.Scheduler] (EJB default - 9) Programmatic Scheduler: 1434551207185
16:26:47,246 INFO [de.hays.liferay.schedulerprogrammatically.Scheduler] (EJB default - 6) Programmatic Scheduler: 1434551207246
16:26:47,274 INFO [de.hays.liferay.schedulerprogrammatically.Scheduler] (EJB default - 2) Programmatic Scheduler: 1434551207274
16:26:47,489 INFO [de.hays.liferay.schedulerprogrammatically.Scheduler] (EJB default - 5) Programmatic Scheduler: 1434551207489
最佳答案
我已经知道了-我在java任务控件中查找了Wildfly上正在运行的Timers,并且有多个Timer。
因此,我已经编辑了代码,以杀死该应用程序的所有现有计时器,然后创建一个计时器,以确保确实只有一个计时器:
@PostConstruct
public void startScheduler() {
TimerService timerService = sessionContext.getTimerService();
logger.info("Current running timers: {}", timerService.getTimers().toString());
for (Timer t : timerService.getTimers()) {
t.cancel();
logger.info("killed the timer service: {}", t);
}
TimerConfig timerConfig = new TimerConfig();
timerConfig.setPersistent(false);
Timer createdIntervalTimer = sessionContext.getTimerService().createIntervalTimer(0, 5000, timerConfig);
logger.info("Created new Timer: {}", createdIntervalTimer);
}
关于java - Wildfly计划的EJB-执行多次,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/30894140/