How to get messages receive count in Amazon SQS using boto library in Python?

前端 未结 5 1919
不知归路
不知归路 2021-02-09 00:29

I am using boto library in Python to get Amazon SQS messages. In exceptional cases I don\'t delete messages from queue in order to give a couple of more changes to recover tempo

相关标签:
5条回答
  • 2021-02-09 00:42

    Other way could be you can put an extra identifier at the end of the message in your SQS queue. This identifier can keep the count of the number of times the message has been read.

    Also if you want that your service should not poll these message again and again then you can create one more queue say "Dead Message Queue" and can transfer then message which has crossed the threshold to this queue.

    0 讨论(0)
  • 2021-02-09 00:47

    There are at least a couple of ways of doing this.

    When you read a message in boto, you receive a Message object or some subclass thereof. The Message object has an "attributes" field that is a dict containing all message attributes known by SQS. One of the things SQS tracks is the approximate # of times the message has been read. So, you could use this value to determine whether the message should be deleted or not but you would have to be comfortable with the "approximate" nature of the value.

    Alternatively, you could record message ID's in some sort of database and increment a count field in the database each time you read the message. This could be done in a simple Python dict if the messages are always being read within a single process or it could be done in something like SimpleDB if you need to record readings across processes.

    Hope that helps.

    Here's some example code:

    >>> import boto.sqs
    >>> c = boto.sqs.connect_to_region()
    >>> q = c.lookup('myqueue')
    >>> messages = c.receive_message(q, num_messages=1, attributes='All')
    >>> messages[0].attributes
    {u'ApproximateFirstReceiveTimestamp': u'1365474374620',
     u'ApproximateReceiveCount': u'2',
     u'SenderId': u'419278470775',
     u'SentTimestamp': u'1365474360357'}
    >>>
    
    0 讨论(0)
  • 2021-02-09 00:56

    Get ApproximateReceiveCount attribute from message you read. move it to another queue(than you can manage error messages) or just delete it.

    foreach (var message in response.Messages){
           try{
               var notifyMessage = JsonConvert.DeserializeObject<NotificationMessage>(message.Body);
                        Global.Sqs.DeleteMessageFromQ(message.ReceiptHandle);
               }
           catch (Exception ex){
               var  receiveMessageCount = int.Parse(message.Attributes["ApproximateReceiveCount"]);
               if (receiveMessageCount >3 ) 
                  Global.Sqs.DeleteMessageFromQ(message.ReceiptHandle);
                }
            }
    
    0 讨论(0)
  • 2021-02-09 01:00

    aws has in-built support for this, just follow the below steps:

    1. create a dead letter queue
    2. enable Redrive policy for the source queue by checking "Use Redrive Policy"
    3. select the dead letter queue you created in step#1 for "Dead Letter Queue"
    4. Set "Maximum Receives" as "3" or any value between 1 and 1000

    How it works is, whenever a message is received by the worker, the receive count increments. Once it reaches "Maximum Receives" count, the message is pushed to the dead letter queue. Note, even if you access the message via aws console, the receive count increments.

    Source Using Amazon SQS Dead Letter Queues

    0 讨论(0)
  • 2021-02-09 01:06

    It should be done in few steps.

    1. create SQS connection :- sqsconnrec = SQSConnection(AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY)
    2. create queue object :- request_q = sqsconnrec.create_queue("queue_Name")
    3. load the queue messages :- messages= request_q.get_messages()
    4. now you get the array of message objects and to find the total number of messages :- just do len(messages)

    should work like charm.

    0 讨论(0)
提交回复
热议问题