/**
  * @throws AMQPServerException|AMQPServerConnectionException
  * @return integer - the message count in queue
  **/
 public function queueDeclare($name, AMQPQueueConfig $conf)
 {
     $this->checkConnection();
     try {
         $this->queueList[$name] = new AMQPQueue($this->transport->getLink());
         $obj = $this->queueList[$name];
         $result = $obj->declare($name, $conf->getBitmask(new AMQPPeclQueueBitmask()));
     } catch (AMQPQueueException $e) {
         throw new AMQPServerException($e->getMessage(), $e->getCode(), $e);
     }
     $this->checkCommandResult(is_int($result), "Could not declare queue");
     return $result;
 }
 /**
  * @throws AMQPServerException|AMQPServerConnectionException
  * @return integer - the message count in queue
  **/
 public function queueDeclare($name, AMQPQueueConfig $conf)
 {
     $this->checkConnection();
     try {
         if (isset($this->queueList[$name])) {
             unset($this->queueList[$name]);
         }
         $this->queueList[$name] = new AMQPQueue($this->getChannelLink());
         $obj = $this->queueList[$name];
         $obj->setName($name);
         $obj->setFlags($conf->getBitmask(new AMQPPeclQueueBitmask()));
         $obj->setArguments($conf->getArguments());
         $result = $obj->declare();
     } catch (Exception $e) {
         $this->clearConnection();
         throw new AMQPServerException($e->getMessage(), $e->getCode(), $e);
     }
     $this->checkCommandResult(is_int($result), "Could not declare queue");
     return $result;
 }
Exemplo n.º 3
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);
 }
 /**
  * @param AMQPChannelInterface $channel
  * @param string $label
  * @return int
  */
 protected function queueDeclare(AMQPChannelInterface $channel, $label)
 {
     $this->assertTrue(isset(self::$queueList[$label]));
     return $channel->queueDeclare(self::$queueList[$label]['name'], AMQPQueueConfig::create()->setDurable(true)->setArguments(self::$queueList[$label]['args']));
 }