How to stop endless EJB 3 timer?
Asked Answered
R

6

19

I am new to EJB 3 . I use the following code to start endless EJB 3 timer then deploying it on JBOSS 4.2.3

@Stateless
public class SimpleBean  implements SimpleBeanRemote,TimerService  {

@Resource
TimerService timerService;
private Timer timer ;
@Timeout
public void timeout(Timer timer) {
    System.out.println("Hello EJB");

 }
}

then calling it

  timer = timerService.createTimer(10,  5000, null);

It works well. I created a client class that calls a method that creates the timer and a method that is called when the timer times out.

I forget to call cancel then it does not stop .redeploy with cancel call never stop it. restart Jboss 4.2.3 never stop it. How I can stop EJB timer ? Thanks for helping.

Reiche answered 21/1, 2010 at 19:27 Comment(0)
J
30

Put the stop() method into your bean and call it with your timer name as a parameter. It will iterate over all timers and will stop your timer.

public void stop(String timerName) {
    for(Object obj : timerService.getTimers()) {
        Timer t = (Timer)obj;
        if (t.getInfo().equals(timerName)) {
            t.cancel();
        }
    }
}
Jonathanjonathon answered 22/4, 2010 at 10:26 Comment(3)
After three years, Thanks A lot :)Reiche
This cancels all the timers, but if you consult the "getTimers()" they still there :/Methinks
No! This will cancel only the timers that have the same name as timerName.Wyckoff
E
3

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 () {
Ethban answered 3/10, 2012 at 20:55 Comment(0)
S
2

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

Staid answered 29/3, 2012 at 1:11 Comment(1)
Not likely if the timers are persistentHowdah
B
2

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
Barrel answered 24/12, 2014 at 9:27 Comment(0)
N
1

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));
Newmann answered 22/3, 2017 at 7:8 Comment(0)
L
0

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.

Ligan answered 21/1, 2016 at 22:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.