AMQPQueue::consume

(PECL amqp >= Unknown)

AMQPQueue::consumeConsume messages from a queue

Beskrivelse

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.

Parametre

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.

Fejl/Undtagelser

Throws AMQPChannelException if the channel is not open.

Throws AMQPConnectionException if the connection to the broker was lost.

Returnerings Værdier

Eksempler

Eksempel #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");

     
?>

add a note add a note

User Contributed Notes 1 note

up
1
mrxiaosen at 126 dot com
6 years ago
This doc not talk about consumer tag. But the method of consumer,has the third param for consumer tag.
If you want to set the consumer tag,see the example.

$consume_tag = sprintf("%s_%s_%s", php_uname('n'), time(), getmypid());
$queue->consume($callback, \AMQP_NOPARAM, $consume_tag)

public function consume(
        callable $callback = null,
        $flags = AMQP_NOPARAM,
        $consumerTag = null
    ) {}
To Top