我正在使用ejb 3.1和jboss-eap-6.4,我想将EJB调度程序的小时,分钟和第二个动态参数设置为:
非参数代码 - 每5分钟的第30秒运行:
@Singleton
@Startup
public class TriggerJob {
@EJB
//some db injections
@PostConstruct
public void onStartup() {
try {
preparation();
} catch (CertificateVerificationException e) {
e.printStackTrace();
}
}
@Schedule(second = "30", minute = "*/5", hour = "*", persistent = false)
public void preparation() {
//my scheduled tasks
}
}
以上代码正确执行。
动态参数代码 - 该代码应在每5分钟的第30秒内运行:
@Singleton
@Startup
public class TriggerJob {
@EJB
//some injections
private boolean runningFlag = false;
@Resource
private TimerService timerService;
public void setTimerService(TimerService timerService) {
this.timerService = timerService;
}
@Timeout
public void timerTimeout() {
try {
preparation();
} catch (CertificateVerificationException e) {
e.printStackTrace();
}
}
@PostConstruct
private void postCunstruct() {
timerService.createCalendarTimer(createSchedule(),new TimerConfig("EJB timer service timeout at ",false));
}
private ScheduleExpression createSchedule() {
ScheduleExpression expression = new ScheduleExpression();
expression.hour("*")
.minute("*/5")
.second("30");
return expression;
}
public void preparation(){
// my scheduled tasks
}
}
上面的代码无法正确执行,通常在一秒钟内多次执行。
另外,我读了其他一些没有帮助我的问题:
@schedule方法的动态参数3.x
使用计时器服务-Java EE 6教程
任何帮助将不胜感激。
而是使用程序化调度,这是一个exmaple:
@Singleton
@Startup
public class TriggerJob{
@EJB
//some injections
@Resource
private TimerService timerService;
@PostConstruct
public void init() {
createTimer();
//the following code resolve my startup problem
try {
preparation();
} catch (CertificateVerificationException e) {
e.printStackTrace();
}
}
@Timeout
public void timerTimeout() {
try {
preparation();
} catch (CertificateVerificationException e) {
e.printStackTrace();
}
}
private void createTimer() {
ScheduleExpression scheduleExpression = new ScheduleExpression();
scheduleExpression.second("30").minute("*/5").hour("*");
TimerConfig timerConfig = new TimerConfig();
timerConfig.setPersistent(false);
timerService.createCalendarTimer(scheduleExpression, timerConfig);
}
public void preparation(){
// my scheduled tasks
}
}