springBoot(19):定时任务

一、依赖

<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter</artifactId>
</dependency>

二、实现

启动类上需加上@EnableScheduling注解

SpringBootSchedulingApplication.java

package com.example.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling;

@SpringBootApplication
@EnableScheduling
public class SpringBootSchedulingApplication {

   public static void main(String[] args) {
      SpringApplication.run(SpringBootSchedulingApplication.class, args);
   }
}

Task1.java

package com.example.demo.utils.task;

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

/**
 * 每隔六秒执行(cron表达式)
 *
 * @Author: 我爱大金子
 * @Description: 每隔六秒执行(cron表达式)
 * @Date: Create in 18:03 2017/7/4
 */
@Component
public class Task1 {
    private int count=0;

    @Scheduled(cron="*/6 * * * * ?")
    private void process(){
        System.out.println("this is scheduler task runing  "+(count++));
    }

}

Task2.java

package com.example.demo.utils.task;

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

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

/**
 * 每隔六秒执行(fixed方式)
 *
 * @Author: 我爱大金子
 * @Description: 每隔六秒执行(fixed方式)
 * @Date: Create in 18:03 2017/7/4
 */
@Component
public class Task2 {
    private static final SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");

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

效果:

三、说明

3.1、@Scheduled参数

@Scheduled参数可以接受两种定时的设置:一种是我们常用的cron="*/6 * * * * ?",一种是 fixedRate = 6000,两种都表示每隔六秒打印一下内容。

fixedRate说明

@Scheduled(fixedRate = 6000) :上一次开始执行时间点之后6秒再执行

@Scheduled(fixedDelay = 6000) :上一次执行完毕时间点之后6秒再执行

@Scheduled(initialDelay=1000, fixedRate=6000) :第一次延迟1秒后执行,之后按fixedRate的规则每6秒执行一次

3.2、Could not find default TaskScheduler bean异常处理

就会在debug级别的日志输出一个异常:

logger.debug("Could not find default TaskScheduler bean", ex);

当然,这个异常并不影响应用程序运行,如果你不想看到这个异常,就可以通过提升org.springframework.scheduling这个包下日志级别来屏蔽这个不合理的异常。

时间: 2024-08-06 15:38:59

springBoot(19):定时任务的相关文章

SpringBoot | :定时任务的使用

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

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创建定时任务非常简单,目前主要有以下三种创建方式: 一.基于注解(@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之定时任务

本文介绍SpringBoot定时任务的使用,springboot默认已经帮我们实行了,只需要添加相应的注解就可以实现. 1.pom配置文件 pom包里面只需要引入springboot starter包即可. <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId>

springboot跑定时任务

使用@Scheduled注解实现 1.在启动类上加上@EnableScheduling 开启定时任务 2.新建一个任务类,在方法上添加@Scheduled注解 @Componentpublic class CostStateTask { @Scheduled(cron="0 19 9 * * ?") public void test(){ System.out.println("我是定时任务"); } }解析:@Scheduled注解有多种使用方式常用的是 1.fi