简体   繁体   English

使用RabbitMQ和Reactor处理Spring Cloud Stream错误

[英]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. 我试图了解将Spring Cloud Stream与RabbitMQ和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. onErrorResume将处理异常,然后流程继续。 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. 如其他文章所述,onError(及其所有变体)是在特殊情况下使用的。

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. 由于尚未优化,因此欢迎进行改进。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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