Search code examples
spring-bootstreamrabbitmqspring-cloud

Spring Stream - Send event to multiple destinations


I am using Spring Cloud Stream with RabbitMQ and I would need to send an event that needs to be consumed by exactly 2 consumers.

In the producer I have added multiple destinations:

Tags microservice:

public interface OutgoingEventChannels {

    @Output("updateTagNameChannel")
    MessageChannel updateTagName();
}

@Component
@EnableBinding(OutgoingEventChannels.class)
public class EventProducer {

    @Autowired
    private OutgoingEventChannels outgoingEventChannels;

    public void sendUpdateTagNameEvent(UpdateTagNameEvent updateTagNameEvent) {
        outgoingEventChannels.updateTagName().send(new GenericMessage<>(updateTagNameEvent));
    }
}

spring.cloud.stream.bindings.updateTagNameChannel.destination=updateCustomerTagName,updateSectionTagName
spring.cloud.stream.bindings.updateTagNameChannel.group=tags-group

And each of the consumers are binded to the different destinations:

Customers microservice:

public interface IncomingEventChannels {

    @Input("updateTagNameChannel")
    MessageChannel updateTagName();
}

@Component
@EnableBinding(IncomingEventChannels.class)
public class EventListener {

    private static final Logger LOG = LogManager.getLogger(EventListener.class);

    @Autowired
    private CustomerService customerService;

    @StreamListener("updateTagNameChannel")
    public void handleUpdateTagNameEvent(UpdateTagNameEvent updateTagNameEvent) {
        LOG.info("Received update tag event: " + updateTagNameEvent);
        customerService.updateTagName(updateTagNameEvent);
    }
}

spring.cloud.stream.bindings.updateTagNameChannel.destination=updateCustomerTagName
spring.cloud.stream.bindings.updateTagNameChannel.group=tags-group

The event is never received by any of both consumers. Does anybody knows what I am doing wrong?

Thank you in advance!


Solution

  • If I understood correctly, you want both the consumers to get a copy of the data. In that case, you want your two consumers to be in two different consumer groups. If both of the consumers are in the same consumer group then only one of them would receive the event.

    You can find more details on consumer groups here : Spring Docs