Skip to content

Commit 011a565

Browse files
committed
Set rpc client connexion to be lazy
1 parent 15d7d69 commit 011a565

File tree

1 file changed

+14
-5
lines changed

1 file changed

+14
-5
lines changed

RabbitMq/RpcClient.php

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,13 @@ class RpcClient extends BaseAmqp
99
{
1010
protected $requests = 0;
1111
protected $replies = array();
12-
protected $queueName;
1312
protected $expectSerializedResponse;
1413
protected $timeout = 0;
1514

15+
private $queueName;
16+
1617
public function initClient($expectSerializedResponse = true)
1718
{
18-
list($this->queueName, ,) = $this->getChannel()->queue_declare("", false, false, true, true);
1919
$this->expectSerializedResponse = $expectSerializedResponse;
2020
}
2121

@@ -26,7 +26,7 @@ public function addRequest($msgBody, $server, $requestId = null, $routingKey = '
2626
}
2727

2828
$msg = new AMQPMessage($msgBody, array('content_type' => 'text/plain',
29-
'reply_to' => $this->queueName,
29+
'reply_to' => $this->getQueueName(),
3030
'delivery_mode' => 1, // non durable
3131
'expiration' => $expiration*1000,
3232
'correlation_id' => $requestId));
@@ -43,13 +43,13 @@ public function addRequest($msgBody, $server, $requestId = null, $routingKey = '
4343
public function getReplies()
4444
{
4545
$this->replies = array();
46-
$this->getChannel()->basic_consume($this->queueName, '', false, true, false, false, array($this, 'processMessage'));
46+
$this->getChannel()->basic_consume($this->getQueueName(), '', false, true, false, false, array($this, 'processMessage'));
4747

4848
while (count($this->replies) < $this->requests) {
4949
$this->getChannel()->wait(null, false, $this->timeout);
5050
}
5151

52-
$this->getChannel()->basic_cancel($this->queueName);
52+
$this->getChannel()->basic_cancel($this->getQueueName());
5353
$this->requests = 0;
5454
$this->timeout = 0;
5555

@@ -65,4 +65,13 @@ public function processMessage(AMQPMessage $msg)
6565

6666
$this->replies[$msg->get('correlation_id')] = $messageBody;
6767
}
68+
69+
protected function getQueueName()
70+
{
71+
if (null === $this->queueName) {
72+
list($this->queueName, ,) = $this->getChannel()->queue_declare("", false, false, true, true);
73+
}
74+
75+
return $this->queueName;
76+
}
6877
}

0 commit comments

Comments
 (0)