多线程中,ServletUtils获取request对象为空

1.问题描述:
在使用ServletUtils工具类中的静态方法获取request对象时,在controller层可以获取到,在service实现类中无法获取。

LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());

2.原因分析:
controller层开启了多线程。(开启多线程之前可以获取到,开启之后在子线程中无法获取)原因是request 信息是存储在 ThreadLocal 中的,所以子线程根本无法获取到主线程的 request 信息。

//创建线程对象
private void createThead(String key) throws Exception {
    Thread thread = new Thread(new getKey(key));
    thread.start();
}

//开启线程
private class getKey implements Runnable{
   private Key key;
   public getKey(Key key) {
       this.key = key;
   }
   //Runnable接口中的抽象方法
   public void run() {
       try {
           getKey(key);
       } catch (Exception e) {
           System.out.println(e);
       }
   }
}

3.解决方法:
在新开子线程之前增加两行代码,将RequestAttributes对象设置为子线程共享。

//创建线程对象
private void createThead(String key) throws Exception {
	//将RequestAttributes对象设置为子线程共享
	ServletRequestAttributes sra = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
	RequestContextHolder.setRequestAttributes(sra, true);
    Thread thread = new Thread(new getKey(key));
    thread.start();
}
  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在异步线程获取到的request为空,这是因为异步线程和原始的请求线程不在同一个线程,异步线程无法直接访问原始的请求线程的request对象。解决这个问题的方法有两种: 1. 使用Callable接口:可以将异步任务封装成一个Callable对象,并将原始的request对象作为参数传递给Callable对象。在异步任务,可以通过Future对象获取到Callable对象的返回值,从而获取request对象。 代码示例: ``` @RestController public class MyController { @Autowired private AsyncService asyncService; @RequestMapping("/test") public String test(HttpServletRequest request) throws Exception { Callable<String> task = () -> { // 异步任务获取request对象 HttpServletRequest asyncRequest = AsyncRequestContextHolder.getRequest(); // 处理业务逻辑 return "success"; }; Future<String> future = asyncService.execute(task); String result = future.get(); return result; } } @Service public class AsyncService { @Autowired private AsyncTaskExecutor taskExecutor; public <T> Future<T> execute(Callable<T> task) { AsyncRequestContextCallable<T> callable = new AsyncRequestContextCallable<>(task); return taskExecutor.submit(callable); } } public class AsyncRequestContextCallable<T> implements Callable<T> { private final Callable<T> task; private final HttpServletRequest request; public AsyncRequestContextCallable(Callable<T> task) { this.task = task; this.request = AsyncRequestContextHolder.getRequest(); } @Override public T call() throws Exception { AsyncRequestContextHolder.setRequest(request); try { return task.call(); } finally { AsyncRequestContextHolder.resetRequest(); } } } public class AsyncRequestContextHolder { private static final ThreadLocal<HttpServletRequest> requestHolder = new ThreadLocal<>(); public static void setRequest(HttpServletRequest request) { requestHolder.set(request); } public static HttpServletRequest getRequest() { return requestHolder.get(); } public static void resetRequest() { requestHolder.remove(); } } ``` 2. 使用ServletRequestAttributes:可以使用Spring提供的ServletRequestAttributes类来获取request对象。这个类是一个请求属性的存储器,可以在任何线程存储和获取请求属性。在异步任务,可以通过ServletRequestAttributes来获取到原始的request对象。 代码示例: ``` @RestController public class MyController { @Autowired private AsyncService asyncService; @RequestMapping("/test") public String test(HttpServletRequest request) throws Exception { Callable<String> task = () -> { // 异步任务获取request对象 ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes(); HttpServletRequest asyncRequest = attributes.getRequest(); // 处理业务逻辑 return "success"; }; Future<String> future = asyncService.execute(task); String result = future.get(); return result; } } @Service public class AsyncService { @Autowired private AsyncTaskExecutor taskExecutor; public <T> Future<T> execute(Callable<T> task) { return taskExecutor.submit(() -> { RequestAttributes attributes = RequestContextHolder.getRequestAttributes(); try { RequestContextHolder.setRequestAttributes(attributes, true); return task.call(); } finally { RequestContextHolder.resetRequestAttributes(); } }); } } ``` 以上两种方法都可以解决异步线程获取request对象为空的问题,具体选择哪种方法取决于具体的业务需求和开发习惯。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值