RabbitMQ 是否会在有消息时为消费者调用回调函数?
RabbitMQ 是否在有消息时为消费者调用回调函数,或者消费者是否必须轮询 RabbitMQ 客户端?
所以在消费者端,如果有 PHP 脚本,RabbitMQ 可以调用它并将消息/参数传递给它.例如如果在 shard 1 上提交了 rating 并且 aggregateRating 表在 shard 2 上,那么 shard 2 上的 RabbitMQ 消费者会触发脚本 aggRating.php 并传递插入到 shard 1 中的参数吗?
解决方案你想要的是 basic.consume
,它允许代理向客户端推送消息.
也就是说,这些库的实现方式不同.它们中的大多数都支持 basic.consume
,但由于所使用框架的固有限制,有些不支持(最值得注意的是许多其他客户端所基于的官方 RabbitMQ C 客户端).
如果您的 PHP 库不支持 basic.consume
,您要么必须使用轮询(不好),要么可以使用更完整的客户端之一来驱动脚本.例如,您可以编写一个从代理消费的 Python 或 Java 程序(因此,代理 推送 向它们传递)并且他们可以在收到新消息时调用脚本.官方教程很好地介绍了 AMQP API,是一个很好的起点.p>
从大多数角度来看,这很有效,但它确实需要与代理的稳定连接.
如果对各种客户端的功能有疑问,或者如果您需要更多指导,RabbitMQ 讨论 邮件列表是提问的好地方.开发人员会重点回答那里发布的任何问题.
Does RabbitMQ call the callback function for a consumer when it has some message for it, or does the consumer have to poll the RabbitMQ client?
So on the consumer side, if there is a PHP script, can RabbitMQ call it and pass the message/parameters to it. e.g. if rating is submitted on shard 1 and the aggregateRating table is on shard 2, then would RabbitMQ consumer on shard 2 trigger the script say aggRating.php and pass the parameters that were inserted in shard 1?
解决方案What you want is basic.consume
, which allows the broker to push messages to clients.
That said, the libraries are implemented differently. Most of them have support for basic.consume
, but because of inherent limitations of the frameworks used, some don't (most notably the official RabbitMQ C client on which a lot of other clients are based).
If your PHP library does not support basic.consume
, you either have to use polling (bad), or you could use one of the more complete clients to drive the script. For instance, you could write a Python or Java program that consumes from the broker (so, the broker pushes deliveries to them) and they could call the script whenever a new message is received. The official tutorials are a great introduction to the AMQP APIs and are a good place to start.
This is efficient from most points of view, but it does require a stable connection to the broker.
If in doubt about the capabilities of the various clients, or if you need more guidance, the RabbitMQ Discuss mailing list is a great place to ask questions. The developers make a point of answering any query posted there.
相关文章