1.运行环境

开发工具:intellij idea

JDK版本:1.8

项目管理工具:Maven 4.0.0

2.Maven Plugin管理

pom.xml配置代码:

 <?xml version="1.0" encoding="UTF-8"?>
<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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion> <groupId>spring-boot-scheduler</groupId>
<artifactId>spring-boot-scheduler</artifactId>
<version>1.0-SNAPSHOT</version> <!-- Spring Boot 启动父依赖 -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.6.RELEASE</version>
</parent> <dependencies>
<!-- Spring Boot web依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- Spring Boot test依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies> </project>

3.Application启动类编写

通过@EnableScheduling激活上下文中的所有定时任务;如果我们没有这个标注,所有@Scheduled标注都不会执行。

 package com.goku.demo;

 import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.scheduling.annotation.EnableScheduling; /**
* Created by nbfujx on 2017/11/20.
*/
// Spring Boot 应用的标识
@SpringBootApplication
@ServletComponentScan
@EnableScheduling
public class DemoApplication { public static void main(String[] args) {
// 程序启动入口
// 启动嵌入式的 Tomcat 并初始化 Spring 环境及其各 Spring 组件
SpringApplication.run(DemoApplication.class,args);
}
}

4.Scheduled Task创建

通过@Scheduled标注表示这个方法是需要定时执行的任务。

 package com.goku.demo.scheduler;

 import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component; /**
* Created by nbfujx on 2017/12/6.
*/
@Component
public class ExampleScheduler { private final Logger logger = LoggerFactory.getLogger(getClass()); @Scheduled(cron = "*/5 * * * * * ")
public void loginfo() {
this.logger.info("this is one");
} @Scheduled(fixedRate = 5000)
public void loginfo2() {
this.logger.info("this is two");
} @Scheduled(fixedDelay = 5000)
public void loginfo3() {
this.logger.info("this is three");
}
}
  • fixedRate = 5000表示每隔5000ms,Spring scheduling会调用一次该方法,不论该方法的执行时间是多少
  • fixedDelay = 5000表示当方法执行完毕5000ms后,Spring scheduling会再次调用该方法
  • cron = "*/5 * * * * * ?"提供了一种通用的定时任务表达式,这里表示每隔5秒执行一次,更加详细的信息可以参考cron表达式。

5.查看运行情况

6.GITHUB地址

https://github.com/nbfujx/springBoot-learn-demo/tree/master/spring-boot-scheduler

spring-boot 定时任务案例的更多相关文章

  1. Spring Boot定时任务应用实践

    在Spring Boot中实现定时任务功能,可以通过Spring自带的定时任务调度,也可以通过集成经典开源组件Quartz实现任务调度. 一.Spring定时器 1.cron表达式方式 使用自带的定时 ...

  2. spring boot.定时任务问题记录(TaskScheduler/ScheduledExecutorService异常)

    一.背景 spring boot的定时任务非常简单,只需要在启动类中加上@EnableScheduling注解,然后在对应的方法上配置@Scheduled就可以了,系统会自动处理并按照Schedule ...

  3. (14)Spring Boot定时任务的使用【从零开始学Spring Boot】

    本文介绍在 Spring Boot 中如何使用定时任务,使用非常简单,就不做过多说明了. com.kfit.base.scheduling.SchedulingConfig: package com. ...

  4. Spring Boot 定时任务单线程和多线程

    Spring Boot 的定时任务: 第一种:把参数配置到.properties文件中: 代码: package com.accord.task; import java.text.SimpleDat ...

  5. Spring Boot (十一): Spring Boot 定时任务

    在实际的项目开发工作中,我们经常会遇到需要做一些定时任务的工作,那么,在 Spring Boot 中是如何实现的呢? 1. 添加依赖 在 pom.xml 文件中只需引入 spring-boot-sta ...

  6. Spring Boot 定时任务 @Scheduled

    项目开发中经常需要执行一些定时任务,比如在每天凌晨,需要从 implala 数据库拉取产品功能活跃数据,分析处理后存入到 MySQL 数据库中.类似这样的需求还有许多,那么怎么去实现定时任务呢,有以下 ...

  7. Spring Boot定时任务运行一段时间后自动关闭的解决办法

    用Spring Boot默认支持的 Scheduler来运行定时任务,有时在服务器运行一段时间后会自动关闭.原因:Schedule默认是单线程运行定时任务的,即使是多个不同的定时任务,默认也是单线程运 ...

  8. Spring Boot 入门案例与配置说明

    一.Spring Boot简介 官网地址:http://spring.io/projects/spring-boot Spring Boot可以轻松创建可以运行的独立的,生产级的基于Spring的应用 ...

  9. spring boot 小案例

    1. SpringBoot 1.1. 概要 在传统的SSM框架应用过程中,存在大量的配置文件,及相关的配置项,例如: 1. DispatcherServlet 2. CharacterEncoding ...

  10. Spring Boot 定时任务 Quartz 使用教程

    Quartz是一个完全由java编写的开源作业调度框架,他使用非常简单.本章主要讲解 Quartz在Spring Boot 中的使用. 快速集成 Quartz 介绍 Quartz 几个主要技术点 Qu ...

随机推荐

  1. linux系统忘记root密码的解决办法--(超细致讲解!)

    在本博客中我采用虚拟机和radhet6.5作为示范: 首先:重新系统系统,在系统重新启动是未进入系统检查之前按下"e"键: 一定要在倒计时到0秒之前按下"e", ...

  2. Skyline(6.x)-二次开发手册使用技巧

    毕业设计选择 Skyline 的 Web 端二次开发,由于以前没有接触过 ActiveX 控件的使用,二次开发手册是英文的读起来有点吃力,并且 IE 直接控制台输出 ActiveX 控件创建的对象看不 ...

  3. EF框架之——Code First以及踩过的这些“坑”

    传送门 Code First使用步骤 Code First报错和解决办法 以前在上海做了一段时间的Asp.net,基本用的都是.net自带的EF框架连接数据库,不过都是用的Model First,最近 ...

  4. 04 | 基础篇:经常说的 CPU 上下文切换是什么意思?(下)

    上一节,我给你讲了 CPU 上下文切换的工作原理.简单回顾一下,CPU 上下文切换是保证 Linux 系统正常工作的一个核心功能,按照不同场景,可以分为进程上下文切换.线程上下文切换和中断上下文切换. ...

  5. spring容器、spring MVC容器以及web容器的区别

    本文转载自 https://www.cnblogs.com/xiexin2015/p/9023239.html 说到spring和springmvc,其实有很多工作好多年的人也分不清他们有什么区别,如 ...

  6. CNN之池化层tf.nn.max_pool | tf.nn.avg_pool | tf.reduce_mean | padding的规则解释

    摘要:池化层的主要目的是降维,通过滤波器映射区域内取最大值.平均值等操作. 均值池化:tf.nn.avg_pool(input,ksize,strides,padding) 最大池化:tf.nn.ma ...

  7. Python分布式爬虫必学框架Scrapy打造搜索引擎 学习教程

    Python分布式爬虫打造搜索引擎Scrapy精讲—用Django实现搜索的自动补全功能 elasticsearch(搜索引擎)提供了自动补全接口 1.创建搜索自动补全字段suggest自动补全需要用 ...

  8. Ubuntu TTy字体

    目的:修改tty终端下的字体大小,中文显示,字体美观问题 Linux版本:xubuntu14.04 当切换到tty终端模式式,中文乱码,且字体太小,影响阅读.在网上搜了一些资料,将问题及解决方案记录如 ...

  9. 《JAVA设计模式》之单例模式(Singleton)

    在阎宏博士的<JAVA与模式>一书中开头是这样描述单例模式的: 作为对象的创建模式,单例模式确保某一个类只有一个实例,而且自行实例化并向整个系统提供这个实例.这个类称为单例类. 单例模式的 ...

  10. [Linux] 007 目录处理命令

    1. 目录处理命令:mkdir 命令名称:mkdir 命令英文原意:make directories 命令所在路径:/bin/mkdir 执行权限:所有用户 语法:mkdir -p [目录名] 功能描 ...