zoukankan      html  css  js  c++  java
  • 使用多线程时,传递 request 对象丢失

    1.原因描述

    我们在工作中遇到耗时的一些操作时我们会使用多线程或者mq来解决以便提高程序的响应速度。但是使用多线程时遇到一个问题,我单独开一个线程去进行其他逻辑处理时,在发送消息之前(未开启多线程时)我们是可以获取到 request 信息的,但是在新开的线程中确是无法获取到 request 信息(request is  null)。

    2.代码演示

    主线程代码

    子线程代码

     

     3.错误描述

     由上图可知子线程无法获取到 request 信息,直接报了 java.lang.NullPointerException。

    原因:request 信息是存储在 ThreadLocal 中的,所以子线程根本无法获取到主线程的  request 信息。

    /**
    * 返回当前线程上下文request信息
    *
    * @return
    */
    public static HttpServletRequest getCurrentRequestInfo() {
    ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
    return servletRequestAttributes.getRequest();
    }

    源码如下:

    private static final ThreadLocal<RequestAttributes> requestAttributesHolder =
    new NamedThreadLocal<RequestAttributes>("Request attributes");
    /**
    * Return the RequestAttributes currently bound to the thread.
    * @return the RequestAttributes currently bound to the thread,
    * or {@code null} if none bound
    */
    public static RequestAttributes getRequestAttributes() {
    RequestAttributes attributes = requestAttributesHolder.get();
    if (attributes == null) {
    attributes = inheritableRequestAttributesHolder.get();
    }
    return attributes;
    }

    4.解决方案

    在新开子线程之前 新增二行代码,如下,

    // 将RequestAttributes对象设置为子线程共享
    ServletRequestAttributes sra = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
    RequestContextHolder.setRequestAttributes(sra, true);

    子线程代码

    子线程代码

     到此问题完美解决,如果想要深入了解原因  需要看源码。

    获取当前线程上下文信息方法地址:https://www.cnblogs.com/ming-blogs/p/12754837.html

  • 相关阅读:
    第一章嵌入式系统基础1.5嵌入式系统性能评价
    第一章嵌入式系统基础1.4
    1.3嵌入式系统的硬件基础
    学习第二天
    经典的删除字符串中指定的字符
    字符串的两种表示
    股票数据定向爬虫
    淘宝商品比价定向爬虫
    中国大学排名定向爬虫
    python小练习 批量修改文件名
  • 原文地址:https://www.cnblogs.com/ming-blogs/p/12755261.html
Copyright © 2011-2022 走看看