Exemplo n.º 1
0
 public function waitingForCall()
 {
     $this->channel->basic_consume(self::QUEUE_NAME, '', false, false, false, false, array($this, 'receive'));
     while (count($this->channel->callbacks)) {
         $this->channel->wait();
     }
 }
Exemplo n.º 2
0
 /**
  * @param Invoker\Reply $reply
  * @param float         $timeout
  *
  * @return $this
  */
 public function resolve(Invoker\Reply $reply, $timeout = null)
 {
     while (!empty($this->channel->callbacks) && !$this->isResolved($reply)) {
         $this->channel->wait(null, true, $timeout);
     }
     return $this;
 }
Exemplo n.º 3
0
 public function consume(ConsumerInterface $consumer, $callback)
 {
     $this->channel->basic_consume($consumer->getName(), '', false, false, false, false, $callback);
     while (count($this->channel->callbacks)) {
         $this->channel->wait();
     }
 }
Exemplo n.º 4
0
 public function wait()
 {
     // Loop as long as the channel has callbacks registered
     while (count($this->channel->callbacks)) {
         $this->channel->wait();
     }
 }
Exemplo n.º 5
0
 /**
  * Infinite loop: Listens for messages from the queue and sends them to the callback.
  *
  * @param callback $callback
  */
 public function listen($callback)
 {
     $this->channel->basic_qos(null, 1, null);
     $this->channel->basic_consume(self::QUEUE_NAME, '', false, true, false, false, $callback);
     while ($this->channel->callbacks) {
         $this->channel->wait();
     }
 }
 /**
  * @param string $exchangeName
  * @param callable $callback
  */
 public function consume($exchangeName, callable $callback)
 {
     $this->callback = $callback;
     $this->channel->basic_consume($exchangeName, '', false, true, false, false, [$this, 'callback']);
     while (count($this->channel->callbacks)) {
         $this->channel->wait();
     }
 }
Exemplo n.º 7
0
 /**
  * Consumes one event and calls callback for it.
  *
  * @param integer $timeout Optional timeout in seconds. Default is no timeout.
  *
  * @return void
  */
 public function consume($timeout = 0)
 {
     try {
         $this->channel->wait(null, false, $timeout);
     } catch (AMQPTimeoutException $e) {
         return;
     }
 }
Exemplo n.º 8
0
 /**
  * Listen declared queues
  * @param null $cliMessage
  */
 public function listen($cliMessage = null)
 {
     if (!empty($cliMessage)) {
         echo $cliMessage;
     }
     while (count($this->channel->callbacks)) {
         $this->channel->wait();
     }
 }
Exemplo n.º 9
0
 public function consume()
 {
     $queue = $this->initialize();
     $tag = $this->handler->name();
     $this->channel->basic_consume($queue, $tag, false, false, false, false, [$this, 'handle']);
     while (isset($this->channel->callbacks[$tag])) {
         $this->channel->wait();
     }
 }
 public function testPublishConsume()
 {
     $this->msg_body = 'foo bar baz äëïöü';
     $msg = new AMQPMessage($this->msg_body, array('content_type' => 'text/plain', 'delivery_mode' => 1, 'correlation_id' => 'my_correlation_id', 'reply_to' => 'my_reply_to'));
     $this->ch->basic_publish($msg, $this->exchange_name, $this->queue_name);
     $this->ch->basic_consume($this->queue_name, getmypid(), false, false, false, false, array($this, 'process_msg'));
     while (count($this->ch->callbacks)) {
         $this->ch->wait();
     }
 }
Exemplo n.º 11
0
 public function testSendFile()
 {
     $this->msg_body = file_get_contents(__DIR__ . '/fixtures/data_1mb.bin');
     $msg = new AMQPMessage($this->msg_body, array('delivery_mode' => 1));
     $this->ch->basic_publish($msg, $this->exchange_name, $this->queue_name);
     $this->ch->basic_consume($this->queue_name, '', false, false, false, false, array($this, 'process_msg'));
     while (count($this->ch->callbacks)) {
         $this->ch->wait();
     }
 }
 /**
  * @param callable $callback
  * @return void
  */
 public function consume(callable $callback)
 {
     $internCallback = function ($msg) use($callback) {
         //echo " [x] Received ", $msg->body, "\n";
         $callback(unserialize($msg->body));
     };
     $this->exchange->basic_consume($this->exchangeName, '', false, true, false, false, $internCallback);
     while (count($this->exchange->callbacks)) {
         $this->exchange->wait();
     }
 }
Exemplo n.º 13
0
 /**
  * @param $n
  * @return int
  */
 public function call($n)
 {
     $this->response = null;
     $this->corr_id = uniqid();
     $msg = new AMQPMessage((string) $n, ['correlation_id' => $this->corr_id, 'reply_to' => $this->callback_queue]);
     $this->channel->basic_publish($msg, '', 'rpc_queue');
     while (!$this->response) {
         $this->channel->wait();
     }
     return intval($this->response);
 }
Exemplo n.º 14
0
 /**
  * @param string $queue
  * @param string $exchange
  * @param string $routingKey
  * @param callable $callback
  */
 public function consume($queue, $exchange, $routingKey, $callback)
 {
     $this->declareComponents($routingKey, $exchange, $queue);
     $this->channel->basic_consume($queue, '', false, false, false, false, function ($amqpMessage) use($callback) {
         $message = new Message($amqpMessage);
         $callback($message);
     });
     while (count($this->channel->callbacks)) {
         $this->channel->wait();
     }
 }
Exemplo n.º 15
0
 /**
  * {@inheritdoc}
  */
 public function wait(Closure $callback)
 {
     $this->channel->basic_consume($this->queue, '', false, true, false, false, function ($rabbitMessage) use($callback) {
         $message = $this->serializer->unserialize($rabbitMessage->body);
         $callback($message);
         $rabbitMessage->delivery_info['channel']->basic_ack($rabbitMessage->delivery_info['delivery_tag']);
     });
     while (count($this->channel->callbacks)) {
         $this->channel->wait();
     }
 }
 /**
  * Test if AMQPPublisher works as expected.
  */
 public function testPublish()
 {
     $container = $this->getContainer();
     $publisher = $container->get('ongr_task_messenger.publisher.default.amqp');
     $logger = new NullLogger();
     $publisher->setLogger($logger);
     $task = new SyncTask(SyncTask::SYNC_TASK_PRESERVEHOST);
     $task->setName('task_foo');
     $task->setCommand('command_foo');
     $publisher->publish($task);
     $this->channel->wait();
 }
Exemplo n.º 17
0
 public function testFrameOrder()
 {
     $msg = new AMQPMessage('');
     $hdrs = new AMQPTable(array('x-foo' => 'bar'));
     $msg->set('application_headers', $hdrs);
     for ($i = 0; $i < $this->msg_count; $i++) {
         $this->ch->basic_publish($msg, $this->exchange_name, $this->queue_name);
     }
     $this->ch2->basic_consume($this->queue_name, '', false, true, false, false, array($this, 'process_msg'));
     while (count($this->ch2->callbacks)) {
         $this->ch2->wait();
     }
 }
Exemplo n.º 18
0
 /**
  *
  */
 public function consume()
 {
     $this->queue->initialize();
     $service = $this->service;
     $this->channel->basic_consume($this->queue->name(), $this->tag, $this->noLocal, $this->noAck, $this->exclusive, $this->noWait, function (AMQPMessage $message) use($service) {
         $ack = $service->execute($message);
         if ($ack === null || $ack === true) {
             $message->delivery_info['channel']->basic_ack($message->delivery_info['delivery_tag']);
         }
     });
     while (count($this->channel->callbacks)) {
         $this->channel->wait();
     }
 }
Exemplo n.º 19
0
 public function process_msg1($msg)
 {
     $delivery_info = $msg->delivery_info;
     $this->q1msgs++;
     if ($this->q1msgs < 2) {
         $this->ch2->basic_consume($this->queue_name2, "", false, true, false, false, array($this, 'process_msg2'));
     }
     while (count($this->ch2->callbacks)) {
         $this->ch2->wait();
     }
     if ($this->q1msgs == 2) {
         $delivery_info['channel']->basic_cancel($delivery_info['consumer_tag']);
     }
 }
Exemplo n.º 20
0
 /**
  * @Then there should be a message in queue :queue
  *
  * @param              $queue
  * @param PyStringNode $string
  */
 public function thereShouldBeAMessageInQueue($queue, PyStringNode $string = null)
 {
     $expected = $string ? $string->getRaw() : null;
     if (null === $expected) {
         $this->channel->basic_consume($queue);
     } else {
         $consumer = function (AMQPMessage $message) use($expected) {
             $this->channel->basic_ack($message->delivery_info['delivery_tag']);
             Assert::that($message->body)->equal($expected);
         };
         $this->channel->basic_consume($queue, '', false, false, false, false, $consumer);
     }
     $this->channel->wait(null, false, 4);
 }
Exemplo n.º 21
0
 public function work($maxMessages = null, $timeOut = null)
 {
     $callback = function (AMQPMessage $message) {
         $this->handle($message);
     };
     $this->channel->basic_qos(null, 1, null);
     $this->channel->basic_consume($this->queue, '', false, false, false, false, $callback);
     // Loop infinitely (or up to $count) to execute tasks
     while (count($this->channel->callbacks) && (is_null($maxMessages) || $maxMessages > 0)) {
         $this->channel->wait();
         if (!is_null($maxMessages)) {
             $maxMessages--;
         }
     }
 }
 /**
  * Listener for the queue
  * @param string $consumer_url
  * @param array $post_vars
  */
 public function consume($consumer_url, $post_vars = array())
 {
     $this->consumer_url = $consumer_url;
     $this->consumer_post = $post_vars;
     if ($this->consumer_count < $this->consumer_max) {
         $this->consumer_start = time();
         $callback = function (AMQPMessage $message) {
             $post_vars = $this->consumer_post;
             $post_vars['url'] = $this->consumer_url;
             $post_vars['queue'] = $this->queue;
             $post_vars['message'] = $message->body;
             $response = $this->httpRequest->post($this->consumer_url, $post_vars);
             $code = $this->httpRequest->statusCode();
             if ($code == 200) {
                 $message->delivery_info['channel']->basic_ack($message->delivery_info['delivery_tag']);
             } else {
                 // log the error to a file
                 $log_message = $code . "\n\n";
                 $log_message .= $message->body . "\n\n";
                 $log_message .= print_r($message, true);
                 $log_message .= "\n\n\n";
                 $log_message .= print_r($response, true);
                 Log::error($log_message);
             }
             if ($this->consumer_start + $this->ttl < time()) {
                 $this->channel->basic_cancel($message->delivery_info['consumer_tag']);
             }
         };
         $this->channel->basic_consume($this->queue, '', false, false, false, false, $callback);
         while (count($this->channel->callbacks)) {
             $this->channel->wait();
         }
     }
 }
 protected function wait()
 {
     while ($this->valid()) {
         $this->channel->wait();
         break;
     }
 }
Exemplo n.º 24
0
 /**
  * Starts to listen a queue for incoming messages.
  * @param string $queueName The AMQP queue
  * @param array  $handlers  Array of handler class instances
  * @return bool
  */
 public function listenToQueue($queueName, array $handlers)
 {
     $this->queueName = $queueName;
     /* Look for handlers */
     $handlersMap = array();
     foreach ($handlers as $handlerClassPath) {
         if (!class_exists($handlerClassPath)) {
             $handlerClassPath = "RabbitManager\\Handlers\\{$handlerClassPath}";
             if (!class_exists($handlerClassPath)) {
                 $this->logger->addError("Class {$handlerClassPath} was not found!");
                 return false;
             }
         }
         $handlerOb = new $handlerClassPath();
         $classPathParts = explode("\\", $handlerClassPath);
         $handlersMap[$classPathParts[count($classPathParts) - 1]] = $handlerOb;
     }
     /* Create queue */
     $this->channel->queue_declare($queueName, false, true, false, false);
     /* Start consuming */
     $this->channel->basic_qos(null, 1, null);
     $this->channel->basic_consume($queueName, '', false, false, false, false, function ($amqpMsg) use($handlersMap) {
         $msg = Message::fromAMQPMessage($amqpMsg);
         Broker::handleMessage($msg, $handlersMap);
     });
     $this->logger->addInfo("Starting consumption of queue {$queueName}");
     /* Iterate until ctrl+c is received... */
     while (count($this->channel->callbacks)) {
         $this->channel->wait();
     }
 }
 public function actionIndex()
 {
     Yii::info('Started email task', __METHOD__);
     $this->setupConnection();
     echo '[*] Waiting for messages. To exit press CTRL+C', "\n";
     $this->channel->basic_qos(null, 1, null);
     $this->channel->basic_consume($this->queue, '', false, false, false, false, [$this, 'processEmail']);
     while (count($this->channel->callbacks)) {
         $this->channel->wait();
     }
     echo 'Shutting down...', "\n";
     Yii::info('Shutting down...', __METHOD__);
     Yii::trace('Disconnecting...', __METHOD__);
     $this->channel->close();
     $this->connection->close();
     return self::EXIT_CODE_NORMAL;
 }
Exemplo n.º 26
0
 /**
  * @inheritdoc
  */
 public function waitForBasicReturn(float $timeout = 0.0)
 {
     try {
         $this->channel->wait(null, false, $timeout);
     } catch (\Exception $e) {
         throw ChannelException::fromPhpAmqpLib($e);
     }
 }
Exemplo n.º 27
0
 public function consume(Queue $queue, $consumerOptions, $action)
 {
     $this->channel->basic_consume($queue->getName(), $consumerOptions['name'], $consumerOptions['noLocal'], $consumerOptions['noAck'], $consumerOptions['exclusive'], $consumerOptions['noWait'], function ($queueMessage) use($action) {
         $action(new Incoming($queueMessage));
     });
     // Listen to the socket/stream
     while (count($this->channel->callbacks)) {
         $this->channel->wait();
     }
 }
Exemplo n.º 28
0
 /**
  * @param string $queueTitle
  * @param callable $callback
  * @param string $exchange
  * @throws Exception
  */
 public function startListening($queueTitle, $callback, $exchange = '', $noAck = false)
 {
     if (!$this->channel) {
         throw new Exception("Channel didn't created");
     }
     $this->channel->basic_consume($queueTitle, $exchange, false, $noAck, false, false, $callback);
     while (count($this->channel->callbacks)) {
         $this->channel->wait();
     }
 }
Exemplo n.º 29
0
 /**
  * @param callback $callback
  * @param string   $tag
  * @param array    $config
  */
 public function consume($callback, $tag = null, array $config = array())
 {
     $this->initialize();
     $this->channel->basic_consume($this->name(), $tag, !empty($config['no_local']), !empty($config['no_ack']), !empty($config['exclusive']), !empty($config['no_wait']), function (AMQPMessage $message) use($callback) {
         $ack = call_user_func($callback, $message);
         if ($ack === null || $ack === true) {
             $message->delivery_info['channel']->basic_ack($message->delivery_info['delivery_tag']);
         }
     });
     while (count($this->channel->callbacks)) {
         $this->channel->wait();
     }
 }
Exemplo n.º 30
0
 /**
  * Wait result message and assign into $this->message.
  *
  * @param integer $timeout Optional timeout in seconds.
  *
  * @return void
  *
  * @throws Errors\ClientException On AMQP result timeout.
  */
 private function wait($timeout = null)
 {
     $this->channel->basic_consume($this->taskId, '', false, true, false, false, function ($message) {
         $this->message = $message;
     });
     // Get message from result queue with timeout
     $timeout = $timeout === null ? $this->timeout : $timeout;
     try {
         $this->channel->wait(null, false, $timeout);
     } catch (AMQPTimeoutException $e) {
         throw new Errors\ClientException(sprintf("Task '%s' result is not available after %s seconds (taskId: %s)", $this->taskName, $timeout, $this->taskId));
     }
 }