0

I am using RabbitMQ to implement a worker task queue for a search indexer process using the PHP AMQP extension. I need the search indexer demon to listen for messages on the queue and consume them when it's available.

I see two methods for consuming content from a queue:

  • AMQPQueue::get - doesn't block, so probably not what I'm after
  • AMQPQueue::consume - seems promising

However, using consume appears to set up a consumer that is not then removed. Here's the PHP:

$opts = array('min' => 1, 'max' => 10, 'ack' => false);
$messages = array();
while (count($messages) or $messages = $q->consume($opts)) {
    $msg = array_pop($messages);
    var_dump($msg);
    // ...Do work here...
    $q->ack($msg['delivery_tag']);
}

And you can see the consumers building up using rabbitmqctl:

[andrew@localhost ~] rabbitmqctl list_queues name consumers
Listing queues ...
test_queue   3
[andrew@localhost ~] rabbitmqctl list_queues name consumers
Listing queues ...
test_queue   4

So the question is, what is the correct way to bind a PHP daemon to a queue such that it blocks while it waits for messages to be available, and starts blocking/listening again when it has completed the work associated with each message batch?

Nate
  • 12,499
  • 5
  • 45
  • 60
Andrew
  • 2,094
  • 19
  • 24
  • In case of interest, I ended up switching to Beanstalk, which is doing the job for me very nicely. – Andrew Dec 14 '11 at 18:39

2 Answers2

2

I'm not sure how the PHP Pecl extension implements consumers, but my Amqp library allows you to listen out for incoming messages (i.e. consume) by calling a function, and there are several "exit strategies" available in case you don't want to block forever. There's documentation available here, check the section "Implementing a Consumer", and a demo script here.

Robin
  • 4,242
  • 1
  • 20
  • 20
  • Thanks for that. I did see your library, and the tutorial is excellent, but it didn't seem to have any reference docs. Maybe all available elements of the library are used in the demos, but I was a bit nervous of relying solely on reverse engineering demos. For example, I like the concept of consuming multiple messages at a time, which the PECL extension provides via the min and max options. Your docs make reference to invoking basic.qos but it's hard to work out how I would implement a multiple-message receiver class. – Andrew Mar 13 '11 at 09:38
  • @Andrew I've put up this question for +50 bounty. Please see if you can help-- http://stackoverflow.com/questions/9151698/does-rabbitmq-call-the-callback-function-for-a-consumer-when-it-has-some-message – jeff musk Feb 18 '12 at 21:41
0

consume is what you want. It'll block until it receives a message.

The API has changed a big since your code, so it's hard to guess what went wrong.

http://www.php.net/manual/en/amqpqueue.consume.php

has the semi latest documentation and example

NeuroScr
  • 322
  • 1
  • 7