max
max

Reputation: 1266

Execute Java EE 6 timer now

I have a Java EE 6 timer - which I schedule with a ScheduleExpression but I also want to have the possibility to run it "now" on e.g. button click. How can I achieve this?

Example of how I create the timer:

ScheduleExpression schedExp = new ScheduleExpression();
    schedExp.start(jobInfo.getStartDate());
    schedExp.end(jobInfo.getEndDate());
    schedExp.second(jobInfo.getSecond());
    schedExp.minute(jobInfo.getMinute());
    schedExp.hour(jobInfo.getHour());
    schedExp.dayOfMonth(jobInfo.getDayOfMonth());
    schedExp.month(jobInfo.getMonth());
    schedExp.year(jobInfo.getYear());
    schedExp.dayOfWeek(jobInfo.getDayOfWeek());

TimerConfig timerConf = new TimerConfig(jobInfo, false);
Timer newTimer = timerService.createCalendarTimer(schedExp, timerConf);

Now I would like to do something like this:

timerService.runNow(timer);

Unfortunately there is no runNow method.

Upvotes: 0

Views: 732

Answers (1)

Marius Burz
Marius Burz

Reputation: 4655

I don't think this is possible but I somehow doubt it's needed either if you do the rest properly.

Say you could implement your Timeout method like this:

@Timeout
public void doTimeout(Timer timer) {
    logic.doMyTimeout();
}

Then what you need to do when the respective button is clicked is just call logic.doMyTimeout(); as well.

Is there any reason not to use this approach?

Upvotes: 1

Related Questions