I know SQS ain't build for that, but I'm curious is it possible to find messages in a queue that meet some criteria?
I can pull messages in a loop, search the message bodies for some pattern (without even deserializing them), and filter the messages I needed. But then it is possible to end up with an infinite loop - the first messages I read will be back to the queue by the time when I reach the end of the queue...
Extending visibility of the messages possible, but how do I know exactly how long it will take to scan the entire queue, and for how long should I extend the visibility? What if I have literally ten thousand messages in there?
Is there any workaround here? I need to scan the queue for some messages, and delete those...
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.
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.
It is not possible to 'search' messages in an Amazon SQS queue. A ReceiveMessage() call will retrieve one or more messages, but there is no ability to control which messages will be returned.
Short answer: no.
Queues are designed for things like tasks. A machine grabs a new task (i.e., message) from the queue, executes the task, then deletes the task.
If you're trying to search the messages to filter them, I can't help but wonder if you're using the wrong tool for the job…
I do not think the short or long answers are "No".
Here are two counterpoint solutions that are "Yes".
1. Traversing the Queue, maintaining a visited list
Consider the case of a queue with N
messages, with no messages being added or deleted. Without additional information (e.g. if you knew how many messages should match your criteria), you need to traverse all N
messages.
The key point here is knowing when you've traversed all N
messages. There are some issues here.
ApproximateNumberOfMessages
attribute of the queueTo maintain the visited list, as you receive messages and evaluate your match criteria, you could store the message_id
of all visited messages.
Message ID's are nearly unique. See this thread
https://forums.aws.amazon.com/message.jspa?messageID=76119
If you went with (3), you wouldn't be certain how many iterations would be required to exhaust the queue. However, if you performed this indefinitely, you'd be guaranteed to exhuast the queue so long as the weighted random distribution over the SQS shard servers gives them all non-zero probability.
2. Using Enterprise Integration Patterns (message routing) to split your messages into downstreams based on criteria
If you have control over your messaging architecture you could use a Message Router as a "front-end" message processor that dispatches messages to various recipients based on criteria.
And specifically you'd use a Content-Based Router:
http://www.enterpriseintegrationpatterns.com/patterns/messaging/ContentBasedRouter.html
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With