For a Spring Framework
app working with ActiveMQ
and with/without WebSocket
The requirement is prior to send any message to a Topic
a check should be done about Number Of Consumers
, if it returns 1 the message can be sent safely. If it returns 0, the message can't be sent.
The clients could be come from websocket and consider there is no durable subscription. Thus if a message is sent, and there are no clients, the message arrives to the Topic
and practically is lost (never consumed) and Messages Enqueued
increments +1
I already did a research and I have read the following:
- JMSTemplate check if topic exists and get subscriber count
- Notify ActiveMQ producer if consumer on the destination is down
- ActiveMQ get number of consumers listening to a topic from java
- Detect change in Consumers of an ActiveMQ topic
Practically all is based on Advisory Message
. I already have read:
I understand that if exists a Topic
named abc.xyz
then ActiveMQ
creates ActiveMQ.Advisory.Consumer.Topic.abc.xyz
, until here I am ok with this pattern. I can confirm this approach through ActiveMQ Web Console
What is confuse for me is that practically all the examples available from the previous links works around creating a Session
and uses mostly the onMessage
method. For the latter I know it works how a listener.
Question 01: Thus who is expected to call that ActiveMQ.Advisory.Consumer.Topic.abc.xyz
? It to trigger that onMessage
method? That is my confusion.
What I need is work with the Spring Framework
API (The app is already working and running with a CachingConnectionFactory
, thus a Connection
can be retrieved and other @Beans
about infrastructure about ActiveMQ
) and get access to that ActiveMQ.Advisory.Consumer.Topic.abc.xyz
destination and retrieve the Number Of Consumers
value.
Note: even when exists ActiveMQTopic
declared with @Bean
and is possible to retrieve that Destination
for some @Component
, sadly the API does not offer a method such getConsumers()
.
Question 02: How can be accomplished this?
I am assuming the JMS
API for 2.0.x could help perhaps in someway.