简体   繁体   English

使Spring Quartz Job Scheduler运行用户操作

[英]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. 我正在使用Spring Quartz Job Scheduler,以便在用户选择的日期内运行作业。 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. 所以它的cronExpression值也会根据用户选择的日期而改变。 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 Job Scheduler不是实现我需要的方法吗?

Try to get your CronTriggerBean object testme programmatically 尝试以编程方式获取CronTriggerBean对象testme
and set into it the desired cronExpression. 并在其中设置所需的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. 正如Julien所提到的,关键是在用户采取行动时创建作业或触发器。

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 Job Scheduler不是实现我需要的方法吗?

Quartz can do this. Quartz可以做到这一点。 You just need to reschedule the jobs. 你只需要重新安排工作。

You can autowire an instance of SchedulerFactoryBean into your own bean. 您可以将SchedulerFactoryBean的实例自动装配到您自己的bean中。 From SchedulerFactoryBean you can get the Scheduler and tweak the job's triggers as needed or create new jobs/triggers: 从SchedulerFactoryBean,您可以获取调度程序并根据需要调整作业的触发器或创建新的作业/触发器:

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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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