/**
  * Consume messages from the message broker queue.
  *
  * @param $callback
  *  Callback to handle messages the consumer receives.
  * @param integer $consumeAmount
  *  The number of message to set as unacked and reserve when consumer is sent messages.
  */
 public function consume($callback, $consumeAmount = null)
 {
     $channel = $this->connection->channel();
     // Exchange setup
     $this->setupExchange($this->exchangeOptions['name'], $this->exchangeOptions['type'], $channel);
     foreach ($this->queueOptions as $queueOption) {
         // Queue setup
         list($channel, ) = $this->setupQueue($queueOption['name'], $channel);
         // Bind the queue to the exchange
         $channel->queue_bind($queueOption['name'], $this->exchangeOptions['name'], $queueOption['bindingKey']);
         if (!$consumeAmount == null) {
             // @todo: Investigate if large unack amounts result in multi consumers being able to process large queues.
             // This currently set the unacked limit to a single message resulting in every consumer getting access to
             // the next message in the queue.
             // $channel->qos(NULL, $consumeAmount, null);
             $channel->basic_qos(null, 1, null);
         }
         // Start the consumer
         $channel->basic_consume($queueOption['name'], $this->consumeOptions['consumer_tag'], $this->consumeOptions['no_local'], $this->consumeOptions['no_ack'], $this->consumeOptions['exclusive'], $this->consumeOptions['nowait'], $callback);
     }
     // Wait for messages on the channel
     echo ' [*] Waiting for messages = ' . date('D M j G:i:s T Y') . '. To exit press CTRL+C', PHP_EOL;
     while (count($channel->callbacks)) {
         $channel->wait();
     }
     $channel->close();
 }
 /**
  * Return result of task execution for $task_id
  * @param object $connection AMQPConnection object
  * @param string $task_id Celery task identifier
  * @param int $expire expire time result queue, milliseconds
  * @param boolean $removeMessageFromQueue whether to remove message from queue
  * @return array array('body' => JSON-encoded message body, 'complete_result' => AMQPMessage object)
  * 			or false if result not ready yet
  */
 function GetMessageBody($connection, $task_id, $expire = 0, $removeMessageFromQueue = true)
 {
     if (!$this->receiving_channel) {
         $ch = $connection->channel();
         $expire_args = null;
         if (!empty($expire)) {
             $expire_args = array("x-expires" => array("I", $expire));
         }
         $ch->queue_declare($task_id, false, true, false, true, false, $expire_args);
         $ch->queue_bind($task_id, 'celeryresults');
         $ch->basic_consume($task_id, '', false, false, false, false, array($this, 'Consume'));
         $this->receiving_channel = $ch;
     }
     try {
         $this->receiving_channel->wait(null, false, $this->wait_timeout);
     } catch (PhpAmqpLib\Exception\AMQPTimeoutException $e) {
         return false;
     }
     /* Check if the callback function saved something */
     if ($this->message) {
         if ($removeMessageFromQueue) {
             $this->receiving_channel->queue_delete($task_id);
         }
         $this->receiving_channel->close();
         $connection->close();
         return array('complete_result' => $this->message, 'body' => $this->message->body);
     }
     return false;
 }