0

How to achieve reactive message processing in Spring Cloud Stream? I read about Spring Cloud Function and that I should use them for reactive processing so I created sample one:

@Bean
public Consumer<Flux<Message<Loan>>> loanProcess() {
  return loanMessages ->
      loanMessages
          .flatMap(loanMessage -> Mono.fromCallable(() -> {
            if (loanMessage.getPayload().getStatus() == null) {
              log.error("Empty status");
              throw new RuntimeException("Loan status is empty");
            }
            return "Good";
          }))
          .doOnError(throwable -> log.error("Exception occurred: {}", throwable))
          .subscribe(status -> log.info("Message processed correctly: {}", status));
}

Afterwards I started to thinking what is the difference between the above function and the class with @StreamListener and usage of Reactor types:

@StreamListener(Sink.INPUT)
public void loanReceived(Message<Loan> message) {
  Mono.just(message)
      .flatMap(loanMessage -> Mono.fromCallable(() -> {
        if (loanMessage.getPayload().getStatus() == null) {
          log.error("Empty status");
          throw new RuntimeException("Loan status is empty");
        }
        log.info("Correct message");
        return "Correct message received";
      }))
      .doOnError(throwable -> log.error("Exception occurred: {}", throwable.getClass()))
      .subscribe(status -> log.info("Message processed correctly: {}", status));
}

Additionally, in Spring Webflux I understand that there are few threads from netty which handle requests processing (running in event loop). However, I cannot find a documentation how thread model works in Spring Cloud Stream.

Adam
  • 884
  • 7
  • 29
  • `public Consumer>>` is the current approach. The StreamnListener is effectively a deprecated approach and in fact we no longer distribute reactive module with spring-cloud-stream since support natively provided with spring-cloud-functon. Also, the reactive support in s-c-stream has nothing to do with webflux and/or netty. There is no threading model per se. With regard ti mechanics, MessageListener is adapted to stream (e.g., Flux) and passed to user function. That's it. – Oleg Zhurakousky Apr 03 '20 at 10:21
  • I understand the current approach is to use function but in functionality is there a difference between stream listener and stream cloud function? I know Spring Cloud Stream has nothing to do with webflux/netty but I want to know how thread model looks like. How many threads are processing messages? – Adam Apr 03 '20 at 11:20
  • Functionality wise there is absolutely no difference. With regard to the threading model we manage it thru concurrency setting of Message Listener Container - https://cloud.spring.io/spring-cloud-static/spring-cloud-stream/3.0.3.RELEASE/reference/html/spring-cloud-stream.html#_consumer_properties – Oleg Zhurakousky Apr 03 '20 at 11:57
  • So does it mean that with reactive I should be able to process more messages because my thread is non-blocking and instead of waiting for db call or external service it can process another message? – Adam Apr 03 '20 at 12:11

0 Answers0