jjava 查询 集群rocketmq 的所有Topic
时间: 2023-11-24 15:07:04 浏览: 63
您可以使用以下Java代码查询RocketMQ集群中的所有Topic:
```
import org.apache.rocketmq.client.QueryResult;
import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.client.impl.MQAdminImpl;
import org.apache.rocketmq.common.protocol.body.ClusterInfo;
import org.apache.rocketmq.tools.admin.DefaultMQAdminExt;
import org.apache.rocketmq.tools.command.CommandUtil;
import java.util.HashSet;
import java.util.Set;
public class MQTopicQuery {
public static void main(String[] args) {
DefaultMQAdminExt defaultMQAdminExt = new DefaultMQAdminExt();
defaultMQAdminExt.setNamesrvAddr("your_namesrv_address");
try {
defaultMQAdminExt.start();
ClusterInfo clusterInfo = ((MQAdminImpl) defaultMQAdminExt.getDefaultMQAdminImpl()).examineBrokerClusterInfo();
Set<String> topicSet = new HashSet<>();
for (String clusterName : clusterInfo.getClusterAddrTable().keySet()) {
String[] addressArray = CommandUtil.splitAddr(clusterInfo.getClusterAddrTable().get(clusterName));
for (String address : addressArray) {
QueryResult qr = defaultMQAdminExt.queryTopicConsumeByWho(address);
topicSet.addAll(qr.getTopicList());
}
}
System.out.println("All topics: " + topicSet);
} catch (MQClientException e) {
e.printStackTrace();
} finally {
defaultMQAdminExt.shutdown();
}
}
}
```
请将 `your_namesrv_address` 替换为您的NameServer地址。运行后,将打印出RocketMQ集群中的所有Topic。
阅读全文