适用的工具是:Schedule

集成步骤:

1、开启Schedule支持

package com.jsoft.springboottest.springboottest1;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling; /**
* Hello world!
*
*/
@SpringBootApplication
@EnableScheduling
public class App
{
public static void main( String[] args )
{
SpringApplication.run(App.class, args);
}
}

2、使用

package com.jsoft.springboottest.springboottest1.controller;

import java.util.Date;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; @RestController
public class TestController { private static final Logger logger = LoggerFactory.getLogger(TestController.class); @Scheduled(fixedRate = 5000)
public void reportCurrentTime() {
logger.info("每隔五秒钟执行一次: " + new Date().toString());
} @Scheduled(cron = "15 52 21 ? * *")
public void fixTimeExecution() {
logger.info("在指定时间执行" + new Date().toString());
} @RequestMapping("/show")
public String show(){
return "Hello World";
}
}

说明:支持Linux的cron表达式。

cron表达式

参数:
* 第一位,表示秒,取值0-59
* 第二位,表示分,取值0-59
* 第三位,表示小时,取值0-23
* 第四位,日期天/日,取值1-31
* 第五位,日期月份,取值1-12
* 第六位,星期,取值1-7,星期一,星期二...,注:不是第1周,第二周的意思,另外:1表示星期天,2表示星期一。
* 第7为,年份,可以留空,取值1970-2099
(*)星号:可以理解为每的意思,每秒,每分,每天,每月,每年...
(?)问号:问号只能出现在日期和星期这两个位置,表示这个位置的值不确定,每天3点执行,所以第六位星期的位置,我们是不需要关注的,就是不确定的值。同时:日期和星期是两个相互排斥的元素,通过问号来表明不指定值。比如,1月10日,比如是星期1,如果在星期的位置是另指定星期二,就前后冲突矛盾了。
(-)减号:表达一个范围,如在小时字段中使用“10-12”,则表示从10到12点,即10,11,12
(,)逗号:表达一个列表值,如在星期字段中使用“1,2,4”,则表示星期一,星期二,星期四
(/)斜杠:如:x/y,x是开始值,y是步长,比如在第一位(秒) 0/15就是,从0秒开始,每15秒,最后就是0,15,30,45,60 另:*/y,等同于0/y
示例:
0 0 3 * * ? 每天3点执行
0 5 3 * * ? 每天3点5分执行
0 5 3 ? * * 每天3点5分执行,与上面作用相同
0 5/10 3 * * ? 每天3点的 5分,15分,25分,35分,45分,55分这几个时间点执行
0 10 3 ? * 1 每周星期天,3点10分 执行,注:1表示星期天
0 10 3 ? * 1#3 每个月的第三个星期,星期天 执行,#号只能出现在星期的位置

示例工程:https://github.com/easonjim/5_java_example/tree/master/springboottest/springboottest8

参考:

http://www.importnew.com/27287.html(以上内容转自此篇文章)

http://blog.csdn.net/strommaybin/article/details/54767485

Spring Boot使用Schedule实现定时任务的更多相关文章

  1. spring boot注解之@Scheduled定时任务实现

    java实现定时任务一般使用timer,或者使用quartz组件.现在在spring boot提供了更加方便的实现方式. spring boot已经集成了定时任务.使用@Secheduled注解. @ ...

  2. Spring boot 集成三种定时任务方式

    三种定时任务方式分别为 org.springframework.scheduling.annotation.Scheduled java.util.concurrent.ScheduledExecut ...

  3. Spring Boot整合Quartz实现定时任务表配置

    最近有个小项目要做,spring mvc下的task设置一直不太灵活,因此在Spring Boot上想做到灵活的管理定时任务.需求就是,当项目启动的时候,如果有定时任务则加载进来,生成schedule ...

  4. Spring Boot集成quartz实现定时任务并支持切换任务数据源

    org.quartz实现定时任务并自定义切换任务数据源 在工作中经常会需要使用到定时任务处理各种周期性的任务,org.quartz是处理此类定时任务的一个优秀框架.随着项目一点点推进,此时我们并不满足 ...

  5. Spring Boot TImer Schedule Quartz

    Spring Boot 2.X(十二):定时任务-云栖社区-阿里云https://yq.aliyun.com/articles/723876?spm=a2c4e.11155472.0.0.2f8b3a ...

  6. 在Spring Boot中动态实现定时任务配置

    原文路径:https://zhuanlan.zhihu.com/p/79644891 在日常的项目开发中,往往会涉及到一些需要做到定时执行的代码,例如自动将超过24小时的未付款的单改为取消状态,自动将 ...

  7. spring boot 使用Schedule创建轻量级定时任务

    Scheduled SpringBoot配置定时任务可以直接使用自带的Scheduled,这相当于一个轻量级的Quartz,它可以让我们直接使用注解来完成定时任务的配置. Scheduled调度时间设 ...

  8. Spring MVC使用Schedule实现定时任务

    Schedule存在spring-context.jar包中. 实现简单步骤: 1.配置bean.xml开启定时任务支持. <?xml version="1.0" encod ...

  9. Spring Boot 知识笔记(定时任务与异步)

    一.定时任务 1.启动类里面增加注入 @SpringBootApplication //@SpringBootApplication = @Configuration+@EnableAutoConfi ...

随机推荐

  1. Vuex基本概念

    Vuex基本概念 State Getter Mutation Action Module 简单的Store import Vue from 'vue'; import Vuex from 'vuex' ...

  2. vue 封装组件上传img

    var _uploadTemplate = '<div>'+ '<input type="file" name="file" v-on:cha ...

  3. input标签内容改变触发的事件

    原生方法 onchange事件 <input type="text" onchange="onc(this)"> function onc(data ...

  4. String中indexof函数的用法

    int indexOf(int ch) 返回指定字符在此字符串中第一次出现处的索引. int indexOf(int ch, int fromIndex) 从指定的索引开始搜索,返回在此字符串中第一次 ...

  5. RESTful API批量操作的实现

    要解决的问题 RESTful API对于批量操作存在一定的缺陷.例如资源的删除接口: DELETE /api/resourse/<id>/ 如果我们要删除100条数据怎么搞?难道要调用10 ...

  6. jq相关操作

    1事件: <div class="ele">123</div> box.onclick = function(ev){ ev:系统传入的事件对象 ele.i ...

  7. PyCharm学习笔记(一) 界面配置

     通过Ctrl+鼠标滚轮调整字体大小  设置代码区默认字体及大小 设置调试区的字体大小 设置代码风格:如Tab缩进 定义Python模板文件 # @Time : ${DATE} ${TIME} # @ ...

  8. [转]automaticallyAdjustsScrollViewInsets(个人认为iOS7中略坑爹的属性)

    @当我们在一个UIViewController中同时创建2个tableView的时候,如果把它们的frame中的Y坐标设置为一样,你可能会发现它们的位置并没有达到你想要的结果.比如第一tableVie ...

  9. (转)Objective-C语法之动态类型(isKindOfClass, isMemberOfClass,id)等

    本文转自http://blog.csdn.net/totogo2010/article/details/7714960 对象在运行时获取其类型的能力称为内省.内省可以有多种方法实现. 判断对象类型 - ...

  10. 牛客网暑期ACM多校训练营(第六场) J Heritage of skywalkert(数论, eth_element)

    链接: https://www.nowcoder.com/acm/contest/144/J 题意: 给定一个函数, 求它n次结果中任意两次的lcm最大值 分析: 首先要看出这个函数并没有什么含义, ...