简体   繁体   中英

Make Spring Quartz Job Scheduler Run on a user action

I am using Spring Quartz Job Scheduler in order to run a job in user selected days. Instead of automatically calls the job scheduler when application start I need the job scheduler start run on aa particular user action

<bean id="scheduler" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
      <property name="triggers">
            <list>
            <ref bean="testme"/>
           </list>
      </property>     
</bean>

<bean id="testme" class="org.springframework.scheduling.quartz.CronTriggerBean">
        <property name="jobDetail">
            <ref bean="testme1"/>
        </property>
        <property name="cronExpression">
            <value>0 15 10 1,15 * ?</value>  
        </property>
</bean>

Also I need to first user to select the dates he needs to run the job eg: monday and friday and then after click on submit button the scheduler will start from that point? So its like cronExpression values also change depending on the user selected dates. More over user can later change it to different dates too. So is it possible to do this or does Quartz Job Scheduler is not the approach to achieve what i needs?

Try to get your CronTriggerBean object testme programmatically
and set into it the desired cronExpression. This should work, I think.

The scheduler will run at startup. As Julien mentioned, the key is to create the jobs or triggers when the user takes action.

More over user can later change it to different dates too. So is it possible to do this or does Quartz Job Scheduler is not the approach to achieve what i needs?

Quartz can do this. You just need to reschedule the jobs.

You can autowire an instance of SchedulerFactoryBean into your own bean. From SchedulerFactoryBean you can get the Scheduler and tweak the job's triggers as needed or create new jobs/triggers:

Scheduler scheduler = schedulerFactory.getScheduler();
JobDetail job = scheduler.getJobDetail("yourJobName", "yourJobGroup");

// possibly do some stuff with the JobDetail...

// get the current trigger
CronTrigger cronTrigger = (CronTrigger) scheduler.getTrigger("yourTriggerName", "yourTriggerGroupName");

// possibly do some stuff with the current Trigger...
if (cronTrigger != null)
   something.doSomeStuff();

// replace the old trigger with a new one.  unless your users are sysadmins, they
// probably don't want to enter cron-type entries.  there are other trigger types
// besides CronTrigger. RTFM and pick the best one for your needs
CronTrigger newTrigger = new CronTrigger();
newTrigger.setName("newTriggerName");
newTrigger.setGroup("yourTriggerGroup");
newTrigger.setCronExpression("valid cron exp here");

// the new trigger must reference the job by name and group
newTrigger.setJobName("yourJobName");
newTrigger.setJobGroup("yourJobGroup");

// replace the old trigger.  this is by name, make sure it all matches up.
scheduler.rescheduleJob("yourJobName",  "yourTriggerGroup", newTrigger);

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