问题描述

在一个项目中需要实现一个定时上报的任务,由于是分条件上报所以就采用了异步的上报方式,用了自定义的线程池运行上报线程。定时任务执行的时候报了No thread-bound request found: " + "Are you referring to request attributes outside of an actual web request, " + "or processing a request outside of the originally receiving thread? " + "If you are actually operating within a web request and still receive this message, " + "your code is probably running outside of DispatcherServlet: " + "In this case, use RequestContextListener or RequestContextFilter to expose the current request.

问题排查

通过debug的方式找出了问题的所在,在自定义的线程池中为了在子线程中复用父线程中的属性,获取了父线程的requestAttributes。自定义线程池中的线程提交逻辑
跟进到RequestContextHolder类里
在这里插入图片描述
发现异常信息就是定义在这里,抛出异常的原因就是没有获取到对应的requestAttributes。
由于这不是一个web请求,而是由service层发起的一个调用,而导致这个问题。

解决

1、如果无需复用父线程的属性,可以用默认的线程池执行一步线程。
2、定义一个类实现RequestAttributes接口,实现父接口里的方法,可以都返回空

public class NonRequestAttributes implements RequestAttributes {

  @Override
  public Object getAttribute(String name, int scope) {
    return null;
  }

  @Override
  public void setAttribute(String name, Object value, int scope) {

  }

  @Override
  public void removeAttribute(String name, int scope) {

  }

  @Override
  public String[] getAttributeNames(int scope) {
    return new String[0];
  }

  @Override
  public void registerDestructionCallback(String name, Runnable callback, int scope) {

  }

  @Override
  public Object resolveReference(String key) {
    return null;
  }

  @Override
  public String getSessionId() {
    return null;
  }

  @Override
  public Object getSessionMutex() {
    return null;
  }
}

在获取requestAttributes时捕获抛出的异常即可

RequestAttributes requestAttributes;
    try {
      requestAttributes = RequestContextHolder.currentRequestAttributes();
    } catch (IllegalStateException e) {
      requestAttributes = new NonRequestAttributes();
    }
    RequestContextHolder.setRequestAttributes(requestAttributes, true);
    RequestAttributes ra = RequestContextHolder.currentRequestAttributes();

总结

调用controller层接口的时候可以通过请求上下文获取请求信息(RequestContextHolder.currentRequestAttributes()),但是如果是通过定时任务的方式发起的方法调用则无法获取请求上下文的requestAttributes信息。

Logo

旨在为数千万中国开发者提供一个无缝且高效的云端环境,以支持学习、使用和贡献开源项目。

更多推荐