I have a problem with the Timer EJB3.1 service. I find this question rather strange! I have a standless bean session, as shown below, on a 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());
I registered a timer with initialDuration and intervalDuration as above. After I deployed my bean, it works fine. Ok, I get it, and I don't want my server console cluttering up. So, I commented on the line of code timerService.createTimer (..) and redistributed it. But I still see the message being printed. Well, I know that EJB timers are saved and the servers are rebooting. The only way to stop the Timer service is to use the timer.cancel () method and then redeploy. But how can I stop or cancel my timer from the container without using timer.cancel ()?
What time is timer.cancel (); accept to cancel the timer? When I use timer.cancel () and then redistribute my application, I see "Hey, good night!". printed twice before stopping, which means the .cancel () timer takes more than 2000 ms?
source share