简体   繁体   English

io.smallrye.mutiny.Uni 多线程不起作用 (Quarkus)

[英]io.smallrye.mutiny.Uni multi threading doesn't work (Quarkus)

I try to perform 2 different operations with different threads each.Here is my code :我尝试使用不同的线程执行 2 种不同的操作。这是我的代码:

 Uni.combine().all()
            .unis(getItem(), getItemDetails())
            .asTuple().subscribe().with(tuple -> {
                context.setItem(tuple.getItem1());
                context.setItemDetails(tuple.getItem2());
            });

Methods :方法 :

public Uni<ItemResponse> callGetItem(){
    Supplier<ItemResponse> supplier = () -> itemService.getItem("item_id_1");
    return Uni.createFrom().item(supplier);
}

public Uni<ItemDetailsResponse> callGetItemDetail(){
        Supplier<ItemDetailsResponse> supplier = () -> itemService.getItemDetail("dummy_item_id");
        return Uni.createFrom().item(supplier) ;
    }

But when i run the code both callGetItem() and callGetItemDetail() methods works in the same thread (executor-thread-0).但是当我运行代码时, callGetItem()callGetItemDetail()方法都在同一个线程(executor-thread-0)中工作。

Where am i doing wrong?我在哪里做错了?

Edit:编辑:

When i give an executor service Executors.newFixedThreadPool(2) for my Unis, They still work in single thread.当我为我的 Unis 提供执行器服务 Executors.newFixedThreadPool(2) 时,它们仍然在单线程中工作。 I mofified callGetItem() and callGetItemDetail() as :我将 callGetItem() 和 callGetItemDetail() 修改为:

 public Uni<ItemResponse> callGetItem(){
    Supplier<ItemResponse> supplier = () -> itemService.getItem("item_id_1");
    return Uni.createFrom().item(supplier).emitOn(executor);
}

public Uni<ItemDetailsResponse> callGetItemDetail(){
        Supplier<ItemDetailsResponse> supplier = () -> itemService.getItemDetail("dummy_item_id");
        return Uni.createFrom().item(supplier).emitOn(executor) ;
    }

executor is :执行人是:

  ExecutorService executor = Executors.newFixedThreadPool(2);

but they still works in same thread.但它们仍然在同一个线程中工作。 Do you have any idea why it happens?你知道为什么会这样吗?

Since you are composing different Uni s using Uni.combine().all().unis().asTuple() , the combined Uni will emit its result (combination) after the last element has emitted its item.由于您正在使用Uni.combine().all().unis().asTuple()组合不同的Uni ,因此组合的Uni将在最后一个元素发出其项目后发出其结果(组合)。

The last (upstream) Uni will have its item emitted (as is the case for other Uni s as well) on whatever Thread that you have declaratively set it to emit on.最后一个(上游) Uni将在您以声明方式将其设置为发出的任何Thread上发出其项目(其他Uni也是如此)。 Hence the combination Uni will follow execution on the same calling Thread .因此, Uni的组合将在同一个调用Thread上执行。

As a result, if you are accessing the combined group values, you will be accessing these on the same execution carrier Thread .因此,如果您正在访问组合的组值,您将在同一个执行载体Thread上访问这些值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM