spring-boot 定时任务案例
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 定时任务案例的更多相关文章
- Spring Boot定时任务应用实践
在Spring Boot中实现定时任务功能,可以通过Spring自带的定时任务调度,也可以通过集成经典开源组件Quartz实现任务调度. 一.Spring定时器 1.cron表达式方式 使用自带的定时 ...
- spring boot.定时任务问题记录(TaskScheduler/ScheduledExecutorService异常)
一.背景 spring boot的定时任务非常简单,只需要在启动类中加上@EnableScheduling注解,然后在对应的方法上配置@Scheduled就可以了,系统会自动处理并按照Schedule ...
- (14)Spring Boot定时任务的使用【从零开始学Spring Boot】
本文介绍在 Spring Boot 中如何使用定时任务,使用非常简单,就不做过多说明了. com.kfit.base.scheduling.SchedulingConfig: package com. ...
- Spring Boot 定时任务单线程和多线程
Spring Boot 的定时任务: 第一种:把参数配置到.properties文件中: 代码: package com.accord.task; import java.text.SimpleDat ...
- Spring Boot (十一): Spring Boot 定时任务
在实际的项目开发工作中,我们经常会遇到需要做一些定时任务的工作,那么,在 Spring Boot 中是如何实现的呢? 1. 添加依赖 在 pom.xml 文件中只需引入 spring-boot-sta ...
- Spring Boot 定时任务 @Scheduled
项目开发中经常需要执行一些定时任务,比如在每天凌晨,需要从 implala 数据库拉取产品功能活跃数据,分析处理后存入到 MySQL 数据库中.类似这样的需求还有许多,那么怎么去实现定时任务呢,有以下 ...
- Spring Boot定时任务运行一段时间后自动关闭的解决办法
用Spring Boot默认支持的 Scheduler来运行定时任务,有时在服务器运行一段时间后会自动关闭.原因:Schedule默认是单线程运行定时任务的,即使是多个不同的定时任务,默认也是单线程运 ...
- Spring Boot 入门案例与配置说明
一.Spring Boot简介 官网地址:http://spring.io/projects/spring-boot Spring Boot可以轻松创建可以运行的独立的,生产级的基于Spring的应用 ...
- spring boot 小案例
1. SpringBoot 1.1. 概要 在传统的SSM框架应用过程中,存在大量的配置文件,及相关的配置项,例如: 1. DispatcherServlet 2. CharacterEncoding ...
- Spring Boot 定时任务 Quartz 使用教程
Quartz是一个完全由java编写的开源作业调度框架,他使用非常简单.本章主要讲解 Quartz在Spring Boot 中的使用. 快速集成 Quartz 介绍 Quartz 几个主要技术点 Qu ...
随机推荐
- E:\Postgresql\pgAdmin4_binaryPath
e Path to the directory containing the EDB Advanced Server utility programs (pg_dump, pg_restore etc ...
- C#-概念-基础类库:基础类库
ylbtech-C#-概念-基础类库:基础类库 基础类库 (BCL) 是微软所提出的一组标准库可提供.NET Framework所有语言使用. 随着 Windows 以及 .NET Framework ...
- Web安全测试——常见的威胁攻防
SQL注入 部分程序员在编写代码的时候,没有对用户输入数据的合法性进行判断,使应用程序存在安全隐患.用户可以提交一段数据库查询代码,根据程序返回的结果,获得某些他想得知的数据,这就是所谓的SQL In ...
- HTML --JS 密码验证
l> <head> <title>js</title> <script language="JavaScript"> func ...
- 【ABAP系列】SAP ABAP基础-abap数据类型的解析整理
公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[ABAP系列]SAP ABAP基础-abap数 ...
- 2019/11/02 TZOJ
1001 ShaoLin http://www.tzcoder.cn/acmhome/problemdetail.do?&method=showdetail&id=6003 标记一下i ...
- pandas基础(第一章(一))
摘要:通过简单例子,了解功能.以此作为基点,在工作中不断深入 1.设置需要显示的行列宽度(显示的最大列数和最大行数,其余部分用.....表示) 设置显示多少行多少列 import pandas as ...
- golang的数据类型之浮点类型
[root@node3 shangxuetang]# cat float.go package main import "fmt" //演示golang中小数类型使用func ma ...
- python面试题之Python 的特点和优点是什么
Python 可以作为编程的入门语言,因为他具备以下特质: 1. 解释性 2. 动态特性 3. 面向对象 4. 语法简洁 5. 开源 6. 丰富的社区资源 7 库丰富 文章转载自Python黑洞网
- 攻防世界--insanity
测试文件:https://adworld.xctf.org.cn/media/task/attachments/d2a7dde552e647688230e80b2767b912 1.准备 获得信息: ...