使用pika发布消息时如何添加标头键:值对 [英] How to add a header key:value pair when publishing a message with pika

查看:242
本文介绍了使用pika发布消息时如何添加标头键:值对的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个自动测试来测试消费者.到目前为止,发布消息时我不需要包括标题,但现在可以了.似乎缺少相关文档.

I am writing an automated test to test a consumer. So far I did not need to include a header when publishing messages but now I do. And it seems like its lacking documentation.

这是我的发布者:

class RMQProducer(object):

    def __init__(self, host, exchange, routing_key):
        self.host = host
        self.exchange = exchange
        self.routing_key = routing_key

    def publish_message(self, message):
        connection = pika.BlockingConnection(pika.ConnectionParameters(self.host))
        channel = connection.channel()
        message = json.dumps(message)
        channel.basic_publish(exchange=self.exchange,
                              routing_key=self.routing_key,
                              body=message)

我想像这样做:

channel.basic_publish(exchange=self.exchange,
                      routing_key=self.routing_key,
                      body=message,
                      headers={"key": "value"})

将标头添加到此消息的正确方法是什么?

Whats the correct way to add headers to this message?

推荐答案

您将使用有关pika的官方文档确实并未完全涵盖此情况,但是文档中确实列出了规格.如果您要继续使用,我强烈建议您在此页面上添加书签皮卡.

The official documentation for pika does indeed not cover this scenario exactly, but the documentation does have the specifications listed. I would would strongly recommend that you bookmark this page, if you are going to continue using pika.

这篇关于使用pika发布消息时如何添加标头键:值对的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆