当同时发出多个请求时,Log4J ThreadContext映射的值是否可以重叠?

发布于 2025-02-12 09:27:59 字数 1010 浏览 4 评论 0原文

我正在使用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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

野心澎湃 2025-02-19 09:27:59

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文