常用的网站开发技术有哪几种,河北建设广州分公司网站,魅影传说网页游戏开服表,广告设计公司宣传文案代码示例在最后。
认识一下ThreadPoolTaskExecutor
org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor这是由Sping封装的加强版线程池#xff0c;其实是Spring使用装饰者模式对ThreadPoolExecutor进一步优化。 它不仅拥有ThreadPoolExecutor所有的核心参数…代码示例在最后。
认识一下ThreadPoolTaskExecutor
org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor这是由Sping封装的加强版线程池其实是Spring使用装饰者模式对ThreadPoolExecutor进一步优化。 它不仅拥有ThreadPoolExecutor所有的核心参数还有额外的参数可以设置。 我的这个文章有具体的使用案例和ThreadPoolTaskExecutor每个参数的解释【这样用线程池才优雅-企业级线程池示例】该文第三节我给出了我目前在使用的ThreadPoolTaskExecutor完整配置欢迎参考指正。但是文章的最后留了一个尾巴线程池配置中注释掉了这一行//executor.setTaskDecorator(new ContextCopyingDecorator()); 本文将主要介绍ThreadPoolTaskExecutor的setTaskDecorator方法。
解释ThreadPoolTaskExecutor.setTaskDecorator
先看一下setTaskDecorator方法的官方注释
Specify a custom TaskDecorator to be applied to any Runnable about to be executed.
Note that such a decorator is not necessarily being applied to the user-supplied Runnable/Callable but rather to the actual execution callback (which may be a wrapper around the user-supplied task).
The primary use case is to set some execution context around the tasks invocation, or to provide some monitoring/statistics for task execution.
NOTE: Exception handling in TaskDecorator implementations is limited to plain Runnable execution via execute calls. In case of #submit calls, the exposed Runnable will be a FutureTask which does not propagate any exceptions; you might have to cast it and call Future#get to evaluate exceptions. See the ThreadPoolExecutor#afterExecute javadoc for an example of how to access exceptions in such a Future case.
Since:
4.3简单翻译一下就是为线程池的待执行任务Runnable指定一个装饰器主要用途是在任务调用前后设置一些执行上下文或者为任务执行提供一些监控/统计信息。
实际上就是采用装饰者模式给工作任务做了一个切面让我们可以给任务设置一些上下文参数或者监控。
setTaskDecorator的应用场景
1. 解决InheritableThreadLocal与线程池共用的问题
在文章【解决InheritableThreadLocal与线程池共用的问题】的最后我提到ThreadPoolTaskExecutor.setTaskDecorator可以优雅的解决InheritableThreadLocal与线程池共用的问题。 回顾一下问题详细由于线程池中的工作线程是可以复用的所以父线程将任务交给线程池处理的时候并不会调用new Thread()导致父线程中的 InheritableThreadLocal 变量的值不能被线程池中的工作线程继承上面提到的文章有详细解释。为了解决这个问题我们可以自定义TaskDecorator在工作线程执行之前将父线程的InheritableThreadLocal参数赋值给子线程这样子线程间接继承父线程InheritableThreadLocal。代码在最后
2. 传递MDC日志上下文方便日志链路追踪
MDCMapped Diagnostic Context映射调试上下文是 log4j 和 logback 提供的一种方便在多线程条件下记录日志的功能也可以说是一种轻量级的日志跟踪工具。很多的时候项目会在MDC中设置一个会话唯一的trace_id用于追踪整个会话的日志链路。
MDC.put(trace_id, UUID.randomUUID().toString());但是这个trace_id同样也会在线程池的工作线程中丢失。所以我们可以通过自定义TaskDecorator解决这个问题。
3. 传递Http请求上下文
跟上面的情况差不多不做叙述。
自定义TaskDecorator代码示例
public class ContextCopyingDecorator implements TaskDecorator {public static final ThreadLocalString CONTEXT new InheritableThreadLocal(); // 新建继承式上下文Overridepublic Runnable decorate(Runnable runnable) {try {String contextLocal CONTEXT.get(); // 获取主线程上下文RequestAttributes context RequestContextHolder.currentRequestAttributes(); // 获取主线程上下文MapString, String previous MDC.getCopyOfContextMap(); // 获取主线程上下文SecurityContext securityContext SecurityContextHolder.getContext(); // 获取主线程上下文return () - {try {ContextCopyingDecorator.CONTEXT.set(contextLocal); // 线程池继承主线程上下文RequestContextHolder.setRequestAttributes(context); // 子线程继承父线程的RequestAttributesMDC.setContextMap(previous); // 子线程继承父线程的MDCSecurityContextHolder.setContext(securityContext); // 子线程继承父线程的SecurityContextrunnable.run(); // 执行异步任务} finally { // 线程池任务执行结束则清理上下文ContextCopyingDecorator.CONTEXT.remove(); // 子线程执行结束后清理CONTEXTRequestContextHolder.resetRequestAttributes(); // 子线程执行结束后清理RequestContextHolderMDC.clear(); // 子线程执行结束后清理MDCSecurityContextHolder.clearContext(); // 子线程执行结束后清理SecurityContextHolder}};} catch (IllegalStateException e) {return runnable;}}
}这个自定义TaskDecorator可以通过ThreadPoolTaskExecutor.setTaskDecorator方法设置给你的线程池这时再使用该线程池的时候就可以解决以上提到的三个问题。
//填充装饰器用来设置线程的上下文
executor.setTaskDecorator(new ContextCopyingDecorator());