随笔 - 1162  文章 - 0  评论 - 16  阅读 - 59万 

一、订阅关系的一致性

订阅关系的一致性指的是,同一个消费者组(Group ID 相同)下所有 Consumer 实例所订阅的 Topic 与 Tag 及对消息的处理逻辑必须完全一致。否则,消息消费的逻辑就会混乱,甚至导致消息丢失。

二、正确订阅关系

多个消费者组订阅了多个 Topic,并且每个消费者组里的多个消费者实例的订阅关系保持了一致。

三、错误订阅关系

一个消费者组订阅了多个 Topic,但是该消费者组里的多个 Consumer 实例的订阅关系并没有保持一致。

1、订阅了不同 Topic

该例中的错误在于,同一个消费者组中的两个 Consumer 实例订阅了不同的 Topic。

Consumer 实例 1-1:(订阅了 topic 为 jodie_test_A,tag 为所有的消息)

    Properties properties = new Properties(); properties.put(PropertyKeyConst.GROUP_ID,"GID_jodie_test_1");
    Consumer consumer = ONSFactory.createConsumer(properties); consumer.subscribe("jodie_test_A","*",
      new MessageListener() {
         public Action consume (Message message, ConsumeContext context){
            System.out.println(message.getMsgID());
            return Action.CommitMessage;
        }
    });

Consumer 实例 1-2:(订阅了 topic 为 jodie_test_B,tag 为所有的消息)

    Integer pageSize = 10;
    Properties properties = new Properties();
    properties.put(PropertyKeyConst.GROUP_ID,"GID_jodie_test_1");
    Consumer consumer = ONSFactory.createConsumer(properties); consumer.subscribe("jodie_test_B","*", 
    new MessageListener() {
         public Action consume (Message message, ConsumeContext context){
            System.out.println(message.getMsgID());
            return Action.CommitMessage;
        }
    });

2、订阅了不同 Tag

该例中的错误在于,同一个消费者组中的两个 Consumer 订阅了相同 Topic 的不同 Tag。

Consumer 实例 2-1:(订阅了 topic 为 jodie_test_A,tag 为 TagA 的消息)

    Properties properties = new Properties(); properties.put(PropertyKeyConst.GROUP_ID,"GID_jodie_test_2");
    Consumer consumer = ONSFactory.createConsumer(properties); consumer.subscribe("jodie_test_A","TagA", 
    new MessageListener() {
         public Action consume (Message message, ConsumeContext context){
            System.out.println(message.getMsgID());
            return Action.CommitMessage;
        }
    });

Consumer 实例 2-2:(订阅了 topic 为 jodie_test_A,tag 为所有的消息)

    Properties properties = new Properties(); properties.put(PropertyKeyConst.GROUP_ID,"GID_jodie_test_2");
    Consumer consumer = ONSFactory.createConsumer(properties); consumer.subscribe("jodie_test_A","*",new

    MessageListener() {
        public Action consume (Message message, ConsumeContext context){
            System.out.println(message.getMsgID());
            return Action.CommitMessage;
        }
    });

3、订阅了不同数量的 Topic

该例中的错误在于,同一个消费者组中的两个 Consumer 订阅了不同数量的 Topic。

Consumer 实例 3-1:(该 Consumer 订阅了两个 Topic)

    Properties properties = new Properties(); properties.put(PropertyKeyConst.GROUP_ID,"GID_jodie_test_3");
    Consumer consumer = ONSFactory.createConsumer(properties); consumer.subscribe("jodie_test_A","TagA",new

    MessageListener() {
        public Action consume (Message message, ConsumeContext context){
            System.out.println(message.getMsgID());
            return Action.CommitMessage;
        }
    }); consumer.subscribe("jodie_test_B","TagB",new

    MessageListener() {
        public Action consume (Message message, ConsumeContext context){
            System.out.println(message.getMsgID());
            return Action.CommitMessage;
        }
    });

Consumer 实例 3-2:(该 Consumer 订阅了一个 Topic)

    Properties properties = new Properties(); properties.put(PropertyKeyConst.GROUP_ID,"GID_jodie_test_3");
    Consumer consumer = ONSFactory.createConsumer(properties); consumer.subscribe("jodie_test_A","TagB",new

    MessageListener() {
        public Action consume (Message message, ConsumeContext context){
            System.out.println(message.getMsgID());
            return Action.CommitMessage;
        }
    });
posted on   格物致知_Tony  阅读(67)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
· DeepSeek R1 简明指南:架构、训练、本地部署及硬件要求
· 没有源码,如何修改代码逻辑?
· NetPad:一个.NET开源、跨平台的C#编辑器
· 面试官:你是如何进行SQL调优的?
历史上的今天:
2021-08-04 07XML解析器—XStream
2021-08-04 01Web服务器简介
2021-08-04 JavaWeb 之 JavaWeb 相关概念
2021-08-04 00Maven【目录】
2019-08-04 JavaScript 运算符(Operator)
2019-08-04 JavaScript 数据类型转换
2019-08-04 JavaScript 数据类型(基本数据类型)
点击右上角即可分享
微信分享提示

目录导航