(十七)ATP应用测试平台——自定义实现一个springboot2的线程池启动器starter
Posted 北溟溟
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了(十七)ATP应用测试平台——自定义实现一个springboot2的线程池启动器starter相关的知识,希望对你有一定的参考价值。
前言
启动器是springboot的一大特点,我们可以根据项目自身需求按需装配我们的组件。例如我们需要操作redis,项目中可以添加一个redis的启动器spring-boot-starter-data-redis,这样redis的一些客户端操作功能我们就集成好了,不用在繁琐的集成redis客户端的依赖,启动器会帮我们加载一切我们所需要的包。既然这么方便,那么我们如何定制自己的项目启动器呢?本节内容,我们以线程池为例,定制一个线程池的启动类,这样项目引入我们的线程池启动器,并加入相关配置,就可以使用我们自定义的线程池方法了。
正文
- 通过idea工具创建一个springboot项目atp-thread-pool-spring-boot-starter启动器
- 添加pom依赖
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.yundi.atp</groupId>
<artifactId>atp-thread-pool-spring-boot-starter</artifactId>
<version>1.0.0</version>
<name>atp-thread-pool-spring-boot-starter</name>
<description>线程池启动器</description>
<properties>
<java.version>1.8</java.version>
<spring.boot.starter.version>2.3.4.RELEASE</spring.boot.starter.version>
<spring.boot.configuration.processor.version>2.3.4.RELEASE</spring.boot.configuration.processor.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<version>$spring.boot.starter.version</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<version>$spring.boot.configuration.processor.version</version>
<optional>true</optional>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>7</source>
<target>7</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
- 创建properties属性配置类AtpThreadPoolProperties用于线程池核心配置参数的注入
package com.yundi.atp.config;
import org.springframework.boot.context.properties.ConfigurationProperties;
/**
* @Author: 北溟溟
* @Description:
* @Date: 2022/5/6 18:17
* @Version: 1.0.0
*/
@ConfigurationProperties(prefix = "atp.thread")
public class AtpThreadPoolProperties
/**
* 是否开启
*/
private Boolean enable = true;
/**
* 核心线程数量
*/
private Integer corePoolSize;
/**
* 最大线程数
*/
private Integer maximumPoolSize;
/**
* 空闲线程的最长时间
*/
private Integer keepAliveTime;
/**
* 队列大小
*/
private Integer queueSize;
public Boolean getEnable()
return enable;
public void setEnable(Boolean enable)
this.enable = enable;
public Integer getCorePoolSize()
return corePoolSize;
public void setCorePoolSize(Integer corePoolSize)
this.corePoolSize = corePoolSize;
public Integer getMaximumPoolSize()
return maximumPoolSize;
public void setMaximumPoolSize(Integer maximumPoolSize)
this.maximumPoolSize = maximumPoolSize;
public Integer getKeepAliveTime()
return keepAliveTime;
public void setKeepAliveTime(Integer keepAliveTime)
this.keepAliveTime = keepAliveTime;
public Integer getQueueSize()
return queueSize;
public void setQueueSize(Integer queueSize)
this.queueSize = queueSize;
- 创建一个启动器的配置类AtpThreadPoolAutoConfiguration,根据条件规则加载线程池执行器ThreadPoolExecutor
package com.yundi.atp.config;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.util.concurrent.Executors;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
/**
* @Author: 北溟溟
* @Description: 线程池配置
* @Date: 2021/5/13 10:38
* @Version: 1.0.0
*/
@Configuration
@ConditionalOnWebApplication
@EnableConfigurationProperties(AtpThreadPoolProperties.class)
public class AtpThreadPoolAutoConfiguration
/**
* matchIfMissing: 缺少该配置属性时是否可以加载。如果为true,没有该配置属性时也会正常加载;反之则不会生效
* havingValue: 可与name组合使用,比较获取到的属性值与havingValue给定的值是否相同,相同才加载配置
*
* @param pool
* @return
*/
@ConditionalOnProperty(name = "atp.thread.enable", matchIfMissing = true, havingValue = "true")
@Bean
public ThreadPoolExecutor threadPoolExecutor(AtpThreadPoolProperties pool)
ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(pool.getCorePoolSize(),
pool.getMaximumPoolSize(),
pool.getKeepAliveTime(),
TimeUnit.SECONDS,
new LinkedBlockingQueue<Runnable>(pool.getQueueSize()),
Executors.defaultThreadFactory(),
new ThreadPoolExecutor.AbortPolicy());
return threadPoolExecutor;
- 在/resources/META-INF目录下创建spring.factories文件,加载线程池启动器的配置类,在spring容器启动的时候可以将线程池加载到容器中
#spring.factories配置
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\\
com.yundi.atp.config.AtpThreadPoolAutoConfiguration
- 将该线程池的启动器安装到maven的仓库,然后引入该启动器的pom就可以使用线程池的功能
- 使用atp-portal-web项目的springboot工程测试,引入启动器的pom依赖
- application.yml填写自定义线程池配置
- 调用线程池方法测试,启动器测试类StarterController
package com.yundi.atp.contoller;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
/**
* @Author: 北溟溟
* @Description:
* @Date: 2022/5/5 19:53
* @Version: 1.0.0
*/
@Slf4j
@RestController
@RequestMapping(value = "/starter")
public class StarterController
@Autowired
ThreadPoolExecutor threadPoolExecutor;
@GetMapping(value = "/param")
public String getStarterParam()
threadPoolExecutor.execute(() ->
System.out.println(threadPoolExecutor.getCorePoolSize());
System.out.println(threadPoolExecutor.getMaximumPoolSize());
System.out.println(threadPoolExecutor.getKeepAliveTime(TimeUnit.SECONDS));
);
return "success";
- 启动项目,访问声明的接口测试,发现线程池启动器可以调用
结语
关于自定义实现一个springboot的线程池启动器starter到这里就结束了,本节内容只是启动器的一个小小的案例,我们可以根据实际应用场景,将常用的功能封装成一个个启动器,通过按需加载,完成相应功能的引入,是不是也很秀呢。本节内容到此结束,我们下期见。。。
以上是关于(十七)ATP应用测试平台——自定义实现一个springboot2的线程池启动器starter的主要内容,如果未能解决你的问题,请参考以下文章
(十七)ATP应用测试平台——Redis实现API接口访问限流(固定窗口限流算法)
(十七)ATP应用测试平台——Redis实现API接口访问限流(固定窗口限流算法)
(二十)ATP应用测试平台——websocket实现微服务版在线客服聊天室实战案例