فهرست منبع

refactor: 线程池配置从启动类抽离到配置类

weijianghai 2 سال پیش
والد
کامیت
d0feaaf96d

+ 8 - 19
src/main/java/com/nokia/domainb/DomainBApplication.java

@@ -3,30 +3,19 @@ package com.nokia.domainb;
 import lombok.extern.slf4j.Slf4j;
 import org.springframework.boot.SpringApplication;
 import org.springframework.boot.autoconfigure.SpringBootApplication;
-import org.springframework.context.annotation.Bean;
-import org.springframework.scheduling.concurrent.CustomizableThreadFactory;
+import org.springframework.core.env.Environment;
 
-import java.util.concurrent.ArrayBlockingQueue;
-import java.util.concurrent.ThreadPoolExecutor;
-import java.util.concurrent.TimeUnit;
+import java.net.InetAddress;
+import java.net.UnknownHostException;
 
 @Slf4j
 @SpringBootApplication
 public class DomainBApplication {
 
-    public static void main(String[] args) {
-        SpringApplication.run(DomainBApplication.class, args);
-    }
-
-    /**
-     * 欠费接口整合线程池配置
-     */
-    @Bean
-    public ThreadPoolExecutor qryowefeeExecutor() {
-        ThreadPoolExecutor executor = new ThreadPoolExecutor(100, 200, 0L, TimeUnit.MILLISECONDS,
-                new ArrayBlockingQueue<>(10000), new CustomizableThreadFactory("qryowefee-"));
-        executor.prestartAllCoreThreads();
-        log.info("qryowefee poolSize: {}", executor.getPoolSize());
-        return executor;
+    public static void main(String[] args) throws UnknownHostException {
+        SpringApplication app = new SpringApplication(DomainBApplication.class);
+        Environment env = app.run(args).getEnvironment();
+        log.info("╭( ′• o •′ )╭☞ run on http://{}:{}", InetAddress.getLocalHost().getHostAddress(),
+                env.getProperty("server.port"));
     }
 }

+ 26 - 0
src/main/java/com/nokia/domainb/config/ThreadPoolConfig.java

@@ -0,0 +1,26 @@
+package com.nokia.domainb.config;
+
+import lombok.extern.slf4j.Slf4j;
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Configuration;
+import org.springframework.scheduling.concurrent.CustomizableThreadFactory;
+
+import java.util.concurrent.ArrayBlockingQueue;
+import java.util.concurrent.ThreadPoolExecutor;
+import java.util.concurrent.TimeUnit;
+
+@Slf4j
+@Configuration
+public class ThreadPoolConfig {
+    /**
+     * 欠费接口整合线程池配置
+     */
+    @Bean
+    public ThreadPoolExecutor qryowefeeExecutor() {
+        ThreadPoolExecutor executor = new ThreadPoolExecutor(100, 200, 0L, TimeUnit.MILLISECONDS,
+                new ArrayBlockingQueue<>(10000), new CustomizableThreadFactory("qryowefee-"));
+        executor.prestartAllCoreThreads();
+        log.info("qryowefee poolSize: {}", executor.getPoolSize());
+        return executor;
+    }
+}