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.
If you press the , the quartz cron generator screen will open. You can create, with this Quartz Cron Generator, a cron expression for you component. Cron-Expressions are strings that are actually made up of seven sub-expressions. Each sub-expression describes the individual details of the schedule.
Example of Cron Expression In our example, we first create a schedule, a job that needs to be executed, a trigger that defines when to execute and then finally scheduling the job. Our focus is on the cron trigger.
Using Cron expressions, we can specify schedules such as the following. Run every minute every one hour. Run every hour, starting from the 15-minute mark of the hour. Run every hour, except for the hours between 02:00a.m. and 05:00a.m The above list provides a very basic list of schedules that can be written using a single cron expression.
He works as a principal Engineer in the logistics domain. Quartz scheduler offers two kind of triggers, simple trigger and cron trigger. If the schedule is based on specified intervals then a simple trigger will do but if your job needs to be fired based on calendar-like notions then you need to use cron triggers.
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.
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With