Example #1
0
 /**
  * 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;
     }
 }
Example #2
0
 /**
  * @param array $bodies
  * @param string $routingKey
  * @param array|\Traversable|MessageAttributes|null $attributes
  * @triggers publishBatch
  */
 public function publishBatch(array $bodies, $routingKey = '', $attributes = null)
 {
     $params = compact('bodies', 'routingKey', 'attributes');
     $results = $this->getEventManager()->trigger(__FUNCTION__, $this, $params);
     $result = $results->last();
     if (is_array($result)) {
         $bodies = $result['bodies'];
         $routingKey = $result['routingKey'];
         $attributes = $result['attributes'];
     }
     if (!$attributes instanceof MessageAttributes) {
         $attributes = new MessageAttributes($attributes);
     }
     $flags = $attributes->getFlags();
     $attributes = $attributes->toArray();
     foreach ($bodies as $body) {
         $this->exchange->publish($body, $routingKey, $flags, $attributes);
     }
 }
Example #3
0
 /**
  * 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());
 }