Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can we filter messages from Amazon SQS queue by message attributes?

For now I have tried to filter the messages based on Message Attribute Name="Class". As you can see in the below code

//Specify attribute list
        List<string> AttributesList = new List<string>();
        AttributesList.Add("Class");
        receiveMessageRequest.MessageAttributeNames = AttributesList;
        receiveMessageRequest.QueueUrl = urlSQS;
        receiveMessageRequest.MaxNumberOfMessages = 10;
        ReceiveMessageResponse receiveMessageResponse = objClient.ReceiveMessage(receiveMessageRequest);

But the messages are not been filtered based on the provided MessageAttributeName = "class".

like image 795
Rajat Avatar asked May 17 '18 10:05

Rajat


People also ask

Can I filter messages in SQS?

Now that all SNS and SQS resources have been created, you are ready to set filter policies to your SNS subscriptions. A filter policy is simple JSON document, set as an attribute of the SNS subscription, which defines the type of notification the subscriber is interested in.

What is the use of message attributes in SQS?

You can use message attributes to attach custom metadata to Amazon SQS messages for your applications. You can use message system attributes to store metadata for other AWS services, such as AWS X-Ray.

Can I view messages on SQS queue?

Amazon SQS begins to poll servers to find messages in the queue. The progress bar on the right side of the Receive messages section displays the polling duration. The Messages section displays a list of the received messages. For each message, the list displays the message ID, sent date, size, and receive count.

How do I pull a message from SQS?

To receive and delete a message (console) Open the Amazon SQS console at https://console.aws.amazon.com/sqs/ . In the navigation pane, choose Queues. On the Queues page, choose a queue. Choose Send and receive messages.

How do I subscribe to a message queue in Amazon SQS?

In the dialog box, select the topic from the Choose a Topic drop-down list, and click Subscribe. For more information, see Subscribing a Queue to an Amazon SNS Topic in the Amazon SQS Developer Guide. Q: Can I delete all messages in a message queue without deleting the message queue itself? Yes.

How does Amazon SQS handle messages that can't be processed?

Q: How does Amazon SQS handle messages that can't be processed? In Amazon SQS, you can use the API or the console to configure dead letter queues, which receive messages from other source queues.

What is authentication mechanism in Amazon SQS?

Authentication mechanisms ensure that messages stored in Amazon SQS message queues are secured against unauthorized access. You can control who can send messages to a message queue and who can receive messages from a message queue. For additional security, you can build your application to encrypt messages before they are placed in a message queue.

What are the different types of message attributes in Amazon SQS?

Amazon SQS message attributes take the form of name-type-value triples. The supported types include string, binary, and number (including integer, floating-point, and double). For more information, see Using Amazon SQS Message Attributes in the Amazon SQS Developer Guide.


2 Answers

receiveMessageRequest.MessageAttributeNames = AttributesList;

This tells SQS which message attributes you want it to return with the message if the are present on the message. It is not a message filter. If the attributes aren't present, nothing happens.

But your confusion seems understandable -- it's not actually obvious why the API even has this functionality, though it may be a holdover from when SQS supported only smaller messages than it does today, or it may be so that you can avoid spending any time parsing information from the response that you will end up discarding. I almost always just ask for All.

like image 187
Michael - sqlbot Avatar answered Sep 19 '22 17:09

Michael - sqlbot


Please note this regarding messaging services on AWS

SQS : No filtering support ( while picking up messages)

SNS : Supports attributes-based filtering: subscriber can set a subscription attribute (a subscription filter policy) which is applied on incoming messages and only relevant messages can be sent to the subscriber.

EventBridge: Amazon EventBridge supports declarative filtering using event patterns. With event pattern content filtering you can write complex rules that only trigger under very specific conditions. For instance, you might want a rule that will trigger only when a field of the event is within a specific numeric range, if the event comes from a specific IP address, or only if a specific field does not exist in the event JSON.

Please refer my article for a detailed difference between main messaging frameworks on AWS.

https://www.linkedin.com/pulse/mastering-art-decoupling-application-architecture-aws-amit-meena/

enter image description here

like image 33
Amit Meena Avatar answered Sep 21 '22 17:09

Amit Meena