在两个 RabbitMQ 队列中发布消息,而不是一个(使用 py-amqp)

发布于 2024-08-30 13:48:14 字数 1078 浏览 8 评论 0原文

我使用 py-amqpFlopsy 模块。我编写了一个将消息发送到 RabbitMQ 服务器的发布者,并且我希望能够将其发送到指定的队列。在 Flopsy 模块上这是不可能的,所以我对其进行了调整,添加了一个参数和一行来在 Publisher 对象的 _init__ 方法上声明队列

    def __init__(self, routing_key=DEFAULT_ROUTING_KEY,
                  exchange=DEFAULT_EXCHANGE, connection=None,
                  delivery_mode=DEFAULT_DELIVERY_MODE, queue=DEFAULT_QUEUE):
        self.connection = connection or Connection()
        self.channel = self.connection.connection.channel()
        self.channel.queue_declare(queue) # ADDED TO SET UP QUEUE
        self.exchange = exchange
        self.routing_key = routing_key
        self.delivery_mode = delivery_mode

通道对象是 py-amqplib 库的一部分

我遇到的问题是,即使它将消息发送到指定队列,它也会将消息发送到默认队列。在这个系统中,我们希望发送大量消息,我们不想给系统造成无用的重复......我尝试调试代码并进入 py-amqplib 库,但我无法找出任何错误或缺少的步骤。另外,我无法在代码之外找到 py-amqplib 的任何文档。

关于为什么会发生这种情况以及如何纠正它有什么想法吗?

I've got this strange problem using py-amqp and the Flopsy module. I have written a publisher that sends messages to a RabbitMQ server, and I wanted to be able to send it to a specified queue. On the Flopsy module that is not possible, so I tweaked it adding a parameter and a line to declare the queue on the _init__ method of the Publisher object

    def __init__(self, routing_key=DEFAULT_ROUTING_KEY,
                  exchange=DEFAULT_EXCHANGE, connection=None,
                  delivery_mode=DEFAULT_DELIVERY_MODE, queue=DEFAULT_QUEUE):
        self.connection = connection or Connection()
        self.channel = self.connection.connection.channel()
        self.channel.queue_declare(queue) # ADDED TO SET UP QUEUE
        self.exchange = exchange
        self.routing_key = routing_key
        self.delivery_mode = delivery_mode

The channel object is part of the py-amqplib library

The problem I've got it's that, even if it's sending the messages to the specified queue, it's ALSO sending the messages to the default queue. AS in this system we expect to send quite a lot of messages, we don't want to stress the system making useless duplicates... I've tried to debug the code and go inside the py-amqplib library, but I'm not able figure out any error or lacking step. Also, I'm not able to find any documentation form py-amqplib outside the code.

Any ideas on why is this happening and how to correct it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

冷︶言冷语的世界 2024-09-06 13:48:14

好吧,我想我已经明白了。除非其他人有更好的主意。我已检查关于 AMQP 的教程 我假设发布者应该知道队列,但事实并非如此,您需要将消息发送到交换器,并且消费者会声明该队列与交换器相关。正如您在教程中看到的,这允许发送和接收不同的选项。

因此,我一直在包含有关发布者和消费者的交换信息,而不是使用对 queue_declare 的调用,而且它似乎工作得很好。

OK, I've think I've got it. unless anybody else have a better idea. I've check this tutorial on AMQP I was assuming that the publisher should know the queue, but that's not the case, you need to send the message to a exchange, and the consumer will declare that the queue is related to the exchange. That allow different options on sending and receiving, as you can see on the tutorial.

So, I've been including the exchange information on both the publisher and the consumer, not making use of the call to queue_declare and it appears to be working just fine.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文