Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Need to set the quartz cron expression dynamically

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.

like image 648
Sam Avatar asked Aug 31 '12 02:08

Sam


People also ask

How do I create a cron expression for a quartz component?

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.

Which is an example of CRON expression?

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.

How to use cron expressions to create schedules?

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.

What is the difference between simple triggers and Cron triggers in quartz?

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.


2 Answers

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.

like image 55
abhijit nag Avatar answered Sep 29 '22 09:09

abhijit nag


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);
like image 33
atripathi Avatar answered Sep 29 '22 09:09

atripathi