예제 #1
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();
     }
 }
예제 #2
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();
     }
 }
예제 #3
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();
     }
 }
예제 #5
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();
     }
 }
예제 #6
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();
     }
 }
 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();
     }
 }
 /**
  * Set up AMQP connection.
  */
 public function setUp()
 {
     $container = $this->getContainer();
     $exchangeName = 'general';
     $connection = new AMQPConnection($container->getParameter('ongr_task_messenger.publisher.default.amqp.host'), $container->getParameter('ongr_task_messenger.publisher.default.amqp.port'), $container->getParameter('ongr_task_messenger.publisher.default.amqp.user'), $container->getParameter('ongr_task_messenger.publisher.default.amqp.password'));
     $this->channel = $connection->channel();
     list($queueName, , ) = $this->channel->queue_declare();
     $this->channel->queue_bind($queueName, $exchangeName, explode('.', gethostname())[0]);
     $this->channel->basic_consume($queueName, getmypid(), false, true, true, true, [$this, 'verifyMessage']);
 }
 /**
  * Create consumer.
  */
 private function initialize()
 {
     $this->messages = [];
     list($queue) = $this->channel->queue_declare('', false, false, true, true);
     $this->channel->queue_bind($queue, $this->exchange);
     $this->channel->basic_consume($queue, '', false, false, false, false, function (AMQPMessage $message) {
         $this->messages[] = $message;
         $this->channel->basic_cancel($message->delivery_info['consumer_tag']);
     });
 }
예제 #10
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();
     }
 }
 /**
  * @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();
     }
 }
예제 #12
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();
     }
 }
예제 #13
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();
     }
 }
예제 #14
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);
 }
예제 #15
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']);
     }
 }
예제 #16
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();
     }
 }
예제 #17
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--;
         }
     }
 }
예제 #18
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();
     }
 }
 /**
  * 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();
         }
     }
 }
 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;
 }
예제 #21
0
 /**
  * Wait and get job from a queue
  *
  * @access public
  * @return Job|null
  */
 public function pull()
 {
     $message = null;
     $this->channel->basic_consume($this->queue, 'test', false, false, false, false, function ($msg) use(&$message) {
         $message = $msg;
         $message->delivery_info['channel']->basic_cancel($message->delivery_info['consumer_tag']);
     });
     while (count($this->channel->callbacks)) {
         $this->channel->wait();
     }
     if ($message === null) {
         return null;
     }
     $job = new Job();
     $job->setId($message->get('delivery_tag'));
     $job->unserialize($message->getBody());
     return $job;
 }
 /**
  * @param AMQPConnection $conn
  * @param AMQPChannel $generalChannel
  * @param string $generalQueueName
  * @param AMQPChannel $workerQueueChannel
  * @param string $workerExchangeName
  * @param string $workerQueueName
  * @param null|string $consumerTag
  */
 public function __construct(AMQPConnection $conn, AMQPChannel $generalChannel, $generalQueueName, AMQPChannel $workerQueueChannel, $workerExchangeName, $workerQueueName, $consumerTag = null)
 {
     $consumerTag = $consumerTag ?: uniqid();
     $generalChannel->basic_consume($generalQueueName, $consumerTag, false, false, false, false, [$this, 'forwardToWorker']);
     $this->generalChannel = $generalChannel;
     $this->workerQueueChannel = $workerQueueChannel;
     $this->conn = $conn;
     $this->workerExchangeName = $workerExchangeName;
 }
예제 #23
0
 /**
  * @throws \yii\base\InvalidConfigException
  */
 protected function initQueue()
 {
     /** @var Queue */
     $this->queue = Instance::ensure($this->queue, Queue::class);
     $this->channel = $this->queue->getChannel();
     $this->channel->basic_qos($this->prefetchSize, $this->prefetchCount, null);
     /**
      * Message consume callback wrapper
      *
      * @param AMQPMessage $message
      *
      * @throws Exception
      */
     $callback = function (AMQPMessage $message) {
         $this->messageCallback($message);
     };
     $this->consumerTag = $this->channel->basic_consume($this->queue->name, '', false, false, $this->noAck, false, $callback);
 }
예제 #24
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();
     }
 }
예제 #25
0
파일: Channel.php 프로젝트: znk3r/mqlib
 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();
     }
 }
예제 #26
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();
     }
 }
예제 #27
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));
     }
 }
예제 #28
0
 /**
  * @param string $queueName
  * @param callable $callback
  *
  * @throws ConnectorException
  * @throws \Exception
  */
 public function basicConsume($queueName, $callback)
 {
     $this->logger->info(sprintf('Comsuming queue "%s"...', $queueName));
     try {
         $this->channel->basic_consume($queueName, '', false, false, false, false, $callback);
     } catch (\Exception $e) {
         if ($e instanceof AMQPExceptionInterface) {
             throw ConnectorException::internalException($e);
         } else {
             throw $e;
         }
     }
 }
예제 #29
0
 /**
  * Setup consumer.
  */
 protected function setUpConsumer()
 {
     if (isset($this->exchangeOptions['name'])) {
         $this->channel->exchange_declare($this->exchangeOptions['name'], $this->exchangeOptions['type'], $this->exchangeOptions['passive'], $this->exchangeOptions['durable'], $this->exchangeOptions['auto_delete'], $this->exchangeOptions['internal'], $this->exchangeOptions['nowait'], $this->exchangeOptions['arguments'], $this->exchangeOptions['ticket']);
         if (!empty($this->consumerOptions['qos'])) {
             $this->channel->basic_qos($this->consumerOptions['qos']['prefetch_size'], $this->consumerOptions['qos']['prefetch_count'], $this->consumerOptions['qos']['global']);
         }
     }
     list($queueName, , ) = $this->channel->queue_declare($this->queueOptions['name'], $this->queueOptions['passive'], $this->queueOptions['durable'], $this->queueOptions['exclusive'], $this->queueOptions['auto_delete'], $this->queueOptions['nowait'], $this->queueOptions['arguments'], $this->queueOptions['ticket']);
     if (isset($this->exchangeOptions['name'])) {
         $this->channel->queue_bind($queueName, $this->exchangeOptions['name'], $this->routingKey);
     }
     $this->channel->basic_consume($queueName, $this->getConsumerTag(), false, false, false, false, array($this, 'processMessage'));
 }
예제 #30
-1
 public function __construct()
 {
     $this->connection = new AMQPStreamConnection('localhost', 5672, 'guest', 'guest');
     $this->channel = $this->connection->channel();
     list($this->callback_queue, , ) = $this->channel->queue_declare("", false, false, true, false);
     $this->channel->basic_consume($this->callback_queue, '', false, false, false, false, [$this, 'on_response']);
 }