spring-quartz普通任务与可传参任务

两者区别与作用:

普通任务:总调度(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>

  

 
posted @ 2016-11-17 19:28  dba_xyx  阅读(8413)  评论(0编辑  收藏  举报