Search code examples
javaspringspring-cloud-stream

How to configure multiple consumers to a single destination queue in spring cloud stream SQS binder


My application is receiving two events with different payloads. So I wrote two different consumers for each event.

Consumer1:

@Bean
public Consumer<TestEvent1> testEvent1() {
  // my consumer logic
}

Consumer2:

@Bean
public Consumer<TestEvent2> testEvent2() {
  // my consumer logic
}

The following is the spring cloud stream configurations:

spring:
  cloud:
    stream:
      sqs:
        bindings:
          testEvent1-in-0:
            consumer:
              snsFanout: true
              messageDeletionPolicy: ON_SUCCESS
              waitTimeout: 20
              maxNumberOfMessages: 10
              visibilityTimeout: 30
          testEvent2-in-0:
            consumer:
              snsFanout: true
              messageDeletionPolicy: ON_SUCCESS
              waitTimeout: 20
              maxNumberOfMessages: 10
              visibilityTimeout: 30
      bindings:
        testEvent1-in-0:
          destination: events-queue
        testEvent2-in-0:
          destination: events-queue
      default-binder: sqs

But with the above configurations, both the consumers is not working. If I am removing any one of the consumer, the other is working.

How can I able to configure the cloud stream such that both the consumers will work based on the event payload which is produced.


Solution

  • When you have multiple functions, you need to tell the framework which ones need to be activated. When there is only one function in the application, then Spring Cloud Stream will simply activate that one. That is the reason why it works when you remove one consumer. You can use the following configuration to activate both consumers.

    spring.cloud.function.definition: testEvent1;testEvent2