Avoid multiple listens to ActiveMQ topic with Spring Boot microservice instances

Viewed 586

We have configured our ActiveMQ message broker as a Spring Boot project and there's another Spring Boot application (let's call it service-A) that has a listener configured to listen to some topics using @JmsListener annotation. It's a Spring Cloud microservice appilcation.

The problem:

  • It is possible that service-A can have multiple instances running.
  • If we have 2 instances running, then any message coming on topic gets listened to twice.
  • How can we avoid every instance listening to the topic?
  • We want to make sure that the topic is listened to only once no matte the number of service-A instances.

Is it possible to run the microservice in a cluster mode or something similar? I also checked out ActiveMQ virtual destinations but not too sure if that's the solution to the problem.

We have also thought of an approach where we can decide who's the leader node from the multiple instances, but that's the last resort and we are looking for a cleaner approach.

Any useful pointers, references are welcome.

1 Answers

What you really want is a shared topic subscription which was added in JMS 2. Unfortunately ActiveMQ 5.x doesn't support JMS 2. However, ActiveMQ Artemis does.

ActiveMQ Artemis is the next generation broker from ActiveMQ. It supports most of the same features as ActiveMQ 5.x (including full support for OpenWire clients) as well as many other features that 5.x doesn't support (e.g. JMS 2, shared-nothing high-availability using replication, last-value queues, ring queues, metrics plugins for integration with tools like Prometheus, duplicate message detection, etc.). Furthermore, ActiveMQ Artemis is built on a high-performance, non-blocking core which means scalability is much better as well.

Related