/** * @param string $body * @param string $routingKey * @param array|\Traversable|MessageAttributes|null $attributes * @triggers publish */ public function publish($body, $routingKey = '', $attributes = null) { $params = compact('body', 'routingKey', 'attributes'); $results = $this->getEventManager()->trigger(__FUNCTION__, $this, $params); $result = $results->last(); if (is_array($result)) { $body = $result['body']; $routingKey = $result['routingKey']; $attributes = $result['attributes']; } if (!$attributes instanceof MessageAttributes) { $attributes = new MessageAttributes($attributes); } $this->exchange->publish($body, $routingKey, $attributes->getFlags(), $attributes->toArray()); }
/** * Send reply to rpc client * * @param string $body * @param string $client * @param string $correlationId */ protected function sendReply($body, $client, $correlationId) { $messageAttributes = new MessageAttributes(); $messageAttributes->setCorrelationId($correlationId); $this->getExchange()->publish($body, $client, AMQP_NOPARAM, $messageAttributes->toArray()); }
/** * Add a request to rpc client * * @param string $msgBody * @param string $server * @param string $requestId * @param string $routingKey * @param int $expiration * @throws Exception\InvalidArgumentException */ public function addRequest($msgBody, $server, $requestId, $routingKey = '', $expiration = 0) { if (empty($requestId)) { throw new Exception\InvalidArgumentException('You must provide a request Id'); } $params = compact('msgBody', 'server', 'requestId', 'routingKey', 'expiration'); $results = $this->getEventManager()->trigger(__FUNCTION__, $this, $params); $result = $results->last(); if (is_array($result)) { $msgBody = $result['msgBody']; $server = $result['server']; $requestId = $result['requestId']; $routingKey = $result['routingKey']; $expiration = $result['expiration']; } $messageAttributes = new MessageAttributes(); $messageAttributes->setReplyTo($this->queue->getName()); $messageAttributes->setDeliveryMode(MessageAttributes::DELIVERY_MODE_NON_PERSISTENT); $messageAttributes->setCorrelationId($requestId); if (0 != $expiration) { $messageAttributes->setExpiration($expiration * 1000); } $exchange = $this->getExchange($server); $exchange->publish($msgBody, $routingKey, $messageAttributes->getFlags(), $messageAttributes->toArray()); $this->requests++; if ($expiration > $this->timeout) { $this->timeout = $expiration; } }