Skip to content

How to make the consumer keep listening on a specific queue #29

Description

@dahaier

Hi,

I'd like to use this rabbitmq_client to create consumer to keep the connection for listening a queue, in this way I can keep processing the message when someone send message to queue.

Before I use pure pika to do so like below, I'd like to know how to do this using RMQConsumer.

def callback(ch, method, properties, body):
start = time.time()
logger.info("Start processing converter task at {}".format(start))
message = body.decode()
msg = json.loads(message)
logger.info(f"msg: {msg}")
session_uuid = msg['source']['session_uuid']
gogeta_output_dir = msg['source']['output_path']
try:
execute_main(session_uuid)
except Exception as e:
logger.error(f"error {e} when processing {session_uuid}")
ch.basic_ack(delivery_tag=method.delivery_tag)
logger.info("gogetasil run end")

Listenner for processing MQ message

def mq_listenner():
credentials = pika.PlainCredentials(MQ_USER, MQ_PASS)
try:
logger.info("Build connection with MQ")
connection = pika.BlockingConnection(pika.ConnectionParameters(host=config.AMQP_host,
virtual_host='zf',
credentials=credentials,
heartbeat=0))
logger.info("Build channel")
channel = connection.channel()
channel.basic_qos(prefetch_count=1)
logger.info("Listen AUTOSAR SIL channel")
channel.basic_consume(Q_NAME, callback, auto_ack=False)
logger.info("Start consuming message...")
# logger.error(1)
channel.start_consuming()
except Exception as e:
logger.error("MQ connection error!!")
logger.error("Reason: %s" % e)
logger.error("Stop AUTOSAR SIL Operator :(")

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions