兩者區別與作用:
普通任務:總調度(SchedulerFactoryBean)--> 定時調度器(CronTriggerFactoryBean) --> 調度明細自定義執行方法bean(MethodInvokingJobDetailFactoryBean) -->調度bean(我們定義的job類)
可傳參任務:總調度(SchedulerFactoryBean)--> 定時調度器(CronTriggerFactoryBean) --> 調度明細bean(JobDetailFactoryBean)
如上是我們在配置調度器時的具體步驟及相互之間的依賴,區別主要在調度明細bean上,普通任務是(MethodInvokingJobDetailFactoryBean),而可傳參任務是(JobDetailFactoryBean):
1)普通任務可以自定義執行方法,也就是說在其配置的調度bean(我們定義的job類)中我們可以自定義調度器最終執行的方法,可以叫work1也可以叫work2,and so on。而且要記住的一點,必須是無入參的方法!!!(做了一個測試,測試目的是看通過此類型調度類型是否也能傳入參數所以定義了一個帶入參的自定義方法即public void work(JobExecutionContext jobExecutionContext),最中測試結果是spring quartz會報找不到work方法及No such method work(),其默認執行我們生命的work無入參的方法。
2)可傳參任務必須繼承QuartzJobBean,重寫protected void executeInternal(JobExecutionContext jobExecutionContext) throws JobExecutionException方法,其中JobExecutionContext就是我們在定義調度器明細時傳入參數的上下文,我們可以通過JobExecutionContext取出傳入的map,調度任務最終執行的就是executeInternal方法,使用該調度明細任務無法使用自定義方法。
public class NormalJob { /*不會調此方法,如果僅生命此方法會報No such Method work public void work(JobExecutionContext jobExecutionContext){ JobDataMap jobDataMap = jobExecutionContext.getJobDetail().getJobDataMap(); for (Map.Entry entry : jobDataMap.entrySet()){ System.out.println("key Normal ---: " + entry.getKey() + "value Normal---: " + entry.getValue()); } }*/ public void work(){ System.out.println("33333333333333333333333333333333333333"); } }
<!-- 封裝工作任務處理的工作類 --> <bean id="quartzJob" class="com.x.x.job.NormalJob" /> <bean id="jobtask" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean" > <property name="targetObject"> <ref bean="quartzJob" /> </property> <property name="targetMethod"> <value>work</value> </property> </bean> <bean id="excuteTime" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean"> <property name="jobDetail"> <ref bean="jobtask" /> </property> <!--<property name="jobDataMap"> <ref bean="hasReturnData"></ref> </property>--> <property name="cronExpression"> <value>${cron.every10minute}</value> </property> </bean> <!-- 總管理類(調度程序啟動模塊) 如果將lazy-init="false"那麼容器啟動就會執行調度程序 --> <bean id="startQuartz" lazy-init="false" autowire="no" class="org.springframework.scheduling.quartz.SchedulerFactoryBean"> <property name="triggers"> <list> <ref bean="excuteTime" /> <!--<ref bean="every10minute" />--> </list> </property> </bean>
public class CanTransferParm extends QuartzJobBean{ @Override protected void executeInternal(JobExecutionContext jobExecutionContext) throws JobExecutionException { JobDataMap jobDataMap = jobExecutionContext.getJobDetail().getJobDataMap(); for (Map.Entry entry : jobDataMap.entrySet()){ System.out.println("key---: " + entry.getKey() + "value---: " + entry.getValue()); } } }
<bean id="hasReturnParm" class="org.quartz.JobDataMap"> <constructor-arg> <map> <entry key="validateVendorQuotationInterface" value="http://x.x.x.x/validateVendorQuotationInterface" /> <entry key="validateComparsionInterface" value="http://x.x.x.x/validateComparsionInterface" /> </map> </constructor-arg> </bean> <bean id="hasReturnJobDetail" class="org.springframework.scheduling.quartz.JobDetailFactoryBean"> <property name="jobClass" value="com.x.x.CanTransferParm" /> <property name="jobDataMap"> <ref bean="hasReturnParm"></ref> </property> </bean> <bean id="every5minute" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean"> <!-- 工作類bean --> <property name="jobDetail"> <ref bean="hasReturnJobDetail" /> </property> <!-- cron表達式 --> <property name="cronExpression"> <value>${cron.every5minute}</value> </property> </bean> <bean id="startQuartz" lazy-init="false" autowire="no" class="org.springframework.scheduling.quartz.SchedulerFactoryBean"> <property name="triggers"> <list> <ref bean="every5minute" /> </list> </property> </bean>