Spring3.2.4集成quartz2.2.1定时任务(demo).

在JavaEE系统中,我们会经常用到定时任务,下面是我自己写的一个demo.

源码地址:http://pan.baidu.com/s/1BXHv3

1.所需要的jar

image

2.实体bean

package cn.zyc.quartz;

import java.util.Date;

public class Study {

	public void doStudy() {
		System.out.println("It is " + new Date());
	}
}

3.applicationContext.xml

<?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:p="http://www.springframework.org/schema/p"
	xmlns:aop="http://www.springframework.org/schema/aop" 
	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-3.0.xsd
	http://www.springframework.org/schema/tx 
	http://www.springframework.org/schema/tx/spring-tx-3.0.xsd   
    http://www.springframework.org/schema/aop 
	http://www.springframework.org/schema/aop/spring-aop-3.0.xsd   
    http://www.springframework.org/schema/context 
	http://www.springframework.org/schema/context/spring-context-3.0.xsd">

	<!-- bean -->
	<bean id="study" class="cn.zyc.quartz.Study" />

	<!-- 定义调用对象和调用对象的方法 -->
	<bean id="studyDetail"
		class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
		<!-- 调用的类 -->
		<property name="targetObject" ref="study" />
		<!-- 调用类中的方法 -->
		<property name="targetMethod" value="doStudy" />
		<!-- 是否允许任务并发执行。当值为false时,表示必须等到前一个线程处理完毕后才再启一个新的线程 -->
		<property name="concurrent" value="false"/>
	</bean>
	
<!-- quartz-1.8以前的配置 
<bean id="myJobTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean"> 
<property name="jobDetail"> <ref bean="studyDetail" /> </property> 
<property name="cronExpression"> <value>0/1 * * * * ?</value> </property> </bean> -->
		
	<!-- quartz-2.x的配置 -->
	 <!-- 定义触发时间 -->
	<bean id="myJobTrigger"
		class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
		<property name="jobDetail">
			<ref bean="studyDetail" />
		</property>
		<property name="cronExpression">
			<value>0/5 * * * * ?</value>
		</property>
	</bean>
	
	<!-- 总管理类 如果将lazy-init='false'那么容器启动就会执行调度程序  -->
	<!-- 如果lazy-init='true',则需要实例化该bean才能执行调度程序 -->
	<bean name="startQuertz" lazy-init="false" autowire="no"
		class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
		<property name="triggers">
			<list>
				<ref bean="myJobTrigger" />
			</list>
		</property>
		<!-- <property name="autoStartup" value="true"/>  -->
	</bean>
</beans>

4.web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" 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_3_0.xsd">

	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>/WEB-INF/classes/applicationContext.xml</param-value>
	</context-param>

	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>

</web-app>

你可能感兴趣的:(Spring3.2.4集成quartz2.2.1定时任务(demo).)