diff --git a/epmet-module/data-statistical/data-statistical-server/pom.xml b/epmet-module/data-statistical/data-statistical-server/pom.xml index f0d1c67ade..dc90aa7c1c 100644 --- a/epmet-module/data-statistical/data-statistical-server/pom.xml +++ b/epmet-module/data-statistical/data-statistical-server/pom.xml @@ -132,6 +132,12 @@ false + + + 10 + 20 + 10 + 30 @@ -179,6 +185,12 @@ true + + + 10 + 30 + 10 + 30 diff --git a/epmet-module/data-statistical/data-statistical-server/src/main/java/com/epmet/config/AsyncConfig.java b/epmet-module/data-statistical/data-statistical-server/src/main/java/com/epmet/config/AsyncConfig.java new file mode 100644 index 0000000000..ba50e40a30 --- /dev/null +++ b/epmet-module/data-statistical/data-statistical-server/src/main/java/com/epmet/config/AsyncConfig.java @@ -0,0 +1,49 @@ +package com.epmet.config; + +import com.epmet.properties.ThreadProperties; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.context.properties.EnableConfigurationProperties; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.scheduling.annotation.EnableAsync; +import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor; + +import java.util.concurrent.Executor; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.ThreadPoolExecutor; + +/** + * 线程池配置类 + */ +@Configuration +@EnableConfigurationProperties(ThreadProperties.class) +@EnableAsync +public class AsyncConfig { + + @Autowired + private ThreadProperties threadProperties; + + @Bean + public Executor executor() { + ThreadProperties.ThreadPoolProperties threadPoolProps = threadProperties.getThreadPool(); + + ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor(); + executor.setCorePoolSize(threadPoolProps.getCorePoolSize()); + executor.setMaxPoolSize(threadPoolProps.getMaxPoolSize()); + executor.setQueueCapacity(threadPoolProps.getQueueCapacity()); + executor.setThreadNamePrefix("data-stats-"); + // rejection-policy:当pool已经达到max size的时候,如何处理新任务 + // CALLER_RUNS:不在新线程中执行任务,而是由调用者所在的线程来执行 + executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy()); //对拒绝task的处理策略 + executor.setKeepAliveSeconds(threadPoolProps.getKeepAlive()); + executor.initialize(); + return executor; + } + + @Bean + public ExecutorService executorService() { + ThreadPoolTaskExecutor executor = (ThreadPoolTaskExecutor) executor(); + return executor.getThreadPoolExecutor(); + } + +} diff --git a/epmet-module/data-statistical/data-statistical-server/src/main/java/com/epmet/controller/DemoController.java b/epmet-module/data-statistical/data-statistical-server/src/main/java/com/epmet/controller/DemoController.java index 08b6880295..ae5394b285 100644 --- a/epmet-module/data-statistical/data-statistical-server/src/main/java/com/epmet/controller/DemoController.java +++ b/epmet-module/data-statistical/data-statistical-server/src/main/java/com/epmet/controller/DemoController.java @@ -6,6 +6,12 @@ import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; +import java.time.LocalDateTime; +import java.util.concurrent.Callable; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Future; + @RequestMapping("demo") @RestController public class DemoController { @@ -13,6 +19,9 @@ public class DemoController { @Autowired private StatsDemoService demoService; + @Autowired + private ExecutorService executorService; + @GetMapping("testlist") public void testList() { demoService.testList(); @@ -23,5 +32,44 @@ public class DemoController { demoService.testTx(); } + @GetMapping("testthreadpool") + public void testThreadPool() { + System.out.println(LocalDateTime.now().getSecond());; + System.out.println("----------->>"); + Future future1 = executorService.submit(() -> demoService.testThreadPool()); + + Future future2 = executorService.submit(() -> demoService.testThreadPool()); + + Future future3 = executorService.submit(() -> demoService.testThreadPool()); + + try { + + Boolean o1 = future1.get(); + System.out.println(LocalDateTime.now().getSecond()); + Boolean o2 = future2.get(); + System.out.println(LocalDateTime.now().getSecond()); + + Boolean o3 = future3.get(); + System.out.println(LocalDateTime.now().getSecond()); + + System.out.println("<<-----------"); + } catch (InterruptedException e) { + e.printStackTrace(); + } catch (ExecutionException e) { + e.printStackTrace(); + } + } + + @GetMapping("testthreadpoolasyncs") + public void testThreadPoolAsync() { + System.out.println(LocalDateTime.now().getSecond()); + System.out.println("----------->>"); + + demoService.testThreadPoolAsync(); + demoService.testThreadPoolAsync(); + demoService.testThreadPoolAsync(); + demoService.testThreadPoolAsync(); + demoService.testThreadPoolAsync(); + } } diff --git a/epmet-module/data-statistical/data-statistical-server/src/main/java/com/epmet/properties/ThreadProperties.java b/epmet-module/data-statistical/data-statistical-server/src/main/java/com/epmet/properties/ThreadProperties.java new file mode 100644 index 0000000000..aaec7cb719 --- /dev/null +++ b/epmet-module/data-statistical/data-statistical-server/src/main/java/com/epmet/properties/ThreadProperties.java @@ -0,0 +1,25 @@ +package com.epmet.properties; + +import lombok.Data; +import org.springframework.boot.context.properties.ConfigurationProperties; + +/** + * 线程池属性类 + */ +@ConfigurationProperties(prefix = "thread") +@Data +public class ThreadProperties { + + private ThreadPoolProperties threadPool; + + @Data + public static class ThreadPoolProperties { + private int corePoolSize; + private int maxPoolSize; + private int queueCapacity; + private int keepAlive; + + public ThreadPoolProperties() { + } + } +} diff --git a/epmet-module/data-statistical/data-statistical-server/src/main/java/com/epmet/service/StatsDemoService.java b/epmet-module/data-statistical/data-statistical-server/src/main/java/com/epmet/service/StatsDemoService.java index 1952ade8bf..5d3e2ce24b 100644 --- a/epmet-module/data-statistical/data-statistical-server/src/main/java/com/epmet/service/StatsDemoService.java +++ b/epmet-module/data-statistical/data-statistical-server/src/main/java/com/epmet/service/StatsDemoService.java @@ -4,5 +4,7 @@ public interface StatsDemoService { void testList(); void testTx(); + Boolean testThreadPool(); + void testThreadPoolAsync(); } diff --git a/epmet-module/data-statistical/data-statistical-server/src/main/java/com/epmet/service/impl/StatsDemoServiceImpl.java b/epmet-module/data-statistical/data-statistical-server/src/main/java/com/epmet/service/impl/StatsDemoServiceImpl.java index 19cd01f578..59c2907d10 100644 --- a/epmet-module/data-statistical/data-statistical-server/src/main/java/com/epmet/service/impl/StatsDemoServiceImpl.java +++ b/epmet-module/data-statistical/data-statistical-server/src/main/java/com/epmet/service/impl/StatsDemoServiceImpl.java @@ -7,8 +7,10 @@ import com.epmet.service.StatsDemoService; import com.epmet.service.org.DemoGovOrgService; import com.epmet.service.stats.DemoDataStatsService; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.scheduling.annotation.Async; import org.springframework.stereotype.Service; +import java.time.LocalDateTime; import java.util.List; /** @@ -37,4 +39,30 @@ public class StatsDemoServiceImpl implements StatsDemoService { public void testTx() { demoDataStatsService.testTx(); } + + @Override + public Boolean testThreadPool() { + try { + Thread.sleep(2000l); + } catch (InterruptedException e) { + System.err.println("睡眠发生异常"); + e.printStackTrace(); + } + return true; + } + + /** + * 推荐 + */ + @Async //注意此处注解 + @Override + public void testThreadPoolAsync() { + try { + Thread.sleep(2000l); + System.out.println(LocalDateTime.now().getSecond()); + } catch (InterruptedException e) { + System.err.println("睡眠发生异常"); + e.printStackTrace(); + } + } } diff --git a/epmet-module/data-statistical/data-statistical-server/src/main/resources/bootstrap.yml b/epmet-module/data-statistical/data-statistical-server/src/main/resources/bootstrap.yml index e9e53f6acb..838cf424d4 100644 --- a/epmet-module/data-statistical/data-statistical-server/src/main/resources/bootstrap.yml +++ b/epmet-module/data-statistical/data-statistical-server/src/main/resources/bootstrap.yml @@ -142,4 +142,14 @@ dynamic: driver-class-name: com.mysql.cj.jdbc.Driver url: @datasource.druid.issue.url@ username: @datasource.druid.issue.username@ - password: @datasource.druid.issue.password@ \ No newline at end of file + password: @datasource.druid.issue.password@ + +thread: + # 线程池配置 + threadPool: + corePoolSize: @thread.pool.core-pool-size@ + maxPoolSize: @thread.pool.max-pool-size@ + queueCapacity: @thread.pool.queue-capacity@ + keepAlive: @thread.pool.keep-alive@ + + \ No newline at end of file