简体   繁体   中英

Spring Cloud Stream error handling using RabbitMQ and Reactor

I'm trying to understand the proper way of handling errors when using Spring Cloud Stream together with RabbitMQ and Reactor. When the received messages are validated properly everything is working nicely and the chain is able to process messages. When an error occurs the chain breaks and terminates.

To give an indication of what is happening (a little simplified):

@StreamListener
@Output(Processor.OUTPUT)
public Flux<byte[]> receive(@Input(Processor.INPUT) Flux<Message> input) {
    input.map(this::transformDataToJSON)
         .onErrorResume(MessageValidationException.class, this::processValidationException)
         .map(m -> m.getBytes())
}

There are a few simple transformations happening which might cause a validation exception to be thrown. The onErrorResume will handle the exception and the flow continues. But as the exception is handled the chain is terminated and consequently no new messages are received by it.

Since this is not very robust, I'm looking for a best practice in this case. What would be a proper way of handling such a case? Would validating and skipping be a better way of handling this or is there another way of properly handling such stream exceptions?

I've come to the conclusion that this approach is not the right one. As stated in other articles, onError (and all its variants) is for exceptional circumstances.

The approach I take now is to validate the data before it's send down the chain, if the data doesn't validate it's being handled by a different subscriber.

@StreamListener
@Output(Processor.OUTPUT)
public Flux<byte[]> receive(@Input(Processor.INPUT) Flux<Message> input) {
    input.filter(b -> !validate(b)).doOnNext(this::handleInvalidData).subscribe();    
    input.filter(this::validate)
     .map(this::transformDataToJSON)
     .map(m -> m.getBytes())
}

This is not optimized so improvements are welcome.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM