How to stop endless EJB 3 timer?

孤人 提交于 2019-12-02 20:43:15
    public void stop(String timerName) {
    for(Object obj : timerService.getTimers()) {
        Timer t = (Timer)obj;
        if (t.getInfo().equals(timerName)) {
        t.cancel();
        }
    }
}

I had the same problem, with my JBoss AS 6.1.
After killing this endless (persistent) timers I found the following solution for AVOIDING this problem in the future:
With JBoss AS 6.1 (EJB 3.1) it is possible to create non-persistent automatic timers, they DO NOT SURVIVE a server restart:

@Schedule(minute=”*/10”, hour=”*”, persistent=false)
public void automaticTimeout () {

You can also undeploy your application, this will "kill" all timers.

Another method is to creat ethe automatic timer (@Schedule) with the 'info' attribute and then check in the timer service for timers with the same info and if available cancel it:

@Schedule(hour="*", minute="*",second="3", persistent=false,info="AUTO_TIMER_0")
void automaticTimeOut(){

    if(timerCount==0){System.out.println("FROM AUTOMATIC TIME OUT -----------------> CALLED");timerCount++;}
    else{

        Iterator<Timer> timerIterator=timerService.getTimers().iterator();

        Timer timerToCancel=null;
        while(timerIterator.hasNext()){

            Timer tmpTimer=timerIterator.next();
            if(tmpTimer.getInfo().equals("AUTO_TIMER_0")){timerToCancel=tmpTimer;break;}

        }//while closing

        if(timerToCancel!=null){

            timerToCancel.cancel();
            System.out.println("AUTOMATIC TIMER HAS BEEN CANCELED ----------------->>>>");

        }//if closing

    }//else closing

}//automaticTimeOut closing
Varada Pujari

Try the @PreDestroy annotation within the bean where you want to close.

For example:

@PreDestroy
private void undeployTimer() {
 //..
}

Generally the resource de-allocation is done here.

Since EJB 3.1, there are new methods on TimerService which take a TimerConfig instead of a Serializable payload. Using TimerConfig allows to make the Timer non-persistent.

timerService.createIntervalTimer(10, 5000, new TimerConfig(null, false));
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!