/**
  * Close the running connection
  */
 public function close()
 {
     if (null !== $this->connection && $this->connection->isConnected()) {
         $this->channel->close();
         $this->connection->close();
     }
 }
Example #2
0
 public function tearDown()
 {
     if ($this->ch2) {
         $this->ch2->close();
     }
     if ($this->conn) {
         $this->conn->close();
     }
 }
Example #3
0
 public function tearDown()
 {
     if ($this->ch) {
         $this->ch->exchange_delete($this->exchange_name);
         $this->ch->close();
     }
     if ($this->conn) {
         $this->conn->close();
     }
 }
 public function tearDown()
 {
     try {
         $this->object->deleteQueue('/', self::QUEUE_TEST_NAME);
     } catch (\Exception $e) {
     }
     try {
         $this->object->deleteExchange('/', self::EXCHANGE_TEST_NAME);
     } catch (\Exception $e) {
     }
     $this->channel->close();
     $this->conn->close();
 }
 /**
  * Process incoming request to generate pdf invoices and send them through 
  * email.
  */
 public function listen()
 {
     $this->log->addInfo('Begin listen routine');
     $connection = new AMQPConnection('localhost', 5672, 'guest', 'guest');
     $channel = $connection->channel();
     $channel->queue_declare('invoice_queue', false, true, false, false);
     /**
      * don't dispatch a new message to a worker until it has processed and 
      * acknowledged the previous one. Instead, it will dispatch it to the 
      * next worker that is not still busy.
      */
     $channel->basic_qos(null, 1, null);
     /**
      * indicate interest in consuming messages from a particular queue. When they do 
      * so, we say that they register a consumer or, simply put, subscribe to a queue.
      * Each consumer (subscription) has an identifier called a consumer tag
      */
     $channel->basic_consume('invoice_queue', '', false, false, false, false, array($this, 'process'));
     $this->log->addInfo('Consuming from queue');
     while (count($channel->callbacks)) {
         $this->log->addInfo('Waiting for incoming messages');
         $channel->wait();
     }
     $channel->close();
     $connection->close();
 }
Example #6
0
 function processvideo()
 {
     $this->load->config('amqp');
     $exchange = 'video';
     $queue = 'video_q';
     $consumer_tag = 'consumer';
     $connection = new AMQPConnection($this->config->item('host'), $this->config->item('port'), $channel = $this->config->item('user'), $channel = $this->config->item('pass'), "/");
     $channel = $connection->channel();
     $channel->queue_declare($queue, false, true, false, false);
     $callback = function ($msg) {
         print_r($msg);
         die;
         $collection = $this->mongo_db->db->selectCollection('video');
         $result = $collection->update(["video_id" => $msg->video_id], ["status" => "processing"]);
         sleep(range(5, 10));
         $start_date = new DateTime('2000-01-01 ' . $msg->start_time);
         $since_start = $start_date->diff(new DateTime('2012-09-11 ' . $msg->end_time));
         $video_len = $since_start->h . ":" . $since_start->i . ":" . $since_start->s;
         $result = $collection->update(["video_id" => $msg->video_id], ["status" => "done", "link" => "https://youtube.com?dffd", "video_len" => $video_len]);
         $msg->delivery_info['channel']->basic_ack($msg->delivery_info['delivery_tag']);
     };
     $channel->basic_qos(null, 1, null);
     $channel->basic_consume($queue, '', false, false, false, false, $callback);
     while (count($channel->callbacks)) {
         $channel->wait();
     }
     $channel->close();
     $connection->close();
 }
 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;
 }
 public function disconnect()
 {
     if ($this->isConnected()) {
         $this->channel->close();
         $this->connection->close();
         $this->logger->info("Mq Service Disconnected");
     }
 }
 /**
  * @inherit
  */
 public function sendMessage()
 {
     $msg = new AMQPMessage($this->data, $this->messageProperties);
     $this->channel->basic_publish($msg, $this->publishExchange, $this->publishRoutingKey, $this->publishMandatory, $this->publishImmediate, $this->publishTicket);
     $this->channel->close();
     $this->connection->close();
     return true;
 }
Example #10
0
 /**
  * Close the connection with the RabbitMQ server
  *
  * @return void
  */
 public function close()
 {
     if (isset($this->AMQPConnection)) {
         $this->AMQPConnection->close();
     }
     if (isset($this->channel)) {
         $this->channel->close();
     }
 }
 /**
  */
 public function shutdown()
 {
     if ($this->channel) {
         $this->channel->close();
     }
     if ($this->connection) {
         $this->connection->close();
     }
 }
Example #12
0
 public function close()
 {
     if ($this->channel != null) {
         $this->channel->close();
     }
     if ($this->connection != null) {
         $this->connection->close();
     }
 }
 /**
  * Sends an invoice generation task to the workers
  * 
  * @param int $invoiceNum
  */
 public function execute($invoiceNum)
 {
     $connection = new AMQPConnection('localhost', 5672, 'guest', 'guest');
     $channel = $connection->channel();
     $channel->queue_declare('invoice_queue', false, true, false, false);
     $msg = new AMQPMessage($invoiceNum, array('delivery_mode' => 2));
     $channel->basic_publish($msg, '', 'invoice_queue');
     $channel->close();
     $connection->close();
 }
 public static function execute($entry)
 {
     if (!empty($entry)) {
         $connection = new AMQPConnection('impact.ccat.eu', 5672, 'myjar', 'myjar');
         $channel = $connection->channel();
         $msg = new AMQPMessage($entry, array('content_type' => 'application/json'));
         $channel->basic_publish($msg, '', 'solved-interest-queue');
         $channel->close();
         $connection->close();
     }
 }
Example #15
0
 function send($nachricht)
 {
     $connection = new AMQPConnection('141.22.29.97', 5672, 'invoiceSender', 'invoiceSender');
     $channel = $connection->channel();
     $channel->queue_declare('controllerInvoice', false, false, false, false);
     settype($nachricht, "string");
     $msg = new AMQPMessage($nachricht);
     $channel->basic_publish($msg, '', 'controllerInvoice');
     $channel->close();
     $connection->close();
 }
Example #16
0
 /**
  * Sends an invoice generation task to the workers
  * 
  * @param int $invoiceNum
  */
 public function execute($invoiceNum)
 {
     $this->log->addInfo('Received invoice for processing: ' . $invoiceNum);
     $connection = new AMQPConnection('localhost', 5672, 'guest', 'guest');
     $channel = $connection->channel();
     $channel->queue_declare('invoice_queue', false, true, false, false);
     $msg = new AMQPMessage($invoiceNum, array('delivery_mode' => 2));
     $channel->basic_publish($msg, '', 'invoice_queue');
     $this->log->addInfo('Published task to worker');
     $channel->close();
     $connection->close();
 }
 public function actionIndex($name, $message)
 {
     // Create a connection with RabbitMQ server.
     $connection = new AMQPConnection('localhost', 5672, 'guest', 'guest');
     $channel = $connection->channel();
     // Create and publish the message to the exchange.
     $data = array('type' => 'chat', 'data' => array('name' => $name, 'message' => $message, 'dateTime' => date('d/m/Y H:i:s')));
     $message = new AMQPMessage(json_encode($data));
     $channel->basic_publish($message, 'chats');
     // Close connection.
     $channel->close();
     $connection->close();
 }
Example #18
0
 /**
  * Sends a message to the pizzaTime queue.
  * 
  * @param string $message
  */
 public function execute($message)
 {
     $this->log->addInfo('Received message to send: ' . $message);
     $connection = new AMQPConnection('localhost', 5672, 'guest', 'guest');
     /** @var $channel AMQPChannel */
     $channel = $connection->channel();
     $channel->queue_declare('pizzaTime', false, false, false, false);
     $msg = new AMQPMessage($message);
     $channel->basic_publish($msg, '', 'pizzaTime');
     $this->log->addInfo('Message sent');
     $channel->close();
     $connection->close();
 }
Example #19
0
function send_queue($mq_host, $mq_port, $mq_user, $mq_password, $messageData, $queue_name)
{
    $connection = new AMQPConnection($mq_host, $mq_port, $mq_user, $mq_password);
    $channel = $connection->channel();
    $channel->queue_declare($queue_name, false, true, false, false);
    if (is_array($messageData)) {
        $messageData = json_encode($messageData);
    }
    $msg = new AMQPMessage($messageData, array('delivery_mode' => 2));
    $channel->basic_publish($msg, '', $queue_name);
    log_message('info', ' [x] Sent ' . $messageData);
    $channel->close();
    $connection->close();
}
 /**
  * Listens for incoming messages
  */
 public function listen()
 {
     $this->log->addInfo('Start listening routine');
     $connection = new AMQPConnection('localhost', 5672, 'guest', 'guest');
     $channel = $connection->channel();
     $channel->queue_declare('pizzaTime', false, false, false, false);
     $channel->basic_consume('pizzaTime', '', false, true, false, false, array($this, 'addLog'));
     $this->log->addInfo('Consuming from channel');
     while (count($channel->callbacks)) {
         $channel->wait();
     }
     $channel->close();
     $connection->close();
 }
Example #21
0
 /**
  * Send a message to Queue about indexing entry.
  *
  * @param EventInterface $event
  */
 public function index(EventInterface $event)
 {
     try {
         $connection = new AMQPConnection('localhost', 5672, 'guest', 'guest');
         $channel = $connection->channel();
         $channel->queue_declare('search-index', false, false, false, false);
         $params = $event->getParams();
         $msg = new AMQPMessage(json_encode($params));
         $channel->basic_publish($msg, '', 'search-index');
         $channel->close();
         $connection->close();
     } catch (\Exception $e) {
         $this->logger->warn("Queue Service when indexing entries: " . $e->getMessage());
     }
 }
 /**
  * запустить сервис RabbitMQ
  * запустить из папки C:\xampp\htdocs\yii2-advanced-rabbitmq-nodejs.advanced\frontend\web\node
  * команду node js/server.js в Git Bash
  * запустить из папки C:\xampp\htdocs\yii2-advanced-rabbitmq-nodejs.advanced
  * команду yii example/test
  * и обновить страницу http://yii2-advanced-rabbitmq-nodejs.frontend/
  * и запустится обновление через каждые 3 секунды страницы
  *
  * полезные ссылки:
  * http://makeomatic.ru/blog/2013/10/16/RabbitMQ/
  * http://saboteur.me/delayed-tasks-rabbitmq/
  * https://github.com/vanbosse/rabbitmq-demo
  * http://vanbosse.be/blog/detail/pub-sub-with-rabbitmq-and-websocket
  * http://rabbitmq.demo/
  * http://localhost:15672
  *
  * // установить пакеты:
  * zhanat@zhanat-530U4E-540U4E:~/sites/yii2-advanced-rabbitmq-nodejs.advanced/
  * frontend/web/node/js$ npm install rabbit.js
  * zhanat@zhanat-530U4E-540U4E:~/sites/yii2-advanced-rabbitmq-nodejs.advanced/
  * frontend/web/node/js$ npm install socket.io
  */
 public function actionIndex()
 {
     // Create a connection with RabbitMQ server.
     $connection = new AMQPConnection('localhost', 5672, 'guest', 'guest');
     $channel = $connection->channel();
     // Create and publish the message to the exchange.
     while (true) {
         $data = array('type' => 'update', 'data' => array('minutes' => rand(0, 60), 'seconds' => rand(0, 60)));
         $message = new AMQPMessage(json_encode($data));
         $channel->basic_publish($message, 'updates');
         sleep(3);
     }
     // Close connection.
     $channel->close();
     $connection->close();
 }
Example #23
0
 /**
  * Listens for incoming messages
  */
 public function listen()
 {
     $this->log->addInfo('Begin listen routine');
     $connection = new AMQPConnection('localhost', 5672, 'guest', 'guest');
     $channel = $connection->channel();
     $channel->queue_declare('rpc_queue', false, false, false, false);
     $channel->basic_qos(null, 1, null);
     $channel->basic_consume('rpc_queue', '', false, false, false, false, array($this, 'callback'));
     $this->log->addInfo('Consuming from queue');
     while (count($channel->callbacks)) {
         $this->log->addInfo('Waiting for incoming messages');
         $channel->wait();
     }
     $channel->close();
     $connection->close();
 }
 /**
  * consumer
  */
 public function edit()
 {
     $connection = new AMQPConnection('localhost', 5672, 'guest', 'guest');
     $channel = $connection->channel();
     $channel->queue_declare('hello', false, false, false, false);
     echo ' [*] Waiting for messages. To exit press CTRL+C', "\n";
     $callback = function ($msg) {
         echo " [x] Received ", $msg->body, "\n";
     };
     $channel->basic_consume('hello', '', false, true, false, false, $callback);
     while (count($channel->callbacks)) {
         $channel->wait();
     }
     $channel->close();
     $connection->close();
 }
 public function push($data)
 {
     $exchange = 'router';
     $queue = 'msgs';
     $conn = new AMQPConnection('localhost', '5672', 'guest', 'guest', '/');
     $ch = $conn->channel();
     $ch->queue_declare($queue, false, true, false, false);
     $ch->exchange_declare($exchange, 'direct', false, true, false);
     $ch->queue_bind($queue, $exchange);
     $msg_body = $data;
     //implode(' ', array_slice($argv, 1));
     $msg = new AMQPMessage($msg_body, array('content_type' => 'text/plain', 'delivery_mode' => 2));
     $ch->basic_publish($msg, $exchange);
     $ch->close();
     $conn->close();
 }
 public function actionIndex()
 {
     $connection = new AMQPConnection('localhost', 5672, 'guest', 'guest');
     $channel = $connection->channel();
     $channel->exchange_declare('images', 'direct', false, false, false);
     list($queue_name, , ) = $channel->queue_declare("", false, false, true, false);
     $channel->queue_bind($queue_name, 'images', 'images routing key');
     $channel2 = $connection->channel();
     $channel2->exchange_declare('files', 'fanout', false, false, false);
     $callback = function ($msg) use($channel2) {
         $data = json_decode($msg->body);
         $width = ImagesHelper::width($data->source);
         $height = ImagesHelper::height($data->source);
         // создание миниатюры изображения
         $thumbWidth = 207;
         // ширина будущего изображения в px
         $thumbHeight = $thumbWidth * $height / $width;
         // высота будущего изображения в px
         if ($fixResizedImage = ImagesHelper::resize($data->source, $thumbWidth, $thumbHeight)) {
             // сохранение полученного изображения
             $widthThumb = ImagesHelper::save($fixResizedImage, $data->source, 'w_207_', $data->extension);
         }
         // создание миниатюры изображения
         $thumbHeight = 207;
         // высота будущего изображения в px
         $thumbWidth = $thumbHeight * $width / $height;
         // ширина будущего изображения в px
         if ($fixResizedImage2 = ImagesHelper::resize($data->source, $thumbWidth, $thumbHeight)) {
             // сохранение полученного изображения
             $heightThumb = ImagesHelper::save($fixResizedImage2, $data->source, 'h_207_', $data->extension);
         }
         $image = ['type' => 'file', 'data' => ['name' => $data->name, 'widthThumb' => DIRECTORY_SEPARATOR . 'uploads' . DIRECTORY_SEPARATOR . 'w_207_' . $data->name . '.' . $data->extension, 'heightThumb' => DIRECTORY_SEPARATOR . 'uploads' . DIRECTORY_SEPARATOR . 'h_207_' . $data->name . '.' . $data->extension]];
         $msg = new AMQPMessage(json_encode($image));
         $channel2->basic_publish($msg, 'files');
     };
     /*
      * 4-й - false - по ум-ю (то есть все сообщ-я хранятся в очереди всегда,
      * даже после прочтения), если true - то сообщ-е уд-ся из очереди сразу после прочтения
      */
     /**/
     $channel->basic_consume($queue_name, '', false, true, false, false, $callback);
     while (count($channel->callbacks)) {
         $channel->wait();
     }
     $channel->close();
     $connection->close();
 }
Example #27
0
function send_mail($to, $from, $from_password, $message)
{
    define('HOST', 'localhost');
    define('PORT', 5672);
    define('USER', 'guest');
    define('PASS', 'guest');
    define('VHOST', '/');
    //If this is enabled you can see AMQP output on the CLI
    //    define('AMQP_DEBUG', true);
    $exchange = 'tester';
    $queue = 'test_queue';
    $conn = new AMQPConnection(HOST, PORT, USER, PASS, VHOST);
    $ch = $conn->channel();
    /*
     The following code is the same both in the consumer and the producer.
     In this way we are sure we always have a queue to consume from and an
     exchange where to publish messages.
    */
    /*
     name: $queue
     passive: false
     durable: true // the queue will survive server restarts
     exclusive: false // the queue can be accessed in other channels
     auto_delete: false //the queue won't be deleted once the channel is closed.
    */
    $ch->queue_declare($queue, false, false, false, false);
    /*
     name: $exchange
     type: direct
     passive: false
     durable: true // the exchange will survive server restarts
     auto_delete: false //the exchange won't be deleted once the channel is closed.
    */
    $ch->exchange_declare($exchange, 'fanout', false, false, false);
    $ch->queue_bind($queue, $exchange, "");
    $msg_body = array("to" => $to, "from" => $from, "from_pass" => $from_password, "message" => $message);
    $msg_body = json_encode($msg_body);
    //    $msg_body = 'hello kamil';
    $msg = new AMQPMessage($msg_body, array('content_type' => 'text/plain', 'delivery_mode' => 2));
    $ch->basic_publish($msg, $exchange);
    $ch->close();
    $conn->close();
}
 public function push($data)
 {
     if (Auth::check()) {
         $exchange = 'router';
         $queue = 'msgs';
         $conn = new AMQPConnection('localhost', '5672', 'guest', 'guest', '/');
         $ch = $conn->channel();
         $ch->queue_declare($queue, false, true, false, false);
         $ch->exchange_declare($exchange, 'direct', false, true, false);
         $ch->queue_bind($queue, $exchange);
         $msg_body = $data;
         $msg = new AMQPMessage($msg_body, array('content_type' => 'text/plain', 'delivery_mode' => 2));
         $ch->basic_publish($msg, $exchange);
         $ch->close();
         $conn->close();
     } else {
         return redirect(url());
     }
 }
Example #29
0
function publish_msg($data)
{
    //广播信息到外面
    $exchange = 'check_cost_exchange';
    $conn = new AMQPConnection(HOST, PORT, USER, PASS, VHOST);
    $ch = $conn->channel();
    $ch->exchange_declare($exchange, 'fanout', false, true, false);
    /*
    	name: $exchange
    	type: fanout
    	passive: false // don't check is an exchange with the same name exists
    	durable: false // the exchange won't survive server restarts
    	auto_delete: true //the exchange will be deleted once the channel is closed.
    */
    //$ch->exchange_declare($exchange, 'fanout', false, true, false);
    $msg = new AMQPMessage(json_encode($data), array('content_type' => 'text/plain'));
    $ch->basic_publish($msg, $exchange);
    $ch->close();
    $conn->close();
}
 public function actionIndex()
 {
     $cache = Yii::$app->cache;
     if (!$cache->get('basicIsStart')) {
         $nodeCommand = 'node ' . Yii::getAlias('@nodejs') . DIRECTORY_SEPARATOR . 'server.js';
         $nodeProcess = new Process($nodeCommand);
         $nodePid = $nodeProcess->getPid();
         Yii::$app->session->set('chatNodePid', $nodePid);
         $cache->set('basicIsStart', true, 1800);
         // Create a connection with RabbitMQ server.
         $connection = new AMQPConnection('localhost', 5672, 'guest', 'guest');
         $channel = $connection->channel();
         // Create a fanout exchange.
         // A fanout exchange broadcasts to all known queues.
         $channel->exchange_declare('updates', 'fanout', false, false, false);
         // Close connection.
         $channel->close();
         $connection->close();
     }
     return $this->render('index');
 }