Spring定时任务

(1) 用spring配置文件实现定时器,没有用注解

(2) 两个独立的定时器,主要为了分离执行时间较长(但不重要)的一类任务和执行较短(重要)的一类任务,提高系统实时性。如果把执行时间较长的任务和执行时间教短的任务混在一起,那么执行时间较长的任务会阻碍其它定时任务的实时性(只能排队等待)


step1 配置两个独立的调度器

<!-- 配置调度器 -->
<task:scheduler id="heavy-scheduler"/>
<task:scheduler id="light-scheduler"/>

step2 分别给两个定时器配置定时任务(方法)

<!--  重任务调度器 -->
<task:scheduled-tasks scheduler="heavy-scheduler">
    <task:scheduled ref="classA" method="f_A1" cron="0 0 0,18 * * ?"/>
    <task:scheduled ref="classB" method="f_B1" cron="0 0 2,20 * * ?"/>
</task:scheduled-tasks>


<!--  轻任务调度器 -->
<task:scheduled-tasks>
    <task:scheduled ref="classA" method="f_A2" fixed-delay="30000"/>
    <task:scheduled ref="classB" method="f_B2" fixed-delay="30000"/>
</task:scheduled-tasks>


另外classA 和 classB 要么采用扫描,要么采用<bean> 读到spring中


需要指出,一个定时器如果没有显示的指定线程,默认只有一个线程,即有可能定时方法出现定时时间到了,却无法执行的情况,但是时序是正确的。(定时排队中)

如下:这样线程池中就3个线程,但是如果3个线程都用于执行定时方法后,新的定时方法来了,同样会排队等待执行

<task:scheduler id="heavy-scheduler" pool-size="3"/>
<task:scheduler id="light-scheduler" pool-size="3"/>


注意如果采用了以上配置文件,就不用注解:

@EnableScheduling 和 @Scheduled


to enable both @Scheduled and @Async annotations, simply include the 'annotation-driven' element from the task namespace in your configuration.

使能@Scheduled and @Async注解,需要配置以下
<task:annotation-driven executor="myExecutor" scheduler="myScheduler"/>
<task:executor id="myExecutor" pool-size="5"/>
<task:scheduler id="myScheduler" pool-size="10"/>}

Notice that an executor reference is provided for handling those tasks that correspond to methods with the @Async annotation, and the scheduler reference is provided for managing those methods annotated with @Scheduled.

@Async异步执行是使用myExecutor"实例,@Scheduled定时器是使用myScheduler,他们是两码事!


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值