简体   繁体   English

大学如何<t>在夸库斯工作</t>

[英]How does Uni<T> works in Quarkus

I am trying to understand the behavior of Uni in Quarkus framework, after checking out their official tutorial getting started with async guide .在查看了他们的官方教程开始使用异步指南之后,我试图了解 Uni 在 Quarkus 框架中的行为。 In the service method I have made the following changes在服务方法中,我做了以下更改

package org.acme.getting.started.async;

import javax.enterprise.context.ApplicationScoped;

import io.smallrye.mutiny.Uni;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;


@ApplicationScoped
public class GreetingService {

    ExecutorService executor = Executors.newFixedThreadPool(10, r -> {
        return new Thread(r, "CUSTOM_TASK_EXECUTION_THREAD");
    });


    public Uni<String> greeting(String name) {
        System.out.println("greeting Executing on Thread "+Thread.currentThread().getName());
        return Uni.createFrom().item(ioSimulation(name))
                .emitOn(executor);//Infrastructure.getDefaultExecutor()
    }

    public String ioSimulation(String param){
        System.out.println("ioSimulation Executing on Thread "+Thread.currentThread().getName());
        try {
            Thread.sleep(8000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return "hello "+param;

    }

}

Then I have tested the /greeting/{name} resource, the execution was not async at all, in fact it executed all related methods within the same thread and in synchronous manner.然后我测试了 /greeting/{name} 资源,执行根本不是异步的,实际上它在同一个线程中以同步方式执行了所有相关方法。

then what would be the difference between那么这之间有什么区别

@GET
    @Produces(MediaType.TEXT_PLAIN)
    @Path("/greeting/{name}")
    public Uni<String> greeting(@PathParam String name) {
    }

and

@GET
    @Produces(MediaType.TEXT_PLAIN)
    @Path("/greeting/{name}")
    public String greeting(@PathParam String name) {
    }

and how is it asynchronous?它是如何异步的? please help me to understand it.请帮助我理解它。

The problem is in Uni.createFrom().item(ioSimulation(name)) .问题出在Uni.createFrom().item(ioSimulation(name))中。 You synchronously call ioSimulation before the Uni is created.在创建Uni之前同步调用ioSimulation

You should at least use the Supplier variant: Uni.createFrom().item(() -> ioSimulation(name)) .您至少应该使用Supplier变体: Uni.createFrom().item(() -> ioSimulation(name)) That should help.那应该有帮助。

In addition to @Ladicek answer, I believe you want to use runSubscriptionOn and not emitOn .除了@Ladicek 的回答,我相信你想使用runSubscriptionOn而不是emitOn See https://smallrye.io/smallrye-mutiny/guides/emit-subscription as reference.请参阅https://smallrye.io/smallrye-mutiny/guides/emit-subscription作为参考。

both the answers above are correct.以上两个答案都是正确的。 I made some changes in service class and ioSimulation(String param) method was offloaded to the custom thread pool.我对服务 class 进行了一些更改,并将ioSimulation(String param)方法卸载到自定义线程池。 The final solution is given below最终解决方案如下

public class GreetingService {

    ThreadFactory threadFactory = new NameableThreadFactory("CUSTOM_TASK_EXECUTION_THREAD");
    ExecutorService executor = Executors.newFixedThreadPool(10, threadFactory);

    public Uni<String> greeting(String name) {
        System.out.println("greeting Executing on Thread "+Thread.currentThread().getName());
        return Uni.createFrom().item(()->ioSimulation(name))
                .runSubscriptionOn(executor);//Infrastructure.getDefaultExecutor()
    }


    public String ioSimulation(String param){
        System.out.println("ioSimulation Executing on Thread "+Thread.currentThread().getName());
        try {
            Thread.sleep(8000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return "hello "+param;

    }
}

-- update -- --更新--

emitOn(Executor executor) will also works asynchronously if you pass supplier in item method but the approach is slightly different, I have figured that while experimenting with Uni , this is my repo, incase if anyone is interested.如果您在item方法中传递supplieremitOn(Executor executor)也将异步工作,但方法略有不同,我认为在尝试Uni时,是我的 repo,以防有人感兴趣。

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

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