当前位置: 首页 > article >正文

spring boot通过文件配置yaml里面的属性

yaml文件

fsg:
batch-approval:
  # 批量审批
  batch-approval:
    pool:
      core-size: 2
      max-size: 10
      queue-capacity: 100
      keep-alive: 60
      name-prefix: ApprovalThread-
    shutdown:
      await-termination: true
      await-termination-period: 60

ConfigurationProperties配置


import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;

import java.time.Duration;

@Data
@ConfigurationProperties("fsg.batch-approval")
public class BatchApprovalProperties {
    private Pool pool = new Pool();

    private Shutdown shutdown = new Shutdown();

    @Data
    public static class Pool {

        /**
         * Queue capacity. An unbounded capacity does not increase the pool and therefore
         * ignores the "max-size" property.
         */
        private int queueCapacity = 200;

        /**
         * Core number of threads.
         */
        private int coreSize = 2;

        /**
         * Maximum allowed number of threads. If tasks are filling up the queue, the pool
         * can expand up to that size to accommodate the load. Ignored if the queue is
         * unbounded.
         */
        private int maxSize = 10;

        /**
         * Whether core threads are allowed to time out. This enables dynamic growing and
         * shrinking of the pool.
         */
        private boolean allowCoreThreadTimeout = true;

        /**
         * Time limit for which threads may remain idle before being terminated.
         */
        private Integer keepAlive = 60;

        private String namePrefix = "ApprovalThread-";
    }

    @Data
    public static class Shutdown {

        /**
         * Whether the executor should wait for scheduled tasks to complete on shutdown.
         */
        private boolean awaitTermination;

        /**
         * Maximum time the executor should wait for remaining tasks to complete.
         */
        private Duration awaitTerminationPeriod;
    }

}

使用配置文件的属性


import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.concurrent.Executor;
@EnableAsync
@Configuration
@EnableConfigurationProperties(BatchApprovalProperties.class)
public class BatchApprovalAsyncConfig {

    @Bean(name = "batchApprovalTaskExecutor")
    public Executor batchApprovalTaskExecutor(BatchApprovalProperties props) {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        executor.setCorePoolSize(props.getPool().getCoreSize()); // 核心线程数
        executor.setMaxPoolSize(props.getPool().getMaxSize()); // 最大线程数
        executor.setQueueCapacity(props.getPool().getQueueCapacity()); // 队列大小
        executor.setKeepAliveSeconds(props.getPool().getKeepAlive()); // 线程空闲时的存活时间
        executor.setThreadNamePrefix(props.getPool().getNamePrefix()); // 线程名称前缀
        executor.initialize(); // 初始化线程池
        return executor;
    }

}

http://www.kler.cn/a/463137.html

相关文章:

  • 感知机参数更新策略
  • 【51单片机零基础-chapter6:LCD1602调试工具】
  • OpenStack的核心组件、主要特点和使用场景
  • LinuxC高级day5
  • Nacos配置中心总结
  • 【服务器】上传文件到服务器并训练深度学习模型下载服务器文件到本地
  • 从数据映射到文件生成:一个R语言实践案例
  • 自己电脑搭建个人知识库,一般电脑也能玩(支持通义千问、GPT等)。
  • VSCode 插件开发实战(十六):详解插件生命周期
  • selenium(三)
  • Midjourney技术浅析(三):文本编码
  • .NET | 详解通过Win32函数实现本地提权
  • 计算机网络—————考研复试
  • WOFOST作物模型(2.1):模型参数介绍
  • Python基于Django的web漏洞挖掘扫描技术的实现与研究(附源码,文档说明)
  • 数据库在大数据领域的探索与实践:动态存储与查询优化
  • 二叉树的基本数据结构类型(c语言)
  • OpenCV 图像处理之形态学转换
  • 数据结构(Java)—— 栈(Stack)
  • OpenCV的TickMeter计时类
  • 【Rust自学】8.3. String类型 Pt.1:字符串的创建、更新与拼接
  • Sentinel 介绍与使用指南:构建高可用、可靠的微服务架构
  • 大数据面试笔试宝典之大数据运维面试
  • 【文献精读笔记】Explainability for Large Language Models: A Survey (大语言模型的可解释性综述)(二)
  • 【Spring】Spring DI(依赖注入)详解—集合类型的注入——List、Set、Map的配置与注入
  • linux tar 文件解压压缩