从石英重复工作(Spring + Quartz应用程序) - Job Chaining

时间:2012-08-30 11:09:54

标签: java spring quartz-scheduler

我的石英作业有以下设置 -

    <bean id="jobDetail" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
    <property name="targetObject" ref="actualObject" /><br>
    <property name="targetMethod" value="processData"/>
    <property name="concurrent" value="false"/>
</bean>

    <bean id="simpleTrigger" class="org.springframework.scheduling.quartz.SimpleTriggerFactoryBean">
    <property name="jobDetail" ref="jobDetail" />
    <property name="startDelay" value="10000" />
    <property name="repeatInterval" value="1000" />
</bean>

这对我有用。 我想做的是一旦完成就调用processData agai,n。 我知道古老而真实的方法最适合,但我想用石英做到这一点。

1 个答案:

答案 0 :(得分:4)

首先,您必须解释为什么“想要使用quartz ”,因为“ Good old while(true)”是实现您的正确方法用例(当然你需要一个额外的线程,但Quartz也需要它)。这听起来像过度工程,所以你最好有充分的理由。

据说你有两个选择:

  • 重新安排作业,以便在您离开时立即运行。原则上:

    public class HelloJob implements Job {
    
        public HelloJob() {
        }
    
        public void execute(JobExecutionContext context) throws JobExecutionException
        {
            //do your job...
    
            Trigger trigger = newTrigger().build();
            JobDetail job = newJob(HelloJob.class).build();
            context.getScheduler().scheduleJob(trigger, job);
        }
    }
    

    您不需要XML配置,但必须安排此作业以某种方式第一次运行(例如直接使用@PostConstruct Scheduler)。你的工作完成后,它将重新安排同样的工作。

  • JobChainingJobListener可能适合您,请参阅:Can Quartz Scheduler Run jobs serially?

与“ good old while(true)”相比,这两种解决方案都非常重要。