Merge pull request #6 from beycheng/main

修复线程池任务异常日志无法获取问题
This commit is contained in:
zongzibinbin
2023-05-31 20:52:04 +08:00
committed by GitHub
4 changed files with 64 additions and 5 deletions

View File

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

View File

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

View File

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

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