rabbitmq AMQP :: consume()

编程入门 行业动态 更新时间:2024-10-14 22:17:21
本文介绍了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()

本文发布于:2023-11-24 12:33:11,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1625217.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:rabbitmq   AMQP   consume

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!