码迷,mamicode.com
首页 > 编程语言 > 详细

Spring @Scheduled关键字

时间:2017-01-13 01:50:59      阅读:249      评论:0      收藏:0      [点我收藏+]

标签:spring @scheduled

备注:本示例中我们使用的是spring 4.2.5版本。

 

spring的上下文文件中修改标签:

增加Xmlns标签:xmlns:task=http://www.springframework.org/schema/task

修改xsi:schemaLocation标签,增加内容如下:

http://www.springframework.org/schema/task

http://www.springframework.org/schema/task/spring-task-4.2.xsd

 

增加任务扫描:

<task:annotation-driven/>

 

XML如下:

<?xml version="1.0"encoding="UTF-8"?>

<beansxmlns="http://www.springframework.org/schema/beans"

         xmlns:context="http://www.springframework.org/schema/context"

xmlns:xsi=http://www.w3.org/2001/XMLSchema-instancexmlns:jaxws="http://cxf.apache.org/jaxws"

xmlns:tx="ttp://www.springframework.org/schema/tx"

xmlns:mvc="http://www.springframework.org/schema/mvc"

xmlns:aop="ttp://www.springframework.org/schema/aop"

xmlns:util="http://www.springframework.org/schema/util"

         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.2.xsd

         http://www.springframework.org/schema/context

         http://www.springframework.org/schema/context/spring-context-4.2.xsd

         http://cxf.apache.org/jaxws

         http://cxf.apache.org/schemas/jaxws.xsd

         http://www.springframework.org/schema/tx

         http://www.springframework.org/schema/tx/spring-tx-4.2.xsd

         http://www.springframework.org/schema/mvc

         http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd

         http://www.springframework.org/schema/util

         http://www.springframework.org/schema/util/spring-util-4.2.xsd

         http://www.springframework.org/schema/aop

         http://www.springframework.org/schema/aop/spring-aop-4.2.xsd

         http://www.springframework.org/schema/task

         http://www.springframework.org/schema/task/spring-task-4.2.xsd">

 

         <context:component-scanbase-package="com" />

         <task:annotation-driven />

         <context:annotation-config/>

</beans>

 

实现类使用注解@Component作为注释

 

Java实例:

@Component

public class Cron {

         @Scheduled(cron= " 0 0 * * * ?")

         publicvoid runTaskPerHour() {

                   System.out.println("每小时的00秒执行一次: "+ new Date());

         }

 

         @Scheduled(fixedRate= 5000)

         publicvoid runTaskPerFiveSecond() {

                   System.out.println("立即执行,每5秒执行一次: "+ new Date());

         }

 

         @Scheduled(fixedRate= 5000, initialDelay = 10000)

         publicvoid runTaskPerFiveSecondDelay() {

                   System.out.println("延迟10秒执行,每5秒执行一次: "+ new Date());

         }

}

以下参考ITEYE

Cron介绍:

一个cron表达式有至少6个有空格分隔的时间元素。

按顺序依次为

1.秒(0~59

2.分钟(0~59

3.小时(0~23

4.天(月)(0~31,但是你需要考虑你月的天数)

5.月(0~11

6.天(星期)(1~7 1=SUN SUNMONTUEWEDTHUFRISAT

 

其中每个元素可以是一个值(6),一个连续区间(9-12),一个间隔时间(8-18/4)(/表示每隔4小时),一个列表(1,3,5),通配符。由于"月份中的日期""星期中的日期"这两个元素互斥的,必须要对其中一个设置?.

 

"0 0 10,14,16 * * ?" 每天上午10点,下午2点,4

"0 0/30 9-17 * * ?"   朝九晚五工作时间内每半小时

"0 0 12 ? * WED" 表示每个星期三中午12

"0 0 12 * * ?" 每天中午12点触发

"0 15 10 ? * *" 每天上午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:102:44触发

"0 15 10 ? * MON-FRI" 周一至周五的上午10:15触发

"0 15 10 15 * ?" 每月15日上午10:15触发

"0 15 10 L * ?" 每月最后一日的上午10:15触发


Spring @Scheduled关键字

标签:spring @scheduled

原文地址:http://xueyuandamao.blog.51cto.com/10165848/1891452

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!