|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace OldSound\RabbitMqBundle\RPC; |
|
4
|
|
|
|
|
5
|
|
|
use OldSound\RabbitMqBundle\Declarations\BindingDeclaration; |
|
6
|
|
|
use OldSound\RabbitMqBundle\Declarations\Declarator; |
|
7
|
|
|
use OldSound\RabbitMqBundle\Declarations\ConsumeOptions; |
|
8
|
|
|
use OldSound\RabbitMqBundle\Declarations\QueueDeclaration; |
|
9
|
|
|
use OldSound\RabbitMqBundle\ExecuteCallbackStrategy\BatchExecuteCallbackStrategy; |
|
|
|
|
|
|
10
|
|
|
use OldSound\RabbitMqBundle\Producer\ProducerInterface; |
|
11
|
|
|
use OldSound\RabbitMqBundle\RabbitMq\Exception\RpcResponseException; |
|
|
|
|
|
|
12
|
|
|
use OldSound\RabbitMqBundle\Serializer\JsonMessageBodySerializer; |
|
13
|
|
|
use OldSound\RabbitMqBundle\Serializer\MessageBodySerializerInterface; |
|
14
|
|
|
use PhpAmqpLib\Channel\AMQPChannel; |
|
15
|
|
|
use PhpAmqpLib\Message\AMQPMessage; |
|
16
|
|
|
use Symfony\Component\Messenger\Transport\Serialization\SerializerInterface; |
|
|
|
|
|
|
17
|
|
|
use Symfony\Component\Serializer\Serializer; |
|
18
|
|
|
|
|
19
|
|
|
class RpcClient implements BatchReceiverInterface |
|
|
|
|
|
|
20
|
|
|
{ |
|
21
|
|
|
const PROPERTY_REPLAY_TO = 'reply_to'; |
|
22
|
|
|
const PROPERTY_CORRELATION_ID = 'correlation_id'; |
|
23
|
|
|
|
|
24
|
|
|
/** @var AMQPChannel */ |
|
25
|
|
|
private $channel; |
|
26
|
|
|
/** @var int */ |
|
27
|
|
|
private $expiration; |
|
28
|
|
|
|
|
29
|
|
|
/** @var QueueDeclaration */ |
|
30
|
|
|
private $anonRepliesQueue; |
|
31
|
|
|
/** @var MessageBodySerializerInterface[] */ |
|
32
|
|
|
private $serializers; |
|
33
|
|
|
|
|
34
|
|
|
/** @var int */ |
|
35
|
|
|
private $requests = 0; |
|
36
|
|
|
/** @var AMQPMessage[] */ |
|
37
|
|
|
private $messages; |
|
38
|
|
|
private $replies = []; |
|
|
|
|
|
|
39
|
|
|
|
|
40
|
|
|
public function __construct( |
|
41
|
|
|
AMQPChannel $channel, |
|
42
|
|
|
int $expiration = 10000 |
|
43
|
|
|
) { |
|
44
|
|
|
$this->channel = $channel; |
|
45
|
|
|
$this->serializer = $serializer ?? new JsonMessageBodySerializer(); |
|
|
|
|
|
|
46
|
|
|
$this->expiration = $expiration; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
public function declareRepliesQueue($repliesQueueName = null): RpcClient |
|
50
|
|
|
{ |
|
51
|
|
|
$this->anonRepliesQueue = $this->createAnonQueueDeclaration(); |
|
52
|
|
|
$this->anonRepliesQueue->name = $repliesQueueName; |
|
53
|
|
|
$declarator = new Declarator($this->channel); |
|
54
|
|
|
[$queueName] = $declarator->declareQueues([$this->anonRepliesQueue]); |
|
55
|
|
|
$this->anonRepliesQueue->name = $queueName; |
|
56
|
|
|
|
|
57
|
|
|
return $this; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
// TODO public move |
|
61
|
|
|
private function createAnonQueueDeclaration(): QueueDeclaration |
|
62
|
|
|
{ |
|
63
|
|
|
$anonQueueDeclaration = new QueueDeclaration(); |
|
64
|
|
|
$anonQueueDeclaration->passive = false; |
|
65
|
|
|
$anonQueueDeclaration->durable = false; |
|
66
|
|
|
$anonQueueDeclaration->exclusive = true; |
|
67
|
|
|
$anonQueueDeclaration->autoDelete = true; |
|
68
|
|
|
$anonQueueDeclaration->nowait = false; |
|
69
|
|
|
|
|
70
|
|
|
return $anonQueueDeclaration; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
public function addRequest($msgBody, $rpcQueue, MessageBodySerializerInterface $serializer = null) |
|
74
|
|
|
{ |
|
75
|
|
|
if (!$this->anonRepliesQueue) { |
|
76
|
|
|
throw new \LogicException('no init anonRepliesQueue'); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
$correlationId = $this->requests; |
|
80
|
|
|
$this->serializers[$correlationId] = $serializer; |
|
81
|
|
|
|
|
82
|
|
|
$serializer = $serializer ?? new JsonMessageBodySerializer(); |
|
83
|
|
|
|
|
84
|
|
|
$replyToQueue = $this->anonRepliesQueue->name; // 'amq.rabbitmq.reply-to'; |
|
85
|
|
|
$msg = new AMQPMessage($serializer->serialize($msgBody, 'json'), [ |
|
|
|
|
|
|
86
|
|
|
self::PROPERTY_REPLAY_TO => $replyToQueue, |
|
87
|
|
|
self::PROPERTY_CORRELATION_ID => $correlationId, |
|
88
|
|
|
'content_type' => 'text/plain', |
|
89
|
|
|
'delivery_mode' => ProducerInterface::DELIVERY_MODE_NON_PERSISTENT, |
|
90
|
|
|
'expiration' => $this->expiration, |
|
91
|
|
|
]); |
|
92
|
|
|
|
|
93
|
|
|
$this->channel->basic_publish($msg, '', $rpcQueue); |
|
94
|
|
|
$this->requests++; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
public function batchExecute(array $messages) |
|
98
|
|
|
{ |
|
99
|
|
|
if ($this->messages !== null) { |
|
100
|
|
|
throw new \LogicException('Rpc client consming should be called once by batch count limit'); |
|
101
|
|
|
} |
|
102
|
|
|
$this->messages = $messages; |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* @param $name |
|
107
|
|
|
* @param MessageBodySerializerInterface $serializer |
|
108
|
|
|
* @return array|AMQPMessage[] |
|
109
|
|
|
*/ |
|
110
|
|
|
public function getReplies($name): array |
|
111
|
|
|
{ |
|
112
|
|
|
if (0 === $this->requests) { |
|
113
|
|
|
throw new \LogicException('request empty'); |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
$consumer = new Consumer($this->channel); |
|
|
|
|
|
|
117
|
|
|
$consuming = new ConsumeOptions(); |
|
118
|
|
|
$consuming->exclusive = true; |
|
119
|
|
|
$consuming->qosPrefetchCount = $this->requests; |
|
120
|
|
|
$consuming->queue = $this->anonRepliesQueue->name; |
|
121
|
|
|
$consuming->receiver = $this; |
|
122
|
|
|
$consumer->consumeQueue($consuming, new BatchExecuteCallbackStrategy($this->requests)); |
|
123
|
|
|
|
|
124
|
|
|
try { |
|
125
|
|
|
$consumer->consume($this->requests); |
|
126
|
|
|
} finally { |
|
127
|
|
|
// TODO $this->getChannel()->basic_cancel($consumer_tag); |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
$replices = []; |
|
131
|
|
|
foreach($this->messages as $message) { |
|
132
|
|
|
/** @var AMQPMessage $message */ |
|
133
|
|
|
if (!$message->has('correlation_id')) { |
|
134
|
|
|
$this->logger->error('unexpected message. rpc replies have no correlation_id '); |
|
135
|
|
|
continue; |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
$correlationId = $message->get('correlation_id'); |
|
139
|
|
|
$serializer = $this->serializers[$correlationId]; |
|
140
|
|
|
$reply = $serializer ? $serializer->deserialize($message->body) : $message; |
|
141
|
|
|
$replices[$correlationId] = $reply; |
|
142
|
|
|
} |
|
143
|
|
|
ksort($replices); |
|
144
|
|
|
return $replices; |
|
145
|
|
|
} |
|
146
|
|
|
} |
|
147
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths