到目前为止,我已经在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);
}下面是调用服务中的客户端代码
@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;
}基本上,我尝试过的所有东西都会创建另一个Uni。也许我只是把这个概念用错了。但是,我如何从Uni中取出Integer,以便获得intValue呢
发布于 2021-09-27 12:57:45
您需要调用一个终端操作,或者使用该值并继续链。
如果您想调用终端操作员,您可以调用await操作来阻塞您的代码并等待响应。
如果您希望将此反应式调用与客户端代码中的另一个合并,则可以使用combine方法将实际的Mutiny流与来自响应的on连接或组合。
如果您只想使用该值而不检索它,那么您可以使用suscribe并获得结果。
如果你有一个multi类型,你可以直接调用toList方法
假设您想要包含一些超时,并且想要获得实际的整数,您可以使用await方法和一个超时。
https://stackoverflow.com/questions/69346708
复制相似问题