AMQPQueue
PHP 手册

AMQPQueue::consume

(PECL amqp >= Unknown)

AMQPQueue::consumeConsume messages from a queue

说明

public void AMQPQueue::consume ( callable $callback [, int $flags = AMQP_NOPARAM ] )

Blocking function that will retrieve the next message from the queue as it becomes available and will pass it off to the callback.

参数

callback

A callback function to which the consumed message will be passed. The function must accept at a minimum one parameter, an AMQPEnvelope object, and an optional second parameter the AMQPQueue from which the message was consumed.

The AMQPQueue::consume() will not return the processing thread back to the PHP script until the callback function returns FALSE.

flags

A bitmask of any of the flags: AMQP_NOACK.

错误/异常

Throws AMQPChannelException if the channel is not open.

Throws AMQPConnectionException if the connection to the broker was lost.

返回值

范例

Example #1 AMQPQueue::consume() example

<?php

     
/* Create a connection using all default credentials: */
     
$connection = new AMQPConnection();
     
$connection->connect();

     
$channel = new AMQPChannel($connection);

     
/* create a queue object */
     
$queue = new AMQPQueue($channel);

     
//declare the queue
     
$queue->declare('myqueue');

     
$i 0;
     function 
processMessage($envelope$queue) {
        global 
$i;
        echo 
"Message $i: " $envelope->getBody() . "\n";
        
$i++;
        if (
$i 10) {
            
// Bail after 10 messages
            
return false;
        }
     }

     
// Consume messages on queue
     
$queue->consume("processMessage");

     
?>


AMQPQueue
PHP 手册