0

I have a service that wants to receive events from multiple sources, and do the same thing with all of them. In attempt to reduce the amount of code I need to write, I'd like to have multiple queues point to the same consumer.

eg:

#application.yml
...
      bindings:
        myEventConsumer-in-0:
          binder: binder1
          destination: my-event.exchange
          contentType: application/json
          group: ${some-group}
        myEventConsumer-in-0:
          binder: binder2
          destination: my-event.exchange
          contentType: application/json
          group: ${some-group}

MessagingConfig.java
package my.config

import ...

@Configuration
public class MessagingConfig {

    @Bean
    Consumer<Event<someEventCreate>> myEventConsumer(myService myService) {
        return new MyEventConsumer(myService);
    }

}

Is it possible using the application.yml config to have multiple bindings pointing to the same consumer? With the configuration above we have duplicate key errors so obviously that's not going to work, but is there another way?

1 Answers1

0

See the documentation: https://docs.spring.io/spring-cloud-stream/docs/current/reference/html/spring-cloud-stream.html#_common_binding_properties

destination

The target destination of a binding on the bound middleware (for example, the RabbitMQ exchange or Kafka topic). If binding represents a consumer binding (input), it could be bound to multiple destinations, and the destination names can be specified as comma-separated String values. If not, the actual binding name is used instead.

Also see multiplex

https://docs.spring.io/spring-cloud-stream/docs/current/reference/html/spring-cloud-stream.html#_consumer_properties

multiplex

When set to true, the underlying binder will natively multiplex destinations on the same input binding.

Default: false.

When false, a separate listener container is used for each queue; when true, the same container consumes from all the queues.

Gary Russell
  • 166,535
  • 14
  • 146
  • 179