简体   繁体   中英

Get specific message from queue(Rabbitmq) which is in json from a bunch of mesggae in the queue,python

Suppose I have two scripts - one is for cloudfront(cf),and another elb.I have some assigned value in json inside a queue(Rabbitmq), called request queue.when I run my elb scripts it should get only the msg with file_type=elb

{'file_type':'elb','elb_name':'name1','customer_id':'1','s3_bucket':'elb-logs-bucket'}

when I run my cf scripts it should get only the msg with file_type=cf

{'file_type':'cf','cf_name':'name1','customer_id':'1','s3_bucket':'cf-logs-bucket'}

My below code works only for the first msg I published to the queue, not type.Anyone can provide the idea what can I do to get file_type=cf msg from cf scripts and vice-versa..I am using default exchange

import json
import pika
import logging

def get_rmq_request_queue(channel,connection,req_queue_name):
    payload_dict={}
    try:
        queue_message=channel.basic_get(queue=req_queue_name,no_ack=False)
        payload=queue_message[2]
        print payload
        print payload
        payload_dict=json.loads(payload)
        return payload_dict

    except Exception as e:
        payload_dict={}
        payload_dict['error']=str(e)        
        return payload_dict

main():
res_req_que=get_rmq_request_queue(channel,connection,req_queue_name)
    if 'error' not in res_req_que:
        s3_bucket=res_req_que['s3_bucket']
        customer_id=res_req_que['customer_id']
        file_type=res_req_que['file_type']
        cf_name=res_req_que['cf_name']

I might have many messages in a queue, the challenge is I need to get the recent message with file type...

One of the points of RabbitMQ is that you can define queues to bind to exchanges with particular routing keys. You should use the type parameter as part of your routing key, then bind your queue so that it listens to the type you want.

See for example part 4 of the RabbitMQ tutorial .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM