当前位置: 代码迷 >> Web前端 >> Spring Quartz Java工程版跟Web工程版示例
  详细解决方案

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

热度:760   发布时间:2012-07-30 16:19:05.0
Spring Quartz Java工程版和Web工程版示例

环境: 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的配置






  相关解决方案