一、引入两个jar包
spring-context-support-3.1.3.RELEASE.jar和quartz-1.8.6.jar
二、配置文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:util="http://www.springframework.org/schema/util" 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:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:jaxws="http://cxf.apache.org/jaxws"
xsi:schemaLocation="
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.1.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.1.xsd
<!-- ======================== 类方法 ======================== -->
<bean id="vehChangeStatusQueueJob" class="org.springframework.scheduling.quartz.JobDetailBean">
<property name="jobClass" value="testwebapp.com.wangzuojia.schedule.ScheduleTest"/>
<!-- <property name="jobDataAsMap">
<map>
<entry key="autoCommonBsh2014" value-ref="autoCommonBsh2014"/>
<entry key="changeStatusQueueBsh" value-ref="changeStatusQueueBsh"/>
</map>
</property> -->
</bean>
<!-- ======================== 触发器 ======================== -->
<bean id="vehChangeStatusQueueCronTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">
<property name="jobDetail" ref="vehChangeStatusQueueJob" />
<property name="cronExpression" value="2/3 * * * * ?" />
</bean>
<!-- ======================== 调度工厂 ======================== -->
<bean id="SpringJobSchedulerFactoryBean" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<list>
<ref bean="vehChangeStatusQueueCronTrigger"/>
</list>
</property>
</bean>
</beans>
三、测试类ScheduleTest
package testwebapp.com.wangzuojia.schedule;
import java.util.Date;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.scheduling.quartz.QuartzJobBean;
public class ScheduleTest extends
QuartzJobBean {
@Override
protected void executeInternal(JobExecutionContext arg0) throws JobExecutionException {
System.out.println("Schedule start at:"+ new Date());
}
}
还有一种配置方法如下
<?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:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util-3.0.xsd"
default-lazy-init="true">
<bean id="scheduleManager" class="com.cpic.auap.schedule.ScheduleManager">
<property name="scheduler" ref="scheduleBean"/>
<property name="scheduleMap">
<map>
<!-- 测试Job -->
<entry key="testJob" value-ref="testJobController"/>
</map>
</property>
</bean>
<!-- 测试批处理-->
<bean id="testTrigger" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
<property name="jobDetail" ref="testJob"/>
<property name="cronExpression" value="0 0/2 * * * ?"></property>
</bean>
<bean id="testJob" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<!-- 对应我们的类-->
<property name="targetObject">
<ref bean="scheduleManager"/>
</property>
<!-- 对应我们的类方法-->
<property name="targetMethod">
<value>doCommonJob</value>
</property>
<!-- 对应我们的类方法参数-->
<property name="arguments">
<list>
<value>testJob</value>
<value>T01/T02/T03/T04</value>
</list>
</property>
<property name="concurrent">
<value>false</value>
</property>
</bean>
</beans>