Exemplo n.º 1
0
 public function testDestruct()
 {
     $this->channelMock->expects($this->once())->method('close');
     $this->clientMock->expects($this->once())->method('close');
     $queue = new MessageQueue($this->clientMock);
     unset($queue);
 }
Exemplo n.º 2
0
 /**
  * @param DomainMessage $domainMessage
  */
 private function publishWithAMQP(DomainMessage $domainMessage)
 {
     $payload = $domainMessage->getPayload();
     $eventClass = get_class($payload);
     $this->logger->info("publishing message with event type {$eventClass} to exchange {$this->exchange}");
     $this->channel->basic_publish($this->messageFactory->createAMQPMessage($domainMessage), $this->exchange);
 }
 protected function setupConnection()
 {
     Yii::trace('Connecting to broker...', __METHOD__);
     $this->connection = new AMQPConnection($this->host, $this->port, $this->user, $this->password, $this->vhost, $this->insist, $this->login_method, $this->login_response, $this->locale, $this->connection_timeout, $this->read_write_timeout, $this->context);
     $this->channel = $this->connection->channel();
     $this->channel->queue_declare($this->queue, false, true, false, false);
 }
Exemplo n.º 4
0
 /**
  * Close the running connection
  */
 public function close()
 {
     if (null !== $this->connection && $this->connection->isConnected()) {
         $this->channel->close();
         $this->connection->close();
     }
 }
 /**
  * Test declaring a queue on the channel using the configured queue name
  */
 public function testQueueDeclaration()
 {
     $qName = 'queue_name';
     $this->setStubConfigData(['getQueueName' => $qName, 'getQueueConfiguration' => ['queue' => $qName, 'passive' => false, 'durable' => true, 'exclusive' => true, 'auto_delete' => true, 'nowait' => true]]);
     $this->channel->expects($this->once())->method('queue_declare')->with($this->identicalTo($qName));
     $this->invokeRestrictedMethod($this->amqpApi, 'declareQueue');
 }
 protected function setUpAmqp()
 {
     $this->conn = new AMQPSocketConnection($_SERVER['AMQP_HOST'], $_SERVER['AMQP_PORT'], $_SERVER['AMQP_USER'], $_SERVER['AMQP_PASS'], $_SERVER['AMQP_VHOST']);
     $this->channel = $this->conn->channel();
     $this->channel->exchange_declare('event_band.test.exchange', 'topic');
     $this->channel->queue_declare('event_band.test.event');
     $this->channel->queue_bind('event_band.test.event', 'event_band.test.exchange', 'event.#');
 }
 /**
  * Gets publisher AMQ channel
  *
  * @return AMQPChannel
  */
 protected function getChannel()
 {
     if (null === $this->channel) {
         $this->channel = $this->amq->channel();
         $this->channel->exchange_declare($this->exchange, 'topic', false, true, false);
     }
     return $this->channel;
 }
 function it_releases_job_onto_rabbitmq(AMQPChannel $channel, RabbitMQQueue $queue)
 {
     // delete
     $channel->basic_ack('fooTagId')->shouldBeCalled();
     // release with attempts added into body
     $queue->later(1, 'foo', [0 => "someData", "attempts" => 1], 'default')->shouldBeCalled();
     $this->release(1);
 }
Exemplo n.º 9
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.º 10
0
 private function getChannel()
 {
     if ($this->channel) {
         return $this->channel;
     }
     $this->channel = $this->connection->channel();
     $this->channel->queue_declare($this->queue, false, false, false, false);
     return $this->channel;
 }
Exemplo n.º 11
0
 /**
  * Destructor.
  *
  * Closes RabbitMQ connection and channel if necessary.
  */
 public function __destruct()
 {
     if ($this->channel) {
         $this->channel->close();
     }
     if ($this->connection) {
         $this->connection->close();
     }
 }
Exemplo n.º 12
0
 public function __destroy()
 {
     if ($this->_channel) {
         $this->_channel->close();
     }
     if ($this->getConnection && $this->getConnection->isConnected()) {
         $this->getConnection->close();
     }
 }
Exemplo n.º 13
0
 public function tearDown()
 {
     if ($this->ch2) {
         $this->ch2->close();
     }
     if ($this->conn) {
         $this->conn->close();
     }
 }
Exemplo n.º 14
0
 /**
  * Creates (if not yet created) and returns an AMQP channel.
  *
  * @return \PhpAmqpLib\Channel\AMQPChannel
  */
 protected function getChannel()
 {
     if (null === $this->channel) {
         $this->channel = $this->connection->channel();
         $this->channel->queue_declare($this->queueName, false, false, false, false);
         $this->channel->basic_qos(null, 1, null);
     }
     return $this->channel;
 }
Exemplo n.º 15
0
 /**
  * @param $exchange_name
  */
 private function connect($exchange_name)
 {
     if (null !== $this->channel) {
         return;
     }
     $this->channel = $this->connection->channel();
     $this->channel->exchange_declare($exchange_name, 'fanout', false, true, false);
     $this->channel->queue_declare($exchange_name, false, true, false, false);
     $this->channel->queue_bind($exchange_name, $exchange_name);
 }
Exemplo n.º 16
0
 public function tearDown()
 {
     if ($this->ch) {
         $this->ch->exchange_delete($this->exchange_name);
         $this->ch->close();
     }
     if ($this->conn) {
         $this->conn->close();
     }
 }
Exemplo n.º 17
0
 /**
  *
  * {@inheritDoc}
  *
  */
 protected function write(array $record)
 {
     $data = $record["formatted"];
     $routingKey = sprintf('%s.%s', substr($record['level_name'], 0, 4), $record['channel']);
     if ($this->exchange instanceof AMQPExchange) {
         $this->exchange->publish($data, strtolower($routingKey), 0, array('delivery_mode' => 2, 'Content-type' => 'application/json'));
     } else {
         $this->exchange->basic_publish(new AMQPMessage((string) $data, array('delivery_mode' => 2, 'content_type' => 'application/json')), $this->exchangeName, strtolower($routingKey));
     }
 }
Exemplo n.º 18
0
 /**
  * @param $queue
  *
  * @return mixed
  */
 public function consume($queue)
 {
     $messageBody = false;
     $message = $this->ch->basic_get($queue);
     if ($message) {
         $this->ch->basic_ack($message->delivery_info['delivery_tag']);
         $messageBody = $message->body;
     }
     return $messageBody;
 }
Exemplo n.º 19
0
 /**
  * @return void
  */
 public function initialize()
 {
     if ($this->initialized) {
         return;
     }
     $this->initialized = true;
     $this->exchange->initialize();
     $this->queue->initialize();
     $this->channel->queue_bind($this->queue->name(), $this->exchange->name());
 }
Exemplo n.º 20
0
 /**
  * @return AMQPChannel
  */
 public function getAmqpChannel()
 {
     if ($this->amqp_channel) {
         return $this->amqp_channel;
     }
     $this->amqp_channel = $this->connection->getAmqpConnection()->channel();
     $this->amqp_channel->queue_declare($this->queue_config['queue_name'], false, $is_durable = true, false, false);
     $this->amqp_channel->basic_qos(null, $this->queue_config['fetch_count'], null);
     return $this->amqp_channel;
 }
Exemplo n.º 21
0
 /**
  * @return void
  */
 public function initialize()
 {
     if ($this->initialized) {
         return;
     }
     $this->initialized = true;
     $this->channel->queue_declare($this->name, $this->passive, $this->durable, $this->exclusive, $this->autoDelete, $this->noWait);
     foreach ($this->bindings as $binding) {
         $binding->initialize();
     }
 }
Exemplo n.º 22
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);
 }
 /**
  * @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.º 24
0
 /**
  * Initializes the message queue class
  *
  * @param \PhpAmqpLib\Channel\AMQPChannel $channel AMQP channel
  * @param string $queue Message queue name
  * @throws \Aimeos\MW\MQueue\Exception
  */
 public function __construct(\PhpAmqpLib\Channel\AMQPChannel $channel, $queue)
 {
     try {
         $channel->queue_declare($queue, false, true, false, false);
         $channel->basic_qos(null, 1, null);
     } catch (\Exception $e) {
         throw new \Aimeos\MW\MQueue\Exception($e->getMessage());
     }
     $this->channel = $channel;
     $this->queue = $queue;
 }
Exemplo n.º 25
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();
 }
 public function __construct($name, AMQPChannel $channel, $options)
 {
     $this->name = $name;
     $this->channel = $channel;
     $this->type = isset($options["type"]) ? $options["type"] : "direct";
     $this->passive = isset($options["passive"]) ? $options["passive"] : false;
     $this->durable = isset($options["durable"]) ? $options["durable"] : true;
     $this->auto_delete = isset($options["auto_delete"]) ? $options["auto_delete"] : false;
     if ($name != "") {
         $channel->exchange_declare($name, $this->type, $this->passive, $this->durable, $this->auto_delete);
     }
 }
 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();
 }
Exemplo n.º 29
0
 /**
  * Driver should compose AMQP message and publish it to the channel.
  */
 public function testPublish()
 {
     $reply = $this->getMockBuilder(Invoker\Reply::class)->disableOriginalConstructor()->getMock();
     $this->channel->expects($this->once())->method('basic_publish')->willReturnCallback(function (AMQPMessage $message, $exchange, $routingKey) use($reply) {
         $headers = ['base' => 'dec', 'to' => 'calc', 'topic' => 'add', 'version' => '1.0'];
         $this->assertJsonStringEqualsJsonString('{"a": 10, "b": 53}', $message->body);
         $this->assertEquals($headers, $message->get('application_headers')->getNativeData());
         $this->assertEquals($this->exchange, $exchange);
         $this->assertEquals('add', $routingKey);
         return $reply;
     });
     $this->driver->publish(new Location('calc'), $this->createRequest());
 }
Exemplo n.º 30
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();
     }
 }