Actually, am migrating an application from EJB2.1
to EJB3.1
. After changed the application I got an issue while calling getTimers()
method.
I am using the Websphere server.
Here is my code:
@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();
}
}
}
Log:
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)
Finally I got the solution. I have implement the TimedObject interface in my bean Its working fine. Here is my code.
@Stateless
@TransactionManagement(TransactionManagementType.BEAN)
public class TimedRequestBean implements TimedRequestLocal, TimedObject {
@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();
}
}
}
Source : http://itdoc.hitachi.co.jp/manuals/3020/30203Y0610e/EY060069.HTM I think it will be useful for others.