Entropy
Entropy

Reputation: 1281

Rescheduling Quartz at Runtime

We are novice Quartz users at my project, writing our first Quartz tasks. We have our tasks running, but obviously we want to learn about managing them. We have them configured in Spring like so:

<bean name="enoteExpirationTask"    class="org.springframework.scheduling.quartz.JobDetailBean">
    <property name="jobClass" value="gov.usdoj.afms.enote.job.DailyExpirationJob" />
    <property name="jobDataAsMap">
        <map>
            <entry key="messageService" value-ref="enoteMessageService" />
            <entry key="logicalDeleteAge" value="${expiryProcess.logical.age}" />
            <entry key="physicalDeleteAge" value="${expiryProcess.physical.age}" />
        </map>
    </property>
</bean>
<bean id="cronEnoteExpirationTaskTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">
    <property name="jobDetail" ref="enoteExpirationTask" />
    <property name="cronExpression" value="0 0 7 * * ?" />
</bean>

Question 1: Can I make Quartz re-read this configuration so that if I alter it at runtime it changes its schedule? This would be the simplest solution, but we didn't see anything on it. I'm hoping we missed something.

Question 2: If not, I understand there are supposed to be third party tools to do this, Teracotta being one. Are there any opensource or freeware utilities that would let you very simply change the schedule?

Question 3: If not, what is involved with writing a little Java utility to do it? Is it worth it to write one? Or does Teracotta have enough value-add that you would recommend buying it? And if so, what are the difference making features I can sell to management?

Upvotes: 3

Views: 5962

Answers (2)

Krayo
Krayo

Reputation: 2510

My implementation (Spring 3.0.6, Quartz 2.2.1):

public interface SchedulerService {

    void register( Class<? extends Job> jobClass, String cronExpression );

    void reschedule( Class<? extends Job> jobClass, String cronExpression );

}

@Service
public class SchedulerServiceImpl implements SchedulerService {

    private Scheduler scheduler;

    @PostConstruct
    void init() {
        try {
            scheduler = StdSchedulerFactory.getDefaultScheduler();
            scheduler.start();
        } catch ( Exception e ) {
            // handle exception
        }
    }

    @PreDestroy
    void destroy() {
        try {
            scheduler.shutdown();
        } catch ( Exception e ) {
            // handle exception
        }
    }

    @Override
    public void register( Class<? extends Job> jobClass, String cronExpression ) {
        try {
            String name = jobClass.getSimpleName();
            CronScheduleBuilder cronScheduleBuilder = CronScheduleBuilder.cronSchedule( cronExpression );

            JobDetail jobDetail = JobBuilder.newJob( jobClass ).withIdentity( name ).build();
            CronTrigger cronTrigger = TriggerBuilder.newTrigger().withIdentity( name ).withSchedule( cronScheduleBuilder ).build();

            scheduler.scheduleJob( jobDetail, cronTrigger );
        } catch ( Exception e ) {
            // handle exception
        }
    }

    @Override
    public void reschedule( Class<? extends Job> jobClass, String cronExpression ) {
        try {
            String name = jobClass.getSimpleName();
            CronScheduleBuilder cronScheduleBuilder = CronScheduleBuilder.cronSchedule( cronExpression );

            CronTrigger newCronTrigger = TriggerBuilder.newTrigger().withIdentity( name ).withSchedule( cronScheduleBuilder ).build();

            scheduler.rescheduleJob( TriggerKey.triggerKey( name ), newCronTrigger );
        } catch ( Exception e ) {
            // handle exception
        }
    }

}

Example of use:

@Service
public class MyServiceImpl implements MyService {

    @Autowired
    SchedulerService schedulerService;

    @PostConstruct
    void init() {
        schedulerService.register( MyJob.class, "0 10 * * * ?" );
    }

    @Override
    public void reschedule( String cronExpression ) {
        schedulerService.reschedule( MyJob.class, cronExpression );
    }

    @DisallowConcurrentExecution
    public static class MyJob implements Job {

        @Override
        public void execute( JobExecutionContext context ) throws JobExecutionException {
            // ...
        }

    }

}

Upvotes: 8

fvu
fvu

Reputation: 32983

As you're using Quartz through Spring that's probably more a Spring problem than a Quartz problem, and I don't know enough about Spring to help you there.

However, if you're willing to use Quartz without its Spring wrapper, Quartz has everything on board you need to specify schedules in an XML file which it can optionally scan for updates at configurable intervals. This plugin is called org.quartz.plugins.xml.XMLSchedulingDataProcessorPlugin and its basic configuration is explained in the documentation. The Quartz distribution contains several examples, Example 10 shows how to use XMLSchedulingDataProcessorPlugin and its accompanying job configuration file that in the default setting will be rescanned every 120 seconds. Playing with that demo and studying its sourcecode should learn you enough about Quartz to get started.

Upvotes: 0

Related Questions