Browse Source

增加线程池配置

将发送消息逻辑改造成异步的
dev
wxz 5 years ago
parent
commit
a2680974d8
  1. 24
      epmet-module/epmet-message/epmet-message-server/pom.xml
  2. 49
      epmet-module/epmet-message/epmet-message-server/src/main/java/com/epmet/config/AsyncConfig.java
  3. 8
      epmet-module/epmet-message/epmet-message-server/src/main/java/com/epmet/controller/WxmpMessageController.java
  4. 25
      epmet-module/epmet-message/epmet-message-server/src/main/java/com/epmet/properties/ThreadProperties.java
  5. 10
      epmet-module/epmet-message/epmet-message-server/src/main/resources/bootstrap.yml

24
epmet-module/epmet-message/epmet-message-server/pom.xml

@ -165,6 +165,12 @@
<nacos.ip/>
<spring.flyway.enabled>false</spring.flyway.enabled>
<!--线程池配置-->
<thread.pool.core-pool-size>5</thread.pool.core-pool-size>
<thread.pool.max-pool-size>8</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>
@ -197,6 +203,12 @@
<nacos.ip/>
<spring.flyway.enabled>false</spring.flyway.enabled>
<!--线程池配置-->
<thread.pool.core-pool-size>5</thread.pool.core-pool-size>
<thread.pool.max-pool-size>8</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>
@ -229,6 +241,12 @@
<nacos.ip/>
<spring.flyway.enabled>true</spring.flyway.enabled>
<!--线程池配置-->
<thread.pool.core-pool-size>5</thread.pool.core-pool-size>
<thread.pool.max-pool-size>8</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>
@ -258,6 +276,12 @@
<nacos.ip/>
<spring.flyway.enabled>true</spring.flyway.enabled>
<!--线程池配置-->
<thread.pool.core-pool-size>5</thread.pool.core-pool-size>
<thread.pool.max-pool-size>8</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/epmet-message/epmet-message-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("epmet-message-");
// 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();
}
}

8
epmet-module/epmet-message/epmet-message-server/src/main/java/com/epmet/controller/WxmpMessageController.java

@ -30,6 +30,7 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.util.concurrent.ExecutorService;
/**
@ -48,6 +49,9 @@ public class WxmpMessageController {
@Autowired
private LoginUserUtil loginUserUtil;
@Autowired
private ExecutorService executorService;
/**
* @Description 保存系统自身的弹框授权信息
* @return com.epmet.commons.tools.utils.Result
@ -74,7 +78,9 @@ public class WxmpMessageController {
for (WxSubscribeMessageFormDTO wxSubscribeMessageFormDTO : msgList) {
ValidatorUtils.validateEntity(wxSubscribeMessageFormDTO);
}
wxmpMessageService.sendWxSubscribeMessage(msgList);
executorService.execute(() -> {
wxmpMessageService.sendWxSubscribeMessage(msgList);
});
return new Result();
}

25
epmet-module/epmet-message/epmet-message-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() {
}
}
}

10
epmet-module/epmet-message/epmet-message-server/src/main/resources/bootstrap.yml

@ -118,4 +118,12 @@ ribbon:
#pageHelper分页插件
pagehelper:
helper-dialect: mysql
reasonable: false #分页合理化配置,例如输入页码为-1,则自动转化为最小页码1
reasonable: false #分页合理化配置,例如输入页码为-1,则自动转化为最小页码1
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