修复线程池错误日志问题

This commit is contained in:
风雨行
2023-05-30 23:52:35 +08:00
parent e50fde3ed6
commit 1decddb6b1
4 changed files with 49 additions and 9 deletions

View File

@@ -2,6 +2,7 @@ package com.abin.mallchat.common.common.config;
import cn.hutool.core.thread.NamedThreadFactory;
import cn.hutool.core.thread.ThreadFactoryBuilder;
import com.abin.mallchat.common.common.factory.MyThreadFactory;
import com.abin.mallchat.common.common.handler.GlobalUncaughtExceptionHandler;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@@ -45,10 +46,7 @@ public class ThreadPoolConfig implements AsyncConfigurer {
executor.setQueueCapacity(200);
executor.setThreadNamePrefix("mallchat-executor-");
executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());//满了调用线程执行,认为重要任务
executor.getThreadPoolExecutor().setThreadFactory(
new ThreadFactoryBuilder()
.setUncaughtExceptionHandler(new GlobalUncaughtExceptionHandler("mallchat-executor-")).build()
);
executor.setThreadFactory(new MyThreadFactory(executor.getThreadNamePrefix()));
executor.initialize();
return executor;
}
@@ -61,11 +59,10 @@ public class ThreadPoolConfig implements AsyncConfigurer {
executor.setQueueCapacity(1000);//支持同时推送1000人
executor.setThreadNamePrefix("websocket-executor-");
executor.setRejectedExecutionHandler(new ThreadPoolExecutor.DiscardPolicy());//满了直接丢弃,默认为不重要消息推送
executor.getThreadPoolExecutor().setThreadFactory(
new ThreadFactoryBuilder()
.setUncaughtExceptionHandler(new GlobalUncaughtExceptionHandler("websocket-executor-")).build()
);
executor.setThreadFactory(new MyThreadFactory(executor.getThreadNamePrefix()));
executor.initialize();
return executor;
}
}

View File

@@ -0,0 +1,24 @@
package com.abin.mallchat.common.common.factory;
import com.abin.mallchat.common.common.handler.MyUncaughtExceptionHandler;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.concurrent.CustomizableThreadFactory;
import java.util.concurrent.ThreadFactory;
@Slf4j
public class MyThreadFactory implements ThreadFactory {
private String name;
public MyThreadFactory(String name){
this.name=name;
}
public MyThreadFactory(){}
@Override
public Thread newThread(Runnable r) {
Thread thread = new Thread(r);
thread.setUncaughtExceptionHandler(new MyUncaughtExceptionHandler(name));
return thread;
}
}

View File

@@ -11,7 +11,7 @@ public class GlobalUncaughtExceptionHandler implements Thread.UncaughtException
}
@Override
public void uncaughtException(Thread t, Throwable e) {
log.error("current thread ",t.getName()," is error[{}]",e);
log.error("current thread name is",t.getName()," is error[{}]",e);
}
}

View File

@@ -0,0 +1,19 @@
package com.abin.mallchat.common.common.handler;
import lombok.AllArgsConstructor;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;
@Slf4j
@AllArgsConstructor
@NoArgsConstructor
public class MyUncaughtExceptionHandler implements Thread.UncaughtExceptionHandler {
private String name;
@Override
public void uncaughtException(Thread t, Throwable e) {
log.error("线程池名称:[{}],错误信息如下:",name);
e.printStackTrace();
}
}