mirror of
https://github.com/zongzibinbin/MallChat.git
synced 2026-03-14 06:03:42 +08:00
修复线程池错误日志问题
This commit is contained in:
@@ -2,6 +2,7 @@ package com.abin.mallchat.common.common.config;
|
|||||||
|
|
||||||
import cn.hutool.core.thread.NamedThreadFactory;
|
import cn.hutool.core.thread.NamedThreadFactory;
|
||||||
import cn.hutool.core.thread.ThreadFactoryBuilder;
|
import cn.hutool.core.thread.ThreadFactoryBuilder;
|
||||||
|
import com.abin.mallchat.common.common.factory.MyThreadFactory;
|
||||||
import com.abin.mallchat.common.common.handler.GlobalUncaughtExceptionHandler;
|
import com.abin.mallchat.common.common.handler.GlobalUncaughtExceptionHandler;
|
||||||
import org.springframework.context.annotation.Bean;
|
import org.springframework.context.annotation.Bean;
|
||||||
import org.springframework.context.annotation.Configuration;
|
import org.springframework.context.annotation.Configuration;
|
||||||
@@ -45,10 +46,7 @@ public class ThreadPoolConfig implements AsyncConfigurer {
|
|||||||
executor.setQueueCapacity(200);
|
executor.setQueueCapacity(200);
|
||||||
executor.setThreadNamePrefix("mallchat-executor-");
|
executor.setThreadNamePrefix("mallchat-executor-");
|
||||||
executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());//满了调用线程执行,认为重要任务
|
executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());//满了调用线程执行,认为重要任务
|
||||||
executor.getThreadPoolExecutor().setThreadFactory(
|
executor.setThreadFactory(new MyThreadFactory(executor.getThreadNamePrefix()));
|
||||||
new ThreadFactoryBuilder()
|
|
||||||
.setUncaughtExceptionHandler(new GlobalUncaughtExceptionHandler("mallchat-executor-")).build()
|
|
||||||
);
|
|
||||||
executor.initialize();
|
executor.initialize();
|
||||||
return executor;
|
return executor;
|
||||||
}
|
}
|
||||||
@@ -61,11 +59,10 @@ public class ThreadPoolConfig implements AsyncConfigurer {
|
|||||||
executor.setQueueCapacity(1000);//支持同时推送1000人
|
executor.setQueueCapacity(1000);//支持同时推送1000人
|
||||||
executor.setThreadNamePrefix("websocket-executor-");
|
executor.setThreadNamePrefix("websocket-executor-");
|
||||||
executor.setRejectedExecutionHandler(new ThreadPoolExecutor.DiscardPolicy());//满了直接丢弃,默认为不重要消息推送
|
executor.setRejectedExecutionHandler(new ThreadPoolExecutor.DiscardPolicy());//满了直接丢弃,默认为不重要消息推送
|
||||||
executor.getThreadPoolExecutor().setThreadFactory(
|
executor.setThreadFactory(new MyThreadFactory(executor.getThreadNamePrefix()));
|
||||||
new ThreadFactoryBuilder()
|
|
||||||
.setUncaughtExceptionHandler(new GlobalUncaughtExceptionHandler("websocket-executor-")).build()
|
|
||||||
);
|
|
||||||
executor.initialize();
|
executor.initialize();
|
||||||
return executor;
|
return executor;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -11,7 +11,7 @@ public class GlobalUncaughtExceptionHandler implements Thread.UncaughtException
|
|||||||
}
|
}
|
||||||
@Override
|
@Override
|
||||||
public void uncaughtException(Thread t, Throwable e) {
|
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);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user