Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to stop endless EJB 3 timer?

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.

like image 237
mebada Avatar asked Jan 21 '10 19:01

mebada


3 Answers

    public void stop(String timerName) {
    for(Object obj : timerService.getTimers()) {
        Timer t = (Timer)obj;
        if (t.getInfo().equals(timerName)) {
        t.cancel();
        }
    }
}
like image 131
Davide Consonni Avatar answered Nov 11 '22 11:11

Davide Consonni


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 () {
like image 3
Wolfgang Adamec Avatar answered Nov 11 '22 12:11

Wolfgang Adamec


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

like image 2
Steve Avatar answered Nov 11 '22 10:11

Steve