我有一个无状态bean来管理我的计时器服务:
@Stateless
@Depends("jboss.ha:service=HASingletonDeployer,type=Barrier")
public class TimerManagerBean implements ITimerManagerLocal {
private static final Logger log = Logger.getLogger(TimerManagerBean.class);
private @Resource
SessionContext ctx;
private @EJB ISomeEjbLocal someEjb;
public void scheduleTimer(long initialDuration, long intervalDuration, Serializable info) {
if (info.equals(ITimerManagerLocal.TIMER_ID_1)) {
ctx.getTimerService().createTimer(initialDuration, intervalDuration, info);
}
}
@Override
public void cancelTimer(Serializable info) {
final Collection<Timer> timers = ctx.getTimerService().getTimers();
for (final Timer timer : timers) {
if (timer.getInfo().equals(info)) {
log.info("Timer[info=" + info + "] found, cancelling...");
timer.cancel();
log.info("Timer[info=" + info + "] cancelled");
}
}
}
@Override
@Timeout
public void timeoutHandler(Timer timer) throws Exception {
Serializable info = timer.getInfo();
if (ITimerManagerLocal.TIMER_ID_1.equals(info)) {
someEjb.doSomething(); // what happen if this throw exception?
}
}
}
}
这是我的本地接口:
@Local
public interface ITimerManagerLocal {
public static final String TIMER_ID_1 = "TIMER_ID_1";
public void scheduleTimer(long initialDuration, long intervalDuration, Serializable info);
public void cancelTimer(Serializable info);
public void timeoutHandler(Timer timer) throws Exception;
}
我用它来创建/取消/安排计时器。这个服务过去几个月都能正常工作,但是现在它开始将null计时器对象传递给timeoutHandler方法,这会抛出NullPointerException。每次调用timeoutHandler方法时都会发生这种情况。 我使用postgres存储计时器信息,而不是hsdb。 我认为这是在迁移到postgres 9后开始发生的,但我不明白为什么,也许我错了。 此外,当timer.getInfo()抛出NullPointerException(因为timer为null)时,timeoutHandler方法中会发生什么。抛出这个异常在哪里?它会阻止正确的计时器重新安排吗?
感谢。