我的 Timersession bean 有 4 个方法,比如 a()
、b()
、c()
和 d()
.
a()
应每 6 小时执行一次b()
应每 3 小时执行一次c()
应每 1 小时执行一次
如何使用 EJB 3.0 计时器服务来做到这一点?
最佳答案
安排三个独立的计时器,并使用“info”对象来编码需要从@Timeout方法调用哪个方法。例如:
timerService.createTimer(..., 6 * 60 * 60 * 1000, "a");
...
timerService.createTimer(..., 3 * 60 * 60 * 1000, "b");
...
timerService.createTimer(..., 1 * 60 * 60 * 1000, "c");
...
@Timeout
private void timeout(Timer timer) {
String info = timer.getInfo();
if ("a".equals(info)) {
a();
} else if ("b".equals(info)) {
b();
} else if ("c".equals(info)) {
c();
} else {
throw new IllegalStateException("Unknown method: " + info);
}
}
关于java - 关于ejb3.0定时器服务的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5856775/