Parameterize EJB scheduler with Schedule Expression

5.2k views Asked by At

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.

1

There are 1 answers

7
javadev On BEST ANSWER

Instead, use programmatic scheduling, here is an 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
    }
}