簡體   English   中英

定時服務:Bean在EJB3.1中沒有定時器

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

實際上,我正在將應用程序從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:參見嵌套異常; 嵌套異常是: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)

如果bean尚未聲明為具有任何計時器,則TimerService.getTimers()將拋出IllegalStateException。 為了避免這種情況,bean必須使用@Scheulde來聲明自動計時器,或者使用@Timeout來聲明程序化計時器的超時回調方法(或者是注釋的XML等價物)。

基本上,不能使用定時器的bean無法訪問TimerService 由於沒有@Timeout方法,因此TimerService上不會調用任何create方法; 類似地,由於bean不存在定時器,因此也不允許調用getTimers()

最后我得到了解決方案。 我在我的bean中實現了TimedObject接口它的工作正常。 這是我的代碼。

@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();
            }
       }
}

來源: http//itdoc.hitachi.co.jp/manuals/3020/30203Y0610e/EY060069.HTM我認為這對其他人有用。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM