Commit 1d628344 authored by duanruiming's avatar duanruiming

[update] 优化线程池配置,日志打印

parent 77c1b559
...@@ -36,8 +36,8 @@ public class RealTimeCarTask { ...@@ -36,8 +36,8 @@ public class RealTimeCarTask {
@Resource @Resource
private ConsumerHandler consumerHandler; private ConsumerHandler consumerHandler;
@Autowired @Autowired
@Qualifier(value = "threadPoolExecutor") @Qualifier(value = "commonThreadPoolExecutor")
private ThreadPoolTaskExecutor threadPoolExecutor; private ThreadPoolTaskExecutor commonThreadPoolExecutor;
private static final List<TempPojo> tempList = new ArrayList<>(7); private static final List<TempPojo> tempList = new ArrayList<>(7);
private static final AtomicInteger atomicInteger = new AtomicInteger(0); private static final AtomicInteger atomicInteger = new AtomicInteger(0);
......
...@@ -60,7 +60,7 @@ mybatis-plus: ...@@ -60,7 +60,7 @@ mybatis-plus:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
#线程池配置 #线程池配置
threadPoolConfig: threadPoolConfig:
threadPoolName: threadPoolExecutor threadPoolName: ${spring.application.name}-threadPool-thread-%d
coreSize: 8 coreSize: 8
maxSize: 16 maxSize: 16
queueCapacity: 200 queueCapacity: 200
......
package net.wanji.com.config;
import com.google.common.util.concurrent.ThreadFactoryBuilder;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadPoolExecutor;
/**
* @author duanruiming
* @date 2023/02/14 10:17
*/
@Configuration
public class ThreadPoolConfig {
@Value("${threadPoolConfig.threadPoolName}")
private String threadPoolName;
@Value("${threadPoolConfig.coreSize}")
private int coreSize;
@Value("${threadPoolConfig.maxSize}")
private int maxSize;
@Value("${threadPoolConfig.queueCapacity}")
private int queueCapacity;
@Value("${threadPoolConfig.keepAliveTime}")
private int keepAliveTime;
@Value("${threadPoolConfig.allowCoreTimeOut}")
private boolean allowCoreTimeOut;
@Bean("threadPoolExecutor")
public ThreadPoolTaskExecutor threadPoolExecutor() {
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
ThreadFactory build = new ThreadFactoryBuilder().setNameFormat(threadPoolName).build();
executor.setThreadFactory(build);
executor.setCorePoolSize(coreSize);
executor.setMaxPoolSize(maxSize);
executor.setQueueCapacity(queueCapacity);
executor.setKeepAliveSeconds(keepAliveTime);
executor.setAllowCoreThreadTimeOut(allowCoreTimeOut);
executor.setRejectedExecutionHandler(new ThreadPoolExecutor.DiscardOldestPolicy());
executor.initialize();
return executor;
}
}
...@@ -30,8 +30,8 @@ import java.util.concurrent.TimeUnit; ...@@ -30,8 +30,8 @@ import java.util.concurrent.TimeUnit;
@Component @Component
public class NettyClient { public class NettyClient {
private static ChannelFuture udpChannelFuture = null; private static ChannelFuture udpChannelFuture = null;
@Resource(name = "threadPoolExecutor") @Resource(name = "commonThreadPoolExecutor")
private ThreadPoolTaskExecutor threadPoolExecutor; private ThreadPoolTaskExecutor commonThreadPoolExecutor;
/** /**
* 建立连接 * 建立连接
...@@ -99,7 +99,7 @@ public class NettyClient { ...@@ -99,7 +99,7 @@ public class NettyClient {
public void checkWaitTimeout() { public void checkWaitTimeout() {
long now = LocalDateTime.now().toInstant(ZoneOffset.of("+8")).toEpochMilli(); long now = LocalDateTime.now().toInstant(ZoneOffset.of("+8")).toEpochMilli();
NettyMessageCache.NETTY_MESSAGE_RESULT_MAP.forEach((k, v) -> { NettyMessageCache.NETTY_MESSAGE_RESULT_MAP.forEach((k, v) -> {
threadPoolExecutor.execute(() -> { commonThreadPoolExecutor.execute(() -> {
if (now - v.getStartTime() > v.getWaitMillisecond()) { if (now - v.getStartTime() > v.getWaitMillisecond()) {
log.warn("wait {}ms [{}] timeout", v.getWaitMillisecond(), k); log.warn("wait {}ms [{}] timeout", v.getWaitMillisecond(), k);
v.getCountDownLatch().countDown(); v.getCountDownLatch().countDown();
......
...@@ -15,7 +15,6 @@ import net.wanji.com.pojo.netty.MessageResultPojo; ...@@ -15,7 +15,6 @@ import net.wanji.com.pojo.netty.MessageResultPojo;
import net.wanji.common.framework.spring.ServiceBeanContext; import net.wanji.common.framework.spring.ServiceBeanContext;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import java.net.InetSocketAddress; import java.net.InetSocketAddress;
...@@ -29,9 +28,6 @@ public class NettyServerHandler extends SimpleChannelInboundHandler<DatagramPack ...@@ -29,9 +28,6 @@ public class NettyServerHandler extends SimpleChannelInboundHandler<DatagramPack
@Autowired @Autowired
CrossInfoCache crossInfoCache; CrossInfoCache crossInfoCache;
@Autowired
ThreadPoolTaskExecutor threadPoolExecutor;
/** /**
* 读取消息 * 读取消息
......
...@@ -61,7 +61,7 @@ mybatis-plus: ...@@ -61,7 +61,7 @@ mybatis-plus:
#线程池配置 #线程池配置
threadPoolConfig: threadPoolConfig:
threadPoolName: ${spring.application.name}-threadPool threadPoolName: ${spring.application.name}-threadPool-thread-%d
coreSize: 8 coreSize: 8
maxSize: 16 maxSize: 16
queueCapacity: 200 queueCapacity: 200
......
<?xml version="1.0" encoding="UTF-8" ?>
<configuration>
<property name="encoding" value="UTF-8"/>
<property name="normal-pattern"
value="%date{yyyy-MM-dd HH:mm:ss.SSS} %-5level[%thread]%logger{56}.%method:%L -%msg%n"/>
<property name="LOG_PATH" value="./logs"/>
<!-- 配置打印DEBUG级别日志的环境. 多个使用逗号隔开. -->
<springProfile name="dev,test">
<!-- 如果需要,请自行开启spring或其他组件的debug级别 -->
<logger name="net.wanji.com" level="info"/>
</springProfile>
<!-- 配置打印INFO级别日志的环境 -->
<springProfile name="prod">
<logger name="net.wanji.com" level="info" />
</springProfile>
<appender name="consoleLog" class="ch.qos.logback.core.ConsoleAppender">
<layout class="ch.qos.logback.classic.PatternLayout">
<pattern>
${normal-pattern}
</pattern>
</layout>
</appender>
<appender name="fileInfoLog" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${LOG_PATH}/info.log</file>
<filter class="ch.qos.logback.classic.filter.LevelFilter">
<level>ERROR</level>
<onMatch>DENY</onMatch>
<onMismatch>ACCEPT</onMismatch>
</filter>
<encoder>
<pattern>${normal-pattern}</pattern>
<charset>${encoding}</charset>
</encoder>
<!--滚动策略-->
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!--路径-->
<fileNamePattern>${LOG_PATH}/info.%d.log</fileNamePattern>
<MaxHistory>30</MaxHistory>
</rollingPolicy>
</appender>
<appender name="fileErrorLog" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${LOG_PATH}/error.log</file>
<filter class="ch.qos.logback.classic.filter.ThresholdFilter">
<level>ERROR</level>
</filter>>
<encoder>
<pattern>${normal-pattern}</pattern>
<charset>${encoding}</charset>
</encoder>
<!--滚动策略-->
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!--路径-->
<fileNamePattern>${LOG_PATH}/error.%d.log</fileNamePattern>
<MaxHistory>30</MaxHistory>
</rollingPolicy>
</appender>
<root level="INFO">
<appender-ref ref="consoleLog"/>
<appender-ref ref="fileInfoLog"/>
<appender-ref ref="fileErrorLog"/>
</root>
</configuration>
\ No newline at end of file
package net.wanji.utc.config;
import com.google.common.util.concurrent.ThreadFactoryBuilder;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadPoolExecutor;
/**
* @author duanruiming
* @date 2023/02/14 10:17
*/
@Configuration
public class ThreadPoolConfig {
@Value("${threadPoolConfig.threadPoolName}")
private String threadPoolName;
@Value("${threadPoolConfig.coreSize}")
private int coreSize;
@Value("${threadPoolConfig.maxSize}")
private int maxSize;
@Value("${threadPoolConfig.queueCapacity}")
private int queueCapacity;
@Value("${threadPoolConfig.keepAliveTime}")
private int keepAliveTime;
@Value("${threadPoolConfig.allowCoreTimeOut}")
private boolean allowCoreTimeOut;
@Bean
public ThreadPoolTaskExecutor threadPoolExecutor() {
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
ThreadFactory build = new ThreadFactoryBuilder().setNameFormat(threadPoolName).build();
executor.setThreadFactory(build);
executor.setCorePoolSize(coreSize);
executor.setMaxPoolSize(maxSize);
executor.setQueueCapacity(queueCapacity);
executor.setKeepAliveSeconds(keepAliveTime);
executor.setAllowCoreThreadTimeOut(allowCoreTimeOut);
executor.setRejectedExecutionHandler(new ThreadPoolExecutor.DiscardOldestPolicy());
executor.initialize();
return executor;
}
}
...@@ -31,8 +31,8 @@ import java.util.concurrent.ConcurrentHashMap; ...@@ -31,8 +31,8 @@ import java.util.concurrent.ConcurrentHashMap;
public class SignalStatusTask { public class SignalStatusTask {
@Autowired @Autowired
SignalStatusService signalStatusService; SignalStatusService signalStatusService;
@Resource(name = "threadPoolExecutor") @Resource(name = "commonThreadPoolExecutor")
ThreadPoolTaskExecutor threadPoolExecutor; ThreadPoolTaskExecutor commonThreadPoolExecutor;
@Resource @Resource
private HkLightsStatusService hkLightsStatusService; private HkLightsStatusService hkLightsStatusService;
...@@ -49,12 +49,11 @@ public class SignalStatusTask { ...@@ -49,12 +49,11 @@ public class SignalStatusTask {
// 灯态,每秒一次 // 灯态,每秒一次
@Scheduled(fixedRate = 1 * 1000) @Scheduled(fixedRate = 1 * 1000)
public void lightStatus() { public void lightStatus() {
ConcurrentHashMap<String, RealTimeDataWebSocket> evenWarnWebSocketMap = RealTimeDataWebSocket.getEvenWarnWebSocketMap(); ConcurrentHashMap<String, RealTimeDataWebSocket> evenWarnWebSocketMap = RealTimeDataWebSocket.getEvenWarnWebSocketMap();
try { try {
if (!evenWarnWebSocketMap.isEmpty()) { if (!evenWarnWebSocketMap.isEmpty()) {
for (Map.Entry<String, RealTimeDataWebSocket> entry : evenWarnWebSocketMap.entrySet()) { for (Map.Entry<String, RealTimeDataWebSocket> entry : evenWarnWebSocketMap.entrySet()) {
threadPoolExecutor.execute(() -> { commonThreadPoolExecutor.execute(() -> {
String crossId = null; String crossId = null;
String crossIdStr = entry.getKey(); String crossIdStr = entry.getKey();
if (StringUtils.isNotBlank(crossIdStr)) { if (StringUtils.isNotBlank(crossIdStr)) {
......
...@@ -60,7 +60,7 @@ pagehelper: ...@@ -60,7 +60,7 @@ pagehelper:
#线程池配置 #线程池配置
threadPoolConfig: threadPoolConfig:
threadPoolName: threadPoolExecutor threadPoolName: ${spring.application.name}-threadPool-thread-%d
coreSize: 8 coreSize: 8
maxSize: 16 maxSize: 16
queueCapacity: 200 queueCapacity: 200
......
<?xml version="1.0" encoding="UTF-8" ?>
<configuration>
<property name="encoding" value="UTF-8"/>
<property name="normal-pattern"
value="%date{yyyy-MM-dd HH:mm:ss.SSS} %-5level[%thread]%logger{56}.%method:%L -%msg%n"/>
<property name="LOG_PATH" value="./logs"/>
<!-- 配置打印DEBUG级别日志的环境. 多个使用逗号隔开. -->
<springProfile name="dev,test">
<!-- 如果需要,请自行开启spring或其他组件的debug级别 -->
<logger name="net.wanji.utc" level="info"/>
</springProfile>
<!-- 配置打印INFO级别日志的环境 -->
<springProfile name="prod">
<logger name="net.wanji.utc" level="info" />
</springProfile>
<appender name="consoleLog" class="ch.qos.logback.core.ConsoleAppender">
<layout class="ch.qos.logback.classic.PatternLayout">
<pattern>
${normal-pattern}
</pattern>
</layout>
</appender>
<appender name="fileInfoLog" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${LOG_PATH}/info.log</file>
<filter class="ch.qos.logback.classic.filter.LevelFilter">
<level>ERROR</level>
<onMatch>DENY</onMatch>
<onMismatch>ACCEPT</onMismatch>
</filter>
<encoder>
<pattern>${normal-pattern}</pattern>
<charset>${encoding}</charset>
</encoder>
<!--滚动策略-->
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!--路径-->
<fileNamePattern>${LOG_PATH}/info.%d.log</fileNamePattern>
<MaxHistory>30</MaxHistory>
</rollingPolicy>
</appender>
<appender name="fileErrorLog" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${LOG_PATH}/error.log</file>
<filter class="ch.qos.logback.classic.filter.ThresholdFilter">
<level>ERROR</level>
</filter>>
<encoder>
<pattern>${normal-pattern}</pattern>
<charset>${encoding}</charset>
</encoder>
<!--滚动策略-->
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!--路径-->
<fileNamePattern>${LOG_PATH}/error.%d.log</fileNamePattern>
<MaxHistory>30</MaxHistory>
</rollingPolicy>
</appender>
<root level="INFO">
<appender-ref ref="consoleLog"/>
<appender-ref ref="fileInfoLog"/>
<appender-ref ref="fileErrorLog"/>
</root>
</configuration>
\ No newline at end of file
package net.wanji.web.config; package net.wanji.common.framework.config;
import com.google.common.util.concurrent.ThreadFactoryBuilder; import com.google.common.util.concurrent.ThreadFactoryBuilder;
import org.springframework.beans.factory.annotation.Value; import org.springframework.beans.factory.annotation.Value;
...@@ -28,7 +28,7 @@ public class ThreadPoolConfig { ...@@ -28,7 +28,7 @@ public class ThreadPoolConfig {
@Value("${threadPoolConfig.allowCoreTimeOut}") @Value("${threadPoolConfig.allowCoreTimeOut}")
private boolean allowCoreTimeOut; private boolean allowCoreTimeOut;
@Bean("threadPoolExecutor") @Bean("commonThreadPoolExecutor")
public ThreadPoolTaskExecutor threadPoolExecutor() { public ThreadPoolTaskExecutor threadPoolExecutor() {
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor(); ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
ThreadFactory build = new ThreadFactoryBuilder().setNameFormat(threadPoolName).build(); ThreadFactory build = new ThreadFactoryBuilder().setNameFormat(threadPoolName).build();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment