开发者

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.


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?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜