java - JMSTemplate 检查主题是否存在并获取订阅者数量

标签 java spring jms activemq publish-subscribe

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

我使用以下代码向主题发送消息 -

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

                return message;
            }
        });

这段代码似乎创建了主题并向主题发布消息。

  1. 我需要在创建主题之前检查该主题是否存在。
  2. 检查该主题是否有订阅者。

提前致谢

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

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

最佳答案

要获取目的地名称,正如您所做的那样,它是正确的,您可以通过 JMX 来专门获取订阅者计数等统计信息...

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

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.

关于java - JMSTemplate 检查主题是否存在并获取订阅者数量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44707064/

相关文章:

java - 在 Spring boot 中添加 catch block 的代码覆盖率

java - JMS - 在消费者使用之前浏览队列消息

java - 设置后的属性始终为nil

java - @ConditionalOnProperty 可以使用嵌套属性值吗?

java - 如何让 Spring 为我的 bean 创建 AOP 代理

spring - 配置多个 DefaultJmslistenercontainerfactory

java - 如何在jms上支持事务/故障转移?

java - 错误 "E/MediaPlayer: Should have subtitle controller already set"导致没有音频播放

java - 使用 aws sns 服务发送短信

java - 如何从 Xades4J 签名中的签名元素中删除 namespace ?