kslr
V2EX  ›  问与答

AWS SQS 是否有办法在处理时通过 AttributeName 过滤想要的数据的呢?

  •  
  •   kslr · Oct 19, 2017 · 1085 views
    This topic created in 3134 days ago, the information mentioned may be changed or developed.

    提交消息的时候 message 携带了消息属性字段,比如由谁谁谁来单独处理。

    {
      'Id': '1',
      'MessageBody': 'world'
    },
    {
      'Id': '2',
      'MessageBody': 'boto3',
      'MessageAttributes': {
        'Author': {
          'StringValue': 'Daniel',
          'DataType': 'String'
         }
       }
    }
    

    但是发现在处理的时候只能根据属性名称过滤,没有办法根据内容处理。

    for message in queue.receive_messages(MessageAttributeNames=['Author']):
        # Get the custom author message attribute if it was set
        author_text = ''
        if message.message_attributes is not None:
            author_name = message.message_attributes.get('Author').get('StringValue')
            if author_name:
                author_text = ' ({0})'.format(author_name)
    
        # Print out the body and author (if set)
        print('Hello, {0}!{1}'.format(message.body, author_text))
    
        # Let the queue know that the message is processed
        message.delete()
    

    假如每次都要拿到数据判断一下再放回去,这样进程就会陷入恶性竞争吧?

    速度比较快的 work 永远让消息处于不可用中,有没有办法只获得某种特殊类型的消息呢?

    比如 Author = Daniel ?

    No Comments Yet
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   5746 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 52ms · UTC 06:18 · PVG 14:18 · LAX 23:18 · JFK 02:18
    ♥ Do have faith in what you're doing.