rabbitmq AMQP::consume()

编程入门 行业动态 更新时间:2024-10-14 20:25:41
本文介绍了rabbitmq AMQP::consume()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

AMQP 函数 consume() 是一个带有回调的阻塞函数,是否可以为 consume() 函数设置超时,这样在特定时间后它不再阻塞并且代码执行完成?

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);

那么当你在队列上调用consume()时,如果在超时时间内没有消息到达,consume()会抛出一个AMQPException,消息是资源暂时不可用".如果您曾经中断消费()或遇到超时,请务必在队列对象上调用取消()以正确重置消费者.为此,您需要生成一个全局唯一的消费者标签,并将其作为未记录的第三个参数传入消费:

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);

这样,您以后可以再次调用consume(),而不会出现令您头晕目眩的奇怪问题.

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

发布评论

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

>www.elefans.com

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