Beispiel #1
0
 /**
  * Declare this exchange.
  *
  * @recoil-coroutine
  *
  * @param boolean      $passive True to use a "passive" declare mode.
  * @param Channel|null $channel The application-managed channel to use (null = auto-managed).
  *
  * @throws ResourceNotFoundException The exchange does not exist.
  * @throws DeclareException          The exchange already exists with different a type or options.
  * @throws DeclareException          A reserved exchange name was used.
  * @throws ConnectionException       The connection is closed.
  * @throws ChannelException          The channel is closed.
  */
 private function declareExchange($passive, Channel $channel = null) : Generator
 {
     if ('amq.' === substr($this->name, 0, 4)) {
         return reject(DeclareException::exchangeNameIsReserved($this->name));
     }
     $deferred = new Deferred();
     $this->broker->acquireChannel(function ($exception, $brokerChannel) use($channel, $passive, $deferred) {
         if ($exception) {
             $deferred->reject($exception);
         } else {
             $brokerChannel->call(ExchangeDeclareFrame::create($this->name, $this->type->value(), $passive, $this->options->durable, $this->options->autoDelete, $this->options->internal, false, $this->arguments), ExchangeDeclareOkFrame::METHOD_ID, function ($exception, $frame) use($brokerChannel, $channel, $deferred) {
                 if (!$channel) {
                     $this->broker->releaseChannel($brokerChannel);
                 }
                 if ($exception) {
                     if ($exception instanceof AmqpException) {
                         if ($exception->getCode() === Constants::PRECONDITION_FAILED) {
                             $exception = DeclareException::exchangeTypeOrOptionMismatch($this->name, $this->type, $this->options, $exception);
                         } elseif ($exception->getCode() === Constants::NOT_FOUND) {
                             $exception = ResourceNotFoundException::exchangeNotFound($this->name, $exception);
                         }
                     }
                     $deferred->reject($exception);
                 } else {
                     $deferred->resolve($this);
                 }
             });
         }
     }, $channel);
     return $deferred->promise();
 }
Beispiel #2
0
 /**
  * Check that this queue exists and that the options match the broker.
  *
  * @recoil-coroutine
  *
  * @param Channel|null $channel The application-managed channel to use (null = auto-managed).
  *
  * @return tuple<int,int>            A 2-tuple containing the queue's current message and consumer count.
  * @throws ResourceNotFoundException The queue does not exist.
  * @throws ResourceLockedException   Another connection has exclusive access to the queue.
  * @throws ConnectionException       The connection is closed.
  * @throws ChannelException          The channel is closed.
  */
 public function check(Channel $channel = null) : Generator
 {
     try {
         return (yield $this->declareQueue(true, $channel));
     } catch (AmqpException $e) {
         if ($e->getCode() === Constants::NOT_FOUND) {
             throw ResourceNotFoundException::queueNotFound($this->name, $e);
         }
         throw $e;
     }
 }
Beispiel #3
0
 /**
  * Open the consumer.
  *
  * @recoil-coroutine
  *
  * While a consumer is open, it will consume messages from the queue.
  * Messages are read by using the consumer as a readable channel.
  *
  * The consumer may be reopened after it has been closed.
  *
  * @throws ResourceLockedException   Another connection has exclusive access to the queue.
  * @throws ResourceNotFoundException The queue does not exist.
  * @throws ConnectionException       The connection is closed.
  * @throws ChannelException          The channel is closed.
  */
 public function open() : Generator
 {
     assert($this->state === self::STATE_CLOSED);
     try {
         $this->state = self::STATE_OPENING;
         $this->channelManager = (yield $this->connectionManager->acquireChannel($this->channel));
         if ($this->preFetchCount !== null || $this->preFetchSize !== null) {
             $this->channelManager->setDirty();
             (yield $this->channelManager->call(BasicQosFrame::create($this->preFetchSize, $this->preFetchCount, false), BasicQosOkFrame::METHOD_ID));
         }
         try {
             $frame = (yield $this->channelManager->call(BasicConsumeFrame::create($this->queue->name(), $this->tag, $this->options->noLocal, $this->options->noAck, $this->options->exclusive), BasicConsumeOkFrame::METHOD_ID));
         } catch (AmqpException $e) {
             if ($e->getCode() === Constants::ACCESS_REFUSED) {
                 throw ResourceLockedException::queueHasExclusiveConsumer($this->queue->name(), $e);
             } elseif ($e->getCode() === Constants::NOT_FOUND) {
                 throw ResourceNotFoundException::queueNotFound($this->queue->name(), $e);
             } else {
                 throw $e;
             }
         }
         $this->tag = $frame->consumerTag;
         $this->state = self::STATE_OPEN;
         $this->channelManager->addDeliverListener($this->tag, $this);
     } catch (Throwable $e) {
         $this->cleanup();
         throw $e;
     }
 }