package com.zooper.demo;

import java.text.SimpleDateFormat;
import java.util.Date; import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component; @Component
public class ScheduledTasks {
private static final Logger log = LoggerFactory.getLogger(ScheduledTasks.class); private static final SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss"); /**
* 每隔5秒执行一次
*/
@Scheduled(fixedRate = 5000)
public void reportCurrentTime() {
log.info("The time is now {}", dateFormat.format(new Date()));
} /**
* 根据cron表达式格式触发定时任务
* cron表达式格式:
* 1.Seconds Minutes Hours DayofMonth Month DayofWeek Year
* 2.Seconds Minutes Hours DayofMonth Month DayofWeek
* 顺序:
* 秒(0~59)
* 分钟(0~59)
* 小时(0~23)
* 天(月)(0~31,但是你需要考虑你月的天数)
* 月(0~11)
* 天(星期)(1~7 1=SUN 或 SUN,MON,TUE,WED,THU,FRI,SAT)
* 年份(1970-2099)
*
* 注:其中每个元素可以是一个值(如6),一个连续区间(9-12),一个间隔时间(8-18/4)(/表示每隔4小时),一个列表(1,3,5),通配符。
* 由于"月份中的日期"和"星期中的日期"这两个元素互斥的,必须要对其中一个设置?.
*/
@Scheduled(cron="5 * * * * ?")
public void cronScheduled() {
log.info("cron : The time is now {}", dateFormat.format(new Date()));
}
}

启用定时任务

package com.zooper.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling; @SpringBootApplication
@EnableScheduling
public class Application { public static void main(String[] args) throws Exception {
SpringApplication.run(Application.class);
}
}

Spring Boot 配置定时任务的更多相关文章

  1. Spring Boot配置定时任务

    在项目开发过程中,经常需要定时任务来做一些内容,比如定时进行数据统计(阅读量统计),数据更新(生成每天的歌单推荐)等. Spring Boot默认已经实现了,我们只需要添加相应的注解就可以完成定时任务 ...

  2. spring boot配置定时任务设置

    一.定时任务的时间写法: 每天凌晨2点  0 0 2 * * ?和每天隔一小时 0 * */1 * * ? 每隔5秒执行一次:*/5 * * * * ? 每隔5分执行一次:0 */5 * * * ? ...

  3. spring boot 创建定时任务

    @Scheduled默认创建的线程是单线程,任务的执行会受到上一个任务的影响,创建定时任务也比较简单 123456789101112 @Component@Configuration //1.主要用于 ...

  4. Spring Boot -- 配置切换指南

    一般在一个项目中,总是会有好多个环境.比如: 开发环境 -> 测试环境 -> 预发布环境 -> 生产环境 每个环境上的配置文件总是不一样的,甚至开发环境中每个开发者的环境可能也会有一 ...

  5. Spring Boot 配置优先级顺序

    一般在一个项目中,总是会有好多个环境.比如: 开发环境 -> 测试环境 -> 预发布环境 -> 生产环境 每个环境上的配置文件总是不一样的,甚至开发环境中每个开发者的环境可能也会有一 ...

  6. spring boot 配置注入

    spring boot配置注入有变量方式和类方式(参见:<spring boot 自定义配置属性的各种方式>),变量中又要注意静态变量的注入(参见:spring boot 给静态变量注入值 ...

  7. Spring boot配置多个Redis数据源操作实例

    原文:https://www.jianshu.com/p/c79b65b253fa Spring boot配置多个Redis数据源操作实例 在SpringBoot是项目中整合了两个Redis的操作实例 ...

  8. spring boot配置springMVC拦截器

    spring boot通过配置springMVC拦截器 配置拦截器比较简单, spring boot配置拦截器, 重写preHandle方法. 1.配置拦截器: 2重写方法 这样就实现了拦截器. 其中 ...

  9. spring boot配置mybatis和事务管理

    spring boot配置mybatis和事务管理 一.spring boot与mybatis的配置 1.首先,spring boot 配置mybatis需要的全部依赖如下: <!-- Spri ...

随机推荐

  1. “Hello world!”团队—选题展示

    本次选题展示内容: 一.视频展示 链接:http://v.youku.com/v_show/id_XMzA5Mzk5NjYwOA==.html?sharefrom=iphone 视频截图链接:http ...

  2. 互评Alpha版本——Thunder团队

    基于NABCD评论作品 Hello World! :http://www.cnblogs.com/120626fj/p/7807544.html 欢迎来怼 :http://www.cnblogs.co ...

  3. 算法与数据结构3.3 calculator

    ★实验任务 小 V 发明了一个神奇的整数计算器: 给定一个合法的表达式,这个计算器能求出这个表达式的最终答案. 表达式可能包含: +:运算符,整数加法.如 1+1=2 -:运算符,整数减法.如 1-1 ...

  4. ZOJ 1229 M-Gift?!

    https://vjudge.net/contest/67836#problem/M There is a beautiful river in a small village. N rocks ar ...

  5. Android 布局方式学习

    一.LinearLayout线性布局: 线性布局是程序中最常见的一种布局方式,线性布局可以分为水平线性布局和垂直线性布局两种, 通过android:orientation属性可以设置线性布局的方向 1 ...

  6. /var/redis/run/redis_6379.pid exists, process is already running or crashed的解决办法

    命令:service redis start /var/redis/run/redis_6379.pid exists, process is already running or crashed 引 ...

  7. ifstat查看网络流量的原理

    ifstat查看网卡流量的原理:读的是哪个/proc/ 接口啊 同diskIO一样,网络的IO也同样有统计计数的,是/proc/net/dev一个典型的输出就是这个样子的: root@station6 ...

  8. 内存交换空间(swap)的构建

    一.使用物理分区构建swap 1.先进行分区的行为. [root@iZ255cppmtxZ ~]# fdisk /dev/xvdb Welcome to fdisk (util-linux ). Ch ...

  9. change object keys & UpperCase & LowerCase

    change object keys & UpperCase & LowerCase .toLocaleUpperCase(); && .toLocaleLowerCa ...

  10. MVC绕过登陆界面验证时HttpContext.Current.User.Identity.Name取值为空问题解决方法

    Global.asax界面添加如下方法: void FormsAuthentication_Authenticate(object sender, FormsAuthenticationEventAr ...