spring注解@Scheduled中fixedDelay、fixedRate和cron表达式的区别

来源:互联网 发布:重庆邮电大学就业知乎 编辑:程序博客网 时间:2024/06/08 10:43
<?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"xmlns:task="http://www.springframework.org/schema/task"xsi:schemaLocation="http://www.springframework.org/schema/beans                             http://www.springframework.org/schema/beans/spring-beans-3.0.xsd                             http://www.springframework.org/schema/task                             http://www.springframework.org/schema/task/spring-task-3.0.xsd                             http://www.springframework.org/schema/context                             http://www.springframework.org/schema/context/spring-context-3.0.xsd"><context:component-scan base-package="com.test.*"/><!--需要扫描的包--><task:annotation-driven/> <!-- 这句是重点 定时器开关--></beans>

添加task定时任务的xml文件,配置在spring文件中

@Scheduled注解可以控制方法定时执行,其中有三个参数可选择:

1、fixedDelay控制方法执行的间隔时间,是以上一次方法执行完开始算起,如上一次方法执行阻塞住了,那么直到上一次执行完,并间隔给定的时间后,执行下一次。

2、fixedRate是按照一定的速率执行,是从上一次方法执行开始的时间算起,如果上一次方法阻塞住了,下一次也是不会执行,但是在阻塞这段时间内累计应该执行的次数,当不再阻塞时,一下子把这些全部执行掉,而后再按照固定速率继续执行。

3、cron表达式可以定制化执行任务,但是执行的方式是与fixedDelay相近的,也是会按照上一次方法结束时间开始算起。


具体使用方法参看使用Spring的@Scheduled实现定时任务

阅读全文
0 0
原创粉丝点击