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、写一个简单例子:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
import org.springframework.context.annotation.Lazy;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import java.text.SimpleDateFormat;
import java.util.Date;
/**
 * Created by leizhimin on 2014/8/16.
 *
 * @author leizhimin 2014/8/16 17:46
 */
@Component
@Lazy(false)
public class TestJob {
    public static SimpleDateFormat sdf_yyyyMMddHHmmss = new SimpleDateFormat("yyyyMMddHHmmss");
    @Scheduled(cron = "0/5 * * * * ?")
    public void exejob() {
        System.out.println(sdf_yyyyMMddHHmmss.format(new Date()) + " :执行中。。。");
    }
}

每隔5秒中就会输出一条信息。

------------------------------------

还可以在一个类上写多个任务:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
@Component
@Lazy(false)
public class TestJob {
    public static SimpleDateFormat sdf_yyyyMMddHHmmss = new SimpleDateFormat("yyyyMMddHHmmss");
    @Scheduled(cron = "0/5 * * * * ?")
    public void exejob1() {
        System.out.println(sdf_yyyyMMddHHmmss.format(new Date()) + " :AAAA 执行中。。。");
    }
    @Scheduled(cron = "0/5 * * * * ?")
    public void exejob2() {
        System.out.println(sdf_yyyyMMddHHmmss.format(new Date()) + " :BBBB 执行中。。。");
    }
    @Scheduled(cron = "0/5 * * * * ?")
    public void exejob3() {
        System.out.println(sdf_yyyyMMddHHmmss.format(new Date()) + " :CCCC 执行中。。。");
    }
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
20141010225850 :AAAA 执行中。。。
20141010225850 :BBBB 执行中。。。
20141010225850 :CCCC 执行中。。。
20141010225855 :AAAA 执行中。。。
20141010225855 :BBBB 执行中。。。
20141010225855 :CCCC 执行中。。。
20141010225900 :AAAA 执行中。。。
20141010225900 :BBBB 执行中。。。
20141010225900 :CCCC 执行中。。。
20141010225905 :AAAA 执行中。。。
20141010225905 :BBBB 执行中。。。
20141010225905 :CCCC 执行中。。。
20141010225910 :AAAA 执行中。。。
20141010225910 :BBBB 执行中。。。
20141010225910 :CCCC 执行中。。。
20141010225915 :AAAA 执行中。。。
20141010225915 :BBBB 执行中。。。
20141010225915 :CCCC 执行中。。。
20141010225920 :AAAA 执行中。。。
20141010225920 :BBBB 执行中。。。
20141010225920 :CCCC 执行中。。。
20141010225925 :AAAA 执行中。。。
20141010225925 :BBBB 执行中。。。
20141010225925 :CCCC 执行中。。。
20141010225930 :AAAA 执行中。。。
20141010225930 :BBBB 执行中。。。
20141010225930 :CCCC 执行中。。。
20141010225935 :AAAA 执行中。。。

本文转自 leizhimin 51CTO博客,原文链接:http://blog.51cto.com/lavasoft/1541841,如需转载请自行联系原作者

Spring @Scheduled相关推荐

  1. Java Spring @Scheduled 定时任务crontab表达式设置

    Java Spring @Scheduled 定时任务crontab表达式设置 1. Cron详解 2. 例子 参考 1. Cron详解 Cron表达式是一个字符串,字符串以5或6个空格隔开,分为6或 ...

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

    WEB项目中需要加入一个定时执行任务,可以使用Quartz来实现,由于项目就一个定时任务,所以想简单点,不用去配置那些Quartz的配置文件,所以就采用了Spring @Scheduled注解来实现了 ...

  3. Spring @Scheduled 使用详解

    一起养成写作习惯!这是我参与「掘金日新计划 · 4 月更文挑战」的第4天,点击查看活动详情. Spring 为任务调度和基于使用@Scheduled 注释的 cron 表达式的异步方法执行提供了极好的 ...

  4. Spring scheduled 执行 2 次原因

    Spring scheduled 执行 2 次原因 Spring scheduled 定时任务 执行一次定时任务,日志输出出现两次,而且两次执行行数号不一致. Spring scheduled 定时任 ...

  5. Spring @Scheduled 多线程配置

    Spring @Scheduled 多线程配置 环境 SpringBoot 2.0.2.RELEASE JDK8 两种方式实现 1.@Scheduled+@Async 测试代码: @SpringBoo ...

  6. Spring @Scheduled关键字

    备注:本示例中我们使用的是spring 4.2.5版本. 在spring的上下文文件中修改标签: 增加Xmlns标签:xmlns:task=http://www.springframework.org ...

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

    在springMVC里使用spring的定时任务非常的简单,如下: (一)在xml里加入task的命名空间 xmlns 多加下面的内容 1 xmlns:task="http://www.sp ...

  8. spring @Scheduled 注解实现的定时任务 3步走

    适用场景 1) Java自带的java.util.Timer类,这个类允许你调度一个java.util.TimerTask任务. 最早的时候就是这样写定时任务的.  2) 开源的第三方框架: Quar ...

  9. Spring @Scheduled定时任务调度配置的详解

    Spring定时器中配置文件中一些配置信息,供各位参考及指正 创建一个包含定时器配置的Spring配置文件:如spring-taskTime.xml.以下均为配置信息: <?xml versio ...

最新文章

  1. html弹出输入语言框,JavaScript如何弹出输入窗口?
  2. Oracle之唯一性约束(UNIQUE Constraint)使用方法具体解释
  3. RabbitMQ(四)交换机exchange
  4. 【NLP】21个经典深度学习句间关系模型|代码技巧
  5. 网络知识入门:路由器基础知识全接触
  6. Unity学习笔记:unity介绍(一)
  7. 微服务 数据库耦合_mysql – 与其他服务共享的微服务数据库
  8. 继微博之后,抖音、今日头条、小红书宣布将显示账号IP属地
  9. java卡 应用_一种实现多发行方的多应用Java卡的方法与流程
  10. 用C语言实现正则表达式匹配器
  11. 最经典的人生定律、法则、效应总结
  12. 电信光猫F420破解
  13. TCP/IP入土指南
  14. 机器学习实战案例—验证码(CAPTCHA)识别基于Logistic
  15. “云上贵州”成全国首个国密算法应用试点项目 阿里政务云实现“国家级”安全保护...
  16. webrtc中的带宽自适应算法
  17. Vue实例--音乐播放器:歌单数据接口分析
  18. tf.constant(常量)
  19. useImperativeHandle使用实例
  20. 如何查看计算机在广域网的地址,如何知道本机当前局域网IP地址和广域网IP地址...

热门文章

  1. ASP.Net后台 实现先弹出对话框,再跳转到另一个网页的实现方法
  2. 给Eclipse提速的7个技巧(转)
  3. Spring环境搭建,IoC容器初体验~
  4. CSDN Blog V3.0 升级公告
  5. PHP全栈学习笔记20
  6. 使用putty上传文件
  7. 你的sql查询为什么这么慢?
  8. NUC1003 Hangover
  9. c#发送http请求
  10. Windows Azure平台Win VM密码重置