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

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配置

java 复制代码
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;
    }

}

使用配置文件的属性

java 复制代码
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;
    }

}
相关推荐
eleven_4u几秒前
QPS如何计算,如何部署架构
java·linux·运维·jvm·中间件
牧瀬クリスだ12 分钟前
Redis Hash实战:高效存储对象与购物车
java·数据库·redis·算法·哈希算法
文丑颜不良啊15 分钟前
LeetCode1141~1160题目解析
java·leetcode
木井巳20 分钟前
【动态规划: 路径问题】下降路径最小和
java·算法·leetcode·动态规划·推荐算法
秋饼34 分钟前
MySQL 分库分表完全指南:ShardingSphere-JDBC 实战、分片键选型、场景边界、20 个生产坑与 30 道面试点
java·mysql·面试·shardingsphere·分库分表·后端架构
郭小李39 分钟前
spring 的 @CookieValue 注解 用法
java·后端·spring
天远Date Lab1 小时前
零信任架构实战:基于天远企业股权变更构建自动化招投标供应商股东穿透审查网关
java·人工智能·架构·自动化
god_cvz1 小时前
麒麟 V10(ARM64 / aarch64)源码编译 3dtiles 全步骤
后端·gis
武子康1 小时前
批量设备任务显示 COMPLETED,为什么还有设备失败?
人工智能·后端·agent
weixin_399380691 小时前
TongWeb7.0.4.9M11如何接入到idea(by lqw)
java·ide·intellij-idea