rabbitmq AMQP :: consume() [英] rabbitmq AMQP::consume()
问题描述
AMQP函数consumer()是一个带有回调的阻塞函数, 是否可以为consumer()函数设置一个超时时间,以便在经过指定的时间后不再阻塞并且代码执行完成?
AMQP function consume() is a blocking function with a callback, Is it possible to set a timeout for consume() function, so after specific amount of time it doesn't block anymore and the code execution completes ?
推荐答案
是的,方法如下:
$amqp = new AMQPConnection($your_connection_params);
$amqp->setTimeout($seconds);
然后,当您在队列上调用consumer()时,如果在超时时间内没有消息到达,则会从enume()中抛出一条AMQPException,消息为资源暂时不可用".如果您突破了consumpt()或遇到超时,请确保在队列对象上调用cancel()以正确重置使用者.为此,您需要生成一个全局唯一的使用者标签并将其作为未记录的第三个参数传递给使用者:
Then when you call consume() on a queue, if no messages arrive within the timeout period, an AMQPException will be thrown from consume() with the message, "Resource temporarily unavailable". If you ever break out of consume() or hit a timeout, be sure to call cancel() on the queue object to properly reset the consumer. In order to do this, you need to generate a globally unique consumer tag and pass it in as an undocumented, third parameter to consume:
$tag = uniqid() . microtime(true);
$queue->consume($callback, $flags, $tag);
$queue->cancel($tag);
这样,您可以稍后再调用一次insert(),而不会出现奇怪的问题,而这些问题会使您的头部旋转.
That way, you can call consume() again later without weird issues that will make your head spin.
这篇关于rabbitmq AMQP :: consume()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!