【Spring学习】spring提供的三种定时任务

在spring中我们有三种不同的定时任务:基于Quartz的定时机制、基于Timer的定时机制、基于Executor的定时机制。

1、基于Quartz的定时任务机制

这里写图片描述

下面详细解释这个类图中涉及的关键类及其使用场景

1.1. SchedulerFactoryBean

这是Spring中基于Quartz的定时机制入口,只要Spring容器装载了这个类,Quartz定时机制就会启动,并加载定义在这个类中的所有trigger

1.2. CronTriggerBean

实现了Trigger接口,基于Cron表达式的触发器。这种触发器的好处是表达式与linux下的crontab一致,能够满足非常复杂的定时需求,也容易配置

1.3. MethodInvokingJobDetailFactoryBean

Spring提供的一个不错的JobDetail包装工具,能够包装任何bean,并执行类中指定的任何stati或非static的方法,避免强制要求bean去实现某接口或继承某基础类。

Spring配置实例:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" 
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
       xmlns:context="http://www.springframework.org/schema/context" 
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
                http://www.springframework.org/schema/context 
                http://www.springframework.org/schema/context/spring-context-3.0.xsd">

    <bean id="jobDetail_1" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
       <property name="targetObject">
            <ref bean="TSupeviseServiceImpl" />
       </property>
       <property name="targetMethod">
            <value>processTInterface</value><!-- 处理数据到业务督办表T_Interface中 -->
       </property>
    </bean>

    <bean id="cronTrigger_1" class="org.springframework.scheduling.quartz.CronTriggerBean">
       <property name="jobDetail">
            <ref bean="jobDetail_1" />
       </property>
       <property name="cronExpression">
            <value>0 0 10 * * ? *</value><!-- 每天上午10点执行 -->
       </property>
    </bean>

    <bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
        <property name="triggers">
            
  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值