0

I'm using spring-cloud-starter-stream-rabbit 4.0.2, and want to write my own ErrorHandler for those sending failed messages. Refer to Spring docs, using 'error-handler-definition' property, but it doesn't work, myErrorHandler not invoked, instead Spring's default LoggingHandler been invoked, what's wrong with my settings below?

spring.cloud.stream:
  binders:
    rabbit:
      type: rabbit
      environment:
        spring.rabbitmq:
          publisher-confirms: true
          publisher-returns: true
  bindings:
    output:
      error-handler-definition: myErrorHandler

the error handler:

@Bean
public Consumer<ErrorMessage> myErrorHandler() {
   return err -> {
      log.error("msg:{}", err);
   };
}

send msg use StreamBridge:

public class SenderService {
  @Autowired
  private StreamBridge streamBridge;

  public void send(String msg) {
    streamBridge.send("output", msg);
  }
}

ttdys108
  • 11
  • 2

1 Answers1

1

Ehhh, no one help me :( I read springcloud source codes, and find it will look for errorChannel bean to handle failed messages, so I defined a customized bean, and it works, though very silly!

@Bean
public PublishSubscribeChannel errorChannel() {
    return new MyErrorChannel(myErrorHandler());
}

public static class MyErrorChannel extends PublishSubscribeChannel {
    public MyErrorChannel(MessageHandler handler) {
        this.dispatcher.addHandler(handler);
    }
}
ttdys108
  • 11
  • 2