0

I have a standard activeMQ Broker

    private static String localVMurl = "vm://localhost";

    broker = new BrokerService();
    broker.addConnector(localVMurl); 
    broker.start();

and all is well. what my goal is that a consumer connects to the broker with a specific topic. Once this connection is detectd the broker will either pass along messages if a Producer is actively producing to the topic, OR the broker will start a new Producer for that specific topic. However to do this, i need to someone detect when a new consumer connects and requests a specific topic.

my basic consumer code:

    ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(remoterURL);
    Connection connection = connectionFactory.createConnection();
    connection.setClientID("clinet1");
    connection.start();

    session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
    Topic topic = session.createTopic("some_topic");
    MessageConsumer consumer = session.createConsumer(topic);
    consumer.setMessageListener(new MyMessageListener());

and I can see in the Broker logs:

  <161005 11:03:41> [.0.1:64433@5001] DEBUG tRegion - localhost adding destination: topic://ActiveMQ.Advisory.Consumer.Topic.some_topic

So i know the consumer connects, and subscribes to this topic, I just need to catch that event somehow.

Any thoughts as to how to do this ?

user1772250
  • 319
  • 5
  • 15

1 Answers1

1

Advisory Message is what you need. each time you got a message with this code this means you have new consumer starting or stopping.

doc http://activemq.apache.org/advisory-message.html

example:

    //org.apache.activemq.advisory.AdvisorySupport.getDestinationAdvisoryTopic(Destination)
Destination advisoryDestination = AdvisorySupport.getConsumerAdvisoryTopic(topic )
MessageConsumer consumer = session.createConsumer(advisoryDestination);
consumer.setMessageListener(this);

public void onMessage(Message msg){
    if (msg instanceof ActiveMQMessage){
        try {
            ActiveMQMessage aMsg =  (ActiveMQMessage)msg;
            ConsumerInfo consumer = (ConsumerInfo) aMsg.getDataStructure();
        } catch (JMSException e) {
            log.error("Failed to process message: " + msg);
        }
    }
}
Hassen Bennour
  • 3,885
  • 2
  • 12
  • 20
  • i need to listen on the Broker, not on the consumer. the broker doesn't know which topic the consumer will be subscribing to ahead of time. this doesn't look like what i want ? – user1772250 Oct 05 '16 at 15:57
  • can you explain please, what i understood is **So i know the consumer connects, and subscribes to this topic, I just need to catch that event somehow.** and my code connect to the broker and listen on it, what do you mean by **i need to listen on the Broker, not on the consumer.** – Hassen Bennour Oct 05 '16 at 16:07
  • if you need this event without knowing the destination in advance you can replace `Destination advisoryDestination = AdvisorySupport.getConsumerAdvisoryTopic(topic );` by `Topic advisoryDestination = session.createTopic("ActiveMQ.Advisory.Consumer.Topic.*");` to be notified for any subscription to any topic in the broker and with ConsumerInfo you can retrieve another informations like destination of the consumer `consumer.getDestination();` – Hassen Bennour Oct 05 '16 at 16:16
  • i have a broker in code, with no producers and no topics. once a consumer connects to the broker looking to subscribe to a specific topic, i want the broker to spawn a producer connected to that topic. to do this i need the detect when a consumer connects to the broker, once that happens then the broker can spawn the relevant producer. does that make sense ? – user1772250 Oct 05 '16 at 16:17
  • sorry but i dont understand the definition of spawn and **spawn the relevant producer** :) can you use another term – Hassen Bennour Oct 05 '16 at 16:22
  • nevermind i figured it out. in the broker itself i need a consumer of AdvisoryTopic messgaes, from there I can handle it. thanks! – user1772250 Oct 05 '16 at 16:46
  • Yes this my last proposition – Hassen Bennour Oct 05 '16 at 16:52
  • agreed, it wasnt clear where that consumer should be placed, but now I've got it and everything is working thanks for your help! – user1772250 Oct 05 '16 at 17:32
  • Does anyone know how to find if the advisory message indicates that the consumer has started or stopped? thx – beta-brad Apr 18 '19 at 20:57
  • @beta-brad certainly, here http://activemq.apache.org/advisory-message.html – Hassen Bennour Apr 23 '19 at 20:00