refactor: 重构动态线程池自动装配.

pull/161/head
chen.ma 3 years ago
parent 2d4c0e19c0
commit 05557ddbf9

@ -1,13 +1,17 @@
package io.dynamic.threadpool.starter.config; package io.dynamic.threadpool.starter.config;
import io.dynamic.threadpool.common.config.CommonConfiguration;
import io.dynamic.threadpool.starter.adapter.ThreadPoolConfigAdapter; import io.dynamic.threadpool.starter.adapter.ThreadPoolConfigAdapter;
import io.dynamic.threadpool.starter.controller.PoolRunStateController;
import io.dynamic.threadpool.starter.core.ConfigService; import io.dynamic.threadpool.starter.core.ConfigService;
import io.dynamic.threadpool.starter.core.ThreadPoolConfigService; import io.dynamic.threadpool.starter.core.ThreadPoolConfigService;
import io.dynamic.threadpool.starter.enable.DynamicThreadPoolMarkerConfiguration;
import io.dynamic.threadpool.starter.listener.ThreadPoolRunListener; import io.dynamic.threadpool.starter.listener.ThreadPoolRunListener;
import io.dynamic.threadpool.starter.operation.ThreadPoolOperation; import io.dynamic.threadpool.starter.operation.ThreadPoolOperation;
import lombok.AllArgsConstructor; import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.boot.autoconfigure.ImportAutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
@ -22,7 +26,8 @@ import org.springframework.context.annotation.Configuration;
@Configuration @Configuration
@AllArgsConstructor @AllArgsConstructor
@EnableConfigurationProperties(DynamicThreadPoolProperties.class) @EnableConfigurationProperties(DynamicThreadPoolProperties.class)
@ConditionalOnProperty(prefix = DynamicThreadPoolProperties.PREFIX, name = "enabled", havingValue = "true", matchIfMissing = true) @ConditionalOnBean(DynamicThreadPoolMarkerConfiguration.Marker.class)
@ImportAutoConfiguration({OkHttpClientConfig.class, CommonConfiguration.class})
public class DynamicThreadPoolAutoConfiguration { public class DynamicThreadPoolAutoConfiguration {
private final DynamicThreadPoolProperties properties; private final DynamicThreadPoolProperties properties;
@ -46,4 +51,10 @@ public class DynamicThreadPoolAutoConfiguration {
public ThreadPoolOperation threadPoolOperation() { public ThreadPoolOperation threadPoolOperation() {
return new ThreadPoolOperation(properties); return new ThreadPoolOperation(properties);
} }
@Bean
public PoolRunStateController poolRunStateController() {
return new PoolRunStateController();
}
} }

@ -34,11 +34,6 @@ public class DynamicThreadPoolProperties {
*/ */
private String itemId; private String itemId;
/**
* 线
*/
private boolean enabled;
/** /**
* banner * banner
*/ */

@ -5,7 +5,6 @@ import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import okhttp3.OkHttpClient; import okhttp3.OkHttpClient;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import javax.net.ssl.SSLContext; import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSocketFactory; import javax.net.ssl.SSLSocketFactory;
@ -20,7 +19,6 @@ import java.util.concurrent.TimeUnit;
* @date 2021/6/10 13:28 * @date 2021/6/10 13:28
*/ */
@Slf4j @Slf4j
@Configuration
public class OkHttpClientConfig { public class OkHttpClientConfig {
/** /**

@ -4,7 +4,6 @@ import io.dynamic.threadpool.common.model.PoolRunStateInfo;
import io.dynamic.threadpool.common.web.base.Result; import io.dynamic.threadpool.common.web.base.Result;
import io.dynamic.threadpool.common.web.base.Results; import io.dynamic.threadpool.common.web.base.Results;
import io.dynamic.threadpool.starter.handler.ThreadPoolRunStateHandler; import io.dynamic.threadpool.starter.handler.ThreadPoolRunStateHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
@ -18,12 +17,9 @@ import org.springframework.web.bind.annotation.RestController;
@RestController @RestController
public class PoolRunStateController { public class PoolRunStateController {
@Autowired
private ThreadPoolRunStateHandler threadPoolRunStateHandler;
@GetMapping("/run/state/{tpId}") @GetMapping("/run/state/{tpId}")
public Result<PoolRunStateInfo> getPoolRunState(@PathVariable("tpId") String tpId) { public Result<PoolRunStateInfo> getPoolRunState(@PathVariable("tpId") String tpId) {
PoolRunStateInfo poolRunState = threadPoolRunStateHandler.getPoolRunState(tpId); PoolRunStateInfo poolRunState = ThreadPoolRunStateHandler.getPoolRunState(tpId);
return Results.success(poolRunState); return Results.success(poolRunState);
} }
} }

@ -0,0 +1,23 @@
package io.dynamic.threadpool.starter.enable;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* 线
*
* @author chen.ma
* @date 2021/7/8 23:30
*/
@Configuration(proxyBeanMethods = false)
public class DynamicThreadPoolMarkerConfiguration {
@Bean
public Marker dynamicThreadPoolMarkerBean() {
return new Marker();
}
public class Marker {
}
}

@ -0,0 +1,19 @@
package io.dynamic.threadpool.starter.enable;
import org.springframework.context.annotation.Import;
import java.lang.annotation.*;
/**
* Annotation to activate Dynamic ThreadPool related configuration.
*
* @author chen.ma
* @date 2021/7/8 23:28
*/
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Import(DynamicThreadPoolMarkerConfiguration.class)
public @interface EnableDynamicThreadPool {
}

@ -4,7 +4,6 @@ import io.dynamic.threadpool.common.model.PoolRunStateInfo;
import io.dynamic.threadpool.starter.core.GlobalThreadPoolManage; import io.dynamic.threadpool.starter.core.GlobalThreadPoolManage;
import io.dynamic.threadpool.starter.wrap.CustomThreadPoolExecutor; import io.dynamic.threadpool.starter.wrap.CustomThreadPoolExecutor;
import io.dynamic.threadpool.starter.wrap.DynamicThreadPoolWrap; import io.dynamic.threadpool.starter.wrap.DynamicThreadPoolWrap;
import org.springframework.stereotype.Component;
import java.net.InetAddress; import java.net.InetAddress;
import java.net.UnknownHostException; import java.net.UnknownHostException;
@ -17,7 +16,6 @@ import java.util.concurrent.ThreadPoolExecutor;
* @author chen.ma * @author chen.ma
* @date 2021/7/7 19:37 * @date 2021/7/7 19:37
*/ */
@Component
public class ThreadPoolRunStateHandler { public class ThreadPoolRunStateHandler {
private static InetAddress addr; private static InetAddress addr;
@ -31,7 +29,7 @@ public class ThreadPoolRunStateHandler {
} }
} }
public PoolRunStateInfo getPoolRunState(String tpId) { public static PoolRunStateInfo getPoolRunState(String tpId) {
DynamicThreadPoolWrap executorService = GlobalThreadPoolManage.getExecutorService(tpId); DynamicThreadPoolWrap executorService = GlobalThreadPoolManage.getExecutorService(tpId);
ThreadPoolExecutor pool = executorService.getPool(); ThreadPoolExecutor pool = executorService.getPool();

@ -30,7 +30,7 @@ public class DynamicThreadPoolWrap {
* @param threadPoolId * @param threadPoolId
*/ */
public DynamicThreadPoolWrap(String threadPoolId) { public DynamicThreadPoolWrap(String threadPoolId) {
this(threadPoolId, null); this(threadPoolId, CommonThreadPool.getInstance(threadPoolId));
} }
/** /**

@ -6,12 +6,6 @@
"defaultValue": "localhost:6691", "defaultValue": "localhost:6691",
"description": "dynamic thread-pool server address." "description": "dynamic thread-pool server address."
}, },
{
"name": "spring.dynamic.thread-pool.enabled",
"type": "java.lang.Boolean",
"defaultValue": false,
"description": "dynamic thread-pool enabled."
},
{ {
"name": "spring.dynamic.thread-pool.namespace", "name": "spring.dynamic.thread-pool.namespace",
"type": "java.lang.String", "type": "java.lang.String",

@ -1,3 +1,2 @@
org.springframework.boot.autoconfigure.EnableAutoConfiguration=io.dynamic.threadpool.common.config.CommonConfiguration, \ org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
io.dynamic.threadpool.starter.config.OkHttpClientConfig,\
io.dynamic.threadpool.starter.config.DynamicThreadPoolAutoConfiguration io.dynamic.threadpool.starter.config.DynamicThreadPoolAutoConfiguration

@ -1,9 +1,11 @@
package io.dynamic.threadpool.example; package io.dynamic.threadpool.example;
import io.dynamic.threadpool.starter.enable.EnableDynamicThreadPool;
import org.springframework.boot.SpringApplication; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication(scanBasePackages = "io.dynamic.threadpool") @SpringBootApplication
@EnableDynamicThreadPool
public class ExampleApplication { public class ExampleApplication {
public static void main(String[] args) { public static void main(String[] args) {

Loading…
Cancel
Save