1

I am planning a "consumer aware" service that will handle some type of information only if there is at least one consumer on that topic. Using spring, I was able to create the following code to monitor when consumers connect or disconnect from a topic:

@JmsListener(destination = "ActiveMQ.Advisory.Consumer.>")
public void processaConsumidorConectado(Message message){
    System.out.println("Registrou um consumidor");
}

That works pretty well if the event occurs after I register to that topic. But it could happen that I already have a consumer in a topic before I registered this listener.

Is there a way to force activeMQ to send all advisory messages of a topic or something like that?

Raphael do Vale
  • 931
  • 2
  • 11
  • 28

2 Answers2

1

I think it is possible with retroactive consumers but i do not tested this or with duplicating messages to another topic by camel route. I don't know if this fit your beeds but you can subscribe to

ActiveMQ.Advisory.NoConsumer.Topic.YourTopic

If you receive a message on this topic this means there no consumers connected

http://activemq.apache.org/retroactive-consumer.html

Hassen Bennour
  • 3,885
  • 2
  • 12
  • 20
  • You can send a test message to your topic and before this listen to 'ActiveMQ.Advisory.NoConsumer.Topic.YourTopic' if messages are non-persistent and if there is no consumers connected you will receive your message @ NoConsumers topic – Hassen Bennour Sep 20 '16 at 21:23
1
  1. Tracking active subscribers via advisories is going to be pretty flaky. You probably should just connect up to JMX and see if there is an active consumer or not.

  2. You can add a subscription to the advisory topic you want, or use a wild card to match a group pattern, such as topic://ActiveMQ.Advisory.Consumer.>

Friendly disclaimer-- In general, designing to send a message only if a consumer exists is a race condition, and I'd generally advise against that.. there is a risk the consumer goes away right after you send.

Edit: Correct #2 to reflect durable subscription is not available for Advisory Topics

Matt Pavlovich
  • 4,087
  • 1
  • 9
  • 17
  • Is this possible **durable subscription to the advisory** – Hassen Bennour Sep 20 '16 at 18:29
  • Yes. Advisory Topics are just normal topics. Create a durable subscription as usual. – Matt Pavlovich Sep 20 '16 at 18:37
  • I removed this from my response because i never tested this and seen this post , maybe this feature added after http://stackoverflow.com/questions/17488272/activemq-how-to-create-subscriber-for-activemq-advisory-connection-topic – Hassen Bennour Sep 20 '16 at 18:50
  • I will test your suggestion and reply here once I have an answer. – Raphael do Vale Sep 20 '16 at 19:55
  • Thank you for your disclaimer, in my case, I feel that is not a problem: I have a stream of data and older information is not important. The idea to only process topics with consumers is just to reduce the amount of processing power for nothing. – Raphael do Vale Sep 20 '16 at 19:58
  • I thought about using JMX, but doesn´t fell wrong? I mean, JMX can be disabled and it seems much more an monitoring/analytics tool than something to use in a production code. – Raphael do Vale Sep 20 '16 at 20:01
  • I hadn't seen that before, but I just confirmed that no durable subscriptions are supported for Advisories.. I've updated my answer accordingly. – Matt Pavlovich Sep 20 '16 at 20:03
  • @RaphaeldoVale sending a message only if a consumer is present is the "wrong" part here. Using JMX is just making it less wrong than trying to rely on Advisories. Why do you feel like you must know if a consumer is present? – Matt Pavlovich Sep 20 '16 at 20:07
  • @matt-pavlovich, maybe I am thinking without a 'messaging mindset'. As I said, I have a stream of data that will be send to this topic. The stream is costly to process and I am willing to not process if there are not consumers listening to the topic as those messages are useless if not processed in the time it was sent. Do you have any suggestion? – Raphael do Vale Sep 20 '16 at 20:19
  • JMX is a good option, as it'll give you realtime view. It won't solve for stuck or leaky consumers. A convoluted, but effective sol'n to work around the "stuck/leaky consumer" problem would be to have the consumer app produce a "keep alive" message that the producer looks for. Consumer produces every 5 minutes, producer sends messages for a 10-15 minute period for every keep-alive received (just needs to overlap by a reasonable amount of time) – Matt Pavlovich Sep 21 '16 at 02:39