简体   繁体   中英

Dynamically Scheduled Jobs with Quartz

I implemented a Scheduled Job in Spring, I'm using Spring 3.1.1 with Hibernate and Struts2. The configuration works fine, but I want to change de cron dynamically, so I found several examples but I could not implement them, everywhere are different configurations, I only need to read cron values from the database instead of configuration file. Is this possible somehow?

My configuration now looks like that:

<!-- Scheduler Tasks -->
<bean name="statTask" class="com.bvc.spring.schedulers.MarketStatusJob"></bean>

<!-- Scheduler jobs -->
<bean id="statJobDetail" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
    <property name="targetObject" ref="statTask" />
    <property name="targetMethod" value="execute" />
</bean>

<!-- Cron Triggers -->
<bean id="statCronTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">
    <property name="jobDetail" ref="statJobDetail" />
    <property name="cronExpression" value="0 30 12 1/1 * ? *"/>
</bean>

<!-- Triggers -->
<bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
    <property name="triggers">
        <list>
            <ref bean="statCronTrigger"/>
        </list>
    </property>
</bean>

Thanks in advance for help guys.

The possibly easiest approach would be, to subclass CronTriggerBean and implement database property resolution in an overridden setCronExpression(..) method, where you go to the database, fetch the desired cron, and call super.setCronExpression(dbValue)

An alternative, harder approach is to implement a PropertyPlaceholderConfigurer that reads them from a database rather than a properties file. It may not be trivial, though. There is no support for that, because it is more customary to read the values from a properties file. Also note that you won't be able to change the cron dynamically during execution.

You dont need to have statCronTrigger, need to implement quartz trigger in your main class Job detail is fine.

    CronTrigger trigger = null;
    JobDetail jobD;


    //Load context
    ApplicationContext context = new ClassPathXmlApplicationContext("YOUR_CONTEXT_FILES.xml");

    //Setup JobDetail

    jobD = (JobDetail) context.getBean("statJobDetail");

    //Setup CronTrigger
    try {
        trigger = new CronTrigger();
        trigger.setName("AppTrigger");
        trigger.setGroup(jobD.getGroup());
        trigger.setJobName(jobD.getName());
        trigger.setJobGroup(jobD.getGroup());
        trigger.setCronExpression("*/10 * * * * ?");// you can read this from DB as well or any other configured string 

    } catch (ParseException e1) {
        e1.printStackTrace();
    }

    //Scheduler
try{

        Scheduler scheduler =  (Scheduler) context.getBean("Scheduler");
        scheduler.scheduleJob(jobD, trigger);

You may add quartz scheduler bean in context

  <bean id="Scheduler" class="org.springframework.scheduling.quartz.SchedulerFactoryBean"></bean>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM