Mutiny Uni 转换为原始类型

Pau*_*ner 2 quarkus mutiny

到目前为止,我已经在 Quarkus 中使用 Smallrye Mutiny 做了非常基本的事情。基本上,我有一两个非常小的 Web 服务,它们仅与 Web 应用程序交互。这些服务返回一个Uni<Response>.

现在我正在编写一个日志服务,我希望其他人向其传递信息。在此日志记录服务中,我需要返回一个值来调用服务。日志记录服务将返回该值作为Uni<Integer>. 我正在努力解决的是如何将调用服务中的返回值提取为int.

这是日志服务中的函数

    @GET
    @Path("/requestid")
    @Produces(MediaType.TEXT_PLAIN)
    public Uni<Integer> getMaxRequestId(){
        return service.getMaxRequestId();
    }

    public Uni<Integer> getMaxRequestId() {
        Integer result = Integer.valueOf(em.createQuery("select MAX(request_id) from service_requests").getFirstResult());
        
        if(result == null) {
            result = 0;
        }
        return Uni.createFrom().item(result += 1);
    }

Run Code Online (Sandbox Code Playgroud)

这是调用服务中的客户端代码

@Path("/requests")
public class RequestIdResource {
    
    @RestClient
    RequestIdServices service;
    
    @GET
    @Path("/requestid")
    @Produces(MediaType.TEXT_PLAIN)
    public Uni<Integer> getMaxRequestId(){
        return service.getMaxRequestId();
    }
}

    public void filter(ContainerRequestContext requestContext) throws IOException {

        int requestid = client.getMaxRequestId();

        rm.name = ConfigProvider.getConfig().getValue("quarkus.application.name", String.class);
        rm.server = requestContext.getUriInfo().getBaseUri().getHost();
        rm.text = requestContext.getUriInfo().getPath(true);
        rm.requestid = requestid;
        
    }
Run Code Online (Sandbox Code Playgroud)

基本上我尝试过的一切都会创建另一个Uni. 也许我只是错误地使用了这个概念。但我怎样才能摆脱Integer以便Uni我能得到intValue

kar*_*lss 6

您需要调用终端操作,或使用该值并继续该链。

如果您想调用终端操作符,您可以调用该await操作来使代码阻塞并等待响应。

如果您想将此反应式调用与客户端代码中存在的另一个反应式调用合并,您可以使用该方法将实际的 Mutiny 流与来自响应的 on 加入或组合combine

如果您只想使用该值而不检索它,您可以订阅并获取结果。

如果你有一个multi,你可以直接调用该方法toList

假设您想要涉及一些超时并且想要获得实际的整数,您可以使用该await方法和超时。