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. win10半夜自动开机的问题分析

    win10半夜自动开机的系统日志: 解决方法一: 1.根据日志判断自动唤醒后,windows更新了时间和代理 服务管理器中,关闭windows update, 但是半夜还会自动开 再关闭服务管理器的w ...

  2. vue-过滤器(filter)

    1.全局过滤器(项目中所有的vue文件都可以使用) 1.1  直接注册全局过滤器 在main.js中注册: 在项目中使用; 前面的为时间,作为filter过滤器的第一个参数. 1.2 所有过滤器写在一 ...

  3. MySQL 增删改语句

    # DML语言 /* 数据操作语言: 插入:insert 修改:update 删除: delete */ 一.插入语句 insert /* 语法: 方式一: insert into 表名(列名,..) ...

  4. 应用安全-工具使用-Burpsuite

    A cheat sheet for PortSwigger Burp Suite application security testing framework. Send to Repeater Ct ...

  5. mybatis中Oracle分页语句的写法

    最近一段时间使用oracle数据库查询分页, 用的是springboot. Oracle数据库中没有像mysql中limit的写法, 只能换其他方式写. 考虑到oracle中的ROWNUM变量, 使用 ...

  6. Pair Testing

    All-Pairs Testing is a test design method to deal with the combinatorics problem of defining test ca ...

  7. dp(最长公共上升子序列)

    This is a problem from ZOJ 2432.To make it easyer,you just need output the length of the subsequence ...

  8. 开源企业IM-免费企业即时通讯-ENTBOOST V2014.183 Linux版本号正式公布

    版权声明:本文为博主原创文章,欢迎转载,转载请尽量保持原文章完整,谢谢! https://blog.csdn.net/yanghz/article/details/37807975 ENTBOOST, ...

  9. python ORM的使用

    安装 >pip install sqlalchemy #coding=utf-8 ''' 原始的sql语句 CREATE TABLE user ( id INTEGER NOT NULL AUT ...

  10. day64--pymysql模块的使用、视图、触发器、函数、存储过程、事务

    一.pymysql的下载和使用 (一)pymysql模块的下载:pip3 install pymysql # 实现:使用Python实现用户登录,如果用户存在则登录成功(假设该用户已在数据库中) im ...