Rabbitmq AMQP :: consume ()

The AMQP function consumes () - a blocking function with a callback. Is it possible to set a timeout for the consumption function (), so after a certain time it is no longer blocked and code execution ends?

+4
php rabbitmq amqp
source share
1 answer

Yes, here's how:

$amqp = new AMQPConnection($your_connection_params); $amqp->setTimeout($seconds); 

Then, when you call consume () in the queue, if no messages arrive during the waiting period, an AMQPException will be thrown from consume () with the message "Resource temporarily unavailable." If you ever exit consumption () or time out, be sure to call cancel () on the queue object to correctly reset the consumer. To do this, you need to create a globally unique consumer tag and pass it as an undocumented third parameter for consumption:

 $tag = uniqid() . microtime(true); $queue->consume($callback, $flags, $tag); $queue->cancel($tag); 

That way, you can call consume () again later without any weird problems that will make your head spin.

+5
source share

All Articles