我们正在使用@Async进行多线程处理。直到每个多线程方法都可以看到RequestContextHolder.getRequestAttributes()的值。
但是当我在方法内部调试时,我将请求属性作为NULL。
有什么想法吗?
答案 0 :(得分:2)
为解决此问题,我们创建了一个//#include "stdafx.h"
#include <iostream>
#include <conio.h>
using std::cout;
using std::cin;
int main()
{
int num1 = 0;
int num2 = 0;
cout << "Please enter two numbers (remember to put a space between them):\n";
cin >> num1 >> num2;
int num_limit = num1 * num2,LCM;
for (int i = 1; i <= num_limit; i++)
{
LCM = num1 * i;
// test for when multiples are equal
if (LCM%num2==0) break;
}
cout << "The LCM of " << num1 << " and " << num2 << " is: " << LCM;
_getch();// replace with getch(); if didn't work
}
对象,该对象已预先填充了当前的requestHolder,securityContextHolder等,以便所有产生的线程都能够像在主线程中运行一样执行
答案 1 :(得分:1)
默认情况下,ThreadLocal
变量用作请求属性的持有者。这意味着只有处理整个https请求的单个线程才能访问请求属性。相反,@Async
方法由来自单独线程池的线程处理,因此它们无法访问属性。
不过,还有一个InheritableThreadLocal
变量可以用作请求属性的持有者,而不是默认变量。您可以通过在threadContextInheritable
或true
中将DispatcherServlet
属性设置为RequestContextFilter
来启用它。
请查看RequestContextHolder
的实现以了解更多详细信息。