Spring中使用Quartz的两种方式

作业类是否继承QuartzJobBean

  • 继承
  • 不继承

不继承QuartzJobBean,声明JobDetail时,使用MethodInvokingJobDetailFactoryBean

继承QuartzJobBean,声明JobDetail时,使用JobDetailFactoryBean

任务调度的触发时机

  • 每隔指定时间触发一次:触发器使用SimpleTriggerBean
  • 每到指定时间触发一次:触发器使用CronTriggerBean

这两种触发方式都可以和两种作业继承方式相互组合来使用。

也就是有四种写法。

举个例子:

不继承QuartzJobBean,由SimpleTriggerBean触发

<bean id="simpleJobDetail" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
        <property name="targetObject" ref="myBean"/>
        <property name="targetMethod" value="printMessage"/>
    </bean>


<!-- Run the job every 2 seconds with initial delay of 1 second -->
    <bean id="simpleTrigger" class="org.springframework.scheduling.quartz.SimpleTriggerFactoryBean">
        <property name="jobDetail" ref="simpleJobDetail"/>
        <property name="startDelay" value="1000"/>
        <property name="repeatInterval" value="2000"/>
    </bean>

public class MyBean {

    public void printMessage(){
        System.out.println("不继承QuartzJobBean的作业类。called by MethodInvokingJobDetailFactoryBean using SimpleTriggerFactoryBean");
    }
}

不继承QuartzJobBean,由CronTriggerBean触发

继承QuartzJobBean,由SimpleTirggerBean触发

继承QuartzJobBean,由CronTriggerBean触发





public class ScheduledJob extends QuartzJobBean {

    private AnotherBean anotherBean;

    protected void executeInternal(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        anotherBean.printMessage();
    }

    public void setAnotherBean(AnotherBean anotherBean) {
        this.anotherBean = anotherBean;
    }
}

public class AnotherBean {
    public void printMessage(){
        System.out.println("继承QuartzJobBean的作业类,通过AnotherBean传递数据。called by JobDetailFactoryBean using CronTriggerFactoryBean");
    }
}
<!-- For times when you need more complex processing, passing data to the scheduled job -->
    <bean name="complexJobDetail" class="org.springframework.scheduling.quartz.JobDetailFactoryBean">
        <property name="jobClass" value="com.penelope.quickstart.jobs.ScheduledJob"/>
        <property name="jobDataAsMap">
            <map>
                <entry key="anotherBean" value-ref="anotherBean"/>
            </map>
        </property>
        <property name="durability" value="true"/>
    </bean>


<!-- Run the job every 5 seconds only on Weekends -->
    <bean id="cronTrigger" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
        <property name="jobDetail" ref="complexJobDetail"/>
        <property name="cronExpression" value="0/5 * * ? * SAT-SUN"/>
    </bean>

第二个例子是复杂的,在任务类中调用另一个Bean的方法。另一个Bean就是该作业类的JobData。使用jobDataAsMap属性,在JobDetail中声明。

小菜虫使用jobDataMap属性传递AnotherBean失败,原因不明。。。日后填坑。

转载于:https://my.oschina.net/u/3672057/blog/1545432

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值