如何创建一次性ManagedScheduledExecutorService触发器

时间:2018-07-16 21:12:20

标签: java-ee concurrency scheduled-tasks

我想利用JEE7中的ManagedScheduledExecutorService为延迟的任务创建单触发(一次性)触发器。遵循javadoc中的示例,我创建了一个带有固定时间戳的触发器,希望该触发器可以触发一次,并在触发后自动从调度程序中删除,但事实并非如此。即使触发器触发后,触发器仍会每毫秒被调用一次。

   /**
     * Trigger class to create a fixed delay trigger by a set number of seconds from the time it is created
     */
    private static class FixedDelayTrigger implements Trigger {

        private Date fireTime;

        /**
         * Create the trigger with a delay from te time it is created
         * @param delayInSeconds
         */
        public FixedDelayTrigger( int delayInSeconds ) {
            fireTime = Date.from(LocalDateTime.now().plusSeconds(delayInSeconds).atZone(ZoneId.systemDefault()).toInstant());
        }

        @Override
        public Date getNextRunTime(LastExecution lastExecutionInfo, Date taskScheduledTime) {
            logger.info("Next scheduled time:{}", taskScheduledTime.toString());
            if( taskScheduledTime.before(fireTime)){
                return fireTime;
            } else {
                return null;
            }
        }

        @Override
        public boolean skipRun(LastExecution lastExecutionInfo, Date scheduledRunTime) {
            // skip if the scheduledRunTime is after the scheduled firetime
            return scheduledRunTime.after(fireTime);
        }
    }

消费者:

@Override
public void success() {

    // schedule the event to be sent after EVENT_DELAY seconds (enough time for the brute force detection to run)
    try {
        executorService = InitialContext.doLookup("java:jboss/ee/concurrency/scheduler/default");
        executorService.schedule(() -> { super.success();}, new FixedDelayTrigger(delay));
    } catch (NamingException e) {
        // TODO
        throw new RuntimeException(e);
    }
}

在我的日志中,我看到以下内容,因此很明显,即使在预定时间之后,调度程序仍继续调用触发器。

16:58:22,584 INFO  [managers.events.EventBuilder] (EE-ManagedScheduledExecutorService-default-Thread-4) Next scheduled time:Mon Jul 16 16:57:54 EDT 2018
16:58:22,584 INFO  [managers.events.EventBuilder] (EE-ManagedScheduledExecutorService-default-Thread-4) Next scheduled time:Mon Jul 16 16:57:54 EDT 2018
16:58:22,584 INFO  [managers.events.EventBuilder] (EE-ManagedScheduledExecutorService-default-Thread-4) Next scheduled time:Mon Jul 16 16:57:54 EDT 2018
16:58:22,584 INFO  [managers.events.EventBuilder] (EE-ManagedScheduledExecutorService-default-Thread-4) Next scheduled time:Mon Jul 16 16:57:54 EDT 2018
16:58:22,584 INFO  [managers.events.EventBuilder] (EE-ManagedScheduledExecutorService-default-Thread-4) Next scheduled time:Mon Jul 16 16:57:54 EDT 2018
16:58:22,584 INFO  [managers.events.EventBuilder] (EE-ManagedScheduledExecutorService-default-Thread-4) Next scheduled time:Mon Jul 16 16:57:54 EDT 2018
16:58:22,584 INFO  [managers.events.EventBuilder] (EE-ManagedScheduledExecutorService-default-Thread-4) Next scheduled time:Mon Jul 16 16:57:54 EDT 2018
16:58:22,584 INFO  [managers.events.EventBuilder] (EE-ManagedScheduledExecutorService-default-Thread-4) Next scheduled time:Mon Jul 16 16:57:54 EDT 2018
16:58:22,584 INFO  [managers.events.EventBuilder] (EE-ManagedScheduledExecutorService-default-Thread-4) Next scheduled time:Mon Jul 16 16:57:54 EDT 2018

如果getNextRunTime保持不变,即使作业运行后,我如何利用skipRun和/或taskScheduledTime?有没有一种方法可以配置它以减少检查频率?过去的getNextRunTime()返回值是否不应该通知调度程序该作业不再运行并将其从调度程序中删除?

0 个答案:

没有答案