RabbitMQ是否在有消息的情况下为使用者调用回调函数? [英] Does RabbitMQ call the callback function for a consumer when it has some message for it?
问题描述
RabbitMQ 是否在有某些消息的情况下为使用者调用回调函数,或者消费者需要轮询RabbitMQ客户端吗?
因此,在使用者方面,如果有PHP脚本,RabbitMQ可以调用它并将消息/参数传递给它.例如如果评级是在分片1上提交的,并且aggregateRating表在分片2上,那么分片2的RabbitMQ使用者会触发脚本aggRating.php并传递在分片1中插入的参数吗?
您想要的是basic.consume
,它允许代理将消息推送到客户端.
也就是说,库的实现方式有所不同.他们中的大多数人都支持basic.consume
,但是由于所使用框架的固有局限性,因此有些人没有(最引人注目的是许多其他客户端所基于的官方RabbitMQ C客户端).
如果您的PHP库不支持basic.consume
,则要么必须使用轮询(错误),要么可以使用功能更完善的客户端之一来驱动脚本.例如,您可以编写一个从代理使用的Python或Java程序(因此,代理推动交付给他们),并且他们可以在收到新消息时调用脚本. 官方教程是对AMQP API的很好介绍,也是一个很好的起点. >
从大多数角度来看,这是有效的,但确实需要与代理建立稳定的连接.
如果不确定各个客户端的功能,或者需要更多指导,请 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.
这篇关于RabbitMQ是否在有消息的情况下为使用者调用回调函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!