Пример #1
0
 /**
  * @inheritdoc
  */
 public function consume(callable $callback = null, int $flags = Constants::AMQP_NOPARAM, string $consumerTag = '')
 {
     if (null !== $callback) {
         $innerCallback = function (\AMQPEnvelope $envelope, \AMQPQueue $queue) use($callback) {
             $envelope = new Envelope($envelope);
             return $callback($envelope, $this);
         };
     } else {
         $innerCallback = null;
     }
     try {
         $this->queue->consume($innerCallback, $flags, $consumerTag);
     } catch (\AMQPConnectionException $e) {
         throw QueueException::fromAmqpExtension($e);
     } catch (\AMQPChannelException $e) {
         throw QueueException::fromAmqpExtension($e);
     } catch (\AMQPQueueException $e) {
         throw QueueException::fromAmqpExtension($e);
     }
 }
Пример #2
0
 /**
  * @inheritdoc
  */
 public function consume(callable $callback = null, int $flags = Constants::AMQP_NOPARAM, string $consumerTag = null)
 {
     if (null !== $callback) {
         $innerCallback = function (AMQPMessage $envelope) use($callback) {
             $result = $callback(new Envelope($envelope), $this);
             if (false === $result) {
                 $this->cancel($envelope->delivery_info['consumer_tag']);
             }
             return $result;
         };
     } else {
         $innerCallback = null;
     }
     if (null === $consumerTag) {
         $consumerTag = bin2hex(random_bytes(24));
     }
     $args = [];
     // see: https://github.com/php-amqplib/php-amqplib/issues/405
     $supportedDataTypes = AMQPAbstractCollection::getSupportedDataTypes();
     foreach ($this->arguments as $k => $v) {
         if (is_array($v)) {
             if (empty($v) || array_keys($v) === range(0, count($v) - 1)) {
                 $args[$k] = [$supportedDataTypes[AMQPAbstractCollection::T_ARRAY], new AMQPArray($v)];
             } else {
                 $args[$k] = [$supportedDataTypes[AMQPAbstractCollection::T_TABLE], new AMQPTable($v)];
             }
         } elseif (is_int($v)) {
             $args[$k] = [$supportedDataTypes[AMQPAbstractCollection::T_INT_LONG], $v];
         } elseif (is_bool($v)) {
             $args[$k] = [$supportedDataTypes[AMQPAbstractCollection::T_BOOL], $v];
         } elseif (is_string($v)) {
             $args[$k] = [$supportedDataTypes[AMQPAbstractCollection::T_STRING_LONG], $v];
         } else {
             throw new Exception\InvalidArgumentException('Unknown argument type ' . gettype($v));
         }
     }
     try {
         $this->channel->getResource()->basic_consume($this->name, $consumerTag, (bool) ($flags & Constants::AMQP_NOLOCAL), (bool) ($flags & Constants::AMQP_AUTOACK), (bool) ($flags & Constants::AMQP_EXCLUSIVE), (bool) ($flags & Constants::AMQP_NOWAIT), $innerCallback, null, $args);
         if (isset($this->channel->getResource()->callbacks[$consumerTag])) {
             while (count($this->channel->getResource()->callbacks)) {
                 $this->channel->getResource()->wait();
             }
         }
     } catch (\Throwable $e) {
         throw Exception\QueueException::fromPhpAmqpLib($e);
     }
 }