From e50fde3ed687db63ad5462638ba5b3ff9529f6ce Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=A3=8E=E9=9B=A8=E8=A1=8C?= <2283550574@qq.com> Date: Tue, 30 May 2023 20:55:19 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E7=BA=BF=E7=A8=8B=E6=B1=A0?= =?UTF-8?q?=E4=BB=BB=E5=8A=A1=E5=BC=82=E5=B8=B8=E6=97=A5=E5=BF=97=E6=97=A0?= =?UTF-8?q?=E6=B3=95=E8=8E=B7=E5=8F=96=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../common/common/config/ThreadPoolConfig.java | 12 ++++++++++++ .../handler/GlobalUncaughtExceptionHandler.java | 17 +++++++++++++++++ .../common/user/service/impl/IpServiceImpl.java | 17 ++++++++++++----- 3 files changed, 41 insertions(+), 5 deletions(-) create mode 100644 mallchat-common/src/main/java/com/abin/mallchat/common/common/handler/GlobalUncaughtExceptionHandler.java 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 9650c0a..1fee693 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 @@ -1,5 +1,8 @@ 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.handler.GlobalUncaughtExceptionHandler; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Primary; @@ -8,6 +11,7 @@ import org.springframework.scheduling.annotation.EnableAsync; import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor; import java.util.concurrent.Executor; +import java.util.concurrent.ThreadFactory; import java.util.concurrent.ThreadPoolExecutor; /** @@ -41,6 +45,10 @@ 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.initialize(); return executor; } @@ -53,6 +61,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.initialize(); return executor; } 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 new file mode 100644 index 0000000..79ad561 --- /dev/null +++ b/mallchat-common/src/main/java/com/abin/mallchat/common/common/handler/GlobalUncaughtExceptionHandler.java @@ -0,0 +1,17 @@ +package com.abin.mallchat.common.common.handler; + +import lombok.extern.slf4j.Slf4j; + +@Slf4j +public class GlobalUncaughtExceptionHandler implements Thread.UncaughtExceptionHandler { + private String name; + public GlobalUncaughtExceptionHandler(){} + public GlobalUncaughtExceptionHandler(String name){ + this.name=name; + } + @Override + public void uncaughtException(Thread t, Throwable e) { + log.error("current thread ",t.getName()," is error[{}]",e); + } + +} diff --git a/mallchat-common/src/main/java/com/abin/mallchat/common/user/service/impl/IpServiceImpl.java b/mallchat-common/src/main/java/com/abin/mallchat/common/user/service/impl/IpServiceImpl.java index c443958..877abb1 100644 --- a/mallchat-common/src/main/java/com/abin/mallchat/common/user/service/impl/IpServiceImpl.java +++ b/mallchat-common/src/main/java/com/abin/mallchat/common/user/service/impl/IpServiceImpl.java @@ -1,16 +1,19 @@ package com.abin.mallchat.common.user.service.impl; import cn.hutool.core.lang.TypeReference; + import cn.hutool.core.thread.NamedThreadFactory; import cn.hutool.core.util.StrUtil; import cn.hutool.http.HttpUtil; import cn.hutool.json.JSONUtil; +import com.abin.mallchat.common.common.handler.GlobalUncaughtExceptionHandler; import com.abin.mallchat.common.user.dao.UserDao; import com.abin.mallchat.common.user.domain.dto.IpResult; import com.abin.mallchat.common.user.domain.entity.IpDetail; import com.abin.mallchat.common.user.domain.entity.IpInfo; import com.abin.mallchat.common.user.domain.entity.User; import com.abin.mallchat.common.user.service.IpService; +import jodd.util.concurrent.ThreadFactoryBuilder; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.DisposableBean; import org.springframework.beans.factory.annotation.Autowired; @@ -18,10 +21,7 @@ import org.springframework.stereotype.Service; import java.util.Date; import java.util.Objects; -import java.util.concurrent.ExecutorService; -import java.util.concurrent.LinkedBlockingQueue; -import java.util.concurrent.ThreadPoolExecutor; -import java.util.concurrent.TimeUnit; +import java.util.concurrent.*; /** * Description: ip @@ -33,11 +33,16 @@ import java.util.concurrent.TimeUnit; public class IpServiceImpl implements IpService, DisposableBean { private static ExecutorService executor = new ThreadPoolExecutor(1, 1, 0L, TimeUnit.MILLISECONDS, - new LinkedBlockingQueue(500), new NamedThreadFactory("refresh-ipDetail", false)); + new LinkedBlockingQueue(500), + new NamedThreadFactory("refresh-ipDetail", (ThreadGroup)null,false, + new GlobalUncaughtExceptionHandler("refresh-ipDetail"))); @Autowired private UserDao userDao; + + + @Override public void refreshIpDetailAsync(Long uid) { executor.execute(() -> { @@ -116,5 +121,7 @@ public class IpServiceImpl implements IpService, DisposableBean { log.error("Timed out while waiting for executor [{}] to terminate", executor); } } + + } }