1

Consider the following example,

@RestController
public class MyController {

  private final MyService service;

  public MyController(MyService service) {
    this.service = service;
  }

  @GetMapping
  public Mono<MyObject> endpoint(String id) {
    String newId = String.toLowerCase()
    Mono.just(service.getObject(newId));
  }
}

All of the logic from the repository, to the service, & to any another class interactions don't deal with Mono's or Flux's. In this example, the service method getObject returns a MyObject data type which isn't wrapped in a Mono.

So, is there really a purpose to using webflux or a reactive approach in general with this setup or are we actually still getting benefit from using this Mono publisher type ?

turbofood
  • 192
  • 9
  • AFAIK, it depends on what `service#getObject` does exactly, but if it runs blocking operations like synchronous IOs for example, then it defeats the whole purpose of a Netty/WebFlux-based architecture, where request threads shouldn't be blocked. – sp00m Apr 11 '22 at 16:44
  • @sp00m Say it just does things like object/string manipulation ? That would still be non-blocking ? But, if it were to do database calls, or repository calls, or database insertions then, i'm guessing that would be blocking ? – turbofood Apr 11 '22 at 17:02
  • It depends on the string manipulation I suppose, but if it isn't too computationally intensive (unlike passphrase hashing for example), then you should be fine with the above code. As for database calls, it depends on the driver you're using: JDBC/JPA will block indeed, Spring R2DBC allows not to for example. See this other answer of mine which relates I believe: https://stackoverflow.com/a/65185737/1225328. – sp00m Apr 11 '22 at 17:12
  • Yea jdbc/jpa in WebFlux doesn't even make sense; those are inherently blocking in nature. But, I guess what I want to say or ask is that making items in your flow Monos & Fluxes can only help with the non-blocking flow & not introduce other unexpected issues – turbofood Apr 11 '22 at 17:41
  • It doesn't really make sense to wrap things like String manipulation into reactive types. It doesn't give you any benefit, on the contrary, it could cause confusion for the reader of the code. – Martin Tarjányi Apr 13 '22 at 18:41

0 Answers0