Example #1
0
 public function execute()
 {
     /**
      * @var \ReflectionMethod $method
      * @var Annotation\Consumer $annotation
      */
     foreach ($this->taskClasses as $consumers) {
         foreach ($consumers as list($taskClass, $method, $annotation)) {
             $connectionStorage = $this->connectionFactory->getConnectionStorage($annotation->connectionName);
             $connectionStorage->getQueue($annotation->queueName);
             $connectionStorage->getExchange($annotation->exchangeName);
         }
     }
     $queues = $this->connectionFactory->getAllQueues();
     /** @var \AMQPQueue $mainQueue */
     $mainQueue = array_pop($queues);
     foreach ($queues as $queue) {
         $queue->consume();
     }
     $callback = function (\AMQPEnvelope $msg) use($mainQueue) {
         $deliveryTag = $msg->getDeliveryTag();
         $connection = $mainQueue->getConnection();
         $connectionStorage = $this->connectionFactory->getConnectionStorageByConnection($connection);
         $connectionName = $connectionStorage->getConnectionName();
         $channel = $mainQueue->getChannel();
         $channelName = $connectionStorage->getChannelName($channel);
         $exchangeName = $msg->getExchangeName();
         $routingKey = $msg->getRoutingKey();
         $consumers = [];
         $key = $connectionName . '_' . $channelName . '_' . $exchangeName . '_' . $routingKey;
         if (isset($this->taskClasses[$key])) {
             $consumers = array_merge($consumers, $this->taskClasses[$key]);
         }
         $keyNull = $connectionName . '_' . $channelName . '_' . $exchangeName . '_';
         if ($keyNull !== $key && isset($this->taskClasses[$keyNull])) {
             $consumers = array_merge($consumers, $this->taskClasses[$keyNull]);
         }
         if (empty($consumers)) {
             $exceptionMessage = 'Consumer not found: ';
             $exceptionMessage .= implode(', ', ['connection=' . $connectionName, 'channel=' . $channelName, 'exchange=' . $exchangeName, 'routingKey=' . $routingKey]);
             throw new \LogicException($exceptionMessage);
         }
         $isAsk = true;
         $isContinue = true;
         foreach ($consumers as list($taskClass, $method)) {
             /** @var \Closure $method */
             /** @var MessageInterface $taskClass */
             try {
                 if ($taskClass !== false) {
                     $task = $taskClass::createFromString($msg->getBody());
                     $method($task);
                 } else {
                     $method();
                 }
             } catch (RejectMessageException $e) {
                 $mainQueue->nack($deliveryTag);
                 $isAsk = false;
                 break;
             } catch (ExitConsumerWorkerException $e) {
                 $isContinue = false;
             }
         }
         if ($isAsk) {
             $mainQueue->ack($deliveryTag);
         }
         return $isContinue;
     };
     $mainQueue->consume($callback);
 }
Example #2
0
 public function publish(MessageInterface $message)
 {
     $connectionStorage = $this->connectionFactory->getConnectionStorage($this->connectionName);
     $exchange = $connectionStorage->getExchange($message->getExchangeName(), $this->channelName);
     $exchange->publish($message->toString(), $message->getRoutingKey());
 }