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

<?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

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

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 @ 2014-05-20 22:58  safetys  阅读(8487)  评论(1编辑  收藏  举报