diff --git a/mallchat-common/src/main/java/com/abin/mallchat/common/common/config/ThreadPoolConfig.java b/mallchat-common/src/main/java/com/abin/mallchat/common/common/config/ThreadPoolConfig.java index 1fee693..2ff719a 100644 --- a/mallchat-common/src/main/java/com/abin/mallchat/common/common/config/ThreadPoolConfig.java +++ b/mallchat-common/src/main/java/com/abin/mallchat/common/common/config/ThreadPoolConfig.java @@ -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; } + + } diff --git a/mallchat-common/src/main/java/com/abin/mallchat/common/common/factory/MyThreadFactory.java b/mallchat-common/src/main/java/com/abin/mallchat/common/common/factory/MyThreadFactory.java new file mode 100644 index 0000000..6dd9871 --- /dev/null +++ b/mallchat-common/src/main/java/com/abin/mallchat/common/common/factory/MyThreadFactory.java @@ -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; + } +} diff --git a/mallchat-common/src/main/java/com/abin/mallchat/common/common/handler/GlobalUncaughtExceptionHandler.java b/mallchat-common/src/main/java/com/abin/mallchat/common/common/handler/GlobalUncaughtExceptionHandler.java index 79ad561..12dde06 100644 --- a/mallchat-common/src/main/java/com/abin/mallchat/common/common/handler/GlobalUncaughtExceptionHandler.java +++ b/mallchat-common/src/main/java/com/abin/mallchat/common/common/handler/GlobalUncaughtExceptionHandler.java @@ -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); } } diff --git a/mallchat-common/src/main/java/com/abin/mallchat/common/common/handler/MyUncaughtExceptionHandler.java b/mallchat-common/src/main/java/com/abin/mallchat/common/common/handler/MyUncaughtExceptionHandler.java new file mode 100644 index 0000000..6348ca8 --- /dev/null +++ b/mallchat-common/src/main/java/com/abin/mallchat/common/common/handler/MyUncaughtExceptionHandler.java @@ -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(); + } + +}