1、新建一个计划任务类(只能和主类平级或在主类的下级)

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 logger = LoggerFactory.getLogger(ScheduledTasks.class);
private static final SimpleDateFormat dataFromat = new SimpleDateFormat("HH:mm:ss"); @Scheduled(fixedRate = 1000)
public void reportCurrent(){
logger.info("现在时间:{}",dataFromat.format(new Date()));
} }

 

本示例中使用的是 fixedRate函数,它指定的是从调用开始时间到指定时间之后,单位毫秒。还有 fixedDelay指定从

  完成任务测量的时间间隔。还可以指定具体时间,使用 Scheduled(cron="... ")

  cron参数说明: 0 0 10,14,16 * * ? 每天上午10点,下午2点,4点

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

  0 0 12 ? * WED 表示每个星期三中午12点 
  "0 0 12 * * ?" 每天中午12点触发

  其中 按顺序依次为:

    秒(0~59)

    分钟(0~59)

    小时(0~23)

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

    月(0~11)

    天(星期)(1~7 1=SUN 或 SUN,MON,TUE,WED,THU,FRI,SAT)

    7.年份(1970-2099)

 

2、启用定时任务

 当上面一切被设置好之后,还需要在主类中加入 @EnableScheduling 注解来启动任务,否则定时任务不会被执行

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling; @EnableScheduling //必须加此注解
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}

 3、启动,当一切编译完毕时,可以启动来测试了,在类中右键-->RunAs-->SpringBoot App,一切正常就会看到如下结果

springBoot 定时器任务的更多相关文章

  1. 补习系列(9)-springboot 定时器,你用对了吗

    目录 简介 一.应用启动任务 二.JDK 自带调度线程池 三.@Scheduled 定制 @Scheduled 线程池 四.@Async 定制 @Async 线程池 小结 简介 大多数的应用程序都离不 ...

  2. springBoot 定时器

    程序入口类中加入注解 @EnableScheduling 配置定时任务为并行 @Slf4j @Configuration public class ScheduledConfig implements ...

  3. springboot 定时器 Schdule

    定时器:定时启动任务,执行代码 1.在启动类中加入注解: 2.创建一个类,并且在这个类上加入注解:@Component 3.定义一个方法,在方法上加入注解:@Scheduled(cron=" ...

  4. SpringBoot定时器任务

    Spring Boot使用@Scheduled定时器任务   摘要: Spring Boot之使用@Scheduled定时器任务 假设我们已经搭建好了一个基于Spring Boot项目,首先我们要在A ...

  5. SpringBoot定时器

    使用Component注解注解一个类,这个类就变成了一个组件.组件可以有很多不同的特性,比如Scheduled注解为组件的某个函数添加了定时的特性. @Component public class M ...

  6. 关于springboot的定时器配置

    关于springboot的定时器: 比较重要的两个注解: @EnableScheduling:标注启动定时任务. @Scheduled(fixedRate = 1000 * 30)  定义某个定时任务 ...

  7. SpringBoot系列——EnableScheduling,对定时器的支持

    前言 定时器功能在项目里面往往会用到,比如定时发送邮件.定时释放数据库资源:这里记录一下springboot对定时器的支持的简单实例 cron表达式 开始之前要先介绍一下cron表达式,这里当一下百度 ...

  8. Springboot+websocket+定时器实现消息推送

    由于最近有个需求,产品即将到期(不同时间段到期)时给后台用户按角色推送,功能完成之后在此做个小结 1. 在启动类中添加注解@EnableScheduling package com.hsfw.back ...

  9. 关于给springboot添加定时器的两种方式

    原文:https://blog.csdn.net/liboyang71/article/details/72781526 首先,搭建好一个springboot项目,可使用maven或者gradle或者 ...

随机推荐

  1. 文件格式——fastq格式

    fastQ格式 FASTQ是一种存储了生物序列(通常是核酸序列)以及相应的质量评价的文本格式. 他们都是以ASCII编码的.现在几乎是高通量测序的标准格式.NCBI Short Read Archiv ...

  2. IDEA如何启动debug?

    选择remote,然后,修改host和port: host是我们访问backstop的ip,端口是监听端口8787,点击ok即可. 打断点,调试,即可.

  3. Python短小精悍的Orator基本使用技巧

    基本使用 配置 设置数据库配置参数,创建一个DatabaseManager实例. from orator import DatabaseManager config = { 'mysql': { 'd ...

  4. 用Python解析XMind

    本文来自网易云社区 作者:孙圣翔 转自:http://shengxiang.me/article/35/python-parse-xmind.html XMind是画思维脑图很好的工具,正好组里有个需 ...

  5. Note: ENDBOX: Scalable Middlebox Functions Using Client-Side Trusted Execution

    ENDBOX enable secure networking by client-Side trusted execution. What ENDBOX is a scalable middlebo ...

  6. HDP3.1 中配置 YARN 的 timeline server 使用外部的 HBase

    HDP3.1 中的 YARN 的 timeline server 默认使用内置的 HBase,不知道为什么,总是过几天就挂掉,暂时还没找到原因.后来想着让它使用外部的 HBase 看看会不会还有此问题 ...

  7. 洛谷P2474 [SCOI2008]天平

    P2474 [SCOI2008]天平 题目背景 2008四川NOI省选 题目描述 你有n个砝码,均为1克,2克或者3克.你并不清楚每个砝码的重量,但你知道其中一些砝码重量的大小关系.你把其中两个砝码A ...

  8. 洛谷P1282 多米诺骨牌

    P1282 多米诺骨牌 题目描述 多米诺骨牌有上下2个方块组成,每个方块中有1~6个点.现有排成行的 上方块中点数之和记为S1,下方块中点数之和记为S2,它们的差为|S1-S2|.例如在图8-1中,S ...

  9. 整合spring和hibernate框架

    一)整合spring和hibernate框架整合要点:(1)数据源配置在Spring的配置文件中,供Spring和Hibernate框架共同使用:(2)不再需要hibernate.hbm.xml配置文 ...

  10. Meter - 连续性能测试 - JMeter + ANT + Jenkins集成 - 第1部分

    目标: 创建包含性能测试流程的持续交付管道, 以尽早检测任何与性能相关的问题. 通常,全面的性能测试将在分段/预生产环境中完成,该环境可能与您的生产环境相同.在完成QA功能/回归验证后,将代码推送到分 ...