Quartz Spring 整合入門

Quartz 任務調度器


簡介:

Quartz 是一個開源的作業調度框架,它完全由 Java 寫成,並設計用於 J2SE  J2EE 應用中。它提供了巨大的靈活性而不犧牲簡單性。你能夠用它來爲執行一個作業而創建簡單的或複雜的調度。最新版本 2.2.1


下載:

進入官網下載:http://www.quartz-scheduler.org/

或者在Maven中配置 下載Jar包

pom.xml中配置

  <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> 
就OK了 !!!!

導包quartz-jobs-2.2.1.jar和quartz-2.2.1.jar 和依賴的 slf4j包 log4j包 即可


因爲和Spring整合 首先 老套路 在萬能的Spring.xml中配置 

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
	xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
	http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
	http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd 
<span style="font-family: Arial, Helvetica, sans-serif;"><span style="white-space:pre">		</span> http://www.springframework.org/schema/tx   http://www.springframework.org/schema/tx/spring-tx-4.0.xsd</span><span style="font-family: Arial, Helvetica, sans-serif;">	
<span style="white-space:pre">		</span> http://www.springframework.org/schema/util  http://www.springframework.org/schema/util/spring-util-4.0.xsd">
</span><span style="font-family: Arial, Helvetica, sans-serif;"><span style="white-space:pre">		</span>
</span><span style="font-family: Arial, Helvetica, sans-serif;"><span style="white-space:pre">		</span>
<span style="white-space:pre">		<!--Quartz 任務調度器-->
    		<!-- 集成方式:JobDetailFactoryBean,並且任務類需要繼承QuartzJobBean-->
    		<!-- 定義jobDetail -->
    		<bean id="jobDetail" class="org.springframework.scheduling.quartz.JobDetailFactoryBean">
       		 	<!-- durability 表示任務完成之後是否依然保留到數據庫,默認false   -->
        		<property name="durability" value="true" />  
       		 	<!--     目標類  </span>就是自己寫的調度類<span style="white-space:pre">-->
       		 	<property name="jobClass" value="</span>寫調度的類的全限定名<span style="white-space:pre">"></property>
  		  </bean>
   		 <!-- 另一種觸發器是CornTrigger -->
  		   <bean id="cornTrigger" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
     		  		<property name="jobDetail" ref="jobDetail"/>
  		     		<!-- 每個10秒觸發 --> 
   		    		<property name="cronExpression" value="0/10 * * * * ?"/>
		    </bean> 
  	  <!-- 定義核心調度器 -->
    		<bean id="scheduler" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
      			<property name="triggers">
        			<ref bean="cornTrigger"/>
      			</property>
	    </bean></span>
</beans></span>


創建調度的類並繼承 QuartzJobBear 

public class QuartzTest extends QuartzJobBean {

	@Override
	protected void executeInternal(JobExecutionContext context) throws JobExecutionException {
		System.out.println("當前時間"+new Date());
		
	}

}

這樣的話 一個簡單的HelloWorld就完了最終結果 就會每10秒運行一次 這個調度器

在系統中 就可以直接調用自己的 業務代碼 這樣就OK了 


簡單的Spring整合入門




發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章