EJB3.1计时器取消问题 [英] EJB3.1 Timer cancel issue
问题描述
我遇到EJB3.1定时服务问题。我发现这个问题很奇怪!我在JBoss7.1.1应用服务器上部署了一个无状态会话bean,如下所示。
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();
}
}
我用initialDuration注册了一个Timer intervalDuration如上。在我部署我的bean之后,它运行正常。好的,我得到了这个东西,我不希望我的服务器控制台变得杂乱无章。所以,我注释掉了timerService.createTimer(..)代码行并重新部署。但是,我仍然看到正在打印的消息。好的,我知道EJB Timers是持久存在的,并且在服务器重启后仍然存在。我可以停止Timer服务的唯一方法是使用timer.cancel()方法然后重新部署。但是,如何在不使用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()?
timer.cancel();拿取定时器?当我使用timer.cancel()然后重新部署我的应用程序时,我可以看到嘿!晚安!在停止之前打印两次,这意味着当然timer.cancel()花费超过2000毫秒?
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?
推荐答案
你可以使用< a href =http://docs.oracle.com/javaee/6/api/javax/ejb/Schedule.html =nofollow> @ Schedule 而是用<$ c $配置你的计时器C>持久=假。如果已经创建了持久性计时器,看起来像删除它的唯一非编程方式是删除 JBOSS_HOME / server /< servername> / data / timer-service-data
,它并不好,而且我记得,在JBoss4天里,这可以通过JMX控制台完成,但是那些日子以来发生了很多变化,祝你好运。
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.
这篇关于EJB3.1计时器取消问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!