停止应用程序EJB 3.0时计时器取消

时间:2016-10-17 22:13:52

标签: java java-ee timer weblogic ejb-3.0

在weblogic 10.3.4.0(ejb 3.0)上,当我停止应用程序时,我想在EJB无状态上清除预先破坏fase的计时器。

但weblogic提出此消息

java.lang.IllegalStateException: [EJB:010193]Illegal call to EJBContext method. The bean is in "null" state. It cannot perform 'getting the Timer Service' action(s). Refer to the EJB specification for more details. 

我的preDestroy方法是

@Resource 
private TimerService timerService;

@PreDestroy 
public void stopMyTimers() {
    if(timerService!=null){
        for(Timer timer : timerService.getTimers()) {
            if(timer!=null) timer.cancel();
        }
    }
}    

在PreDestroy之前存在的东西,当bean不在" null"状态?

当我停止申请时如何取消计时器?

1 个答案:

答案 0 :(得分:0)

添加如下内容应该有效:

@javax.ejb.Singleton
// you might need @javax.ejb.Startup as well
public class ShutdownController {

    @EJB
    private YourEjbWithTimers ejbWithTimers;

    @PreDestroy
    void shutdown() {
        ejbWithTimers.stopMyTimers();
    }

}

并将您的stopMyTimers方法更改为:

       // no annotation
       public void stopMyTimers() {
           // null tests not needed
           // - if something is null then it's broken
           for(Timer timer : timerService.getTimers()) {
               timer.cancel();
           }
       }