edit-icon download-icon

Message filtering

Last Updated: Jun 25, 2018

This topic describes how consumers filter messages on a MQ server according to tags.

A tag is a label that classifies messages into different types under an MQ topic. By enabling message filtering by tag, MQ ensures consumers consume only the messages of the types they require.

Take an e-commerce transaction as an example, the process from placing an order to receiving the product by the user will produce a series of messages, such as messages about order creation, payment, and logistics. These messages will be sent to the queue with the topic name of Trade_Topic and received by different systems, such as the payment system, logistics system, transaction success rate analysis system, and real-time computing system. Among them, the logistics system only needs to receive the logistics message, and the real-time computing system requires all the messages related to the transaction (order, payment, and logistics).

Message filtering

Note: To classify messages, you can create multiple topics, or create multiple tags under the same topic. However, in general, there is no relationship between messages in different topics, and tags are used to distinguish related messages within the same topic, such as the relationship between the full set and the subset, and the relationship between the processes in sequence.

Sample codes

Sending Messages

When sending a message, tag must be specified for each message:

  1. Message msg = new Message("MQ_TOPIC","TagA","Hello MQ".getBytes());

Consumption Method - 1

If a consumer needs to subscribe to all types of messages under a topic, the * symbol can be used to represent the tags:

  1. consumer.subscribe("MQ_TOPIC", "*", new MessageListener() {
  2. public Action consume(Message message, ConsumeContext context) {
  3. System.out.println(message.getMsgID());
  4. return Action.CommitMessage;
  5. }
  6. });

Consumption Method - 2

If a consumer needs to subscribe to a certain type of messages under a topic, the tag should be specified:

  1. consumer.subscribe("MQ_TOPIC", "TagA", new MessageListener() {
  2. public Action consume(Message message, ConsumeContext context) {
  3. System.out.println(message.getMsgID());
  4. return Action.CommitMessage;
  5. }
  6. });

Consumption Method - 3

If a consumer needs to subscribe to multiple types of messages under a topic, “|” can be used to separate tags:

  1. consumer.subscribe("MQ_TOPIC", "TagA||TagB", new MessageListener() {
  2. public Action consume(Message message, ConsumeContext context) {
  3. System.out.println(message.getMsgID());
  4. return Action.CommitMessage;
  5. }
  6. });

Consumption Method - 4 (error example)

If a consumer subscribes to messages with different tags under a topic for multiple times, the latter will overwrite the former:

  1. // As shown in the following erroreous code, the consumer can only receive messages with TagB under MQ_TOPIC, but cannot receive messages with TagA.
  2. consumer.subscribe("MQ_TOPIC", "TagA", new MessageListener() {
  3. public Action consume(Message message, ConsumeContext context) {
  4. System.out.println(message.getMsgID());
  5. return Action.CommitMessage;
  6. }
  7. });
  8. consumer.subscribe("MQ_TOPIC", "TagB", new MessageListener() {
  9. public Action consume(Message message, ConsumeContext context) {
  10. System.out.println(message.getMsgID());
  11. return Action.CommitMessage;
  12. }
  13. });
Thank you! We've received your feedback.