首页 > 解决方案 > 在 QUARKUS 中使用 Reactive RestEasy 和 GraphQlClient 时出现线程阻塞问题

问题描述

我正在使用 quarkus 版本2.3.0.Final

我在Controller图层中有一个休息端点:

    @POST
    @Path("/upload")
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.MULTIPART_FORM_DATA)
    public Uni<Response> uploadFile(@MultipartForm FormData formData) {

        return documentService.uploadFile(formData).onItem()
                .transform(value -> Response.status(200)
                        .entity(value)
                        .build());
    }

service层中的代码


public Uni<?> uploadFile(@NonNull FormData formData) throws Exception {
   // Call to graphQl client using blocking process - the problem occurs here,

   RevisionResponse revisionResponse = entityRepository.createRevision(formData);

   // Do upload to s3 using s3 Async
   return Uni.createFrom()
               .future(
                    storageProviderFactory.getDefaultStorageProvider().upload(formData)))
               .map(storageUploadResponse -> DocumentResponse.builder()
                        .id(revisionResponse.getId())
                        .entityMasterId(revisionResponse.getEntityMasterId())
                        .type(revisionResponse.getType())
                        .path(formData.getFilePath())
                        .description(formData.getDescription())
                        .build());

}

这是我使用的依赖项:

    <dependency>
      <groupId>io.quarkus</groupId>
      <artifactId>quarkus-smallrye-graphql-client</artifactId>
    </dependency>
    <dependency>
      <groupId>io.quarkus</groupId>
      <artifactId>quarkus-resteasy-reactive</artifactId>
    </dependency>
    <dependency>
      <groupId>io.quarkus</groupId>
      <artifactId>quarkus-resteasy-reactive-jsonb</artifactId>
    </dependency>

当我运行这个函数时,它被阻塞了entityRepository.createRevision(formData)(控制台显示了graphql请求日志,但实际上,请求甚至没有命中目标graphql端点)

@Blocking但是,如果我在图层中添加注释controller,一切都会按预期工作。

我也尝试了Uni响应,Uni<RevisionResponse> revisionResponse = entityRepository.createRevision(formData);但发生了同样的错误。

有没有人遇到这些问题,我是否为非阻塞处理配置了错误?

谢谢你。

标签: javaresteasyquarkusreactivegraphql-java

解决方案


对于那些对我有同样问题的人,我通过用 Uni 包装阻塞代码来解决它:

Uni<RevisionResponse> revisionResponse = Uni.createForm().item(entityRepository.createRevision(formData));

参考链接:https ://smallrye.io/smallrye-mutiny/guides/imperative-to-reactive#running-blocking-code-on-subscription


推荐阅读