自学内容网 自学内容网

RocketMQ rocketmq-tools管理主题

RocketMQ rocketmq-tools管理主题

环境和软件版本

Win10IDEAJdk1.8rocketmq 5.1.3rocketmq-tools 5.1.3
引入依赖

<dependency>
    <groupId>org.apache.rocketmq</groupId>
    <artifactId>rocketmq-tools</artifactId>
    <version>5.1.3</version>
</dependency>

增删改查

  1. 启动rocketmq
mqnamersrv.cmd
mqbroker.cmd -n localhost:9876
  1. 测试代码

创建/更新主题

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

在这里插入图片描述
查询主题

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

在这里插入图片描述
删除主题

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

在这里插入图片描述


原文地址:https://blog.csdn.net/q4444tita/article/details/144146328

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!