Browse Source

1.增加线程池配置

dev_shibei_match
wxz 5 years ago
parent
commit
124ddd543d
  1. 12
      epmet-module/data-statistical/data-statistical-server/pom.xml
  2. 49
      epmet-module/data-statistical/data-statistical-server/src/main/java/com/epmet/config/AsyncConfig.java
  3. 48
      epmet-module/data-statistical/data-statistical-server/src/main/java/com/epmet/controller/DemoController.java
  4. 25
      epmet-module/data-statistical/data-statistical-server/src/main/java/com/epmet/properties/ThreadProperties.java
  5. 2
      epmet-module/data-statistical/data-statistical-server/src/main/java/com/epmet/service/StatsDemoService.java
  6. 28
      epmet-module/data-statistical/data-statistical-server/src/main/java/com/epmet/service/impl/StatsDemoServiceImpl.java
  7. 12
      epmet-module/data-statistical/data-statistical-server/src/main/resources/bootstrap.yml

12
epmet-module/data-statistical/data-statistical-server/pom.xml

@ -132,6 +132,12 @@
<!--flyway migration 数据库迁移工具-->
<spring.flyway.enabled>false</spring.flyway.enabled>
<!--线程池配置-->
<thread.pool.core-pool-size>10</thread.pool.core-pool-size>
<thread.pool.max-pool-size>20</thread.pool.max-pool-size>
<thread.pool.queue-capacity>10</thread.pool.queue-capacity>
<thread.pool.keep-alive>30</thread.pool.keep-alive>
</properties>
</profile>
<profile>
@ -179,6 +185,12 @@
<!--flyway migration 数据库迁移工具-->
<spring.flyway.enabled>true</spring.flyway.enabled>
<!--线程池配置-->
<thread.pool.core-pool-size>10</thread.pool.core-pool-size>
<thread.pool.max-pool-size>30</thread.pool.max-pool-size>
<thread.pool.queue-capacity>10</thread.pool.queue-capacity>
<thread.pool.keep-alive>30</thread.pool.keep-alive>
</properties>
</profile>
</profiles>

49
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();
}
}

48
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<Boolean> future1 = executorService.submit(() -> demoService.testThreadPool());
Future<Boolean> future2 = executorService.submit(() -> demoService.testThreadPool());
Future<Boolean> 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();
}
}

25
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() {
}
}
}

2
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();
}

28
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();
}
}
}

12
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@
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@
Loading…
Cancel
Save