需要动态设置quartz cron表达式 [英] Need to set the quartz cron expression dynamically

查看:57
本文介绍了需要动态设置quartz cron表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的 web 应用程序(Servlet web 应用程序)中使用石英,下面是石英.property 文件和石英.job.xml 的快照

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

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>

一切正常,按此顺序.我需要允许用户按照他们想要的方式更改时间(cron 表达式).我的问题是如何动态设置 cron 表达式.

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.

推荐答案

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

在上面,采用现有的触发器实例.创建一个新的触发器实例并设置 cron 表达式.

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.

这篇关于需要动态设置quartz cron表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆