如何在CompletableFuture中保留slf4j MDC日志记录上下文?

问题描述 投票:4回答:3

执行异步CompletableFuture时,父线程上下文以及org.slf4j.MDC上下文丢失。

这很糟糕,因为我正在使用某种“鱼标记”来跟踪多个日志文件中的一个请求的日志。

MDC.put("fishid", randomId())

问题:如何在CompletableFutures的任务中保留该id一般?

List<CompletableFuture<UpdateHotelAllotmentsRsp>> futures =
    tasks.stream()
        .map(task -> CompletableFuture.supplyAsync(
            () -> businesslogic(task))
        .collect(Collectors.toList());

List results = futures.stream()
    .map(CompletableFuture::join)
    .collect(Collectors.toList());

public void businesslogic(Task task) {
       LOGGER.info("mdc fishtag context is lost here");
}
java slf4j completable-future mdc
3个回答
4
投票

我解决这个问题最可读的方式如下 -

--------------- Thread utils类--------------------

public static Runnable withMdc(Runnable runnable) {
    Map<String, String> mdc = MDC.getCopyOfContextMap();
    return () -> {
        MDC.setContextMap(mdc);
        runnable.run();
    };
}

public static <U> Supplier<U> withMdc(Supplier<U> supplier) {
    Map<String, String> mdc = MDC.getCopyOfContextMap();
    return (Supplier) () -> {
        MDC.setContextMap(mdc);
        return supplier.get();
    };
}

- - - - - - - -用法 - - - - - - -

CompletableFuture.supplyAsync(withMdc(() -> someSupplier()))
                 .thenRunAsync(withMdc(() -> someRunnable())
                 ....

必须重载ThreadUtils中的WithMdc以包含CompletableFuture接受的其他功能接口

请注意,静态导入withMdc()方法以提高可读性。


4
投票

最后,我创建了一个保持SupplierMDC包装器。如果有人有更好的想法随时发表评论。

public static <U> CompletableFuture<U> supplyAsync(Supplier<U> supplier, Executor executor) {
    return CompletableFuture.supplyAsync(new SupplierMDC(supplier), executor);
}

private static class SupplierMDC<T> implements Supplier<T> {
    private final Supplier<T> delegate;
    private final Map<String, String> mdc;

    public SupplierMDC(Supplier<T> delegate) {
        this.delegate = delegate;
        this.mdc = MDC.getCopyOfContextMap();
    }

    @Override
    public T get() {
        MDC.setContextMap(mdc);
        return delegate.get();
    }
}

0
投票

是的,Twitter Future正确地做到了这一点。他们有一个Future.scala知道的类Local.scala。

该修复程序适用于java作者修复此问题,以便您的本地状态遍历使用CompletableFutures的所有库。基本上,Local.scala由Future使用,并在内部使用ThreadLocal直到.thenApply或.thenAccept,它将捕获状态并在需要时将其传输到下一个on on和on。这适用于所有第三方库,ZERO第三方库更改。

这里有更多但是戳Java作者来修复他们的东西...... http://mail.openjdk.java.net/pipermail/core-libs-dev/2017-May/047867.html

在那之前,MDC将永远不会通过第三方库。

我在这个Does CompletableFuture have a corresponding Local context?的帖子

© www.soinside.com 2019 - 2024. All rights reserved.