12

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".

John Rotenstein
  • 241,921
  • 22
  • 380
  • 470
Rajat
  • 141
  • 1
  • 1
  • 7

4 Answers4

13
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.

Michael - sqlbot
  • 169,571
  • 25
  • 353
  • 427
11

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

Amit Meena
  • 2,884
  • 2
  • 21
  • 33
0

It depends on how the message in question gets onto the queue. If you are pushing the message via SNS then yes you can filtered messages; https://docs.aws.amazon.com/sns/latest/dg/message-filtering.html

Any other filtering mechanism doesn't exist right now.

Hope that helps!

-4

As per the AWS SDK method, we can use following code to do the filter.

 ReceiveMessageRequest receiveMessageRequest = new ReceiveMessageRequest("QUEUE URL" );        
    receiveMessageRequest.setMaxNumberOfMessages(Integer.valueOf(1));
private static AmazonSQS sqs;   
List<Message> messages = sqs.receiveMessage(receiveMessageRequest.withMessageAttributeNames("Attribute Name")).getMessages();

If you want all the message then use given code

    ReceiveMessageRequest receiveMessageRequest = new ReceiveMessageRequest("QUEUE URL" );        
    receiveMessageRequest.setMaxNumberOfMessages(Integer.valueOf(1));
private static AmazonSQS sqs;   
List<Message> messages = sqs.receiveMessage(receiveMessageRequest.withMessageAttributeNames("All")).getMessages();
Vasanth Umapathy
  • 1,571
  • 14
  • 7