quartz 配置运行

这篇文章是对quartz 2.2.1进行配置

分为spring 整合版本和QuartzInitializerServlet整合版本

首先是QuartzInitializerServlet整合版本

主要是参考下面这篇博客

http://liuzidong.iteye.com/blog/1149537

和官网教程

http://www.quartz-scheduler.org/documentation/quartz-2.2.x/tutorials/

和API文档

http://www.quartz-scheduler.org/api/2.2.1/index.html

 

配置文件

porm.xml

http://www.quartz-scheduler.org/downloads/

复制porm

 

<dependency>
    <groupId>org.quartz-scheduler</groupId>
    <artifactId>quartz</artifactId>
    <version>2.2.1</version>
</dependency>
<dependency>
    <groupId>org.quartz-scheduler</groupId>
    <artifactId>quartz-jobs</artifactId>
    <version>2.2.1</version>
</dependency>   

 

还要添加jta

要不会报

java.lang.NoClassDefFoundError: javax/transaction/UserTransaction

 根据:http://www.cnblogs.com/panie2015/p/5625439.html

添加对应jar就好了

<dependency>
  <groupId>javax.transaction</groupId>
  <artifactId>jta</artifactId>
  <version>1.1</version>
</dependency>

之后是web.xml配置

<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>2</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>wait-on-shutdown</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>

 这样子可以让任务在tomcat启动时就自动启动,关闭之后自动关闭

主要参考API文档:

http://www.quartz-scheduler.org/api/2.2.1/org/quartz/ee/servlet/QuartzInitializerServlet.html

里面有示例

之后是

quartz_job.xml
quartz.properties

配置

首先是

quartz.properties配置:

参考:

http://www.quartz-scheduler.org/documentation/quartz-2.2.x/configuration/

有不懂得可以点到相关API文档里去看

org.quartz.scheduler.instanceName = DefaultQuartzScheduler
org.quartz.scheduler.instanceId = one
org.quartz.threadPool.class = org.quartz.simpl.SimpleThreadPool
org.quartz.threadPool.threadCount = 10
org.quartz.threadPool.threadPriority = 5

org.quartz.plugin.jobInitializer.class = org.quartz.plugins.xml.XMLSchedulingDataProcessorPlugin 
org.quartz.plugin.jobInitializer.fileNames = quartz_job.xml 
org.quartz.plugin.jobInitializer.failOnFileNotFound = true 

org.quartz.plugin.shutdownhook.class = org.quartz.plugins.management.ShutdownHookPlugin 
org.quartz.plugin.shutdownhook.cleanShutdown =true

 然后是quartz_job.xml

http://www.quartz-scheduler.org/documentation/quartz-2.x/cookbook/JobInitPlugin.html

注意这里

            <group>DEFAULT</group>

要是default才可以,要不tomcat不会去执行

<?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">
	<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>my-very-clever-job</name>
            <group>DEFAULT</group>
            <description>The job description</description>
            <job-class>com.xmal.dms.quatz.job</job-class>
            <job-data-map>
                <entry>
                    <key>timeout</key>
                    <value>5</value>
                </entry>
            </job-data-map>
        </job>

		<trigger>
    		<cron>
        		<name>my-trigger</name>
            	<group>DEFAULT</group>
            	<job-name>my-very-clever-job</job-name>
            	<job-group>DEFAULT</job-group>
	       	 	<!-- trigger every night at 23:30 am -->
            	<cron-expression>0 30 23 * * ?</cron-expression>
			</cron>
		</trigger>
	</schedule>
</job-scheduling-data>

 之后是job类

package com.xmal.dms.quatz;

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

public class job implements Job {

	private int timeout;
	public void setTimeout(int timeout) {
		this.timeout = timeout;
	}

	//时间超出了就不行
	@Override
	public void execute(JobExecutionContext context) throws JobExecutionException {
		// TODO Auto-generated method stub
		System.out.println("hello1");

		for(int i=0;i<timeout;i++)
			System.out.println("hello");
	}
}

 相当于一个bean

	private int timeout;
	public void setTimeout(int timeout) {
		this.timeout = timeout;
	}
这么写就可以直接在代码里使用

 

 

然后是是spring整合版本

 首先弄明白beanfactory和factorybean区别

http://www.cnblogs.com/redcool/p/6413461.html

https://docs.spring.io/spring/docs/current/spring-framework-reference/html/scheduling.html

http://docs.spring.io/spring-framework/docs/4.3.10.RELEASE/javadoc-api/org/springframework/scheduling/quartz/SchedulerFactoryBean.html#setConfigLocation-org.springframework.core.io.Resource-

<bean id="firstJobDetail"  class="org.springframework.scheduling.quartz.JobDetailFactoryBean"> 
	<property name="jobClass" value="com.xmal.dms.quatz.job"/>  
	<property name="jobDataAsMap">
    	<map>
        	<entry key="timeout" value="5"/>
    	</map>
	</property>        
	<property name="durability" value="true" />     
    <property name="requestsRecovery" value="true" />  
</bean>  
<bean id="firstTrigger" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">  
    <property name="jobDetail" ref="firstJobDetail" />  
    <property name="cronExpression" value="0 0 11 22 * ?" />  
</bean>  
<bean name="quartzScheduler" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">  
    <property name="triggers">  
        <list>  
            <ref bean="firstTrigger"/>  
        </list>  
    </property>  
</bean>     

  

 

 

 

 

posted @ 2017-08-24 11:31  每天一点积累  阅读(486)  评论(0编辑  收藏  举报