Пример #1
0
 /**
  * {@inheritDoc}
  */
 public function get()
 {
     $envelope = $this->queue->get();
     if (!$envelope) {
         return;
     }
     return new Message($envelope->getBody(), array('content_type' => $envelope->getContentType(), 'routing_key' => $envelope->getRoutingKey(), 'delivery_tag' => $envelope->getDeliveryTag(), 'delivery_mode' => $envelope->getDeliveryMode(), 'exchange_name' => $envelope->getExchangeName(), 'is_redelivery' => $envelope->isRedelivery(), 'content_encoding' => $envelope->getContentEncoding(), 'type' => $envelope->getType(), 'timestamp' => $envelope->getTimeStamp(), 'priority' => $envelope->getPriority(), 'expiration' => $envelope->getExpiration(), 'app_id' => $envelope->getAppId(), 'message_id' => $envelope->getMessageId(), 'reply_to' => $envelope->getReplyTo(), 'correlation_id' => $envelope->getCorrelationId(), 'headers' => $envelope->getHeaders(), 'user_id' => $envelope->getUserId(), 'cluster_id' => 0, 'channel' => '', 'consumer_tag' => ''), $envelope->getDeliveryTag());
 }
Пример #2
0
 /**
  * @inheritdoc
  */
 public function get($flags = null)
 {
     if (false !== ($envelope = $this->delegate->get(self::convertToDelegateFlags($flags)))) {
         $envelope = new Envelope($envelope);
     }
     return $envelope;
 }
Пример #3
0
 /**
  * @inheritdoc
  */
 public function get(int $flags = Constants::AMQP_NOPARAM)
 {
     $envelope = $this->queue->get($flags);
     if ($envelope instanceof \AMQPEnvelope) {
         $envelope = new Envelope($envelope);
     }
     return $envelope;
 }
Пример #4
0
 /**
  * @param \AMQPQueue $queue
  * @return bool|string
  */
 public static function read(\AMQPQueue $queue)
 {
     usleep(10000);
     $envelope = $queue->get(\AMQP_NOPARAM);
     if ($envelope) {
         $message = $envelope->getBody();
         $queue->ack($envelope->getDeliveryTag());
         return $message;
     }
     return false;
 }
Пример #5
0
 public function take()
 {
     $q = new \AMQPQueue($this->amqpChannel);
     $q->setName($this->name);
     $msg = $q->get();
     if (empty($msg)) {
         return false;
     } else {
         return new MessageTakeControl($q, $msg);
     }
 }
Пример #6
0
 /**
  * @param int $flags
  *
  * @return Envelope|bool
  */
 public function get($flags = Client::NOPARAM)
 {
     try {
         $message = $this->rawQueue->get($flags);
         if ($message instanceof \AMQPEnvelope) {
             $message = Envelope::createFromRaw($message, $this->decodeStrategy);
         }
         return $message;
     } catch (\Exception $e) {
         ClientHelper::throwRightException($e);
     }
 }
 /**
  * {@inheritDoc}
  */
 protected function execute(InputInterface $input, OutputInterface $output)
 {
     $output->writeln(sprintf('Get %d messages from queue "%s"', $input->getOption('nb-messages'), $input->getArgument('queue')));
     $fromChannel = $this->getChannel($input->getArgument('connection'), $input->getArgument('vhost'));
     $queue = new \AMQPQueue($fromChannel);
     $queue->setName($input->getArgument('queue'));
     $noRequeue = $input->getOption('no-requeue');
     $nbMessages = $input->getOption('nb-messages');
     for ($i = 0; $i < $nbMessages; $i++) {
         if ($noRequeue) {
             $message = $queue->get(AMQP_AUTOACK);
         } else {
             $message = $queue->get();
         }
         if (false === $message) {
             $output->writeln('No more messages in queue.');
             return;
         }
         $output->writeln(print_r($message, true));
     }
 }
Пример #8
0
 public function getProxy($https = false)
 {
     if ($https) {
         $queueName = 'q_proxy_https';
     } else {
         $queueName = 'q_proxy';
     }
     $params = array('host' => '10.168.45.191', 'port' => 5672, 'login' => 'guest', 'password' => 'guest', 'vhost' => '/kwd');
     $conn = new AMQPConnection($params);
     $conn->connect();
     $channel = new AMQPChannel($conn);
     $queue = new AMQPQueue($channel);
     $queue->setName($queueName);
     $message = $queue->get(AMQP_AUTOACK);
     $proxy = $message->getBody();
     while (!$this->_testProxy($proxy, $https)) {
         $message = $queue->get(AMQP_AUTOACK);
         $proxy = $message->getBody();
     }
     $conn->disconnect();
     return $proxy;
 }
Пример #9
0
 public function readQueue($q_name)
 {
     $q = new \AMQPQueue($this->channel);
     $q->setName($q_name);
     //$q->setFlags(AMQP_DURABLE);
     //$q->declare();
     //$q->bind('exchange', $bindingkey);
     //消息获取
     $messages = $q->get(AMQP_AUTOACK);
     if ($messages) {
         return $messages->getBody();
     } else {
         return false;
     }
 }
Пример #10
0
 /**
  * (non-PHPdoc)
  * @see ContentEngine\Request.QueueInterface::get()
  */
 public function get()
 {
     if (!isset($this->queue)) {
         throw new \Exception('No AMQP queue set.');
     }
     $envelope = $this->queue->get($this->flags);
     if (!$envelope) {
         return null;
     }
     // extract data
     $serial = $envelope->getBody();
     $item = $this->serialiser->unserialise($serial);
     $item['amqpDeliveryTag'] = $envelope->getDeliveryTag();
     $item['amqpMessageId'] = $envelop->getMessageId();
     return $item;
 }
Пример #11
0
 /**
  * Return result of task execution for $task_id
  * @param AMQPConnection $connection Connection object
  * @param string $task_id Celery task identifier
  * @param boolean $removeMessageFromQueue whether to remove message from queue
  * @return array array('body' => JSON-encoded message body, 'complete_result' => AMQPEnvelope object)
  * 			or false if result not ready yet
  */
 function GetMessageBody($connection, $task_id, $removeMessageFromQueue = true)
 {
     $this->Connect($connection);
     $ch = $connection->channel;
     $q = new AMQPQueue($ch);
     $q->setName($task_id);
     $q->setFlags(AMQP_AUTODELETE | AMQP_DURABLE);
     $q->declareQueue();
     try {
         $q->bind('celeryresults', $task_id);
     } catch (AMQPQueueException $e) {
         if ($removeMessageFromQueue) {
             $q->delete();
         }
         $connection->disconnect();
         return false;
     }
     $message = $q->get(AMQP_AUTOACK);
     if (!$message) {
         if ($removeMessageFromQueue) {
             $q->delete();
         }
         $connection->disconnect();
         return false;
     }
     if ($message->getContentType() != 'application/json') {
         if ($removeMessageFromQueue) {
             $q->delete();
         }
         $connection->disconnect();
         throw new CeleryException('Response was not encoded using JSON - found ' . $message->getContentType() . ' - check your CELERY_RESULT_SERIALIZER setting!');
     }
     if ($removeMessageFromQueue) {
         $q->delete();
     }
     $connection->disconnect();
     return array('complete_result' => $message, 'body' => $message->getBody());
 }
Пример #12
0
 /**
  * Connect to queue, see if there's a result waiting for us
  * Private - to be used internally
  */
 private function getCompleteResult()
 {
     if ($this->complete_result) {
         return $this->complete_result;
     }
     $this->connection->connect();
     $ch = new AMQPChannel($this->connection);
     $q = new AMQPQueue($ch);
     $q->setName($this->task_id);
     $q->setFlags(AMQP_AUTODELETE);
     #		$q->setArgument('x-expires', 86400000);
     $q->declareQueue();
     try {
         $q->bind('celeryresults', $this->task_id);
     } catch (AMQPQueueException $e) {
         $q->delete();
         $this->connection->disconnect();
         return false;
     }
     $message = $q->get(AMQP_AUTOACK);
     if (!$message) {
         $q->delete();
         $this->connection->disconnect();
         return false;
     }
     $this->complete_result = $message;
     if ($message->getContentType() != 'application/json') {
         $q->delete();
         $this->connection->disconnect();
         throw new CeleryException('Response was not encoded using JSON - found ' . $message->getContentType() . ' - check your CELERY_RESULT_SERIALIZER setting!');
     }
     $this->body = json_decode($message->getBody());
     $q->delete();
     $this->connection->disconnect();
     return false;
 }
Пример #13
0
$rabbit = new AMQPConnection(array('host' => '127.0.0.1', 'port' => '5672', 'login' => 'guest', 'password' => 'guest'));
$rabbit->connect();
$channel = new AMQPChannel($rabbit);
$queue = new AMQPExchange($channel);
$queue->setName('amq.direct');
/**
 * Добавляем очередь откуда будем брать страницы
 */
$q = new AMQPQueue($channel);
$q->setName('pages_to_scan');
$q->declare();
$q->bind('amq.direct', 'analyze_page');
/**
 * Обрабатываем пока в очереди не закончатся сообщения
 */
while (true) {
    $page = $q->get();
    if ($page) {
        $url = $page->getBody();
        echo "Parsing: {$url}\n";
        $analyzer = new PageAnalyzer($url);
        /**
         * Если страница еще не была проанализирована, обрабатываем и добавляем в индекс
         */
        $analyzer->analyze();
        $q->ack($page->getDeliveryTag());
    } else {
        sleep(1);
    }
}
$rabbit->disconnect();
Пример #14
0
 /**
  * Асинхронное чтение ответов
  * @param string $transactionId
  * @param callable $callBack - Функция вызываемая для каждой строки.
  * Приимвет два параметра
  * \AMQPEnvelope $envelope
  * \AMQPQueue $queue
  * Чтобы подтвердить получение нужно вызвать метод $queue->ack($envelope->getDeliveryTag());
  * Чтобы перейти к следующему элементу без подтверждения получения нужно вызвать метод $queue->cancel($envelope->getDeliveryTag());
  * Если возвращает false, то передор возвращается.
  * @return \AMQPQueue - Возвращает очередь чтобы ее можно было удалить, если она уже не нужна
  * @throws \Exception
  */
 public function asyncRead($transactionId, $callBack)
 {
     if (!is_callable($callBack)) {
         throw new \Exception('Callback not callable');
     }
     $chanel = $this->amqp->newChannel();
     $queue = new \AMQPQueue($chanel);
     $queue->setName($transactionId);
     // Таким образом можно проверить существует ли очередь.
     // Если она не существует, то будет ошибка и нужно вернуть пустой ответ
     $queue->setFlags(AMQP_PASSIVE);
     try {
         $queue->declareQueue();
     } catch (\Exception $e) {
         return $queue;
     }
     /** @var \AMQPEnvelope $envelop */
     while ($envelop = $queue->get()) {
         if (call_user_func($callBack, $envelop, $queue) === false) {
             break;
         }
     }
     return $queue;
 }
 /**
  * Check delivered message from MQ
  * @param object $queue
  * @return array $queArray
  * @access public
  */
 public function checkDelivery(\AMQPQueue $queue)
 {
     $queArray = array();
     $queue->bind($this->exchangeOptions['name'], $this->queueOptions['routing_key']);
     while ($message = $queue->get()) {
         if ($message->isRedelivery()) {
             $queArray[] = $message->getDeliveryTag() . "=" . $message->getBody();
         }
     }
     return $queArray;
 }
Пример #16
0
$exchange->setName('benchmark_exchange_' . microtime(true));
$exchange->declareExchange();
$q = new AMQPQueue($ch);
$q->setFlags(AMQP_AUTODELETE);
$q->declareQueue();
$q->bind($exchange->getName());
$message = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.";
$timer = microtime(true);
for ($i = 0; $i < $iterations; $i++) {
    $exchange->publish($message);
}
$timer = microtime(true) - $timer;
echo 'Publish: ', $iterations, ' iterations took ', $timer, 'sec', PHP_EOL;
$timer = microtime(true);
for ($i = 0; $i < $iterations; $i++) {
    if (!$q->get(AMQP_AUTOACK)) {
        echo 'GET failed', PHP_EOL;
    }
}
$timer = microtime(true) - $timer;
echo '    Get: ', $iterations, ' iterations took ', $timer, 'sec', PHP_EOL;
$q->delete();
$exchange->delete();
echo PHP_EOL;
// ==================================
$exchange = new AMQPExchange($ch);
$exchange->setType(AMQP_EX_TYPE_FANOUT);
$exchange->setFlags(AMQP_AUTODELETE);
$exchange->setName('benchmark_exchange_' . microtime(true));
$exchange->declareExchange();
$q = new AMQPQueue($ch);
Пример #17
0
 $conn->connect();
 // assume the AMQP broker has the specified exchange configured.
 #$ex = new AMQPExchange($conn);
 #$ex->declare($exchange, $exType);
 $q = new AMQPQueue($conn, $queue);
 // create a queue, if exsiting, do nothing.
 $q->declare();
 $q->bind($exchange, $rkey);
 // binds the queue to the routing key on the exchange
 #$ex->bind($queue, $rkey);
 // this sample will keep on running until retrieves expected notification message.
 while (true) {
     echo "Please wait ...\n";
     sleep(10);
     // get message from the queue in every 10 seconds.
     $ret = $q->get();
     if (array_key_exists('msg', $ret)) {
         $msg = $ret['msg'];
         // get the message
     }
     if (isset($msg)) {
         echo $msg . "\n";
         // Get Notification data object from the notification message.
         $not = VMware_VCloud_API_Helper::parseString($msg);
         if ('VMware_VCloud_API_Extension_NotificationType' == get_class($not)) {
             // use the entity URL to create an SDK blocking task object.
             $entityLink = getEntityLink('entity', $not);
             $orgLink = getEntityLink('up', $not);
             $userLink = getEntityLink('down', $not);
             echo "entity = " . $entityLink->get_name() . "\n";
             echo "org = " . $orgLink->get_name() . "\n";
Пример #18
0
 /**
  * Gets next message from queue and returnes it or false if queue is emptied
  *
  * @param string $queueName Queue name to consume message from
  *
  * @return AMQPClosure Closure, returns false when queue emptied
  *
  * @throws \AMQPException
  */
 public function get($queueName)
 {
     try {
         $queue = new \AMQPQueue($this->getChannel());
         $queue->setName($queueName);
         $envelope = $queue->get();
         if ($envelope) {
             return new AMQPClosure($envelope->getBody(), $envelope, $queue);
         } else {
             return false;
         }
     } catch (\AMQPException $e) {
         $this->channel = null;
         throw $e;
     }
 }
Пример #19
0
set_time_limit(0);
date_default_timezone_set('Asia/Shanghai');
require_once dirname(__FILE__) . '/class.crawler.php';
require_once dirname(__FILE__) . '/class.proxy.php';
require_once dirname(__FILE__) . '/class.detector.php';
$mysqli = new mysqli('10.168.45.191', 'admin', 'txg19831210', 'crawler');
$mysqli->query('SET NAMES gbk');
$queueName = 'q_price';
$params = array('host' => '10.168.45.191', 'port' => 5672, 'login' => 'guest', 'password' => 'guest', 'vhost' => '/kwd');
$conn = new AMQPConnection($params);
$conn->connect();
$channel = new AMQPChannel($conn);
$queue = new AMQPQueue($channel);
$queue->setName($queueName);
$detector = new detector();
while ($message = $queue->get(AMQP_AUTOACK)) {
    $kwd = $message->getBody();
    $kwdObj = unserialize($kwd);
    $price = $detector->run($kwdObj);
    if ($price['start_price'] && $price['end_price']) {
        $sql = "SELECT * FROM price WHERE kid = {$kwdObj->id} LIMIT 1";
        $result = $mysqli->query($sql);
        if ($result->num_rows) {
            $sql = "UPDATE price SET min_price = '{$price['start_price']}', max_price = '{$price['end_price']}', region = '{$price['region']}', crawl_status = 2, last_update = " . time() . " WHERE kid = " . $kwdObj->id;
        } else {
            $sql = "INSERT INTO price SET kid = {$kwdObj->id}, shop_type = '{$kwdObj->shop_type}', min_price = '{$price['start_price']}', max_price = '{$price['end_price']}', region = '{$price['region']}', crawl_status = 2, last_update = " . time();
        }
        echo $sql . "\n";
        $mysqli->query($sql);
    }
}
Пример #20
0
<?php

echo "<pre>";
$conn_args = array('host' => 'localhost', 'port' => 5672, 'login' => 'guest', 'password' => 'guest', 'vhost' => '/');
$q_name = 't1';
//获取队列名称
$conn = new AMQPConnection($conn_args);
$conn->connect();
$channel = new AMQPChannel($conn);
$q = new AMQPQueue($channel);
$q->setName($q_name);
$q->setFlags(AMQP_DURABLE | AMQP_AUTODELETE);
while ($a = $q->declare()) {
    echo "queue status:" . $a;
    echo "=============\n";
    $message = $q->get(AMQP_AUTOACK);
    print_r($message->getBody());
    echo "\n";
}
$conn->disconnect();
Пример #21
0
$rabbit = new AMQPConnection(array('host' => '127.0.0.1', 'port' => '5672', 'login' => 'guest', 'password' => 'guest'));
$rabbit->connect();
$channel = new AMQPChannel($rabbit);
$queue = new AMQPExchange($channel);
$queue->setName('amq.direct');
/**
 * Добавляем очередь откуда будем брать страницы, которые нужно проиндексировать
 */
$q = new AMQPQueue($channel);
$q->setName('pages_to_scan');
$q->declare();
$q->bind('amq.direct', 'scan');
/**
 * Индексируем, пока в очереди не закончатся сообщения
 */
while ($page = $q->get()) {
    if (!is_object($page)) {
        continue;
    }
    $url = $page->getBody();
    echo "Scanning: {$url}\n";
    $scanner = new WebScanner($url);
    $links = $scanner->getAllLinks();
    foreach ($links as $link) {
        /**
         * Если страница относится к указанному домену и еще не была проиндексирована -- добавляем ее в очередь на индексацию
         */
        if (strpos($link, $domain) !== FALSE && !Storage::getInstance()->containsPage($link)) {
            $queue->publish($link, 'scan');
        }
    }
Пример #22
0
 /**
  * Read all messages
  *
  * @access public
  *
  * @param string $chat name chat room
  * @param string $route name route
  * @param string $nameReader name queue
  *
  * @return array
  * @throws \AMQPConnectionException
  * @throws \AMQPQueueException
  * @throws \AMQPChannelException
  */
 public function readAll($chat, $route, $nameReader)
 {
     $queue = new \AMQPQueue($this->channel);
     $queue->setName($nameReader);
     /** @noinspection PhpUndefinedMethodInspection */
     $queue->declare();
     $queue->bind($chat, $route);
     $result = [];
     while ($envelop = $queue->get()) {
         $queue->ack($envelop->getDeliveryTag());
         $result[] = $envelop;
     }
     return $result;
 }
Пример #23
0
 /**
  * @param \AMQPQueue $queue
  * @param string $correlationId
  * @return \AMQPEnvelope|bool
  */
 protected function waitForResponse(\AMQPQueue $queue, $correlationId)
 {
     $time = microtime(true);
     $timeout = $time + $this->responseTimeout;
     while ($time < $timeout) {
         $envelope = $queue->get(AMQP_AUTOACK);
         if ($envelope === false) {
             usleep($this->responseWaitInterval);
             $time = microtime(true);
             continue;
         }
         if ($envelope->getCorrelationId() !== $correlationId) {
             // This is actually only useful when you override declareResponseQueue and cleanupResponseQueue
             // You can override theses to use only a single queue for all rpc response calls
             // This is still implemented to because I'm a sucker for FC ;)
             continue;
         }
         return $envelope;
     }
     return false;
 }
$rabbit = new AMQPConnection(array('host' => '127.0.0.1', 'port' => '5672', 'login' => 'guest', 'password' => 'guest'));
$rabbit->connect();
$channel = new AMQPChannel($rabbit);
$queue = new AMQPExchange($channel);
$queue->setName('amq.direct');
/**
 * Добавляем очередь откуда будем брать страницы
 */
$q = new AMQPQueue($channel);
$q->setName('images_to_scan');
$q->declare();
$q->bind('amq.direct', 'analyze_image');
/**
 * Обрабатываем пока в очереди не закончатся сообщения
 */
while (true) {
    $image = $q->get();
    if ($image) {
        $url = $image->getBody();
        echo "Checking: {$url}\n";
        $analyzer = new ImageAnalyzer($url);
        /**
         * Если картинка еще не была проанализирована, обрабатываем и добавляем в индекс
         */
        $analyzer->analyze();
        $q->ack($image->getDeliveryTag());
    } else {
        sleep(1);
    }
}
$rabbit->disconnect();
Пример #25
0
<?php

// print_r( get_loaded_extensions() );
$cnn = new AMQPConnection();
echo "------  connect ...  --------\n";
$ret = $cnn->connect();
echo "------  connect Ok  --------\n";
var_dump($ret);
$ch = new AMQPChannel($cnn);
$q = new AMQPQueue($ch);
echo "------  queue Ok  --------\n";
// $cnn->disconnect(AMQP_NOPARAM);
$q->setName('direct_messages');
// 	echo "------  declare  --------\n";
// $q->declare();
// 	echo "------  bind  --------\n";
// $q->bind('amq.direct', 'route_to_everybody');
echo "------  get  --------\n";
$envelope = $q->get();
var_dump($envelope);
$cnn->disconnect();