有没有办法在运行时停止/重新启动ejb 3.1自动计时器? [英] Is there a way to stop/re-start ejb 3.1 automatic timer during runtime?
问题描述
我正在尝试使用一个简单的自动EJB计划/计时器。我的代码是这样的:
@Singleton
@Lock(LockType.READ)
public class调度器{
@Schedule(second =0,minute =* / 20,hour =*),
private void fetchSomeData(){
.. 。
}
@Schedule(second =0,minute =* / 5,hour =*),
private void cleanThingsUp(){
...
}
}
是否一种在运行时停止并重新启动自动计时器的方法?请注意,我不需要更改超时,我只需要停止并启动计时器。目前为止,我发现的所有教程和示例都没有提到stop / start概念(就是简单的 @Schedule 计时器)。
在ejb定时器中,与开始和停止有关的更多创意是创建和取消。
发布的代码显示您正在使用非常容易创建的自动计时器
,但有一个缺点:定时器将仅在部署时由容器自动创建。
这为创建操作留下了一小的余量。
然而,一旦创建,一个Timer可以被取消调用Timer.cancel()方法。
eg :
@Singleton
@Remote
public class MyTimer实现MyTimerRemote {
@Resource
TimerService timerService;
// MyTimer1,注意信息属性
@Schedule(hour =*,minute =*,second =*,info =MyTimer1)
public void doSomthing(){
System.out.println(执行定时器1);
}
// MyTimer2
@Schedule(hour =*,minute =*,second =*,info =MyTimer2)
public void doSomthing2(){
System.out.println(Executing Timer 2);
}
//使用必须取消的Timer信息调用此远程方法
@Override
public void cancelTimer(String timerInfo){
(定时器定时器:timerService.getTimers()){
if(timerInfo.equals(timer.getInfo())){
System.out.println(Cancelling Timer:info:+ timer.getInfo());
timer.cancel();
}
}
}
是创建一个程序定时器
,这意味着更多的代码,但你可以决定何时创建一个特定的计时器。
< pre class =lang-java prettyprint-override>
//你可以随时调用这个远程方法
@Override
public void createProgramaticTimer(String timerInfo){
System.out.println(创建新PT:+ timerInfo);
TimerConfig timerConf = new TimerConfig();
timerConf.setInfo(timerInfo);
//创建一个新的程序定时器
timerService.createIntervalTimer(1,1000,timerConf); //只是一个例子
}
@Timeout
public void executeMyTimer(Timer timer){
System.out.println 我的PT正在执行...);
}
取消操作与自动定时器保持一致。 p>
I'm trying to use a simple automatic EJB schedule/timer. My code goes something like this:
@Singleton
@Lock(LockType.READ)
public class Scheduler {
@Schedule(second = "0", minute = "*/20", hour = "*"),
private void fetchSomeData() {
...
}
@Schedule(second = "0", minute = "*/5", hour = "*"),
private void cleanThingsUp() {
...
}
}
Is there a way to stop and restart automatic timers during the runtime? Please notice that I don't need to change the timeouts, I only need to stop and start the timers. All tutorials and examples I've found so far don't mention the stop/start concept at all (that is for the simple @Schedule timers).
In ejb Timers the closer ideas related to Start&Stop are Create&Cancel.
The posted code shows that you are using Automatic Timer
which are very easy to create but, have a drawback: the Timer will be created automatically by Container only at deploy time.
This leave you a small margin for Create operations.
However, once created, a Timer can be canceled calling the Timer.cancel() method.
e.g.:
@Singleton
@Remote
public class MyTimer implements MyTimerRemote {
@Resource
TimerService timerService;
//MyTimer1, notice the info attribute
@Schedule (hour="*", minute="*", second="*", info="MyTimer1")
public void doSomthing(){
System.out.println("Executing Timer 1");
}
//MyTimer2
@Schedule (hour="*", minute="*", second="*", info="MyTimer2")
public void doSomthing2(){
System.out.println("Executing Timer 2");
}
//call this remote method with the Timer info that has to be canceled
@Override
public void cancelTimer(String timerInfo) {
for (Timer timer: timerService.getTimers()) {
if (timerInfo.equals(timer.getInfo())) {
System.out.println("Canceling Timer: info: " + timer.getInfo());
timer.cancel();
}
}
}
And alternative is to create a Programatic Timer
, this implies a little more code, but you can decide when create a particular Timer.
//you can call this remote method any time
@Override
public void createProgramaticTimer(String timerInfo) {
System.out.println("Creating new PT: " + timerInfo);
TimerConfig timerConf = new TimerConfig();
timerConf.setInfo(timerInfo);
//create a new programatic timer
timerService.createIntervalTimer(1, 1000, timerConf); //just an example
}
@Timeout
public void executeMyTimer(Timer timer){
System.out.println("My PT is executing...");
}
The Cancel operation remains the same as Automatic Timer.
这篇关于有没有办法在运行时停止/重新启动ejb 3.1自动计时器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!