Need to set the quartz cron expression dynamically

痴心易碎 提交于 2020-01-10 11:30:21

问题


I'm using quartz in my web application (Servlet web app) following is snap of quartz.property file and the quartz.job.xml

quartz.property

#===================================================
# Configure the Job Initialization Plugin
#===================================================

org.quartz.plugin.jobInitializer.class = org.quartz.plugins.xml.XMLSchedulingDataProcessorPlugin
org.quartz.plugin.jobInitializer.fileNames = jobs.xml
org.quartz.plugin.jobInitializer.failOnFileNotFound = true
org.quartz.plugin.jobInitializer.scanInterval = 10
org.quartz.plugin.jobInitializer.wrapInUserTransaction = false


<?xml version='1.0' encoding='utf-8'?>
<job-scheduling-data xmlns="http://www.quartz-scheduler.org/xml/JobSchedulingData"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://www.quartz-scheduler.org/xml/JobSchedulingData http://www.quartz-scheduler.org/xml/job_scheduling_data_1_8.xsd"
  version="1.8">

    <schedule>
        <job>
            <name>my-very-clever-job</name>
            <group>MYJOB_GROUP</group>

            <description>The job description</description>
            <job-class>com.acme.scheduler.job.ReportJob</job-class>
        </job>

        <trigger>
            <cron>
                <name>my-trigger</name>
                <group>MYTRIGGER_GROUP</group>
                <job-name>my-very-clever-job</job-name>

                <job-group>MYJOB_GROUP</job-group>
                <!-- trigger every night at 4:30 am -->
                <cron-expression>0 30 4 * * ?</cron-expression>

            </cron>
        </trigger>
    </schedule>
</job-scheduling-data>

Every thing work fine,in this order. I need to allow user to change the time (cron expression) as the way they want.My question is how do i set the cron expression in dynamically.


回答1:


CronTrigger cronTrigger = (CronTrigger) stdScheduler
                .getTrigger(triggerName,triggerGroupName);
CronTrigger newTriggerIns = new CronTrigger();
 newTriggerIns.setJobName(cronTrigger.getJobName());
 newTriggerIns.setName(triggerName);
 newTriggerIns.setCronExpression(newCronExpression);
 stdScheduler.rescheduleJob(triggerName,triggerGroupName,newTriggerIns);

In the above, take the existing trigger instance. Create one new trigger instance and set cron expression.

Then reschedule the existing instance with new one.




回答2:


Creating a new Trigger like this doesn't work.

CronTrigger cronTrigger = (CronTrigger) stdScheduler.getTrigger(triggerName,triggerGroupName);
CronTrigger newTriggerIns = new CronTrigger();
newTriggerIns.setJobName(cronTrigger.getJobName());
newTriggerIns.setName(triggerName);
newTriggerIns.setCronExpression(newCronExpression);
stdScheduler.rescheduleJob(triggerName,triggerGroupName,newTriggerIns); //doesn't work

You just have to edit the original trigger like this:

CronTrigger cronTrigger = (CronTrigger) stdScheduler.getTrigger(triggerName,triggerGroupName);
cronTrigger.setCronExpression(newCronExpression);
stdScheduler.rescheduleJob(triggerName,triggerGroupName,cronTrigger);



回答3:


Use Quartz api. Programmatically take this trigger instance, cast it to CronTrigger instance and use it's setCronExpression to put expression dynamically.



来源:https://stackoverflow.com/questions/12208309/need-to-set-the-quartz-cron-expression-dynamically

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!