RocketMQ rocketmq-tools管理主题
环境和软件版本
Win10
、IDEA
、Jdk1.8
、rocketmq 5.1.3
、rocketmq-tools 5.1.3
引入依赖
xml
<dependency>
<groupId>org.apache.rocketmq</groupId>
<artifactId>rocketmq-tools</artifactId>
<version>5.1.3</version>
</dependency>
增删改查
- 启动
rocketmq
sh
mqnamersrv.cmd
mqbroker.cmd -n localhost:9876
- 测试代码
创建/更新主题
java
@Test
@DisplayName("创建/修改主题")
public void rocketMQCreateTopic() throws Exception {
DefaultMQAdminExt adminExt = new DefaultMQAdminExt();
adminExt.setNamesrvAddr("localhost:9876");
adminExt.start();
TopicConfig topicConfig = new TopicConfig();
topicConfig.setTopicName("testTopic0");
topicConfig.setReadQueueNums(8);
topicConfig.setWriteQueueNums(8);
topicConfig.setPerm(6);
Set<String> brokerAddress = CommandUtil.fetchMasterAddrByClusterName(adminExt, "DefaultCluster");
for (String address : brokerAddress) {
System.out.printf("Broker地址:%s\n", address);
adminExt.createAndUpdateTopicConfig(address, topicConfig);
}
}
查询主题
java
@Test
@DisplayName("查询主题")
public void rocketMQGetTopic() throws Exception {
DefaultMQAdminExt adminExt = new DefaultMQAdminExt();
adminExt.setNamesrvAddr("localhost:9876");
adminExt.start();
TopicList topicList = adminExt.fetchAllTopicList();
Set<String> topicNameList = topicList.getTopicList();
for (String topicName : topicNameList) {
System.out.printf("主题:%s\n", topicName);
}
}
删除主题
java
@Test
@DisplayName("删除主题")
public void rocketMQDeleteTopic() throws Exception {
DefaultMQAdminExt adminExt = new DefaultMQAdminExt();
adminExt.setNamesrvAddr("localhost:9876");
adminExt.start();
adminExt.deleteTopic("testTopic0", "DefaultCluster");
// 删除之后再查一遍
TopicList topicList = adminExt.fetchAllTopicList();
Set<String> topicNameList = topicList.getTopicList();
for (String topicName : topicNameList) {
System.out.printf("主题:%s\n", topicName);
}
}