feat:通知异步发布
This commit is contained in:
@@ -302,7 +302,7 @@ public enum ErrorCodeEnum {
|
||||
CONFIG_NOT_EXIST(1610006,"配置不存在或被禁用,请确认!",null),
|
||||
MESSAGE_NOT_EXIST(1610007,"消息模板不存在或已被删除",null),
|
||||
MESSAGE_NOT_HANDLED(1610008,"当前消息无需处理,请确认消息处理类型!",null),
|
||||
|
||||
MESSAGE_PUBLISH(1610009,"您选择通知任务正在发布中,请稍后重试!",null),
|
||||
|
||||
NOT_FLAGSHIP_STORE(16100005,"非直营店,无法跳过缴费阶段!",null),
|
||||
NOT_FLAGSHIP_STORE_NOT_EXIST(16100006,"当前阶段加盟类型不能变更!",null),
|
||||
|
||||
@@ -0,0 +1,80 @@
|
||||
package com.cool.store.executor;
|
||||
|
||||
|
||||
import com.cool.store.utils.ThreadMdcUtil;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.slf4j.MDC;
|
||||
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
|
||||
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.Callable;
|
||||
import java.util.concurrent.Future;
|
||||
|
||||
|
||||
/**
|
||||
* @author zhangchenbiao
|
||||
* @FileName: MdcTaskExecutor
|
||||
* @Description:
|
||||
* @date 2021-11-02 21:00
|
||||
*/
|
||||
public class MdcTaskExecutor extends ThreadPoolTaskExecutor {
|
||||
private Logger log = LoggerFactory.getLogger(MdcTaskExecutor.class);
|
||||
|
||||
@Override
|
||||
public <T> Future<T> submit(Callable<T> task) {
|
||||
Map<String, String> context = MDC.getCopyOfContextMap();
|
||||
return super.submit(() -> {
|
||||
T result;
|
||||
if (context != null) {
|
||||
//将父线程的MDC内容传给子线程
|
||||
MDC.setContextMap(context);
|
||||
}
|
||||
//直接给子线程设置MDC
|
||||
ThreadMdcUtil.setTraceIdIfAbsent();
|
||||
try {
|
||||
//执行任务
|
||||
result = task.call();
|
||||
} finally {
|
||||
log.info("ThreadMonitor:{}info:ExecutedTasks->{},totalTask->{}, RunningTasks->{}, PendingTasks->{},corePoolSize-{},currentPoolSize->{},LargestPoolSize->{}",
|
||||
this.getThreadNamePrefix(),this.getThreadPoolExecutor().getCompletedTaskCount(),this.getThreadPoolExecutor().getTaskCount(),
|
||||
this.getActiveCount(),this.getThreadPoolExecutor().getQueue().size(),this.getCorePoolSize(),
|
||||
this.getPoolSize(),this.getThreadPoolExecutor().getLargestPoolSize());
|
||||
try {
|
||||
MDC.clear();
|
||||
} catch (Exception e) {
|
||||
log.warn("MDC clear exception", e);
|
||||
}
|
||||
}
|
||||
return result;
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public void execute(Runnable task) {
|
||||
log.info("mdc thread pool task executor execute");
|
||||
Map<String, String> context = MDC.getCopyOfContextMap();
|
||||
super.execute(() -> {
|
||||
if (context != null) {
|
||||
//将父线程的MDC内容传给子线程
|
||||
MDC.setContextMap(context);
|
||||
}
|
||||
//直接给子线程设置MDC
|
||||
ThreadMdcUtil.setTraceIdIfAbsent();
|
||||
try {
|
||||
//执行任务
|
||||
task.run();
|
||||
} finally {
|
||||
log.info("ThreadMonitor:{}info:ExecutedTasks->{},totalTask->{}, RunningTasks->{}, PendingTasks->{},corePoolSize-{},currentPoolSize->{},LargestPoolSize->{}",
|
||||
this.getThreadNamePrefix(),this.getThreadPoolExecutor().getCompletedTaskCount(),this.getThreadPoolExecutor().getTaskCount(),
|
||||
this.getActiveCount(),this.getThreadPoolExecutor().getQueue().size(),this.getCorePoolSize(),
|
||||
this.getPoolSize(),this.getThreadPoolExecutor().getLargestPoolSize());
|
||||
try {
|
||||
MDC.clear();
|
||||
} catch (Exception e) {
|
||||
log.warn("MDC clear exception", e);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
package com.cool.store.executor;
|
||||
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.core.task.TaskExecutor;
|
||||
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
|
||||
|
||||
import java.util.concurrent.ThreadPoolExecutor;
|
||||
|
||||
/**
|
||||
* @Author suzhuhong
|
||||
* @Date 2025/10/15 17:29
|
||||
* @Version 1.0
|
||||
*/
|
||||
@Configuration
|
||||
public class ThreadPoolTask {
|
||||
|
||||
@Bean
|
||||
public TaskExecutor noticeThreadPool() {
|
||||
int cores = Runtime.getRuntime().availableProcessors();
|
||||
|
||||
ThreadPoolTaskExecutor executor = new MdcTaskExecutor();
|
||||
// 核心线程数目
|
||||
executor.setCorePoolSize(cores*2);
|
||||
// 指定最大线程数
|
||||
executor.setMaxPoolSize(100);
|
||||
// 队列中最大的数目
|
||||
executor.setQueueCapacity(10000);
|
||||
// 线程名称前缀
|
||||
executor.setThreadNamePrefix("noticeThreadPool_");
|
||||
// 对拒绝task的处理策略
|
||||
executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
|
||||
// 线程空闲后的最大存活时间
|
||||
executor.setKeepAliveSeconds(60);
|
||||
// 加载
|
||||
executor.initialize();
|
||||
return executor;
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
package com.cool.store.utils;
|
||||
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.slf4j.MDC;
|
||||
|
||||
import java.util.UUID;
|
||||
|
||||
/**
|
||||
* @Author suzhuhong
|
||||
* @Date 2025/10/15 17:32
|
||||
* @Version 1.0
|
||||
*/
|
||||
public class MDCUtils {
|
||||
public MDCUtils() {
|
||||
}
|
||||
|
||||
public static void putIfAbsent(String key, String value) {
|
||||
String k = MDC.get(key);
|
||||
if (StringUtils.isBlank(value)) {
|
||||
value = UUID.randomUUID().toString().replace("-", "");
|
||||
}
|
||||
|
||||
if (StringUtils.isBlank(k)) {
|
||||
MDC.put(key, value);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public static void put(String key, String value) {
|
||||
MDC.put(key, value);
|
||||
}
|
||||
|
||||
public static void put(String key) {
|
||||
MDC.put(key, UUID.randomUUID().toString().replace("-", ""));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
package com.cool.store.utils;
|
||||
|
||||
import com.cool.store.constants.CommonConstants;
|
||||
import org.slf4j.MDC;
|
||||
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.Callable;
|
||||
|
||||
/**
|
||||
* 线程MDC包装类
|
||||
*
|
||||
* @author hetiantian
|
||||
* @version 1.0
|
||||
* @Date 2020/03/18 15:18
|
||||
*/
|
||||
public class ThreadMdcUtil {
|
||||
public static void setTraceIdIfAbsent() {
|
||||
MDCUtils.putIfAbsent(CommonConstants.REQUEST_ID, UUIDUtils.get32UUID());
|
||||
}
|
||||
|
||||
public static <T> Callable<T> wrap(final Callable<T> callable, final Map<String, String> context) {
|
||||
return () -> {
|
||||
if (context == null) {
|
||||
MDC.clear();
|
||||
} else {
|
||||
MDC.setContextMap(context);
|
||||
}
|
||||
setTraceIdIfAbsent();
|
||||
try {
|
||||
return callable.call();
|
||||
} finally {
|
||||
MDC.clear();
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
public static Runnable wrap(final Runnable runnable, final Map<String, String> context) {
|
||||
return new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
if (context == null) {
|
||||
MDC.clear();
|
||||
} else {
|
||||
MDC.setContextMap(context);
|
||||
}
|
||||
setTraceIdIfAbsent();
|
||||
try {
|
||||
runnable.run();
|
||||
} finally {
|
||||
MDC.clear();
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user