java - 在并行流上传播 Sleuth baggage

标签 java spring asynchronous spring-cloud-sleuth

本题与this one一模一样,实际上没有回答(该代码仅使用一个线程)。我的代码现在看起来像这样

CompletableFuture completableFuture = CompletableFuture.supplyAsync(() -> {
            foo.stream().parallel()
                    .forEach(bar -> {
                                //business logic
                            }
                    );
            return null;
        }, new TraceableExecutorService(this.beanFactory, Executors.newFixedThreadPool(threads), "fooBarStream"));

completableFuture.get();

但只有一个线程被正确跟踪。直接使用 .parallelStream()LazyTraceExecutor 而不是 TraceableExecutorService 没有帮助。

最佳答案

由于 this example 似乎可以正常工作.上面的代码片段变为:

TraceableExecutorService executorService = new TraceableExecutorService(this.beanFactory, Executors.newFixedThreadPool(threads), "fooStream");
CompletableFuture.allOf(runnablesBusinessLogic(foo,executorService)).get();

runnablesBusinessLogic 在哪里

private CompletableFuture<Void>[] runnablesBusinessLogic(List<FooBar> foo, ExecutorService executorService) {
    List<CompletableFuture<?>> futures = new ArrayList<>();
    for (FooBar f : foo) {
        futures.add(CompletableFuture.runAsync(() -> {
            businessLogic(f);
            return;
        }, executorService));
    }
    return futures.toArray(new CompletableFuture[futures.size()]);
}

如果我正确理解示例(以及文档当前状态背后的 discussion),Sleuth 无法自动使用 ForkJoinPool(以及并行流)。让它工作的主要想法不是创建一个 CompletableFuture 并将其拆分,而是创建多个 futures(并加入它们)。

关于java - 在并行流上传播 Sleuth baggage,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71606497/

相关文章:

java - "IllegalFormatConversionException: d != java.lang.String"用0填充数字时?

java - Spring Boot App 注册 RequestContextListener 失败

.net - WPF 的 F# 异步事件处理程序类似于 C# 的 async 和 await

java - BufferedReader 抛出异常,我不知道为什么

java - 在 Java-8 中捕获多个异常

java - 尝试将文件中的描述实现到游戏中

spring - 方面编织应如何限制在 aop :advisor pointcuts? 引用的类中

java - 使用 Spring JDBC 的简单交易?

c# - .NET C# 同步接收不阻塞

javascript - 尝试在 Node 中运行同步 REST 请求时出现问题