1
votes

According to this documentation, it should be possible to subscribe to global error channel provided by Spring Integration - "errorChannel".

In my very simple case it does not work:

Application:

@SpringBootApplication
@EnableBinding({MySink.class})
public class LoggingConsumerApplication {

    public static void main(String[] args) {
        SpringApplication.run(LoggingConsumerApplication.class, args);
    }

    @StreamListener(target = MySink.INPUT_ONE)
    public void handle(Person person) {
        System.out.println("Received: " + person);

        if(StringUtils.isEmpty(person.getName())){
            throw new RuntimeException("Wrong person name!");
        }
    }

    @ServiceActivator(inputChannel = "mySink.mySink-group.errors")
    public void error(Message<?> message) {
        System.out.println("Handling ERROR: " + message);
    }


    @ServiceActivator(inputChannel = "errorChannel")
    public void errorGlobal(ErrorMessage message) {
        System.out.println("Handling ERROR GLOBAL SA: " + message);
    }

    @StreamListener("errorChannel")
    public void errorGlobalListener(ErrorMessage message) {
        System.out.println("Handling ERROR GLOBAL Listener: " + message);
    }

    public static class Person {
        private String name;
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
        public String toString() {
            return this.name;
        }
    }
}

MySink

public interface MySink {

    /**
     * Input channel name.
     */
    String INPUT_ONE = "inputOne";

    /**
     * @return input channel.
     */
    @Input(MySink.INPUT_ONE)
    SubscribableChannel inputOne();
}

properties

spring.rabbitmq.host=192.168.0.100
spring.cloud.stream.bindings.inputOne.destination=mySink
spring.cloud.stream.bindings.inputOne.group=mySink-group

The destination-specific handler works (mySink.mySink-group.errors), but two other handlers never get called.

What is wrong here?

Tried with Spring Boot 2.1.6

1

1 Answers

1
votes

There is nothing wrong and it is working as expected. From the doc: "The handle(..) method, which subscribes to the channel named input, throws an exception. Given there is also a subscriber to the error channel input.myGroup.errors all error messages are handled by this subscriber." So, what this means is that your error is handled by either binding specific error handler (mySink.mySink-group.errors) or global (errorChannel).

https://cloud.spring.io/spring-cloud-static/spring-cloud-stream/2.2.0.RELEASE/spring-cloud-stream.html#spring-cloud-stream-overview-error-handling