I think you want to listen for new producers and consumers on a particular destination (a particular queue or topic). Is that right?
You can instantiate ConsumerEventSource and ProducerEventSource, and register your own listeners by calling their setConsumerListener and setProducerListener on them, respectively.
So:
Connection conn = yourconnection; // the connection your listener will use
Destination dest = yourdestination; // the destination you're paying attention to
ConsumerEventSource source = new ConsumerEventSource(conn, dest);
source.setConsumerListener(new ConsumerListener() {
public void onConsumerEvent(ConsumerEvent event) {
if (event.isStarted()) {
System.out.println("a new consumer has started - " + event.getConsumerId());
} else {
System.out.println("a consumer has dropped - " + event.getConsumerId());
}
}
});
If you look at the code for ConsumerEventSource or ProducerEventSource, you'll see that they're simple objects that use the methods of AdvisorySupport to listen on a special advisory topic whose business it is to broadcast news about producers and consumers. You might learn more by reading the source code for those classes.
Your use of "connection" is potentially a problem; in ActiveMQ land (which is a subset of JMS land), a "Connection" is a lower-level object that isn't associated with a particular destination. A particular client creates a "Session" from a Connection - still not specific to a destination - and then creates a destination-specific QueueSender, QueueReceiver, TopicPublisher, or TopicSubscriber. When those are created, or when the sessions that created them die, those are the events you want to hear about, and will hear about if you use the code above.