forked from php-amqplib/RabbitMqBundle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRpcServer.php
37 lines (32 loc) · 1.18 KB
/
RpcServer.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
<?php
namespace OldSound\RabbitMqBundle\RabbitMq;
use OldSound\RabbitMqBundle\RabbitMq\BaseAmqp;
use PhpAmqpLib\Message\AMQPMessage;
class RpcServer extends BaseConsumer
{
public function initServer($name)
{
$this->setExchangeOptions(array('name' => $name, 'type' => 'direct'));
$this->setQueueOptions(array('name' => $name . '-queue'));
}
public function processMessage(AMQPMessage $msg)
{
try
{
$msg->delivery_info['channel']->basic_ack($msg->delivery_info['delivery_tag']);
$result = call_user_func($this->callback, $msg);
$this->sendReply(serialize($result), $msg->get('reply_to'), $msg->get('correlation_id'));
$this->consumed++;
$this->maybeStopConsumer();
}
catch (\Exception $e)
{
$this->sendReply('error: ' . $e->getMessage(), $msg->get('reply_to'), $msg->get('correlation_id'));
}
}
protected function sendReply($result, $client, $correlationId)
{
$reply = new AMQPMessage($result, array('content_type' => 'text/plain', 'correlation_id' => $correlationId));
$this->ch->basic_publish($reply, '', $client);
}
}