quartz 동적 변경 실행 시간

12663 단어 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. 웹. 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 수 없 이 많은 문 제 를 해결 해 주 었 습 니 다.지금도 점점 자신 에 게 탄복 하고 모방 을 너무 잘 한다.

좋은 웹페이지 즐겨찾기