Spring4 使用@Scheduled注解执行定时任务

Spring可以使用@Shcheduled注解定时执行指定的方法,配置大致如下:

1.修改引入依赖

<dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context-support</artifactId>
      <version>${spring.version}</version>
</dependency>

实际上这个jar还依赖好多其他的jar,还有好多,具体可以看spring-context-supportpom.xml

<dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-beans</artifactId>
      <version>4.0.2.RELEASE</version>
      <scope>compile</scope>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context</artifactId>
      <version>4.0.2.RELEASE</version>
      <scope>compile</scope>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-core</artifactId>
      <version>4.0.2.RELEASE</version>
      <scope>compile</scope>
    </dependency>

2.修改配置文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns="http://www.springframework.org/schema/beans"
       xmlns:task="http://www.springframework.org/schema/task"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
         http://www.springframework.org/schema/context
         http://www.springframework.org/schema/context/spring-context-4.0.xsd
         http://www.springframework.org/schema/task
       http://www.springframework.org/schema/task/spring-task-4.0.xsd"
       default-lazy-init="true">
  <!-- 扫描rocoinfo包下的所有类 -->
  <context:component-scan base-package="com.kong"/>

    <!-- 启用spring Task-->
  <!-- 配置执行任务线性池 -->
  <task:executor id="executor" pool-size="3"/>
  <!-- 配置任务调度线程池-->
  <task:scheduler id="scheduler" pool-size="3"/>
  <!-- 启用annotation方式 -->
  <task:annotation-driven scheduler="scheduler"
                          executor="executor" proxy-target-class="true"/>
  <!-- 指定定时执行的方法,如果这里指定了就不要使用@Scheduled在方法上指定了,否则会造成多次执行-->
 <!-- <task:scheduled-tasks scheduler="scheduler">
    <task:scheduled ref="contractService  " method="testScheduled"
                    cron="0 0 12 * * ?" /> -->
  </task:scheduled-tasks>
</beans>

其实我们还可以不在xml配置任何东西,而是在使用JavaConfig,在Java类上使用@EnableScheduling,往下看!

3.使用@Scheduled指定定时的方法

@Service
//如果嫌xml配置麻烦可以直接使用下面这个注解实现配置
//@EnableScheduling
@Lazy(value = false)
public class ContractService  {
    @Scheduled(cron = "0/5 * * * * ?")
    public void testScheduled() {
        System.out.println(getClass() + "执行了定时任务!");
    }
}

注意上面类上面的@Lazy,我在测试Demo的时候就是忘记写了它,而导致定时任务没有执行!原因是Spring中的Bean默认都是懒加载的,没有其他对象引用的情况是不会创建实例的。

其实到这里启动项目就可以执行定时任务了,运行结果如下:

运行结果.png

4.cron-like 表达式

字段 允许值 允许的特殊字符    
秒 0-59 , - * /    
分 0-59 , - * /    
小时 0-23 , - * /    
日期 1-31 , - * ? / L W C    
月份 1-12 或者 JAN-DEC , - * /    
星期 1-7 或者 SUN-SAT , - * ? / L C #    
年(可选) 留空, 1970-2099 , - * /    
表达式意义    
"0 0 12 * * ?" 每天中午12点触发    
"0 15 10 ? * *" 每天上午10:15触发    
"0 15 10 * * ?" 每天上午10:15触发    
"0 15 10 * * ? *" 每天上午10:15触发    
"0 15 10 * * ? 2005" 2005年的每天上午10:15触发    
"0 * 14 * * ?" 在每天下午2点到下午2:59期间的每1分钟触发    
"0 0/5 14 * * ?" 在每天下午2点到下午2:55期间的每5分钟触发    
"0 0/5 14,18 * * ?" 在每天下午2点到2:55期间和下午6点到6:55期间的每5分钟触发    
"0 0-5 14 * * ?" 在每天下午2点到下午2:05期间的每1分钟触发    
"0 10,44 14 ? 3 WED" 每年三月的星期三的下午2:10和2:44触发    
"0 15 10 ? * MON-FRI" 周一至周五的上午10:15触发    
"0 15 10 15 * ?" 每月15日上午10:15触发    
"0 15 10 L * ?" 每月最后一日的上午10:15触发    
"0 15 10 ? * 6L" 每月的最后一个星期五上午10:15触发    
"0 15 10 ? * 6L 2002-2005" 2002年至2005年的每月的最后一个星期五上午10:15触发    
"0 15 10 ? * 6#3" 每月的第三个星期五上午10:15触发    
每天早上6点    
0 6 * * *    
每两个小时    
0 */2 * * *    
晚上11点到早上8点之间每两个小时,早上八点    
0 23-7/2,8 * * *    
每个月的4号和每个礼拜的礼拜一到礼拜三的早上11点    
0 11 4 * 1-3    
1月1日早上4点    
0 4 1 1 *    
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 134,869评论 18 139
  • Spring Boot 参考指南 介绍 转载自:https://www.gitbook.com/book/qbgb...
    毛宇鹏阅读 46,941评论 6 342
  • 本文参考自Spring官方文档 34. Task Execution and Scheduling。 在程序中常常...
    乐百川阅读 5,573评论 0 11
  • 听,那阵阵悲嗥的风你能感受到他的痛吗爱情?亦或亲情?简而言之无谓这和你又有什么关系那么,你能听到你自己的悲嗥吗无谓...
    苏生寻阅读 199评论 0 1
  • hannamama阅读 239评论 0 0