EJB3.1 Timer cancel issue
Asked Answered
C

1

1

I have a problem with EJB3.1 Timer service. I find this issue quite strange! I have a stateless session bean like below deployed on JBoss7.1.1 application server.

import javax.annotation.Resource;
import javax.ejb.Local;
import javax.ejb.Remote;
import javax.ejb.Stateless;
import javax.ejb.Timeout;
import javax.ejb.Timer;
import javax.ejb.TimerService;
import javax.interceptor.Interceptors;

@Stateless
@Local(HelloUserLocal.class)
@Remote(HelloUserRemote.class)
public class HelloUserBean implements HelloUserRemote {

@Resource
private TimerService timerService;

@Interceptors({SayHelloMethodLoggingInterceptor.class})
public String sayHello(String name) {
    System.out.println("In sayHello(name)");
    String customName = "Hello " + name + " welcome to EJB 3 In Action!";
    System.out.println("Before returning, register Timer service");

    timerService.createTimer(10000, 2000, "Hey! Good Night!");

    return customName; 
}

@Timeout
public void activateTimer(Timer timer){
    System.out.println(timer.getInfo());
    //timer.cancel();
}   

}

I registered a Timer with initialDuration and intervalDuration as above. After I deployed my bean, it works fine. Okay, I got the thing and I don't want my server console to clutter. So, I commented out timerService.createTimer(..) line of code and redeployed. But, I still see the message being printed. Okay, I know EJB Timers are persisted and survive server restarts. The only way I can stop my Timer Service is to use timer.cancel() method and then redeploy. But, how can I stop or de-register my Timer from the container without using timer.cancel()?

How much time timer.cancel(); take to cancel the Timer? When I use timer.cancel() and then redeployed my application, I can see "Hey! Good Night!" being printed two times before stopping, which means certainly timer.cancel() is taking more than 2000ms?

Conure answered 8/2, 2014 at 20:57 Comment(5)
Really looks like a bugBinny
Perhaps, but any way to manage timers from container admin console or something?Conure
Not sure. The ejb timer spec is full of missing points. Don't know for JBoss, but for tomee I had to use quartz functions to bypass some spec problems. What JBoss uses as timer implementation? Maybe they have some proprietary extension for that. Can you configure to timer ejb use a jdbc backend?Binny
Yes, you can certainly timer ejb to process on JDBC code. Not sure about any specific JBoss timer implementations.Conure
I think your other questions in the update section are good enough to be asked separately here :-)Binny
S
3

you could use @Schedule instead and configure your timer with persistent=false. If a persistent timer has already been created, looks like the only non-programmatic way to remove it is by deleting JBOSS_HOME/server/<servername>/data/timer-service-data, it's not nice and for what I remember, in the JBoss4 days this could be done through the JMX console, but a lot has changed since those days, good luck.

Sewing answered 9/2, 2014 at 1:25 Comment(2)
Can you manage your EJB3.1 schedules using any management consoles?Conure
In jBoss7, I don't think so, but with persistent=false they'll be gone after restartSewing

© 2022 - 2024 — McMap. All rights reserved.