Home > front end >  Spring cloud stream Producer error handler not works
Spring cloud stream Producer error handler not works

Time:10-06

I'm using spring cloud stream versione 3.1.4, and this is my producer

@Component
public class Producer {    
    @Autowired
    private StreamBridge streamBridge;

    public void produce(int messageId, Object message) {
        Message<Object> msg= MessageBuilder
                .withPayload(message)
                .setHeader("partitionKey", messageId)
                .build();

        streamBridge.send("outputchannel-out-0", msg);
    }

    @ServiceActivator(inputChannel = "errorchannel.errors")
    public void errorHandler(ErrorMessage em) {
        log.info("Error: {}", em);
    }
}

Into application.yaml I set errorChannelEnabled

spring:
  cloud:
    stream:
      bindings:
        #Channel name
        outputchannel-out-0:
          destination: my-topic
          contentType: application/json
          producer:
            partitionKeyExpression: headers['partitionKey']
            partitionCount: 1
            errorChannelEnabled: true

Now, If I change the produce() method in this way, in order to test the error handler

public void produce(int messageId, Object message) {
    throw new RuntimeException("Producer error");
}

Nothing happens. Error handler is not triggered. I'm not sure that it's the right way to setup the error handler in spring cloud stream 3.1.4.

Can you help me?

CodePudding user response:

errorchannel.errors does not exist.

There are two error channels errorChannel is the global error channel; the binding-specific error channel is named <destination>.<group>.errors. You don't currently have a group.

  • Related