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..687f7f4 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,9 @@ 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; import org.springframework.context.annotation.Primary; @@ -8,6 +12,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 +46,7 @@ public class ThreadPoolConfig implements AsyncConfigurer { executor.setQueueCapacity(200); executor.setThreadNamePrefix("mallchat-executor-"); executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());//满了调用线程执行,认为重要任务 + executor.setThreadFactory(new MyThreadFactory(executor)); executor.initialize(); return executor; } @@ -53,7 +59,10 @@ public class ThreadPoolConfig implements AsyncConfigurer { executor.setQueueCapacity(1000);//支持同时推送1000人 executor.setThreadNamePrefix("websocket-executor-"); executor.setRejectedExecutionHandler(new ThreadPoolExecutor.DiscardPolicy());//满了直接丢弃,默认为不重要消息推送 + executor.setThreadFactory(new MyThreadFactory(executor)); 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..b3733eb --- /dev/null +++ b/mallchat-common/src/main/java/com/abin/mallchat/common/common/factory/MyThreadFactory.java @@ -0,0 +1,26 @@ +package com.abin.mallchat.common.common.factory; + +import com.abin.mallchat.common.common.handler.GlobalUncaughtExceptionHandler; +import lombok.AllArgsConstructor; +import lombok.NoArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.springframework.scheduling.concurrent.CustomizableThreadFactory; +import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor; + +import java.util.concurrent.ThreadFactory; + +@Slf4j +@AllArgsConstructor +public class MyThreadFactory implements ThreadFactory { + + private ThreadFactory factory; + + @Override + public Thread newThread(Runnable r) { + Thread thread =factory.newThread(r); + thread.setUncaughtExceptionHandler(new GlobalUncaughtExceptionHandler()); + thread.setDaemon(false); + thread.setPriority(5); + 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 new file mode 100644 index 0000000..79f4e24 --- /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.AllArgsConstructor; +import lombok.NoArgsConstructor; +import lombok.extern.slf4j.Slf4j; + +@Slf4j +public class GlobalUncaughtExceptionHandler implements Thread.UncaughtExceptionHandler { + + + @Override + public void uncaughtException(Thread t, Throwable e) { + log.error("{} task execute is error",t.getName()); + e.printStackTrace(); + } + +} 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); } } + + } }