/** * Declare this queue. * * @recoil-coroutine * * @param bool $passive True to use a "passive" declare mode. * @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 DeclareException The queue already exists with different options. * @throws ConnectionException The connection is closed. * @throws ChannelException The channel is closed. */ private function declareQueue(bool $passive, Channel $channel = null) : Generator { $channelManager = (yield $this->connectionManager->acquireChannel($channel)); try { $frame = (yield $channelManager->call(QueueDeclareFrame::create($this->name, $passive, $this->options->durable, $this->options->exclusive, $this->options->autoDelete, false, $this->arguments), QueueDeclareOkFrame::METHOD_ID)); } catch (AmqpException $e) { if ($e->getCode() === Constants::RESOURCE_LOCKED) { throw ResourceLockedException::queueIsExclusive($this->name, $e); } throw $e; } finally { if ($channel === null) { $channelManager->release(); } } $this->name = $frame->queue; return [$frame->messageCount, $frame->consumerCount]; }
/** * 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; } }