0

I have recently started reactive programming and trying to call a method which should return an Object, but i am getting the error of block()/blockFirst()...while calling that method. Below is the method which i am trying to call

private MyOrderResponse callmyOrderSvc(String path,Log log){
  return WebClient.create().get()
         .uri(myOrderSvcUrl +OrderConstant.SEP+path)
         .header(OrderConstant.SECRETKEY,apiKey)
         .retrieve()
         .onStatus(HttpStatusCode :: isError,
         response -> response.bodyToMono(MyOrderResponse.class)
         .flatMap(error ->{
          LogUtil.logError(logger,"Error occurred",error.getMessage());
          return Mono.error(new CustomException(error.getMessage(),log.getId(),HttpStatus)));
         }))
         .bodyToMono(MyOrderResponse.class).block();
        }

And this method is being called by another method which is also returning some object, like this below

public MyOrderDetailsResponse getMyOrderId(String Id,String type,Log log){
  MyOrderResponse myOrderResponse;
  myOrderResponse = callmyOrderSvc(path,log); //this method is calling the callmyOrderSvc which is returning and Object of MyOrderResponse 

The criteria is i can not return Mono from here , i have to return MyOrderResponse Object. Any help is appreciated.

Mandrek
  • 1,159
  • 6
  • 25
  • 55
  • 1
    This means that the thread where `getMyOrderId` is executed belongs to "reactor-http-nio" pool that cannot be blocked. You violate this rule when you call `.block` as part of `callmyOrderSvc`. To avoid this exception you need to execute `getMyOrderId` in thread that not belongs to "reactor-http-nio", but I can't suggest how to do it because there is not enough code context in your questions. – Yevhenii Semenov May 30 '23 at 20:54
  • If you are forced to block, you will have to schedule your pipeline on a "blocking compatible scheduler", like `Schedulers.boundedElastic()`. For more details, you can check [my answer on this similar question](https://stackoverflow.com/questions/75058585/block-blockfirst-blocklast-are-blocking-which-is-not-supported-in-thread/75096742#75096742) – amanin May 31 '23 at 06:19

0 Answers0