Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 934
Entropy Avatar asked Nov 30 '25 23:11

Entropy


1 Answers

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 {
            // ...
        }

    }

}
like image 182
Krayo Avatar answered Dec 02 '25 14:12

Krayo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!