You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

如何按需获取连接ActiveMQ Broker的所有持久化客户端Client ID?

Absolutely, there are several practical ways to retrieve the list of Client IDs for currently connected persistent clients in ActiveMQ. Let me walk you through the most reliable approaches:

方法1:通过ActiveMQ Web控制台(最直观)

这是最适合快速查看的方式,不需要额外工具:

  • 打开浏览器访问ActiveMQ Web控制台,默认地址是http://localhost:8161/admin(替换localhost为你的Broker主机IP/域名)
  • 登录后,点击左侧菜单栏的Connections选项
  • 在Connections列表中,找到Persistent列(标记为Y表示该客户端是持久化类型)
  • 直接查看并提取对应行的Client ID列内容即可,你甚至可以复制整列数据导出使用
方法2:通过JMX查询(适合脚本/程序化场景)

ActiveMQ exposes rich JMX MBeans for monitoring and management, which is a powerful way to fetch client info:

图形化工具(jconsole)

  • 启动本地的jconsole工具(自带在JDK中)
  • 在连接界面选择"远程进程",输入JMX连接地址:service:jmx:rmi:///jndi/rmi://<broker-host>:1099/jmxrmi(默认JMX端口是1099)
  • 登录后(如果Broker有认证,输入对应账号密码),展开org.apache.activemq -> Broker -> 你的Broker名称 -> Connections
  • 逐个查看每个连接的属性:找到clientIdisPersistent,筛选出isPersistenttrue的条目,记录对应的clientId

命令行工具(jmxterm)

If you need batch or automated retrieval, use tools like jmxterm:

  1. 下载jmxterm并启动:java -jar jmxterm-1.0.4-uber.jar
  2. 连接到Broker的JMX端口:open <broker-host>:1099
  3. 查询所有连接MBean:beans org.apache.activemq:BrokerName=*,Type=Connection,*
  4. 遍历连接获取属性:
foreach -b org.apache.activemq:BrokerName=localhost,Type=Connection,* -- echo "Client ID: {clientId}, Persistent: {isPersistent}"

之后你可以用文本过滤工具(比如grep)提取出Persistent: true对应的Client ID。

方法3:通过ActiveMQ API编写自定义程序

If you need to integrate this logic into your own application, use ActiveMQ's JMX API to write a custom program:
Here's a simple Java example:

import javax.management.MBeanServerConnection;
import javax.management.ObjectName;
import javax.management.remote.JMXConnector;
import javax.management.remote.JMXConnectorFactory;
import javax.management.remote.JMXServiceURL;
import java.util.Set;

public class PersistentClientIdFetcher {
    public static void main(String[] args) throws Exception {
        // Replace with your Broker's JMX URL
        JMXServiceURL jmxUrl = new JMXServiceURL("service:jmx:rmi:///jndi/rmi://localhost:1099/jmxrmi");
        
        // Uncomment below if your Broker requires authentication
        // Map<String, Object> env = new HashMap<>();
        // env.put(JMXConnector.CREDENTIALS, new String[]{"admin", "admin"});
        // JMXConnector connector = JMXConnectorFactory.connect(jmxUrl, env);
        
        JMXConnector connector = JMXConnectorFactory.connect(jmxUrl);
        MBeanServerConnection mBeanConn = connector.getMBeanServerConnection();

        // Query all connection MBeans
        ObjectName connectionPattern = new ObjectName("org.apache.activemq:BrokerName=localhost,Type=Connection,*");
        Set<ObjectName> connectionMBeans = mBeanConn.queryNames(connectionPattern, null);

        // Filter and print persistent client IDs
        for (ObjectName connBean : connectionMBeans) {
            Boolean isPersistent = (Boolean) mBeanConn.getAttribute(connBean, "isPersistent");
            if (Boolean.TRUE.equals(isPersistent)) {
                String clientId = (String) mBeanConn.getAttribute(connBean, "clientId");
                System.out.println("Persistent Client ID: " + clientId);
            }
        }

        connector.close();
    }
}

Note: Running this code requires dependencies like activemq-client and JMX-related libraries.

Key Notes
  • Make sure JMX and Web Console are enabled on your Broker (they're enabled by default; if disabled, configure them in activemq.xml)
  • If your Broker uses access authentication, use the correct username/password when accessing the Web Console, JMX, or API
  • The "persistent clients" here refer to currently online and marked as persistent connections. If you need all clients that have ever created persistent subscriptions (including offline ones), query subscription-related MBeans like org.apache.activemq:Type=Subscription,*

内容的提问来源于stack exchange,提问作者learner

火山引擎 最新活动