Пример #1
0
 /**
  * Listen queue server for given queue name
  *
  * @param string $queue_name  Queue name to listen
  * @param array $options  Options to listen
  * @param Closure $closure Function to run for every message
  *
  * @return void
  */
 public function listen($queue_name, array $options = null, Closure $closure)
 {
     $this->queue_name = $queue_name;
     if ($options) {
         $this->setOptions($options);
     }
     $GLOBALS['messages_proccesed'] = 0;
     $GLOBALS['start_time'] = time();
     $connection = new Connection($this->buildConnectionOptions());
     $connection->open();
     $listenerObject = $this;
     //Check Queue Empty Or Not
     if (!empty($listenerObject->empty_check)) {
         $message = $connection->channel->basic_get($this->queue_name, $no_ack = false, $ticket = null);
         if (empty($message)) {
             echo 0;
             exit;
         } else {
             echo 1;
             exit;
         }
     }
     $connection->channel->basic_consume($this->queue_name, $connection->consumer_tag, false, false, false, false, function ($msg) use($closure, $listenerObject) {
         try {
             $closure($msg->body);
         } catch (Exception $e) {
             throw $e;
         }
         $msg->delivery_info['channel']->basic_ack($msg->delivery_info['delivery_tag']);
         //Update counters
         $GLOBALS['messages_proccesed']++;
         //Stop Consumer..
         if (!empty($listenerObject->stop_queue)) {
             $msg->delivery_info['channel']->basic_cancel($msg->delivery_info['consumer_tag']);
         }
         //Check if necesary to close consumer
         if ($listenerObject->message_limit && $GLOBALS['messages_proccesed'] >= $listenerObject->message_limit) {
             $msg->delivery_info['channel']->basic_cancel($msg->delivery_info['consumer_tag']);
         }
         if ($listenerObject->time && time() - $GLOBALS['start_time'] >= $listenerObject->time) {
             $msg->delivery_info['channel']->basic_cancel($msg->delivery_info['consumer_tag']);
         }
     });
     register_shutdown_function(function ($connection) {
         $connection->close();
     }, $connection);
     // Loop as long as the channel has callbacks registered
     try {
         while (count($connection->channel->callbacks)) {
             $connection->channel->wait(null, false, $this->empty_queue_timeout);
         }
     } catch (AMQPTimeoutException $e) {
         return false;
     } catch (Exception $e) {
         throw $e;
     }
 }
Пример #2
0
 /**
  * Save the current message instance into de queue server
  *
  * @return void
  */
 public function save()
 {
     try {
         $connection = new Connection($this->buildConnectionOptions());
         $connection->open();
         $msg = new AMQPMessage($this->message, array('content_type' => $this->content_type, 'delivery_mode' => 2));
         $connection->channel->basic_publish($msg, $this->exchange, $this->queue_name);
         $connection->close();
     } catch (Exception $e) {
         $connection->close();
         throw new Exception($e);
     }
 }
Пример #3
0
 /**
  * Save the current message instance into de queue server
  *
  * @return void
  */
 public function save()
 {
     try {
         $connection = new Connection($this->buildConnectionOptions());
         $connection->open();
         $msg = new AMQPMessage($this->message, array('content_type' => 'text/plain', 'delivery_mode' => 2));
         if ($this->delay) {
             $headers = new AMQPTable(["x-delay" => $this->delay]);
             $msg->set('application_headers', $headers);
         }
         $connection->channel->basic_publish($msg, $this->queue_name);
         $connection->close();
     } catch (Exception $e) {
         $connection->close();
         throw new Exception($e);
     }
 }