eMAGTechLabs /
RabbitMqBundle
| 1 | <?php |
||
| 2 | |||
| 3 | namespace OldSound\RabbitMqBundle\RabbitMq; |
||
| 4 | |||
| 5 | use PhpAmqpLib\Message\AMQPMessage; |
||
| 6 | |||
| 7 | class RpcServer extends BaseConsumer |
||
| 8 | { |
||
| 9 | private $serializer = 'serialize'; |
||
| 10 | |||
| 11 | public function initServer($name) |
||
| 12 | { |
||
| 13 | $this->setExchangeOptions(array('name' => $name, 'type' => 'direct')); |
||
| 14 | $this->setQueueOptions(array('name' => $name . '-queue')); |
||
| 15 | } |
||
| 16 | |||
| 17 | 1 | public function processMessage(AMQPMessage $msg) |
|
| 18 | { |
||
| 19 | try { |
||
| 20 | 1 | $msg->delivery_info['channel']->basic_ack($msg->delivery_info['delivery_tag']); |
|
|
0 ignored issues
–
show
Deprecated Code
introduced
by
Loading history...
|
|||
| 21 | 1 | $result = call_user_func($this->callback, $msg); |
|
| 22 | 1 | $result = call_user_func($this->serializer, $result); |
|
| 23 | 1 | $this->sendReply($result, $msg->get('reply_to'), $msg->get('correlation_id')); |
|
| 24 | 1 | $this->consumed++; |
|
| 25 | 1 | $this->maybeStopConsumer(); |
|
| 26 | } catch (\Exception $e) { |
||
| 27 | $this->sendReply('error: ' . $e->getMessage(), $msg->get('reply_to'), $msg->get('correlation_id')); |
||
| 28 | } |
||
| 29 | 1 | } |
|
| 30 | |||
| 31 | protected function sendReply($result, $client, $correlationId) |
||
| 32 | { |
||
| 33 | $reply = new AMQPMessage($result, array('content_type' => 'text/plain', 'correlation_id' => $correlationId)); |
||
| 34 | $this->getChannel()->basic_publish($reply, '', $client); |
||
| 35 | } |
||
| 36 | |||
| 37 | 1 | public function setSerializer($serializer) |
|
| 38 | { |
||
| 39 | 1 | $this->serializer = $serializer; |
|
| 40 | 1 | } |
|
| 41 | } |
||
| 42 |