Ejemplo n.º 1
0
 public function testQueueConsumer()
 {
     $c = new AMQPPecl(AMQPCredentials::createDefault());
     $channel = $c->createChannel(1);
     $channel->exchangeDeclare(self::EXCHANGE_NAME, AMQPExchangeConfig::create()->setType(new AMQPExchangeType(AMQPExchangeType::DIRECT))->setDurable(true));
     $inQueueCount = $channel->queueDeclare(self::QUEUE_NAME, AMQPQueueConfig::create()->setDurable(true));
     $this->assertSame(0, $inQueueCount);
     $channel->queueBind(self::QUEUE_NAME, self::EXCHANGE_NAME, self::ROUTING_KEY);
     //cleanup
     $channel->queuePurge(self::QUEUE_NAME);
     for ($i = 1; $i <= self::COUNT_OF_PUBLISH; $i++) {
         $channel->basicPublish(self::EXCHANGE_NAME, self::ROUTING_KEY, AMQPOutgoingMessage::create()->setBody("message {$i}")->setTimestamp(Timestamp::makeNow())->setAppId(__CLASS__)->setMessageId($i)->setContentEncoding('utf-8'));
     }
     $consumer = new AMQPTestCaseQueueConsumer($channel);
     $channel->basicConsume(self::QUEUE_NAME, false, $consumer);
     $i = 0;
     while (true) {
         $message = $consumer->getNextDelivery();
         //send acknowledge to RabbitMQ
         $channel->basicAck($message->getDeliveryTag(), false);
         $i++;
         $this->assertSame($consumer->getConsumerTag(), $message->getConsumerTag());
         $this->assertEquals($i, $message->getMessageId());
         $this->assertEquals(__CLASS__, $message->getAppId());
         $this->assertEquals("message {$i}", $message->getBody());
         if ($i == self::COUNT_OF_PUBLISH) {
             break;
         }
     }
     $channel->basicCancel($consumer->getConsumerTag());
     //observer logic test
     $this->assertSame('ABBBBBC', $consumer->getCheckString());
     //drop channels and close connection
     $c->disconnect();
     $c = new AMQPPecl(AMQPCredentials::createDefault());
     $channel = $c->createChannel(1);
     $channel->exchangeDeclare(self::EXCHANGE_NAME, AMQPExchangeConfig::create()->setType(new AMQPExchangeType(AMQPExchangeType::DIRECT))->setDurable(true));
     //queue must be empty, because we sent acknowledge
     $inQueueCount = $channel->queueDeclare(self::QUEUE_NAME, AMQPQueueConfig::create()->setDurable(true));
     $this->assertSame(0, $inQueueCount);
     //cleanup
     $channel->queuePurge(self::QUEUE_NAME);
 }
 /**
  * @throws AMQPServerExchangeException|AMQPServerConnectionException
  * @return AMQPChannelInterface
  **/
 public function basicPublish($exchange, $routingKey, AMQPOutgoingMessage $msg)
 {
     try {
         $obj = $this->lookupExchange($exchange);
         $result = $obj->publish($msg->getBody(), $routingKey, $msg->getBitmask(new AMQPPeclOutgoingMessageBitmask()), $msg->getProperties());
     } catch (AMQPExchangeException $e) {
         throw new AMQPServerException($e->getMessage(), $e->getCode(), $e);
     }
     $this->checkCommandResult($result, "Could not publish to exchange");
     return $this;
 }
 /**
  * @param AMQPChannelInterface $channel
  * @param bool $check
  * @param string $key
  * @param string $queueName
  */
 protected function publishMessages(AMQPChannelInterface $channel, $check = true, $label = 'basic')
 {
     for ($i = 1; $i <= self::COUNT_OF_PUBLISH; $i++) {
         $channelInterface = $channel->basicPublish(self::$queueList[$label]['exchange'], self::$queueList[$label]['key'], AMQPOutgoingMessage::create()->setBody("message {$i}")->setTimestamp(Timestamp::makeNow())->setAppId(__CLASS__)->setMessageId($i)->setContentEncoding('utf-8'));
         if ($check) {
             $this->assertInstanceOf('AMQPChannelInterface', $channelInterface);
         }
     }
     if ($check) {
         $this->checkMessageCount($channel, $label);
     }
 }