spring+quartz定时任务配置---MethodInvokingJobDetailFactoryBean
?
實現定時任務有兩種方式:
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>
?
以上是定義了調度器
?
轉載于:https://www.cnblogs.com/sailormoon/p/7007857.html
總結
以上是生活随笔為你收集整理的spring+quartz定时任务配置---MethodInvokingJobDetailFactoryBean的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: gulp+browserSync自动刷新
- 下一篇: 阿里云3节点分布式RDS上存放100万数