All Products
Search
Document Center

Message Service:Message filtering

Last Updated:Apr 09, 2024

The message filtering feature of Message Service (MNS) is used to push messages of a topic to different queues by using tags. This topic describes the message filtering feature and how to configure tags for message filtering.

Background information

In normal cases, MNS pushes messages to the specified queue after you create a subscription in a topic. All messages are pushed to the specified queue if no tag is configured for the messages and no message filtering tag is configured for the subscription.

If you configure a message filtering tag for the subscription, a message is pushed to the specified queue only if you configure the same tag for the message.

In special cases, if you do not configure a message filtering tag for the subscription, a message is pushed to the specified queue regardless of whether you configure a tag for the message.

Feature description

The following figure shows how messages that have different tags are filtered based on the message filtering tags of subscriptions and pushed to corresponding destination queues.

过滤标签

In Topic 1 in this example, Subscription 1, Subscription 2, and Subscription 3 are created with different message filtering tags. The destination queues of the three subscriptions are Queue 1, Queue 2, and Queue 3.

  • If the tag of a message is the same as the message filtering tag of a subscription, the message is pushed to the queue that is specified by the subscription. Details:

    • MNS pushes Message 1 to Queue 1.

    • MNS pushes Message 2 to Queue 2.

  • If no message filtering tag is configured for a subscription, all messages are pushed to the queue that is specified by the subscription. Details:

    • MNS pushes Message 1 to Queue 3.

    • MNS pushes Message 2 to Queue 3.

    • MNS pushes Message 3 to Queue 3.

Filter messages

In this example, the tag-a tag is configured for a subscription to Topic A so that only messages that have the tag-a tag are pushed to the specified Queue B. For more information about how to subscribe to a topic, see Create a subscription.

  1. In the Create Subscription panel of Topic A, select Queue from the Push Type drop-down list, select Queue B from the Receiver Endpoint drop-down list, and enter tag-a in the Message Filtering Tag field.

    image.png

  2. If you use an SDK to subscribe to messages, add the tag-a tag to the messages that you want to push to Queue B. For more information about the complete sample code, see Step 5: Publish a message.

    The following sample code adds the tag-a tag to the messages to be pushed to Queue B:

    	TopicMessage message = new Base64TopicMessage();
    
        // The tag must be the same as the message filtering tag that you configure for the subscription. 
        message.setMessageTag("tag-a");