首先是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. 【selenium专题】元素定位之多层框架和窗口

    本节知识点 多层框架或窗口切换样式:WebDrvier.switchto().TargetLocator Interface WebDriver.TargetLocator下所有可切换对象 参考API ...

  2. “全栈2019”Java第八十章:外部类是否能实现本类中的接口?

    难度 初级 学习时间 10分钟 适合人群 零基础 开发语言 Java 开发环境 JDK v11 IntelliJ IDEA v2018.3 文章原文链接 "全栈2019"Java第 ...

  3. 《OD面试》Java面试题整理

    一.面试考察点 1 主语言本身 2 数据库 3 算法 4 Spring/SpringMVC/MyBatis 5 项目经验 1)项目涉及到的技术点深挖: (1)考察候选人技术深度  (2)看候选人遇到问 ...

  4. CentOS6.9 minimal版本安装图形化界面

    CentOS6.9 minimal版本安装图形化界面 安装步骤如下: 1.安装Desktop组 # yum groupinstall "Desktop" -y 2.安装X Wind ...

  5. [Flex] FlashBuilder 4.6运用标签嵌入字体方法

    <?xml version="1.0" encoding="utf-8"?> <s:Application xmlns:fx="ht ...

  6. Spark JavaRDD、JavaPairRDD、Dataset相互转换与打印

    主要内容: 1. List转JavaRDD,打印JavaRDD 2. List转JavaRDD,JavaRDD转JavaPairRDD,打印JavaPairRDD 3. JavaRDD<Stri ...

  7. 8,Phaser__并发且多阶段任务

    使用场景 考选武状元 10 个 武生 参加考试 ,第一个关 靠耐力, 坚持最久的5个人进入第二关, 第二关考 力气,力气最大的 3个人进入第二关,第三关考兵法,兵法最好的当选武状元

  8. &与&& ,|与||的区别

    &&和||是短路运算符,&和|是非短路运算符 &&与& 区别:两者都表示“与”运算,但是&&运算符第一个表达式不成立的话,后面的表达式不 ...

  9. 小众软件:windows 系统下 exe 文件打包软件

    1. Enigma Virtual Box 单文件打包软件 官网:EnigmaProtection 2. 安装包打包软件 官网:Inno Setup 参考文献: [1] 单文件制作工具Enigma V ...

  10. overload_protect_config.txt

    overload_protection_switch=Y reject_uri_list= reject_request_percent=50 period_time=10 period_max_fa ...