java 如何停止无尽的 EJB 3 计时器?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2112190/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 19:31:19  来源:igfitidea点击:

How to stop endless EJB 3 timer?

javatimerjbossejb-3.0

提问by mebada

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

我是 EJB 3 的新手。我使用以下代码启动无尽的 EJB 3 计时器,然后将其部署在 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.

我忘记调用取消然后它不会停止 .redeploy 取消调用永远不会停止它。重新启动 Jboss 4.2.3 永远不要停止它。如何停止 EJB 计时器?谢谢你的帮助。

回答by Davide Consonni

    public void stop(String timerName) {
    for(Object obj : timerService.getTimers()) {
        Timer t = (Timer)obj;
        if (t.getInfo().equals(timerName)) {
        t.cancel();
        }
    }
}

回答by Wolfgang Adamec

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:

我的 JBoss AS 6.1 也遇到了同样的问题。
在杀死这个无限(持久)计时器后,我找到了以下解决方案来避免将来出现这个问题:
使用 JBoss AS 6.1 (EJB 3.1) 可以创建非持久自动计时器,它们不会在服务器重启后幸存下来:

@Schedule(minute=”*/10”, hour=”*”, persistent=false)
public void automaticTimeout () {

回答by Ironluca

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),然后在计时器服务中检查具有相同信息的计时器,并在可用时取消它:

@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

回答by Steve

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

您还可以取消部署您的应用程序,这将“杀死”所有计时器。

回答by Varada Pujari

Try the @PreDestroyannotation within the bean where you want to close.

@PreDestroy在要关闭的 bean 中尝试注释。

For example:

例如:

@PreDestroy
private void undeployTimer() {
 //..
}

Generally the resource de-allocation is done here.

通常,资源取消分配在这里完成。

回答by Xavier Dury

Since EJB 3.1, there are new methods on TimerServicewhich take a TimerConfiginstead of a Serializablepayload. Using TimerConfigallows to make the Timernon-persistent.

由于EJB 3.1中,有新的方法,TimerService它采取TimerConfig的,而不是一个Serializable有效载荷。使用TimerConfig允许使Timer非持久化。

timerService.createIntervalTimer(10, 5000, new TimerConfig(null, false));