EJB3.1 Timer cancel issue

三世轮回 提交于 2020-01-06 03:58:25

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/21651730/ejb3-1-timer-cancel-issue

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