quartz 动态更改执行时间

说明:Quartz + Servlet, 参考国外著名站点的文章:http://stackoverflow.com/questions/12208309/need-to-set-the-quartz-cron-expression-dynamically  看域名,大家就知道了吧。

 

1、简单编写一个 quartz 例子

复制代码
package cn.demo.quartz;

import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;

public class CountJob implements Job
{

    public void execute(JobExecutionContext arg0) throws JobExecutionException
    {
        System.out.println("5s execute");
        
    }

}
复制代码

2、在 src 目录下,添加2个配置文件

quartz_job.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
<?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_2_0.xsd"
    version="2.0">
 
    <pre-processing-commands>
        <delete-jobs-in-group>*</delete-jobs-in-group>
        <!-- clear all jobs in scheduler -->
        <delete-triggers-in-group>*</delete-triggers-in-group>
        <!-- clear all triggers in scheduler -->
    </pre-processing-commands>
 
    <processing-directives>
        <!-- if there are any jobs/trigger in scheduler of same name (as in this file), overwrite them -->
        <overwrite-existing-data>true</overwrite-existing-data>
        <!-- if there are any jobs/trigger in scheduler of same name (as in this file), and over-write is false, ignore them rather then generating an error -->
        <ignore-duplicates>false</ignore-duplicates>
    </processing-directives>
 
 
    <schedule>
        <job>
            <name>job1</name>
            <job-class>cn.demo.quartz.CountJob</job-class>
        </job>
        <trigger>
            <cron>
                <name>trigger_name1</name>
                <group>trigger_group1</group>
                <job-name>job1</job-name>
                <cron-expression>0/5 * * * * ?</cron-expression>
            </cron>
        </trigger>
    </schedule>
 
         
     
</job-scheduling-data>

还有 quartz.properties

1
2
3
4
5
6
7
8
9
10
11
12
13
org.quartz.scheduler.instanceName=MyQuartzScheduler 
org.quartz.scheduler.rmi.export=false 
org.quartz.scheduler.rmi.proxy=false 
org.quartz.scheduler.wrapJobExecutionInUserTransaction=false 
org.quartz.threadPool.class=org.quartz.simpl.SimpleThreadPool 
org.quartz.threadPool.threadCount=10 
org.quartz.threadPool.threadPriority=5 
org.quartz.threadPool.threadsInheritContextClassLoaderOfInitializingThread=true 
org.quartz.jobStore.misfireThreshold=60000 
org.quartz.jobStore.class=org.quartz.simpl.RAMJobStore 
org.quartz.plugin.jobInitializer.class=org.quartz.plugins.xml.XMLSchedulingDataProcessorPlugin 
#job and trigger configuration file 
org.quartz.plugin.jobInitializer.fileNames=quartz_job.xml 

 

3、添加一个 Servlet, ChangeTimeServlet

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package cn.demo.servlet;
 
import java.io.IOException;
 
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
 
import org.quartz.CronExpression;
import org.quartz.CronTrigger;
import org.quartz.Scheduler;
import org.quartz.SchedulerFactory;
import org.quartz.impl.StdSchedulerFactory;
 
public class ChangeTimeServlet extends HttpServlet
{
 
    /**
     *
     */
    private static final long serialVersionUID = 4586397924078936339L;
 
    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException
    {
 
        this.doPost(request, response);
    }
 
    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException
    {
        try
        {
            SchedulerFactory factory = new StdSchedulerFactory();
 
            Scheduler scheduler = factory.getScheduler();
              
            CronTrigger cronTrigger = (CronTrigger) scheduler.getTrigger("trigger_name1", "trigger_group1");
         
            CronTrigger newTriggerIns = new CronTrigger();
             
            newTriggerIns.setJobName(cronTrigger.getJobName());
            newTriggerIns.setName("abc");
            newTriggerIns.setCronExpression(new CronExpression("0/20 * * * * ?"));
             
            scheduler.rescheduleJob("trigger_name1", "trigger_group1", newTriggerIns);
        } catch (Exception e)
        {
            e.printStackTrace();
        }
    }
 
}

4、web.xml 配置文件添加

复制代码
<!-- quartz start -->
    <servlet>
        <servlet-name>QuartzInitializer</servlet-name>
        <display-name>Quartz Initializer Servlet</display-name>
        <servlet-class>
            org.quartz.ee.servlet.QuartzInitializerServlet
        </servlet-class>
        <load-on-startup>3</load-on-startup>
        <init-param>
            <param-name>config-file</param-name>
            <param-value>/quartz.properties</param-value>
        </init-param>
        <init-param>
            <param-name>shutdown-on-unload</param-name>
            <param-value>true</param-value>
        </init-param>
        <init-param>
            <param-name>start-scheduler-on-load</param-name>
            <param-value>true</param-value>
        </init-param>
    </servlet>

    <!-- quartz end -->

    <servlet>
        <servlet-name>ChangeTimeServlet</servlet-name>
        <servlet-class>cn.demo.servlet.ChangeTimeServlet</servlet-class>
    </servlet>

    <servlet-mapping>
        <servlet-name>ChangeTimeServlet</servlet-name>
        <url-pattern>/change</url-pattern>
    </servlet-mapping>
复制代码

 

5、启动 tomcat

每间隔 5s,控制台会打印语句

System.out.println("5s execute");

访问servlet http://localhost:8660/change

这时候,会间隔 20s 打印语句。

 

感谢 http://stackoverflow.com,帮我解决了无数次的问题。现在也越来越佩服自己了,太会模仿了。

 

posted @   safetys  阅读(8563)  评论(1编辑  收藏  举报
编辑推荐:
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· 展开说说关于C#中ORM框架的用法!
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
点击右上角即可分享
微信分享提示