实际上,我正在将应用程序从 EJB2.1
迁移到 EJB3.1
。更改应用程序后,我在调用 getTimers()
方法时遇到问题。
我正在使用 Websphere 服务器。
这是我的代码:
@Stateless
@TransactionManagement(TransactionManagementType.BEAN)
public class TimedRequestBean implements TimedRequestLocal {
@Resource
private SessionContext sessionContext;
public void cancelTimers() {
TimerService ts = this.sessionContext.getTimerService();
Collection timers = ts.getTimers();
Iterator it = timers.iterator();
while (it.hasNext()) {
Timer myTimer = (Timer)it.next();
myTimer.cancel();
}
}
}
日志:
javax.ejb.EJBException: See nested exception; nested exception is: java.lang.IllegalStateException: Timer Service: Bean does not have timers: BeanId(LeadDeliverySystemEAR#timedrequest.jar#TimedRequestBean, null) java.lang.IllegalStateException: Timer Service: Bean does not have timers: BeanId(LeadDeliverySystemEAR#timedrequest.jar#TimedRequestBean, null) at com.ibm.ejs.container.BeanO.getTimers(BeanO.java:1733) at com.ford.it.request.async.TimedRequestBean.cancelTimers(TimedRequestBean.java:460)
最佳答案
如果 Bean 尚未声明为具有任何计时器,
TimerService.getTimers()
将引发 IllegalStateException。为了避免这种情况,bean 必须使用 @Scheulde
来声明自动计时器,或使用 @Timeout
来声明编程计时器的超时回调方法(或者两者的 XML 等效项)注释)。
基本上,不可能有计时器的 bean 无法访问 TimerService
。由于没有@Timeout
方法,因此不能在TimerService
上调用任何创建方法;同样,由于该 bean 不存在计时器,因此也不允许调用 getTimers()
。
关于java - 定时器服务 : Bean does not have timers in EJB3. 1,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55813354/