简体   繁体   English

定时服务:Bean在EJB3.1中没有定时器

[英]Timer Service: Bean does not have timers in EJB3.1

Actually, am migrating an application from EJB2.1 to EJB3.1 . 实际上,我正在将应用程序从EJB2.1迁移到EJB3.1 After changed the application I got an issue while calling getTimers() method. 更改应用程序后,我在调用getTimers()方法时出现问题。

I am using the Websphere server. 我正在使用Websphere服务器。

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; javax.ejb.EJBException:参见嵌套异常; 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) 嵌套异常是:java.lang.IllegalStateException:定时器服务:Bean没有定时器:BeanId(LeadDeliverySystemEAR#timedrequest.jar #TimedRequestBean,null)java.lang.IllegalStateException:定时器服务:Bean没有定时器:BeanId(LeadDeliverySystemEAR#在com.ford.it.request.async.TimedRequestBean.cancelTimers(TimedRequestBean.java:460)的com.ibm.ejs.container.BeanO.getTimers(BeanO.java:1733)上的timedrequest.jar #TimedRequestBean,null)

TimerService.getTimers() throws the IllegalStateException if the bean has not been declared to have any timers. 如果bean尚未声明为具有任何计时器,则TimerService.getTimers()将抛出IllegalStateException。 To avoid this, the bean must use either @Scheulde to declare an automatic timer, or @Timeout to declare the timeout callback method for programmatic timers (or the XML equivalent of either annotation). 为了避免这种情况,bean必须使用@Scheulde来声明自动计时器,或者使用@Timeout来声明程序化计时器的超时回调方法(或者是注释的XML等价物)。

Basically, the TimerService is not accessible to beans that cannot possibly have timers. 基本上,不能使用定时器的bean无法访问TimerService Since there is no @Timeout method, none of the create methods may be called on the TimerService ; 由于没有@Timeout方法,因此TimerService上不会调用任何create方法; similarly, since no timers can exist for the bean, calling getTimers() is also not permitted. 类似地,由于bean不存在定时器,因此也不允许调用getTimers()

Finally I got the solution. 最后我得到了解决方案。 I have implement the TimedObject interface in my bean Its working fine. 我在我的bean中实现了TimedObject接口它的工作正常。 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. 来源: http//itdoc.hitachi.co.jp/manuals/3020/30203Y0610e/EY060069.HTM我认为这对其他人有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM