简体   繁体   中英

JMSTemplate check if topic exists and get subscriber count

I have been looking for some documentation/example for checking if a dynamically created topic exist and if it does, how to get the subscriber count for the topic.

I use following code for sending out message to a topic -

jmsTemplate.send(destination, new MessageCreator() {
            @Override
            public Message createMessage(Session session) throws JMSException {
                TextMessage message = session.createTextMessage();
                message.setText(commandStr);

                return message;
            }
        });

This code seems to create the topic and publish message to topic.

  1. I need to check if the topic exists before creating it.
  2. Check if the topic have any subscriber.

Thanks in advance

i was able to find the solution to (1) problem (Hope this helps)-

ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory("tcp://localhost:61616");
ActiveMQConnection connection = (ActiveMQConnection)connectionFactory.createConnection();
connection.start();
DestinationSource ds = connection.getDestinationSource();
Set<ActiveMQTopic> topics = ds.getTopics();

To get the destination names, as you did it is correct, you can do it by JMX too specifically to get statistical information like subscriber count ...

import java.util.HashMap;
import java.util.Map;

import javax.management.MBeanServerConnection;
import javax.management.MBeanServerInvocationHandler;
import javax.management.ObjectName;
import javax.management.remote.JMXConnector;
import javax.management.remote.JMXConnectorFactory;
import javax.management.remote.JMXServiceURL;

import org.apache.activemq.broker.jmx.BrokerViewMBean;
import org.apache.activemq.broker.jmx.TopicViewMBean;

public class JMXGetDestinationInfos {

    public static void main(String[] args) throws Exception {
        JMXServiceURL url = new JMXServiceURL("service:jmx:rmi:///jndi/rmi://localhost:1099/jmxrmi");
        Map<String, String[]> env = new HashMap<>();
        String[] creds = { "admin", "admin" };
        env.put(JMXConnector.CREDENTIALS, creds);
        JMXConnector jmxc = JMXConnectorFactory.connect(url, env);
        MBeanServerConnection conn = jmxc.getMBeanServerConnection();

        ObjectName activeMq = new ObjectName("org.apache.activemq:type=Broker,brokerName=localhost");

        BrokerViewMBean mbean = MBeanServerInvocationHandler.newProxyInstance(conn, activeMq, BrokerViewMBean.class,
                true);
        for (ObjectName name : mbean.getTopics()) {
            if (("YOUR_TOPIC_NAME".equals(name.getKeyProperty("destinationName")))) {
                TopicViewMBean topicMbean = MBeanServerInvocationHandler.newProxyInstance(conn, name,
                        TopicViewMBean.class, true);
                System.out.println(topicMbean.getConsumerCount());
            }
        }
    }
}

or

import java.util.Set;

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.JMSException;

import org.apache.activemq.ActiveMQConnection;
import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.activemq.advisory.DestinationSource;
import org.apache.activemq.command.ActiveMQQueue;
import org.apache.activemq.command.ActiveMQTopic;

public class AdvisorySupportGetAllDestinationsNames {

    public static void main(String[] args) throws JMSException {
        Connection conn = null;
        try {
            ConnectionFactory cf = new ActiveMQConnectionFactory("tcp://localhost:61616");
            conn = cf.createConnection();
            conn.start();
            DestinationSource destinationSource = ((ActiveMQConnection) conn).getDestinationSource();
            Set<ActiveMQQueue> queues = destinationSource.getQueues();
            Set<ActiveMQTopic> topics = destinationSource.getTopics();
            System.out.println(queues);
            System.out.println(topics);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (conn != null) {
                try {
                    conn.close();
                } catch (Exception e) {
                }
            }
        }
    }
}

UPDATE

You can use AdvisorySupport.getConsumerAdvisoryTopic()

Note that the consumer start/stop advisory messages also have a consumerCount header to indicate the number of active consumers on the destination when the advisory message was sent.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM