I am using EJB 3.1 and jboss-eap-6.4 and I want to set some dynamic parameters for hour, minute and second of ejb scheduler as follows:
Non-parametric code - which run in 30th second of every 5 minutes :
@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
}
}
The above code executes properly.
Dynamic Parametric code - which should run in 30th second of every 5 minutes:
@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
}
}
The above code does not execute correctly, usually it executes multiple times at a second.
Also, I have read some other questions which did not help me:
Dynamic parameters for @Schedule method in an EJB 3.x
Using the Timer Service - The Java EE 6 Tutorial
Any help would be appreciated.
Instead, use programmatic scheduling, here is an exmaple :