endragor
endragor

Reputation: 368

EJB TimerService interval timer with end date

I want to create an EJB Timer running with specific interval, but it also should stop being triggered at some point. EJB's TimerService seems to offer only 2 possibilities:

The precision I need is hours, so any solution allowing hourly repetition which "rolls over" days is fine.

Thank you.

Upvotes: 0

Views: 1841

Answers (3)

Fireworks
Fireworks

Reputation: 515

You can create 2 or more timers with same functionality. Or create single time timer, during execution calculate next execution date/time and create next single time timer

Upvotes: 0

Sabuj Hassan
Sabuj Hassan

Reputation: 39405

This method getTimers() gives you the lists of timers are currently active for the bean TimerService. For each timers from the list, perform getInfo() to get the timer's info. If this is null, then its the scheduler that you have started. Just perform the cancel() then, and it will stop your scheduler.

List<Timer> timerList = timerService.getTimers();
for(Timer t : timerList){
    if(t.getInfo() ==  null){
        t.cancel();
    }
}

So decide from where you want to use this.

Upvotes: 0

arjacsoh
arjacsoh

Reputation: 9242

You can do it with ScheduleExpression as:

ScheduleExpression schedule = new ScheduleExpression();
schedule.start(startDate);
schedule.hour("*/2");
schedule.end(endDate);
Timer timer = timerService.createCalendarTimer(schedule);

The above code trigers the Timer every 2 hours for example.

Upvotes: 0

Related Questions