RequestAttributes requestAttributes = RequestContextHolder.currentRequestAttributes();
ThreadPoolUtils.getThreadPool().execute(new Runnable() {
@Override
public void run() {
RequestContextHolder.setRequestAttributes(requestAttributes);
CheckResult checkResult = checkService.execute(checkUrl, entity.getPsnCode());}
线程执行之前绑定了,然后执行完execute之后header头就丢失了,有人知道怎么回事吗
RequestContextHolder 是把数据放到ThreadLocal里面的实现吗
Runnable啊
代码:
@Slf4j
public class TaskRunnable implements Runnable {
private String taskName;
private Runnable runnable;
private Map<String, String> context;
private RequestAttributes requestAttributes;
public TaskRunnable(String name, Runnable runnable) {
this.taskName = name;
this.runnable = runnable;
this.context = MDC.getCopyOfContextMap();
}
public TaskRunnable(String name, Runnable runnable, RequestAttributes requestAttributes) {
this.taskName = name;
this.runnable = runnable;
this.context = MDC.getCopyOfContextMap();
this.requestAttributes = requestAttributes;
}
@Override
public void run() {
try {
if (this.context != null) {
MDC.setContextMap(context);
}
if (requestAttributes != null) {
RequestContextHolder.setRequestAttributes(requestAttributes);
}
runnable.run();
} finally {
if (requestAttributes != null) {
RequestContextHolder.resetRequestAttributes();
}
MDCUtils.clear();
}
}
}
callable
代码:
@Slf4j
public class TaskCallable<T> implements Callable {
private String taskName;
private Callable<T> callable;
private Map<String, String> context;
private RequestAttributes requestAttributes;
public TaskCallable(String name, Callable<T> callable) {
this.taskName = name;
this.callable = callable;
this.context = MDC.getCopyOfContextMap();
}
public TaskCallable(String name, Callable<T> callable, RequestAttributes requestAttributes) {
this.taskName = name;
this.callable = callable;
this.context = MDC.getCopyOfContextMap();
this.requestAttributes = requestAttributes;
}
@Override
public T call() throws Exception {
try {
if (this.context != null) {
MDC.setContextMap(context);
}
if (requestAttributes != null) {
RequestContextHolder.setRequestAttributes(requestAttributes);
}
return callable.call();
} finally {
if (requestAttributes != null) {
RequestContextHolder.resetRequestAttributes();
}
MDCUtils.clear();
}
}
}
创建线程池
代码:
public class TaskThreadPoolExecutor extends ThreadPoolExecutor {
private static final String DEFAULT_TASK_NAME = "Default Task";
public TaskThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit,
BlockingQueue<Runnable> workQueue) {
super(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue);
}
public TaskThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit,
BlockingQueue<Runnable> workQueue, ThreadFactory threadFactory) {
super(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue, threadFactory);
}
public TaskThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit,
BlockingQueue<Runnable> workQueue, RejectedExecutionHandler handler) {
super(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue, handler);
}
public TaskThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit,
BlockingQueue<Runnable> workQueue, ThreadFactory threadFactory,
RejectedExecutionHandler handler) {
super(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue, threadFactory, handler);
}
@Override
public void execute(Runnable command) {
if (command instanceof TaskRunnable) {
super.execute(command);
} else {
RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
TaskRunnable taskCommand = new TaskRunnable(DEFAULT_TASK_NAME, command, requestAttributes);
super.execute(taskCommand);
}
}
}
使用方法:
private static final ExecutorService executorService = new TaskThreadPoolExecutor(20, 200,
200L, TimeUnit.SECONDS,
new LinkedBlockingQueue<Runnable>(5000), new ThreadFactory() {
final ThreadFactory defaultFactory = Executors.defaultThreadFactory();
@Override
public Thread newThread(Runnable r) {
Thread thread = defaultFactory.newThread(r);
thread.setName("ShopTireService-" + thread.getName());
return thread;
}
}, new ThreadPoolExecutor.AbortPolicy());
如果我的回答对你有帮助,请采纳谢谢
线程共享了变量空间,这是最基本的线程冲突问题。