Consistent subscription relationships

更新时间:
复制 MD 格式

A consistent subscription relationship requires all consumer instances within the same consumer Group ID to subscribe to the same topics and tags, and use the same message processing logic. Inconsistent subscription relationships can lead to unpredictable message consumption logic and even message loss. This topic provides code examples of incorrect subscription relationships to help you subscribe to messages correctly.

订阅关系一致

Background information

In SOFAStack Message Queue, a consumer Group ID represents a consumer group. In most distributed applications, multiple consumer instances are attached to a single consumer Group ID.

A subscription relationship in a message queue consists of a topic and a tag. To maintain a consistent subscription relationship, all consumer instances within the same consumer Group ID must be consistent in the following two aspects:

  • The subscribed topics must be identical.

  • The tags for each subscribed topic must be identical.

Example of a correct subscription relationship

Multiple Group IDs subscribe to multiple topics. Within each Group ID, the subscription relationships for all consumer instances are consistent.

正确订阅关系

Example of an incorrect subscription relationship

A single Group ID subscribes to multiple topics, but the subscription relationships for the consumer instances within that Group ID are inconsistent.

错误订阅关系

Code examples of incorrect subscription relationships

Example 1

Two consumer instances in the same group subscribe to different topics.

  • Consumer 1-1:

      Properties properties = new Properties();
      properties.put(PropertyKeyConst.GROUP_ID, "GID_jodie_test_1");
      Consumer consumer = OMS.builder().driver("sofamq").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:

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

Example 2

The tags for a subscribed topic are inconsistent within the same group. Consumer 2-1 subscribes to TagA, while Consumer 2-2 does not specify a tag.

  • Consumer 2-1:

      Properties properties = new Properties();
      properties.put(PropertyKeyConst.GROUP_ID, "GID_jodie_test_2");
      Consumer consumer = OMS.builder().driver("sofamq").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:

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

Example 3

The number of subscribed topics is inconsistent within the same group.

  • Consumer 3-1:

      Properties properties = new Properties();
      properties.put(PropertyKeyConst.GROUP_ID, "GID_jodie_test_3");
      Consumer consumer = OMS.builder().driver("sofamq").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;
          }
      });

The tags for a subscribed topic are inconsistent within the same group.

  • Consumer 3-2:

      Properties properties = new Properties();
      properties.put(PropertyKeyConst.GROUP_ID, "GID_jodie_test_3");
      Consumer consumer = OMS.builder().driver("sofamq").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;
          }
      });