Springboot非分布式定时任务实现代码

 更新时间:2020年11月19日 09:51:51   作者:大唐冠军侯  
这篇文章主要介绍了Springboot非分布式定时任务实现代码,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下

1. 核心注解

在springboot项目中我们可以很方便地使用spring自己的注解@Scheduled和@EnableScheduling配合来实现便捷开发定时任务。

@EnableScheduling注解的作用是发现注解@Scheduled的任务并后台执行,此注解可以加到启动类上也可以加到执行调度任务类上。

经测试,当有多个包含定时任务的类时,@EnableScheduling注解加在其中一个类上就可以保证所有定时任务的成功实现。

注意:定时任务的类上还需要配合使用@Configuration或@Component注解,这两个注解都可以。

2. 实例代码

2.1 @EnableScheduling加在启动类上;

import com.my.common.util.DateUtil;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * @description:
 * @author: Karl
 * @date: 2020/10/10
 */
@Component
public class TestSchedule01 {

  @Scheduled(cron = "0 * * * * ? ")
  public void test() {
    System.out.println("我是定时任务01,我执行了" + DateUtil.formatDateByDateTime(new Date()));
  }
}

import com.my.common.util.DateUtil;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * @description:
 * @author: Karl
 * @date: 2020/10/10
 */
@Configuration
public class TestSchedule02 {

  @Scheduled(cron = "1 * * * * ? ")
  public void test() {
    System.out.println("我是定时任务02,我执行了" + DateUtil.formatDateByDateTime(new Date()));
  }
}

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

@EnableScheduling
@SpringBootApplication
public class DemoApplication {

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

}

2.1 @EnableScheduling加在任务类上;

import com.my.common.util.DateUtil;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * @description:
 * @author: Karl
 * @date: 2020/10/10
 */
@Component
@EnableScheduling
public class TestSchedule01 {

  @Scheduled(cron = "0 * * * * ? ")
  public void test() {
    System.out.println("我是定时任务01,我执行了" + DateUtil.formatDateByDateTime(new Date()));
  }
}

import com.my.common.util.DateUtil;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * @description:
 * @author: Karl
 * @date: 2020/10/10
 */
@Configuration
public class TestSchedule02 {

  @Scheduled(cron = "1 * * * * ? ")
  public void test() {
    System.out.println("我是定时任务02,我执行了" + DateUtil.formatDateByDateTime(new Date()));
  }
}

注意:只需要在其中一个任务类上加上@EnableScheduling注解,所有的定时任务就都可以正常运行。

3. @Scheduled的几种用法

@Scheduled这个注解支持3种定时方式,即:cron、fixedRate和fixedDelay

cron:是以表达式的形式来表示时间,最常见;

fixedRate:表示Scheduled隔多长时间调用一次,不管任务是否执行完;

fixedDelay:表示该任务执行完后隔多长时间再调用;

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

相关文章

  • SpringBoot ApplicationContextAware拓展接口使用详解

    SpringBoot ApplicationContextAware拓展接口使用详解

    当一个类实现了这个接口(ApplicationContextAware)之后,这个类就可以方便获得ApplicationContext中的所有bean。换句话说,就是这个类可以直接获取spring配置文件中,所有有引用到的bean对象
    2023-04-04
  • 使用Springboot自定义注解,支持SPEL表达式

    使用Springboot自定义注解,支持SPEL表达式

    这篇文章主要介绍了使用Springboot自定义注解,支持SPEL表达式,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-02-02
  • Java 你知道什么是耦合、如何解(降低)耦合

    Java 你知道什么是耦合、如何解(降低)耦合

    这篇文章主要介绍了Java 你知道什么是耦合、如何解(降低)耦合的操作,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2021-09-09
  • SpringBoot初始教程之Servlet、Filter、Listener配置详解

    SpringBoot初始教程之Servlet、Filter、Listener配置详解

    本篇文章主要介绍了SpringBoot初始教程之Servlet、Filter、Listener配置详解,具有一定的参考价值,有兴趣的可以了解一下
    2017-09-09
  • java7钻石语法知识点总结

    java7钻石语法知识点总结

    在本篇文章里小编给大家整理的是关于java7钻石语法的相关知识点内容,有需要的朋友们参考下。
    2019-11-11
  • Java通过工厂、Map容器创建对象的方法

    Java通过工厂、Map容器创建对象的方法

    这篇文章主要介绍了Java通过工厂、Map容器创建对象的方法,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2021-03-03
  • java封装前端查询条件通用版

    java封装前端查询条件通用版

    这篇文章主要为大家详细介绍了java封装前端查询条件的通用版,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2019-05-05
  • java中类之间的数据传递方式

    java中类之间的数据传递方式

    这篇文章主要介绍了java中类之间的数据传递方式,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2021-11-11
  • Springmvc自定义类型转换器实现步骤

    Springmvc自定义类型转换器实现步骤

    这篇文章主要介绍了Springmvc自定义类型转换器实现步骤,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-08-08
  • Spring Boot使用FastJson解析JSON数据的方法

    Spring Boot使用FastJson解析JSON数据的方法

    本篇文章主要介绍了Spring Boot使用FastJson解析JSON数据的方法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-02-02

最新评论