2012-04-16 3 views
0

응답에 따라 작업을 예약하려고합니다.응답에 따라 작업을 예약하십시오.

public Date scheduledTask() { 
    Date nextRun; 
    // ... 
    nextRun = something(); 
    // ... 
    return nextRun; 
} 

가 어떻게이 nextRun에 도달 할 때 같은 작업이 다시 호출되어 있는지 확인 할 수 있습니다 작업은 뭔가처럼?

감사합니다.

+0

이것은 전략 디자인 패턴의 좋은 사례입니다. http://en.wikipedia.org/wiki/Strategy_pattern#Example – Phani

+0

작업 스케줄링을 위해 Spring의 어떤 접근법을 사용하고 있습니까? 예를 들어 [this] (http://darthanthony.wordpress.com/2009/07/07/dynamic-scheduling-with-javaspring/)가 도움이 될 수 있습니다. – nobeh

+0

고맙습니다, @nobeh. 불행히도 귀하의 링크가 많이 도움이되지 않습니다, 나는 이미 비슷한 접근 방식을 시도했지만 분명히 그 결과에 따라 작업 일정을 변경할 방법이 없습니다. 나는 Spring의 Quartz Support와 Spring 3.0의 Scheduling Namespace를 가지고 놀려고 노력했다. 어떤 도움이라도 대단히 감사합니다. 감사합니다 – satoshi

답변

0

이것은 표준 Quartz 스케쥴러 API로 매우 간단합니다. 당신의 Job 컴퓨팅 nextRun 시간 내에 정의 startAt()과 트리거를 만들 :

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

안녕하세요 @ TomaszNurkiewicz, 도와 줘서 고마워. 귀하의 접근 방식은 매우 좋은 것 같아요, 유일한 것은 수동으로 응용 프로그램을 시작할 때 수동으로 트리거해야한다는 것입니다. 가장 좋은 방법은 무엇입니까? 아마도 Quartz XML 설정을 보여줄 수 있을까요? 고맙습니다. – satoshi

0

은 당신이 할 수 있어야 here 언급 한 아이디어를 따르

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을 확인해야 할 수도 있습니다.

관련 문제