forked from rabbitmq/rabbitmq-tutorials
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rpc_client.php
57 lines (45 loc) · 1.45 KB
/
rpc_client.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
<?php
// composer require enqueue/amqp-bunny
require_once __DIR__.'/vendor/autoload.php';
use Enqueue\AmqpBunny\AmqpConnectionFactory;
$config = [
'host' => 'localhost',
'port' => 5672,
'user' => 'guest',
'pass' => 'guest',
'receive_method' => 'basic_consume',
];
class FibonacciRpcClient
{
/** @var \Interop\Amqp\AmqpContext */
private $context;
/** @var \Interop\Amqp\AmqpQueue */
private $callback_queue;
public function __construct(array $config)
{
$this->context = (new AmqpConnectionFactory($config))->createContext();
$this->callback_queue = $this->context->createTemporaryQueue();
}
public function call($n)
{
$corr_id = uniqid();
$message = $this->context->createMessage((string) $n);
$message->setCorrelationId($corr_id);
$message->setReplyTo($this->callback_queue->getQueueName());
$this->context->createProducer()->send(
$this->context->createQueue('rpc_queue'),
$message
);
$consumer = $this->context->createConsumer($this->callback_queue);
while (true) {
if ($message = $consumer->receive()) {
if ($message->getCorrelationId() == $corr_id) {
return (int) ($message->getBody());
}
}
}
}
}
$fibonacci_rpc = new FibonacciRpcClient($config);
$response = $fibonacci_rpc->call(30);
echo ' [.] Got ', $response, "\n";