⑤SpringBoot之定时任务

本文介绍SpringBoot定时任务的使用,springboot默认已经帮我们实行了,只需要添加相应的注解就可以实现。

1.pom配置文件

pom包里面只需要引入springboot starter包即可。

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-devtools</artifactId>
        <optional>true</optional>
    </dependency>
</dependencies>

2、启动类启用定时

在启动类上面加上@EnableScheduling即可开启定时。

@SpringBootApplication
@EnableScheduling
public class SpringbootApplication {
    public static void main(String[] args) {
        SpringApplication.run(SpringbootApplication.class, args);
    }
}

3、创建定时任务实现类

import java.text.SimpleDateFormat;
import java.util.Date;

import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

@Component
public class SchedulerTask {

     private int count=0;
     private static final SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");
     @Scheduled(cron="*/6 * * * * ?")
     private void process(){
        System.out.println("this is scheduler task runing  "+(count++));
     }

     @Scheduled(fixedRate = 6000)
     public void reportCurrentTime() {
        System.out.println("现在时间:" + dateFormat.format(new Date()));
     }
}

参数说明:

@Scheduled 参数可以接受两种定时的设置,一种是我们常用的cron=”*/6 * * * * ?”,一种是 fixedRate = 6000,两种都表示每隔六秒打印一下内容。
fixedRate 说明
@Scheduled(fixedRate = 6000) :上一次开始执行时间点之后6秒再执行;
@Scheduled(fixedDelay = 6000) :上一次执行完毕时间点之后6秒再执行;
@Scheduled(initialDelay=1000, fixedRate=6000) :第一次延迟1秒后执行,之后按fixedRate的规则每6秒执行一次。

控制台输出:

如果使用static变量,假设 private static boolean isStop=false;
在定时器的方法中首行加一行代码:if(isStop) return;
如果需要停止定时器,你写个页面,控制isStop的值就可以开关定时器了。

原文地址:https://www.cnblogs.com/shawWey/p/9431423.html

时间: 2024-07-30 20:42:15

⑤SpringBoot之定时任务的相关文章

SpringBoot创建定时任务

之前总结过spring+quartz实现定时任务的整合http://www.cnblogs.com/gdpuzxs/p/6663725.html,而springboot创建定时任务则是相当简单. (1)在springboot主类中@EnableScheduling注解,启用定时任务的配置,如下: (2)创建定时任务实现类,如下: package springboot.web; import java.text.SimpleDateFormat; import java.util.Date; im

SpringBoot | :定时任务的使用

前言 上一章我们简单的讲解了关于异步请求相关知识点.这一章节,我们来讲讲开发过程也是经常会碰见的定时任务.比如每天定时清理无效数据.定时发送短信.定时发送邮件.支付系统中的定时对账等等,往往都会定义一些定时器,进行此业务的开发.所以,本章节介绍下在SpringBoot中定时任务如何使用及一点分布式定时服务的思考总结. 在JAVA开发领域,目前可以通过以下几种方式进行定时任务: Timer:jdk中自带的一个定时调度类,可以简单的实现按某一频度进行任务执行.提供的功能比较单一,无法实现复杂的调度任

玩转SpringBoot之定时任务详解

序言 使用SpringBoot创建定时任务非常简单,目前主要有以下三种创建方式: 一.基于注解(@Scheduled) 二.基于接口(SchedulingConfigurer) 前者相信大家都很熟悉,但是实际使用中我们往往想从数据库中读取指定时间来动态执行定时任务,这时候基于接口的定时任务就派上用场了. 三.基于注解设定多线程定时任务 一.静态:基于注解 基于注解@Scheduled默认为单线程,开启多个任务时,任务的执行时机会受上一个任务执行时间的影响. 1.创建定时器 使用SpringBoo

SpringBoot整合定时任务和异步任务处理 3节课

1.SpringBoot定时任务schedule讲解   定时任务应用场景: 简介:讲解什么是定时任务和常见定时任务区别 1.常见定时任务 Java自带的java.util.Timer类             timer:配置比较麻烦,时间延后问题             timertask:不推荐 2.Quartz框架             配置更简单             xml或者注解 3.SpringBoot使用注解方式开启定时任务             1)启动类里面 @Ena

SpringBoot整合定时任务和异步任务处理

SpringBoot定时任务schedule讲解 简介:讲解什么是定时任务和常见定时任务区别 1.常见定时任务 Java自带的java.util.Timer类 timer:配置比较麻烦,时间延后问题,不推荐 timertask:不推荐 2.Quartz框架(复杂定时任务可以使用,spring 或springmv项目) 配置更简单 xml或者注解 具体说明后续...... 3.SpringBoot使用注解方式开启定时任务(springboot项目推荐使用) 1)启动类里面 @EnableSched

使用spring-boot创建定时任务。同时创建多线程执行定时任务。

1,下载spring-boot的maven工程:http://start.spring.io/  直接自定义工程名称. 2 , 启动类增加注解:@EnableScheduling 具体的业务代码: package com.huike.ftp.main; import java.util.Date;import java.util.concurrent.Executor; import org.apache.logging.log4j.LogManager;import org.apache.lo

springboot之定时任务@Scheduled

1.pom.xml中导入必要的依赖: <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.1.RELEASE</version> </parent> <dependencies> <!-- SpringBoot

springboot 的定时任务使用

定时任务在Spring Boot中的集成 在启动类中加入开启定时任务的注解: 在SpringBoot中使用定时任务相当的简单.首先,我们在启动类中加入@EnableScheduling来开启定时任务.-------------------这是必要的 然后: @Component public class QuartzService { // 每分钟启动 @Scheduled(cron = "0 0/1 * * * ?") public void timerToNow(){ System

springboot的定时任务

1.定时任务的创建方式: 基于注解@Schedule和,定时任务执行时间较短,并且比较单一 2.@Schedule和@EnableScheduling (1)在pom.xml中加入依赖 <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <scope>runtime</scop