使用 EJB 计时器的 Java EE 调度程序任务
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9783837/
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
Java EE Scheduler Task using EJB Timers
提问by stack
I've a requirement which requires 3-6 scheduled task to run at a given time of the day. I am completely new to EJB timers, but have read that EJB timers is the best way to handle scheduled task in a Java EE container.
我有一个要求,需要在一天中的给定时间运行 3-6 个计划任务。我对 EJB 计时器完全陌生,但读过 EJB 计时器是在 Java EE 容器中处理计划任务的最佳方式。
Design Question:
设计问题:
Let's say I need 10 scheduled tasks. I don't want to have, if possible, 10 EJB timers created. Instead I would like to have a one off EJB timer created and then reuse this for creating as much scheduled jobs as requried, passing the scheduled time to run (as aruguements) for each instance, to is this possible? Can someone please help with a skeleton code on this please?
假设我需要 10 个计划任务。如果可能,我不想创建 10 个 EJB 计时器。相反,我想创建一个一次性的 EJB 计时器,然后重用它来创建尽可能多的预定作业,传递每个实例的预定运行时间(作为参数),这可能吗?有人可以帮忙提供一个骨架代码吗?
N.B I am thinking of using non-persistent EJB timers ...
注意我正在考虑使用非持久性 EJB 计时器......
采纳答案by SimonSez
AFAIK it isn't possible to create one 'reusable' timer in an EJB beacuse you have to tell each timer which method should be invoked.
AFAIK 不可能在 EJB 中创建一个“可重用”计时器,因为您必须告诉每个计时器应该调用哪个方法。
Have a look at this:
看看这个:
The 3rd party library Quartz Schedulershould be capable of creating Timer-objects programmatically. Maybe its worth to have a look at this!
第 3 方库Quartz Scheduler应该能够以编程方式创建 Timer 对象。也许值得看看这个!
Hope this helped, have Fun!
希望这有帮助,玩得开心!
回答by MaDa
You could define a timer in one of your stateless/message driven bean business methods (you'd still have to call it, though, it's not possible to create a timer that would start off on its own). Then, in the @Timeout
method you could recreate the timer based on any logic you find suitable, i.e.
您可以在无状态/消息驱动的 bean 业务方法之一中定义一个计时器(您仍然必须调用它,但是,不可能创建一个可以自行启动的计时器)。然后,在该@Timeout
方法中,您可以根据您认为合适的任何逻辑重新创建计时器,即
@Stateless
public SomeEJB ... {
@Resource
private TimerService timerService;
public void businessMethod() {
timerService.createTimer(...);
}
@Timeout
public void timeout(Timer timer) {
// do some timer-related logic, recreate the timer,
// perhaps with new duration
timerService.createTimer(...);
}
}
This example is EJB 3.0-compatible.
此示例与 EJB 3.0 兼容。
回答by andbi
Another option (in addition to alreay said) is to use singleton with @Schedule
annotation for each of your timed methods:
另一种选择(除了已经说过的)是@Schedule
对每个定时方法使用带有注释的单例:
@Singleton
@Startup
public class TimedTaskManager {
@Schedule(second = "0", minute = "*/5", hour = "*")
public void runTask1() {
//
}
@Schedule(second = "15", minute = "*/5", hour = "6,7,8")
public void runTask2() {
//
}
//
//
@Schedule(second = "0", minute = "*", hour = "1,2,6")
public void runTaskN() {
//
}
}