Home > Blockchain >  How use some blocking methods while using Quarkus Hibernate Reactive with quarkus-reactive-client?
How use some blocking methods while using Quarkus Hibernate Reactive with quarkus-reactive-client?

Time:09-17

I am trying to call a reactive rest but while doing it I have to call another request inside it and create few folders but quarkus-hibernate-reactive is showing below error what is best practice to it?

Error:

Caused by: java.lang.IllegalStateException: The current thread cannot be blocked: vert.x-eventloop-thread-2
    at io.smallrye.mutiny.operators.UniBlockingAwait.await(UniBlockingAwait.java:29)

My code is as below:

@POST
@javax.ws.rs.Path("/add") 
@Produces({MediaType.APPLICATION_JSON, MediaType.TEXT_PLAIN})
@Consumes({ MediaType.APPLICATION_JSON, MediaType.TEXT_PLAIN })
public Uni<Response> addUser(@HeaderParam("userName") String addedBy) throws Exception{
     //Here I have one if condition which is checking from database if user exist!

     if (checkUser(addedBy).await().indefinitely().intValue() > 0) {
        //then creatingfolders

        if (createFolders(addedBy) > 0) {
             UserBean u = new UserBean(addedBy);
             return Uni.createFrom().item(userRepository.addUser(u)).onItem().transform(f -> f != null ? Response.ok(f) : Response.ok(null))
                 .onItem().transform(ResponseBuilder::build);
        }
     }
}

public Uni<Long> addUser(UserBean userIN) {
   return Panache.withTransaction(userIN::persist)
                .replaceWith(userIN.getId());
}

How we can make it wait for completion of creating folders and checking user exists? I have tried using @Blocking but it didn't worked. Please help

CodePudding user response:

You should either switch to regular Hibernate ORM instead of Hibernate Reactive, or chain the operations together in a reactive manner.

Instead of doing checkUser(addedBy).await().indefinitely().intValue() do something like:

checkUser(addedBy).onItem().transformToUni(count -> {
   ....
})

CodePudding user response:

You mentioned that you already tried the Blocking annotation, but did you use the right one? I believe there are two of them and only one work.

  • Related