当同时发出多个请求时,Log4J ThreadContext映射的值是否可以重叠?
我正在使用log4j threadcontext来跟踪Spring Boot应用程序。我通过实现HandlerInterceptor创建了一个拦截器,该操作人员使用threadContext.put(“ correlationId”,“ x')在threadContext映射中拦截请求,然后在threadContext映射中设置“ x”值。值“ x”是从请求标题中检索的。请求完成后,我使用ThreadContext.ClearMap()清除ThreadContext。要求是在日志中查看“相关性”,并与每个登录语句一起满足。
public class RequestHandlerInterceptor implements HandlerInterceptor {
@Override
public boolean preHandle(
final HttpServletRequest request,
final HttpServletResponse response,
final Object handler)
throws Exception {
ThreadContext.put(CORRELATION_ID, request.getHeader(CORRELATION_ID_HEADER));
return true;
}
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, @Nullable Exception ex) throws Exception {
ThreadContext.clearMap();
}}
现在,我的问题是,如果多个用户同时提出多个请求,将是什么行为。在完成旧请求之前,threadContext映射中的相关性ID值是否会被每个新请求替换吗?如果是这样,那么正确的实现将是什么,因为如果在请求完成之前替换了conereLationId的值,则在日志中,我将记录不正确的corsereRationID。 任何帮助将不胜感激。
I am using log4j ThreadContext for tracing in a spring boot application. I have created an interceptor by implementing HandlerInterceptor which intercepts a request and then sets 'x' value in the ThreadContext map using ThreadContext.put("correlationId", 'x'). The value 'x' is retrieved from the request headers. After the request is completed I clear the ThreadContext using ThreadContext.clearMap(). The requirement is to see correlationId in the logs with every logger statement which is getting fulfilled.
public class RequestHandlerInterceptor implements HandlerInterceptor {
@Override
public boolean preHandle(
final HttpServletRequest request,
final HttpServletResponse response,
final Object handler)
throws Exception {
ThreadContext.put(CORRELATION_ID, request.getHeader(CORRELATION_ID_HEADER));
return true;
}
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, @Nullable Exception ex) throws Exception {
ThreadContext.clearMap();
}}
Now my question is what will be the behavior if multiple requests are made by multiple users simultaneously. Will the value of correlationId in ThreadContext map get replaced with every new request before the completion of older requests? If so, then what would be the correct implementation since if the values of correlationId is getting replaced before the request is completed then in the logs I would have incorrect correlationId logged.
Any help would be appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您是否正在使用阻止 - 或 nonofollow noreferrer“ a>?
如果您正在使用Blocking-IO(可能是过去几十年中使用的大多数应用程序),则您的服务器将为用户提出的每个请求创建线程。从接受请求,您的服务课程中的业务逻辑以及对数据库或服务等其他系统的呼叫都被该请求处理(并且在等待这些系统的答案时被阻止)。
threadContext
是按每个线程管理的,因此与其他线程和请求分开。如果您正在与Nio合作,那么事情就会变得更加复杂,因为用户提出的请求由多个线程处理。
Are you working with blocking- or Non-blocking-IO (NIO)?
If you are working with blocking-IO (which probably most of the applications used the last decades), then your server will create a thread for every request made by a user. Everything from accepting the request, your business logic in your service classes and making calls to other systems like databases or services are handled by that request (and is blocked while waiting for answers from these system).
The
ThreadContext
is managed on a per thread basis and so separated from other threads and requests.If you are working with NIO, then things start to get more complicated, because then a request made by an user is handled by several threads.