/** * @param string $queue * @return BackendInterface */ public function getBackend($queue) { $type = null; if ($queue != $this->defaultQueue) { if (!$this->backend instanceof QueueDispatcherInterface) { throw new \RuntimeException(sprintf('Unable to provide sonata backend for queue %s', $queue)); } if ($this->backend instanceof MessageManagerBackendDispatcher) { $queueConfig = $this->getQueueConfig($this->backend, $queue); if (!isset($queueConfig['types']) || !is_array($queueConfig['types']) || empty($queueConfig['types'])) { throw new \RuntimeException('Invalid sonata queue configuration'); } $type = $queueConfig['types'][0]; } elseif ($this->backend instanceof AMQPBackendDispatcher) { $queueConfig = $this->getQueueConfig($this->backend, $queue); if (!isset($queueConfig['routing_key']) || empty($queueConfig['routing_key'])) { throw new \RuntimeException('Invalid sonata queue configuration'); } $type = $queueConfig['routing_key']; } else { throw new \RuntimeException('Unknown backend ' . get_class($this->backend)); } } if ($this->backend instanceof QueueDispatcherInterface) { return $this->backend->getBackend($type); } return $this->backend; }
/** * Publishes a message to the backend. * * @param Message $message * @return void * @throws \RuntimeException If publishing fails */ public function produce(Message $message) { $type = $message->getType(); $body = array('ticket' => $message->getTicket()); try { $this->logger->debug(sprintf('Publish message for job %s to sonata backend', $message->getTicket()), ['type' => $type, 'body' => $body]); $queue = $this->registry->get($message->getType())->getQueue(); $this->backendProvider->getBackend($queue)->createAndPublish($type, $body); } catch (\Exception $e) { $this->logger->error(sprintf('Failed to publish message (Error: %s)', $e->getMessage()), ['exception' => $e]); if (!$e instanceof \RuntimeException) { $e = new \RuntimeException($e->getMessage(), $e->getCode(), $e); } throw $e; } }