Spring Quartz Java工程版和Web工程版示例

Wentasy發表於2012-07-16

環境: MyEclipse8.6 + Tomcat6.0.18 + Spring2.5.6


最近研究Spring Quartz定時器任務排程,寫了兩個Demo,歡迎閱讀指正。


Spring Quartz Java工程版


Mission.java(任務原始檔)


package com.springquartz.bean;

import org.apache.log4j.Logger;

/** 
 * @className:Mission.java
 * @classDescription:
 * @author:Wentasy
 * @createTime:2012-7-15 下午07:14:36
 * @since:JDK 1.6
 */
public class Mission {
	private Logger logger = Logger.getLogger(this.getClass().getName());
	
	
	public void print(){
		logger.info("開始進行任務排程......");
	}
}


SpringQuartzTest.java(測試原始檔)


package com.springquartz.test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/** 
 * @className:SpringQuartzTest.java
 * @classDescription:
 * @author:Wentasy
 * @createTime:2012-7-15 下午07:14:55
 * @since:JDK 1.6
 */
public class SpringQuartzTest {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		System.out.println("測試開始......");
		ApplicationContext  actx = new ClassPathXmlApplicationContext("applicationContext.xml");
		System.out.println("測試結束......");
	}

}



applicationContext.xml(Spring Quartz配置檔案)


<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>

	<!-- 要排程的物件 -->
	<bean id="job" class="com.springquartz.bean.Mission"></bean>
	<!-- 定義目標bean和bean中的方法 -->
	<bean id="jobTask" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
		<property name="targetObject">
			<ref local="job"/>
		</property>
		<property name="targetMethod">
			<value>print</value>
		</property>
	</bean>
	<!-- 定義觸發的時間 -->
	<bean id="cron" class="org.springframework.scheduling.quartz.CronTriggerBean">
	<property name="jobDetail">
		<ref bean="jobTask"/>
	</property>
	<property name="cronExpression">
		<value>10,15,20,25,30,35,40,45,50,55,00 * * * * ?</value>
		<!--  <value>00,05 53,54 * * * ?</value>-->
	</property>
	</bean>
	<!-- 總管理 -->
	<bean autowire="no" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
		<property name="triggers">
			<list>
				<ref local="cron"/>
			</list>
		</property>
	</bean>
</beans>


執行效果截圖:





Spring Quartz Web工程版

PrintInfoJob.java(任務原始檔)


package com.springquartz.bean;

import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.scheduling.quartz.QuartzJobBean;

import com.springquartz.service.IPrintInfoService;

/** 
 * @className:Mission.java
 * @classDescription:
 * @author:Wentasy
 * @createTime:2012-7-15 下午07:14:36
 * @since:JDK 1.6
 */
public class PrintInfoJob extends QuartzJobBean{
	
	private IPrintInfoService prinfInfoService = null;
	public IPrintInfoService getPrinfInfoService() {
		return prinfInfoService;
	}
	public void setPrinfInfoService(IPrintInfoService prinfInfoService) {
		this.prinfInfoService = prinfInfoService;
	}
	@Override
	protected void executeInternal(JobExecutionContext arg0)
			throws JobExecutionException {
		// TODO Auto-generated method stub
		this.prinfInfoService.print();
		
	}
}



IprintInfoService.java(任務介面原始檔)


package com.springquartz.service;
/** 
 * @className:IPrintInfoService.java
 * @classDescription:
 * @author:Wentasy
 * @createTime:2012-7-15 下午08:00:31
 * @since:JDK 1.6
 */
public interface IPrintInfoService {
	
	/**
	 * 方法名:print
	 * 功能:列印資訊
	 */
	public void print();
}


PrintInfoServiceImpl.java(任務實現原始檔)


package com.springquartz.service.impl;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import com.springquartz.service.IPrintInfoService;

/** 
 * @className:PrintInfoServiceImpl.java
 * @classDescription:
 * @author:Wentasy
 * @createTime:2012-7-15 下午07:59:33
 * @since:JDK 1.6
 */
public class PrintInfoServiceImpl implements IPrintInfoService{

	public void print() {
		// TODO Auto-generated method stub
		Calendar now = Calendar.getInstance();
		System.out.println("現在是北京時間:" + this.format(now.getTime()));
	}
	
	public String format(Date date){
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		return sdf.format(date);
	}
	
}


applicationContext.xml(Spring Quartz配置檔案)


<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:tx="http://www.springframework.org/schema/tx"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
				http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
				http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">

	
	<bean id="printInfoService" class="com.springquartz.service.impl.PrintInfoServiceImpl"/>
	<!-- 配置一個Job -->
	<bean id="printInfoJob" class="org.springframework.scheduling.quartz.JobDetailBean">
		<property name="jobClass" value="com.springquartz.bean.PrintInfoJob"/>
		<property name="jobDataAsMap">
			<map>
				<entry key="prinfInfoService" value-ref="printInfoService"></entry>
			</map>
		</property>
	</bean>
	
	<!-- 簡單的觸發器 -->
	<bean id="simplePrintInfoTrigger" class="org.springframework.scheduling.quartz.SimpleTriggerBean">
		<property name="jobDetail">
			<ref bean="printInfoJob"/>
		</property>
		<property name="startDelay">
			<value>6000</value>
		</property>
		<property name="repeatInterval">
			<value>6000</value>		
		</property>
	</bean>
	
	<!--複雜的觸發器 -->
	<bean id="complexPrintInfoTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">
		<property name="jobDetail">
			<ref bean="printInfoJob"/>
		</property>
		<property name="cronExpression">
			<!-- <value>0 0/1 * * * ?</value> -->
			<value>00,05,10,15,20,25,30,35,40,45,50,55 * * * * ?</value>
		</property>
	</bean>
	
	<!-- spring觸發工廠 -->
	<bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
		<property name="triggers">
			<list>
				<ref bean="complexPrintInfoTrigger"/>
			</list>
		</property>
	</bean>
</beans>


web.xml(Web工程配置檔案)


<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 
	xmlns="http://java.sun.com/xml/ns/javaee" 
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
	http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
	
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>classpath:applicationContext*.xml</param-value>
	</context-param>
	
	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>
	
	<welcome-file-list>
    	<welcome-file>index.jsp</welcome-file>
  	</welcome-file-list>
</web-app>


執行效果截圖:



專案注意事項:

1.所需Jar包:

commons-collections-3.2.jar commons-logging-1.1.jar jta.jar log4j-1.2.15.jarquartz-all-1.6.5.jar spring.jar

本文原始碼,歡迎下載:

Spring Quartz Java工程版


Spring Quartz Web工程版


參考資料:

Quartz CronTrigger最完整配置說明


Spring Quartz相關問題


關於Spirng Quartz定時觸發器+原始碼示例!


Spring配置Quartz例子


Spring + Quartz配置例項


Spring中Quartz的配置






相關文章