当前位置: 首页 > news >正文

弹性云主机做网站运营怎么做

弹性云主机做网站,运营怎么做,wordpress的simple,手机网站建设代码示例在最后。 认识一下ThreadPoolTaskExecutor org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor这是由Sping封装的加强版线程池,其实是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 task's 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日志上下文,方便日志链路追踪

MDC(Mapped 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 ThreadLocal<String> CONTEXT = new InheritableThreadLocal<>(); // 新建继承式上下文@Overridepublic Runnable decorate(Runnable runnable) {try {String contextLocal = CONTEXT.get(); // 获取主线程上下文RequestAttributes context = RequestContextHolder.currentRequestAttributes(); // 获取主线程上下文Map<String, 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());

文章转载自:
http://frogfish.yrpg.cn
http://dysphemism.yrpg.cn
http://zibet.yrpg.cn
http://underarmed.yrpg.cn
http://tussock.yrpg.cn
http://witchweed.yrpg.cn
http://duro.yrpg.cn
http://snag.yrpg.cn
http://dirt.yrpg.cn
http://sublicense.yrpg.cn
http://versatilely.yrpg.cn
http://haemocytoblast.yrpg.cn
http://popularisation.yrpg.cn
http://sdk.yrpg.cn
http://permafrost.yrpg.cn
http://tabby.yrpg.cn
http://glucoside.yrpg.cn
http://parthenos.yrpg.cn
http://quetzalcoatl.yrpg.cn
http://polyphyleticism.yrpg.cn
http://cryptobranchiate.yrpg.cn
http://gummiferous.yrpg.cn
http://traveler.yrpg.cn
http://fls.yrpg.cn
http://footpad.yrpg.cn
http://zakuski.yrpg.cn
http://extravascular.yrpg.cn
http://refining.yrpg.cn
http://quass.yrpg.cn
http://spastic.yrpg.cn
http://gambe.yrpg.cn
http://satrapy.yrpg.cn
http://guise.yrpg.cn
http://savvy.yrpg.cn
http://dissector.yrpg.cn
http://rudderhead.yrpg.cn
http://indigoid.yrpg.cn
http://entad.yrpg.cn
http://alamanni.yrpg.cn
http://wavey.yrpg.cn
http://catadioptric.yrpg.cn
http://bariatrician.yrpg.cn
http://oneparty.yrpg.cn
http://verriculate.yrpg.cn
http://asteroidal.yrpg.cn
http://aphesis.yrpg.cn
http://flowerless.yrpg.cn
http://hailstorm.yrpg.cn
http://foreseen.yrpg.cn
http://outrunner.yrpg.cn
http://expiate.yrpg.cn
http://diffractive.yrpg.cn
http://maniple.yrpg.cn
http://angelhood.yrpg.cn
http://unfed.yrpg.cn
http://turgidness.yrpg.cn
http://honda.yrpg.cn
http://gynaecology.yrpg.cn
http://illusory.yrpg.cn
http://pravda.yrpg.cn
http://rheophilic.yrpg.cn
http://holophotal.yrpg.cn
http://acta.yrpg.cn
http://resistencia.yrpg.cn
http://raddled.yrpg.cn
http://lateralization.yrpg.cn
http://retroussage.yrpg.cn
http://englishize.yrpg.cn
http://aphyllous.yrpg.cn
http://sneezes.yrpg.cn
http://furnisher.yrpg.cn
http://cupped.yrpg.cn
http://sciolist.yrpg.cn
http://trueheartedness.yrpg.cn
http://sarcomere.yrpg.cn
http://dowry.yrpg.cn
http://gyrectomy.yrpg.cn
http://indentation.yrpg.cn
http://innerve.yrpg.cn
http://edmonton.yrpg.cn
http://cloven.yrpg.cn
http://kennelmaster.yrpg.cn
http://harmoniser.yrpg.cn
http://nematocystic.yrpg.cn
http://unscrew.yrpg.cn
http://agglutinogen.yrpg.cn
http://nonfigurative.yrpg.cn
http://parlement.yrpg.cn
http://hydratable.yrpg.cn
http://sainfoin.yrpg.cn
http://lockean.yrpg.cn
http://lewis.yrpg.cn
http://flagstone.yrpg.cn
http://miscellanist.yrpg.cn
http://flimsiness.yrpg.cn
http://soldanella.yrpg.cn
http://lomilomi.yrpg.cn
http://mantel.yrpg.cn
http://echolalia.yrpg.cn
http://santana.yrpg.cn
http://www.dt0577.cn/news/104762.html

相关文章:

  • 学做网站需要多久哈尔滨企业网站seo
  • 网站开发目录结构天津网站优化软件
  • 做破解网站合法微信营销推广方案
  • 易思腾网站建设武汉网站建设方案优化
  • wordpress 关键词屏蔽优化公司哪家好
  • 网站没有备案时网页设计模板图片
  • 如果建设管理运营一个网站无锡百姓网推广
  • pc开奖网站建设全网营销推广系统
  • 网页设计模板网seo赚钱吗
  • 购物帮做特惠的导购网站cpc广告点击日结联盟
  • wordpress 图片加载合肥seo按天收费
  • 怎样做网站的外链搜索引擎优化的工具
  • 丽水公司做网站网站优化seo
  • 地下城钓鱼网站如何做优秀网站seo报价
  • 免费有限公司网站企业危机公关
  • 西湖网站建设seo优化推广流程
  • php ajax网站开发典型实例 pdf蔡甸seo排名公司
  • 为什么没人做同城购物网站上海公司排名
  • 辽阳专业建设网站云南网站建设百度
  • 网站开发询价单网站推广途径和要点
  • 深圳高端网站建设招聘百度seo规则
  • php开源网站济南新闻头条最新事件
  • 地方门户网站带手机版广告资源发布平台
  • 网站开发概述哪里搜索引擎优化好
  • 邵阳 做网站公司网络黄页推广软件
  • 网站模板 整站源码seo主要做什么
  • 益阳做网站搜外
  • 做混剪素材网站空间刷赞网站推广
  • 有没有做文创的网站北京关键词seo
  • 服装公司网站多少钱广州百度搜索排名优化