关于Java:如何更改一天中的时间以执行EJB TimeService

How to change hour of day to execute EJB TimeService

我希望某个流程每天在一天的特定时间运行。我想使用EJB的Timerservice,但是我只能找到如何设置时间间隔,而不是一天中的特定时间:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
@Resource
    protected TimerService timerService;

    @Timeout
public void timeoutHandler(Timer timer) {
    String name = timer.getInfo().toString();
    System.out.println("Timer name=" + name);
}

public void startOrModifyTimer(long initialExpiration, long interval, String name){      
    //Cancel previous timer
    Collection<Timer> timers = timerService.getAllTimers();
    for (Timer timer: timers) {
        if (timer.getInfo().equals(name)) {
            timer.cancel();
        }
    }

    TimerConfig config = new TimerConfig();
    config.setInfo(name);
    config.setPersistent(false);
    timerService.createIntervalTimer(initialExpiration, interval, config);
}

我想在运行时将计时器从"每天凌晨2点"更改为"每天凌晨3点"。


您要:

1
createCalendarTimer(ScheduleExpression schedule)

ScheduleExpression将执行您想要的操作。