我在应用程序开始时将一个键插入到ThreadContext映射中,如下所示,
protected void doFilterWrapped(ContentCachingRequestWrapper request,
ContentCachingResponseWrapper response, FilterChain filterChain)
throws ServletException, IOException {
// some code...
ThreadContext.put(Constants.REQUEST_ID, requestID);
ThreadContext.put(requestID + Constants.HASH + "retryCount", "-1");
// some more code...
}现在,在其他类中,我试图像这样更新键requestID + Constants.HASH + "retryCount"的值,
String key = ThreadContext.get(Constants.REQUEST_ID) + Constants.HASH + "retryCount";
if (ThreadContext.containsKey(key)) {
ThreadContext.put(key, String.valueOf(Integer.valueOf(ThreadContext.get(key)) + 1));
} else {
ThreadContext.put(key, "-1");
}
System.out.println("\n\n " + ThreadContext.get(key) + " \n\n");但是它只工作了一次,之后它无法找到key,即ThreadContext.containsKey(key)是false。
有人能解释一下是什么问题吗。
发布于 2022-08-01 16:38:53
是否尝试过设置“标题上下文可继承系统”属性?在应用程序启动时,添加以下一行:
System.setProperty("isThreadContextMapInheritable", "true");启用此属性后,子线程将继承线程上下文。
https://stackoverflow.com/questions/71497444
复制相似问题