JMSTemplate检查主题是否存在并获取订阅者计数 [英] JMSTemplate check if topic exists and get subscriber count

查看:126
本文介绍了JMSTemplate检查主题是否存在并获取订阅者计数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在寻找一些文档/示例来检查是否存在动态创建的主题,如果存在,如何获取该主题的订阅者计数.

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. 创建主题之前,我需要检查主题是否存在.
  2. 检查主题是否有订阅者.

预先感谢

我能够找到解决(1)问题的方法(希望有帮助)-

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();

推荐答案

要获得目的地名称(如您所愿,这是正确的),您也可以通过JMX来专门进行操作,以获取诸如订阅者数量之类的统计信息...

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());
            }
        }
    }
}

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) {
                }
            }
        }
    }
}

更新

您可以使用AdvisorySupport.getConsumerAdvisoryTopic()

请注意,消费者启动/停止咨询消息还包含一个 consumerCount标头指示活动消费者的数量 咨询消息发送到的目的地.

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.

这篇关于JMSTemplate检查主题是否存在并获取订阅者计数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆