Commit 462f90e9 authored by lihao's avatar lihao

add feature task-executor with spring-boot-starter

parent 7cac131b
......@@ -2,12 +2,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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>pubTools</artifactId>
<groupId>com.mushiny</groupId>
<version>1.0.0-RELEASE</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<groupId>com.mushiny</groupId>
<artifactId>task-executor-kit</artifactId>
<version>1.0.0-RELEASE</version>
<packaging>pom</packaging>
......
......@@ -24,7 +24,7 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<version>${spring-boot.version}</version>
<optional>true</optional>
<scope>provided</scope>
</dependency>
<!-- 自动依赖配置 -->
......@@ -32,7 +32,7 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</artifactId>
<version>${spring-boot.version}</version>
<optional>true</optional>
<scope>provided</scope>
</dependency>
<!-- 将被@ConfigurationProperties注解的类的属性注入到元属性 -->
......@@ -40,7 +40,7 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<version>${spring-boot.version}</version>
<optional>true</optional>
<scope>provided</scope>
</dependency>
<dependency>
......
package com.mushiny.task.executor.autoconfig;
import com.alibaba.cloud.nacos.NacosConfigAutoConfiguration;
import com.mushiny.task.executor.core.TaskPoolFactory;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
@Configuration(proxyBeanMethods = false)
@EnableConfigurationProperties(value = TaskExecutorProperties.class)
@AutoConfigureAfter(NacosConfigAutoConfiguration.class)
public class TaskExecutorAutoConfiguration {
@ConditionalOnMissingBean
public ThreadPoolExecutor listLoopThreadPool() {
return null;
@Bean("listLoopThreadPool")
@ConditionalOnMissingBean(name = { "listLoopThreadPool" } )
public ThreadPoolExecutor listLoopThreadPool(TaskExecutorProperties taskExecutorProperties) {
TaskPoolConfig pool = taskExecutorProperties.getPool();
return new ThreadPoolExecutor(pool.getCorePoolSize(), pool.getMaximumPoolSize(), pool.getKeepAliveTime(), pool.getTimeUnit(), new LinkedBlockingQueue<>());
}
@ConditionalOnBean(name={"listLoopThreadPool"})
@Bean
@ConditionalOnBean(name = { "listLoopThreadPool" })
public TaskPoolFactory taskPoolFactory () {
return new TaskPoolFactory();
}
......
package com.mushiny.task.executor.autoconfig;
import org.springframework.boot.context.properties.ConfigurationProperties;
import java.util.concurrent.TimeUnit;
import org.springframework.boot.context.properties.NestedConfigurationProperty;
@ConfigurationProperties(prefix = "com.mushiny.tools.task.executor")
public class TaskExecutorProperties {
private TaskPool pool;
class TaskPool {
private Integer corePoolSize;
private Boolean enabled;
private Integer maximumPoolSize;
@NestedConfigurationProperty
private TaskPoolConfig pool;
private Long keepAliveTime;
private TimeUnit timeUnit;
public Boolean getEnabled() {
return enabled;
}
public void setEnabled(Boolean enabled) {
this.enabled = enabled;
}
public TaskPoolConfig getPool() {
return pool;
}
public void setPool(TaskPoolConfig pool) {
this.pool = pool;
}
}
package com.mushiny.task.executor.autoconfig;
import java.util.concurrent.TimeUnit;
public class TaskPoolConfig {
private Integer corePoolSize;
private Integer maximumPoolSize;
private Long keepAliveTime;
private TimeUnit timeUnit;
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 Long getKeepAliveTime() {
return keepAliveTime;
}
public void setKeepAliveTime(Long keepAliveTime) {
this.keepAliveTime = keepAliveTime;
}
public TimeUnit getTimeUnit() {
return timeUnit;
}
public void setTimeUnit(TimeUnit timeUnit) {
this.timeUnit = timeUnit;
}
}
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.mushiny.task.executor.autoconfig.TaskExecutorAutoConfiguration
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment