修复线程池任务异常日志无法获取问题

This commit is contained in:
风雨行
2023-05-30 20:55:19 +08:00
parent cac5214197
commit e50fde3ed6
3 changed files with 41 additions and 5 deletions

View File

@@ -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;
}

View File

@@ -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);
}
}

View File

@@ -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<Runnable>(500), new NamedThreadFactory("refresh-ipDetail", false));
new LinkedBlockingQueue<Runnable>(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);
}
}
}
}