Parameterizing an EJB Scheduler with a Schedule Expression

I am using EJB 3.1 and jboss-eap-6.4, and I want to set some dynamic parameters for the hour, minute and second of the ejb scheduler as follows:

A nonparametric code that runs after 30 seconds 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 is executing correctly.

Dynamic parametric code - which should run for 30 seconds 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 code above does not execute correctly, it usually executes several times per second.

In addition, I read a few other questions that did not help me:

Dynamic parameters for @Schedule method in EJB 3.x

Using the Timer Service - Java EE 6 Tutorial

Any help would be appreciated.

+1
source share
1 answer

Use program planning instead, here is an example:

 @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 } } 
+1
source

All Articles