首页 > 其他分享 >SpringBoot中使用@Scheduled实现定时任务通过读取配置文件动态开关

SpringBoot中使用@Scheduled实现定时任务通过读取配置文件动态开关

时间:2023-05-25 16:23:05浏览次数:56  
标签:Scheduled task SpringBoot 配置文件 ConditionalOnProperty springframework org import 

场景

SpringBoot中定时任务与异步定时任务的实现:

https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/117083609

上面讲的通过@Scheduled注解实现简单定时任务的方式。

如果定时任务有多个,不同业务场景下需要动态配置某个定时任务的开关。

可以通过@ConditionalOnProperty注解来实现。

@ConditionalOnProperty

@ConditionalOnProperty注解来控制@Configuration是否生效。

把每个定时定位抽离出到单独一个Task类中,将Task类原来的@Component注解改为

@Configuration注解。然后在配置文件中配置对应的定时任务是否执行的表达式,在注解

@ConditionalOnProperty配置对应的配置文件的表达式。

注:

博客:
https://blog.csdn.net/badao_liumang_qizhi

实现

1、配置文件application.yml中添加配置项

task:
  getPositionData: "50000"
  getPositionDataEnable: true
  getSignalData: "50000"
  getSignalDataEnable: false

2、修改Task类

package com.badao.demo.task;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;


@Configuration
@EnableScheduling
@ConditionalOnProperty(name="task.getPositionDataEnable")
public class GetPositionDataTask {

    @Scheduled(fixedRateString = "${task.getPositionData}")
    public void  getPositionData(){
       
    }
   
}

这里task.getPositionDataEnable为true则配置生效,定时任务执行;为false则配置不生效,定时任务不执行。

这里为true,则此定时任务执行。

同理另一个定时任务类也做同样修改

package com.badao.demo.task;

import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;


@Configuration
@EnableScheduling
@ConditionalOnProperty(name="task.getSignalDataEnable")
public class GetSignalDataTask {

    @Scheduled(fixedRateString = "${task.getSignalData}")
    public void  getSignalData(){
      
    }
}

则该定时任务不执行。

标签:Scheduled,task,SpringBoot,配置文件,ConditionalOnProperty,springframework,org,import,
From: https://www.cnblogs.com/badaoliumangqizhi/p/17431686.html

相关文章

  • springboot入门
    1. 介绍  7SpringBoot是Spring中的一个成员, 可以简化Spring,SpringMVC的使用。 他的核心还是IOC容器。1.1 特点:  7Create stand-alone Spring applications创建spring应用Embed Tomcat, Jetty or Undertow directly (no need to deploy WAR files)内嵌的tomca......
  • Springboot @Value注解
    配置文件test:name:123list:1,2,3aa:userInfoServiceImpl.merChantNoController:@Value("${test.name}")publicStringname1;//输出123@Value("#{'${test.list}'}")publicList<Object>list;//输出[1,2,3]......
  • SpringBoot 出现 Content type ‘application/x-www-form-urlencoded;charset=UTF-8‘
    问题点1:如果Content-Type设置为“application/x-www-form-urlencoded;charset=UTF-8”无论是POST请求还是GET请求都是可以通过这种方式成功获取参数,但是如果前端POST请求中的body是Json对象的话,会报上述错误。请求中传JSON时设置的Content-Type如果是application/json或者tex......
  • Mybatis中,SpringMVC中,SpringBoot项目中,出现There is no getter for property named 'x
    现象:Thereisnogetterforpropertynamed'xxxxxx'报错原因:其实说起原因有很多种,百度上都有很详细的说明,其中最重要也是经常发生的就是mapper.xml与是对应的实体类匹配不上导致错误发生,而我报错的原因是从mapper接口中向xml传入参数的时候,传入的是实体类对象,只有这一个参数,而在......
  • 【SpringBoot】SpringBoot常用注解
    一、前言首先这里说的SpringBoot常用注解是指在我们开发项目过程中,我们经常使用的注解,包含Spring、SpringBoot、SpringCloud、SpringMVC等这些框架中的注解,而不仅仅是SpringBoot中的注解。这里只是作一个注解列举,每个注解具体如何使用可以自行搜索查询哈。二、配置启动相关注解2.1......
  • springboot long js 长整形
    项目中,数据库用bigint类型存储主键,java实体类中用long类型来存储对应的属性。这个时候前台通过ajax请求获取json数据时,使用了jackson来转换。但是javascript中number类型存储的长度小于long,精度为17位,超过17位的部分就会自动补0,而long类型数据是19或者20位,所以会出现丢失精度的问......
  • java基于springboot+vue的书籍学习平台管理系统,学期学习论坛管理系统,附源码+数据库+lw
    1、项目介绍困扰管理层的许多问题当中,书籍学习将会是不敢忽视的一块。但是管理好书籍学习又面临很多麻烦需要解决,在工作琐碎,记录繁多的情况下将书籍学习的当前情况反应给相关部门决策,等等。在此情况下开发一款书籍学习平台,于是乎变得非常合乎时宜。经过网上调查和搜集数据,......
  • springboot项目启动报错java.lang.NoSuchMethodError: org.springframework.boot.buil
    产生此问题的原因是由于springboot版本兼容性导致的:java.lang.NoSuchMethodError:org.springframework.boot.builder.SpringApplicationBuilder.<init>([Ljava/lang/Object;)V2019-08-2918:04:54.089ERROR[restartedMain][SpringApplication.java:842]-Applicationrunfail......
  • django配置文件作用,drf 登录功能,drf认证组件,drf权限组件,drf频率组件
    django配置文件作用:    drf登录功能:view内:from.modelsimportUserInfo,UserTokenfromrest_framework.viewsetsimportViewSetimportuuidfromrest_framework.responseimportResponsefromrest_framework.decoratorsimportactionclassUser......
  • spring-boot配置文件中server.context-path不起作用的解决方案
    背景:server.context-path不起作用简单说springboot项目路径默认是ip:port进入项目,通过在application配置文件添加server.context-path属性,可自定义上下文,如ip:port/server.context-path而springboot2.0之后,上下文的配置改为了server.servlet.context-path。  如果还是不懂可......