首先是pom.xml依赖:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.springboot.samples</groupId>
<artifactId>SpringBoot_Cron</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>SpringBoot_Cron Maven Webapp</name>
<url>http://maven.apache.org</url> <parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.2.5.RELEASE</version>
</parent> <dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency> <!-- http://mvnrepository.com/artifact/javax.servlet/javax.servlet-api -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
</dependency> <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
</dependencies>
<build>
<finalName>SpringBoot_Cron</finalName>
</build>
</project>

然后是定时任务类ScheduledTask:

package hello;

import java.text.SimpleDateFormat;
import java.util.Date; import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
@Component
public class ScheduledTask { private static final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
private Integer count0 = 1;
private Integer count1 = 1;
private Integer count2 = 1; @Scheduled(fixedRate = 5000) //fixedRate = 5000表示每隔5000ms,Spring scheduling会调用一次该方法,不论该方法的执行时间是多少
public void reportCurrentTime() throws InterruptedException {
System.out.println(String.format("---第%s次执行,当前时间为:%s", count0++, dateFormat.format(new Date())));
} @Scheduled(fixedDelay = 10000) //fixedDelay = 5000表示当方法执行完毕5000ms后,Spring scheduling会再次调用该方法
public void reportCurrentTimeAfterSleep() throws InterruptedException {
System.out.println(String.format("===第%s次执行,当前时间为:%s", count1++, dateFormat.format(new Date())));
} @Scheduled(cron = "*/15 * * * * *") //cron = "*/5 * * * * * *"提供了一种通用的定时任务表达式,这里表示每隔5秒执行一次
public void reportCurrentTimeCron() throws InterruptedException {
System.out.println(String.format("+++第%s次执行,当前时间为:%s", count2++, dateFormat.format(new Date())));
} }

cron表达式的规则:

*/15 * * * * *  从左至右依次为:分钟(0-59)、小时(1-23)、日期(1-31)、月份(1-12)、星期(0-6,0表示周日)
*/15表示分钟间隔15执行一次即15s执行一次,cron详细介绍猛戳:http://www.manpagez.com/man/5/crontab/

最后是Spring Boot启动类:

package hello;

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

spring-boot之简单定时任务的更多相关文章

  1. 【spring boot】spring boot中使用定时任务配置

    spring boot中使用定时任务配置 =============================================================================== ...

  2. Spring Boot(九):定时任务

    Spring Boot(九):定时任务 一.pom包配置 pom包里面只需要引入springboot starter包即可 <dependencies> <dependency> ...

  3. Spring Boot项目简单上手+swagger配置+项目发布(可能是史上最详细的)

    Spring Boot项目简单上手+swagger配置 1.项目实践 项目结构图 项目整体分为四部分:1.source code 2.sql-mapper 3.application.properti ...

  4. Spring boot 注解简单备忘

    Spring boot 注解简单备忘 1.定义注解 package com.space.aspect.anno;import java.lang.annotation.*; /** * 定义系统日志注 ...

  5. spring boot 基础篇 -- 定时任务

    在日常项目中,常常会碰到定时监控项目中某个业务的变化,下面是spring boot 集成的定时任务具体配置: @Component public class IndexWarningScheduled ...

  6. Spring Boot Mybatis简单使用

    Spring Boot Mybatis简单使用 步骤说明 build.gradle:依赖添加 application.properties:配置添加 代码编写 测试 build.gradle:依赖添加 ...

  7. Spring Boot 中实现定时任务的两种方式

    在 Spring + SpringMVC 环境中,一般来说,要实现定时任务,我们有两中方案,一种是使用 Spring 自带的定时任务处理器 @Scheduled 注解,另一种就是使用第三方框架 Qua ...

  8. Spring boot中的定时任务(计划任务)

    从Spring3.1开始,计划任务在Spring中实现变得异常的简单.首先通过配置类注解@EnableScheduling来开启对计划任务的支持,然后再要执行的计划任务的方法上注释@Scheduled ...

  9. spring-boot-route(二十)Spring Task实现简单定时任务

    Spring Task是Spring 3.0自带的定时任务,可以将它看作成一个轻量级的Quartz,功能虽然没有Quartz那样强大,但是使用起来非常简单,无需增加额外的依赖,可直接上手使用. 一 如 ...

  10. spring boot一个简单用户管理DEMO

    概述 该Demo旨在部署一个简单spring boot工程,包含数据编辑和查看功能 POM配置 <?xml version="1.0" encoding="UTF- ...

随机推荐

  1. 在红帽RHEL7.0里配置网卡的四种方法

    第一种方法 :采用vim编辑器来配置: 1.  如下图的步骤所示: 2.  输入这个命令后进行配置成下方图片里的内容: 3.  然后退出vim 编辑器,然后重新启动一下网络服务配置: 4.这些配置完后 ...

  2. BZOJ4032: [HEOI2015]最短不公共子串(后缀自动机+序列自动机)

    题目描述 在虐各种最长公共子串.子序列的题虐的不耐烦了之后,你决定反其道而行之. 一个串的“子串”指的是它的连续的一段,例如bcd是abcdef的子串,但bde不是. 一个串的“子序列”指的是它的可以 ...

  3. EXPORT Man Information for Linux use COMMAND col

    col命令 是一个标准输入文本过滤器,它从标注输入设备读取文本内容,并把内容显示到标注输出设备.在许多UNIX说明文件里,都有RLF控制字符.当我们运用shell特殊字符>和>>,把 ...

  4. 量化分析获取数据的3种姿势(压箱底的神器Tushare)

    自打入门量化分析起,就有相当部分的时间在与数据打交道,从数据的获取.清洗到使用,对分析而言既是繁琐的,也是必须的.有大牛曾经说,量化分析有8成的开发时间都在处理数据. 为了节省时间,将更多精力投入到策 ...

  5. html中的块与布局

    一.div a.会跳行 b.如果想不跳行则设置 style =“display:inline” ,允许它的前后存在其它的内联元素同行显示. c.或者设置为浮动元素,float:left 允许它的右边存 ...

  6. jquery实现简易的计算器

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  7. 【Leetcode】Binary Tree Traversal

    把三个二叉树遍历的题放在一起了. 递归写法太简单,就不再实现了,每题实现了两种非递归算法. 一种是利用栈,时间和空间复杂度都是O(n). 另一种是借助线索二叉树,也叫Morris遍历,充分利用树中节点 ...

  8. annotation-config和component-scan

    以前学到<context:annotation-config></context:annotation-config>和<context:component-scan b ...

  9. Scrapy——settings配置文件

    # -*- coding: utf-8 -*- # Scrapy settings for tencent project # # For simplicity, this file contains ...

  10. 第八次 Scrum Meeting

    第八次 Scrum Meeting 写在前面 会议时间 会议时长 会议地点 2019/4/12 22:00 20min 大运村1号楼6F 附Github仓库:WEDO 例会照片 工作情况总结(4.12 ...