实现定时任务有两种方式:
jobDetail -JobDetailBean
jobDetail -MethodInvokingJobDetailFactoryBean
这里呢先讲的是MethodInvokingJobDetailFactoryBean实现的
第一步需要定义作业类(定时任务处理的主要逻辑)
1 package com.ninefbank.smallpay.admin.query.job; 2 3 import java.beans.BeanInfo; 4 import java.beans.Introspector; 5 import java.beans.PropertyDescriptor; 6 import java.io.InputStream; 7 import java.lang.reflect.Method; 8 import java.math.BigDecimal; 9 import java.util.Date; 10 import java.util.HashMap; 11 import java.util.Map; 12 import java.util.Properties; 13 14 import org.slf4j.Logger; 15 import org.slf4j.LoggerFactory; 16 import org.springframework.beans.factory.annotation.Autowired; 17 import org.springframework.stereotype.Service; 18 19 20 @Service("queryJob") 21 public class QueryJob { 22 @Autowired 23 private static IFyOperateService fyOperateService; 24 private static Logger logger = LoggerFactory.getLogger(QueryJob.class); 25 26 public void query() throws Exception{ 27 logger.info("beginQuery=============="+new Date()); 28 // Class cls = FYGoldAcc.class; 29 // for (Object obj : cls.getEnumConstants()) { 30 // FYGoldAcc fyAccNo = (FYGoldAcc) obj; 31 // goQuery(fyAccNo); 32 // } 33 34 } 35 }
配置文件:
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xmlns:tx="http://www.springframework.org/schema/tx" 5 xsi:schemaLocation="http://www.springframework.org/schema/beans 6 http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 7 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd"> 8 9 <bean id="queryJob" class="com.ninefbank.smallpay.admin.query.job.QueryJob"/> 10 11 <bean id="springContextHolder" class="com.ninefbank.smallpay.common.util.SpringContextHolder" lazy-init="false"></bean> 12 13 <bean id="beginQuery" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean"> 14 <property name="targetObject" ref="queryJob"/> 15 <property name="targetMethod" value="query"/> 16 </bean> 17 <bean id="beginQueryTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean"> 18 <property name="jobDetail" ref="beginQuery"/> 19 <property name="startDelay" value="5000"/> 20 <property name="cronExpression" value="0 0/5 * * * ?"/> 21 <!-- <property name="cronExpression" value="0 30 5,10,15,16,22 * * ?"/> --> 22 <!-- <property name="cronExpression" value="0 30 10,15,16,17,18,19,20,21,22,23 * * ?"/> --> 23 <!-- <property name="cronExpression" value="0 0/2 * * * ?"/> --> 24 </bean> 25 26 <!--Spring触发工厂--> 27 <bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean"> 28 <property name="triggers"> 29 <list> 30 <ref bean="beginQueryTrigger"/> 31 </list> 32 </property> 33 </bean> 34 </beans>
配置文件解析:
<bean id="beginQuery" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<property name="targetObject" ref="queryJob"/>
<property name="targetMethod" value="query"/>
</bean>
targetObject:指定包含任务执行体的Bean实例。(白话就是定时任务要执行的类)
targetMethod:指定将指定Bean实例的该方法包装成任务的执行体。(白话就是我要执行类里面的哪个方法)
<bean id="queryJob" class="com.ninefbank.smallpay.admin.query.job.QueryJob"/> 具体指定要执行的类是哪个
<bean id="beginQueryTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">
<property name="jobDetail" ref="beginQuery"/>
<property name="startDelay" value="5000"/>
<property name="cronExpression" value="0 0/5 * * * ?"/>
<!-- <property name="cronExpression" value="0 30 5,10,15,16,22 * * ?"/> -->
<!-- <property name="cronExpression" value="0 30 10,15,16,17,18,19,20,21,22,23 * * ?"/> -->
<!-- <property name="cronExpression" value="0 0/2 * * * ?"/> -->
</bean>
以上是触发器的定义,jobDetail是具体要执行哪个作业 startDelay是在服务器启动多长时间开始执行任务 cronExpression是定义什么时点执行定时任务
<bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<list>
<ref bean="beginQueryTrigger"/>
</list>
</property>
</bean>
以上是定义了调度器