沃梦达 / 编程问答 / php问题 / 正文

rabbitmq AMQP::consume()

rabbitmq AMQP::consume()(rabbitmq AMQP::consume())

本文介绍了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()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:rabbitmq AMQP::consume()

基础教程推荐