首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >根据响应安排任务

根据响应安排任务
EN

Stack Overflow用户
提问于 2012-04-16 17:14:39
回答 2查看 352关注 0票数 0

我正在尝试根据它的响应来安排一个任务。该任务类似于:

代码语言:javascript
复制
public Date scheduledTask() {
    Date nextRun;
    // ...
    nextRun = something();
    // ...
    return nextRun;
}

如何确保在到达nextRun时再次调用同一任务

谢谢。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-04-17 03:55:12

使用标准的Quartz调度器API,这非常简单。在Job中计算nextRun时间,并创建一个定义了startAt()的触发器:

代码语言:javascript
复制
public class ScheduledJob implements Job {

    @Override
    public void execute(JobExecutionContext context) throws JobExecutionException {
        final Date nextRun = something();

        Trigger trigger = newTrigger().
                startAt(nextRun).
                forJob(context.getJobDetail()).
                build();

        context.getScheduler().scheduleJob(trigger);
    }

}

经过测试,效果很好。

票数 0
EN

Stack Overflow用户

发布于 2012-04-17 03:19:54

遵循here中提到的想法,那么您应该能够拥有:

代码语言:javascript
复制
public class GuaranteeSchedule implements Trigger {

  private Future<?> resultForNextRun;
  private TaskScheduler scheduler;

  public void scheduledTask() {
    // 'this' is this trigger that is used by the scheduler 
    // and the method `nextExecutionTime` is automatically called by the scheduler
    resultForNextRun = scheduler.schedule(theTask, this);
    // theTask is the object that calls something()
  }

  // Implementing Trigger to add control dynamic execution time of this trigger
  @Override
  public Date nextExecutionTime(TriggerContext tc) {
    // make sure the result for the previous call is ready using the waiting feature of Future
    Object result = resultForNextRun.get();
    // Use tc or other stuff to calculate new schedule
    return new Date();
  }

}

剩下的部分,您应该遵循参考中提到的配置。我相信这将解决下一次触发器调用依赖于前一次调用的结果的问题。您可能还需要小心第一次调用scheduledTask以确保resultForNextRun != null

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/10171446

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档