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("每小时的0分0秒执行一次: "+ 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 或 SUN,MON,TUE,WED,THU,FRI,SAT)

其中每个元素可以是一个值(如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:10和2:44触发

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

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

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

时间: 2024-08-10 15:02:11

Spring @Scheduled关键字的相关文章

使用轻量级Spring @Scheduled注解执行定时任务

WEB项目中需要加入一个定时执行任务,可以使用Quartz来实现,由于项目就一个定时任务,所以想简单点,不用去配置那些Quartz的配置文件,所以就采用了Spring @Scheduled注解来实现了定时任务.在这里做个备注. spring配置文件  xmlns中加入一段: xmlns:task="http://www.springframework.org/schema/task" 然后xsi:schemaLocation多加下面的内容: http://www.springframe

分享Spring Scheduled定时器的用法

摘要:在coding中经常会用到定时器,指定每隔1个小时,或是每天凌晨2点执行一段代码段,若是使用java.util.Timer来做这种事情,未免重复造轮子.幸亏Spring中封装有定时器,而且非常好用,采用注解的形式配置某时某刻执行一段代码段.在之前的项目中使用过一次,下面就把代码.配置一并分享与大家. 关键词:Spring, JAVA, Scheduled, 定时器 一. 首先写一个Handler接口(“定时器Handler”),用以说明实现这一接口的类做的处理逻辑都是由定时器驱动的. 1

spring @Scheduled 并发执行

spring @Scheduled ,默认基于单线程执行,如果需要基于多线程执行,需要在配置文件中配置如下 <task:scheduler id="scheduler" pool-size="10" /> <task:executor id="executor" keep-alive="7200" pool-size="100-200" queue-capacity="500&q

使用spring @Scheduled注解运行定时任务、

曾经框架使用quartz框架运行定时调度问题. 老大说这配置太麻烦.每一个调度都须要多加在spring的配置中. 能不能降低配置的量从而提高开发效率. 近期看了看spring的 scheduled的使用注解的方式进行调度. 感觉非常方便.起码配置的东西少了非常多. 所以留下来以备忘了. 首先要配置我们的spring.xml xmlns 多加以下的内容. xmlns:task="http://www.springframework.org/schema/task" 然后xsi:schem

Spring @Scheduled

Spring @Scheduled是Spring计划任务的一种很简洁的实现.用来替代Quartz的方案. 要使用此特性,需要Spring3.2以上版本.用法: 1.在xml的配置中,需要加入: http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.2.xsd" 2.写一个简单例子: import org.springframework.context.

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

首先要配置我们的spring.xml xmlns 多加下面的内容 1 xmlns:task="http://www.springframework.org/schema/task" 然后xsi:schemaLocation多加下面的内容 1 http://www.springframework.org/schema/task 2 http://www.springframework.org/schema/task/spring-task-3.1.xsd 最后是我们的task任务扫描注解

spring @Scheduled注解执行定时任务

以前框架使用quartz框架执行定时调度问题. 这配置太麻烦.每个调度都需要多加在spring的配置中. 能不能减少配置的量从而提高开发效率. 最近看了看spring的 scheduled的使用注解的方式进行调度. 感觉很方便.起码配置的东西少了很多. 所以留下来以备忘了. 首先要配置我们的spring.xml xmlns 多加下面的内容. [html] view plain copy xmlns:task="http://www.springframework.org/schema/task&

spring @Scheduled用法

@Scheduled(cron = "0 5 * * * ?") org.springframework.scheduling.annotation.Scheduled /** * Annotation that marks a method to be scheduled. Exactly one of the * <code>cron</code>, <code>fixedDelay</code>, or <code>fi

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

以前框架使用quartz框架执行定时调度问题. 老大说这配置太麻烦.每个调度都需要多加在spring的配置中. 能不能减少配置的量从而提高开发效率. 最近看了看spring的 scheduled的使用注解的方式进行调度. 感觉很方便.起码配置的东西少了很多. 所以留下来以备忘了. 首先要配置我们的spring.xml xmlns 多加下面的内容. [html] view plaincopyprint? xmlns:task="http://www.springframework.org/sche