Ejemplo n.º 1
0
 /**
  * Starts to listen a queue for incoming messages.
  * @param array  $handlers  Array of handler class instances
  * @param string $queueName The AMQP queue
  * @throws \Kontoulis\RabbitMQLaravel\Exception\BrokerException
  * @return bool
  */
 public function listenToQueue($handlers = [], $queueName = null)
 {
     if (!is_null($queueName)) {
         $this->queueName = $queueName;
     }
     /* Look for handlers */
     $handlersMap = array();
     foreach ($handlers as $handlerClassPath) {
         if (!class_exists($handlerClassPath)) {
             $handlerClassPath = "Kontoulis\\RabbitMQLaravel\\Handlers\\DefaultHandler";
             if (!class_exists($handlerClassPath)) {
                 throw new BrokerException("Class {$handlerClassPath} was not found!");
             }
         }
         $handlerOb = new $handlerClassPath();
         $classPathParts = explode("\\", $handlerClassPath);
         $handlersMap[$classPathParts[count($classPathParts) - 1]] = $handlerOb;
     }
     /* Create queue */
     $this->channel->queue_declare($this->queueName, false, true, false, false);
     /* Start consuming */
     $this->channel->basic_qos(null, 1, null);
     $this->channel->basic_consume($this->queueName, '', false, false, false, false, function ($amqpMsg) use($handlersMap) {
         $msg = Message::fromAMQPMessage($amqpMsg);
         $this->handleMessage($msg, $handlersMap);
     });
     /* Iterate until ctrl+c is received... */
     while (count($this->channel->callbacks)) {
         $this->channel->wait();
     }
 }