5
5
use Interop \Amqp \AmqpConsumer as InteropAmqpConsumer ;
6
6
use Interop \Amqp \AmqpMessage as InteropAmqpMessage ;
7
7
use Interop \Amqp \AmqpQueue as InteropAmqpQueue ;
8
- use Interop \Amqp \Impl \AmqpMessage ;
9
- use Interop \Queue \Exception ;
10
8
use Interop \Queue \InvalidMessageException ;
11
9
use Interop \Queue \PsrMessage ;
12
10
use PhpAmqpLib \Channel \AMQPChannel ;
13
- use PhpAmqpLib \Exception \AMQPTimeoutException ;
14
- use PhpAmqpLib \Message \AMQPMessage as LibAMQPMessage ;
15
- use PhpAmqpLib \Wire \AMQPTable ;
16
11
17
12
class AmqpConsumer implements InteropAmqpConsumer
18
13
{
14
+ /**
15
+ * @var AmqpContext
16
+ */
17
+ private $ context ;
18
+
19
19
/**
20
20
* @var AMQPChannel
21
21
*/
@@ -31,11 +31,6 @@ class AmqpConsumer implements InteropAmqpConsumer
31
31
*/
32
32
private $ buffer ;
33
33
34
- /**
35
- * @var bool
36
- */
37
- private $ isInit ;
38
-
39
34
/**
40
35
* @var string
41
36
*/
@@ -52,31 +47,26 @@ class AmqpConsumer implements InteropAmqpConsumer
52
47
private $ consumerTag ;
53
48
54
49
/**
55
- * @param AMQPChannel $channel
50
+ * @param AmqpContext $context
56
51
* @param InteropAmqpQueue $queue
57
52
* @param Buffer $buffer
58
53
* @param string $receiveMethod
59
54
*/
60
- public function __construct (AMQPChannel $ channel , InteropAmqpQueue $ queue , Buffer $ buffer , $ receiveMethod )
55
+ public function __construct (AmqpContext $ context , InteropAmqpQueue $ queue , Buffer $ buffer , $ receiveMethod )
61
56
{
62
- $ this ->channel = $ channel ;
57
+ $ this ->context = $ context ;
58
+ $ this ->channel = $ context ->getLibChannel ();
63
59
$ this ->queue = $ queue ;
64
60
$ this ->buffer = $ buffer ;
65
61
$ this ->receiveMethod = $ receiveMethod ;
66
62
$ this ->flags = self ::FLAG_NOPARAM ;
67
-
68
- $ this ->isInit = false ;
69
63
}
70
64
71
65
/**
72
66
* {@inheritdoc}
73
67
*/
74
68
public function setConsumerTag ($ consumerTag )
75
69
{
76
- if ($ this ->isInit ) {
77
- throw new Exception ('Consumer tag is not mutable after it has been subscribed to broker ' );
78
- }
79
-
80
70
$ this ->consumerTag = $ consumerTag ;
81
71
}
82
72
@@ -152,7 +142,7 @@ public function receive($timeout = 0)
152
142
public function receiveNoWait ()
153
143
{
154
144
if ($ message = $ this ->channel ->basic_get ($ this ->queue ->getQueueName (), (bool ) ($ this ->getFlags () & InteropAmqpConsumer::FLAG_NOACK ))) {
155
- return $ this ->convertMessage ($ message );
145
+ return $ this ->context -> convertMessage ($ message );
156
146
}
157
147
}
158
148
@@ -177,30 +167,6 @@ public function reject(PsrMessage $message, $requeue = false)
177
167
$ this ->channel ->basic_reject ($ message ->getDeliveryTag (), $ requeue );
178
168
}
179
169
180
- /**
181
- * @param LibAMQPMessage $amqpMessage
182
- *
183
- * @return InteropAmqpMessage
184
- */
185
- private function convertMessage (LibAMQPMessage $ amqpMessage )
186
- {
187
- $ headers = new AMQPTable ($ amqpMessage ->get_properties ());
188
- $ headers = $ headers ->getNativeData ();
189
-
190
- $ properties = [];
191
- if (isset ($ headers ['application_headers ' ])) {
192
- $ properties = $ headers ['application_headers ' ];
193
- }
194
- unset($ headers ['application_headers ' ]);
195
-
196
- $ message = new AmqpMessage ($ amqpMessage ->getBody (), $ properties , $ headers );
197
- $ message ->setDeliveryTag ($ amqpMessage ->delivery_info ['delivery_tag ' ]);
198
- $ message ->setRedelivered ($ amqpMessage ->delivery_info ['redelivered ' ]);
199
- $ message ->setRoutingKey ($ amqpMessage ->delivery_info ['routing_key ' ]);
200
-
201
- return $ message ;
202
- }
203
-
204
170
/**
205
171
* @param int $timeout
206
172
*
@@ -226,63 +192,41 @@ private function receiveBasicGet($timeout)
226
192
*/
227
193
private function receiveBasicConsume ($ timeout )
228
194
{
229
- if (false === $ this ->isInit ) {
230
- $ callback = function (LibAMQPMessage $ message ) {
231
- $ receivedMessage = $ this ->convertMessage ($ message );
232
- $ receivedMessage ->setConsumerTag ($ message ->delivery_info ['consumer_tag ' ]);
233
-
234
- $ this ->buffer ->push ($ receivedMessage ->getConsumerTag (), $ receivedMessage );
235
- };
236
-
237
- $ consumerTag = $ this ->channel ->basic_consume (
238
- $ this ->queue ->getQueueName (),
239
- $ this ->getConsumerTag () ?: $ this ->getQueue ()->getConsumerTag (),
240
- (bool ) ($ this ->getFlags () & InteropAmqpConsumer::FLAG_NOLOCAL ),
241
- (bool ) ($ this ->getFlags () & InteropAmqpConsumer::FLAG_NOACK ),
242
- (bool ) ($ this ->getFlags () & InteropAmqpConsumer::FLAG_EXCLUSIVE ),
243
- (bool ) ($ this ->getFlags () & InteropAmqpConsumer::FLAG_NOWAIT ),
244
- $ callback
245
- );
246
-
247
- $ this ->consumerTag = $ consumerTag ?: $ this ->getQueue ()->getConsumerTag ();
248
-
249
- if (empty ($ this ->consumerTag )) {
250
- throw new Exception ('Got empty consumer tag ' );
251
- }
195
+ if (false == $ this ->consumerTag ) {
196
+ $ this ->context ->subscribe ($ this , function (InteropAmqpMessage $ message ) {
197
+ $ this ->buffer ->push ($ message ->getConsumerTag (), $ message );
252
198
253
- $ this ->isInit = true ;
199
+ return false ;
200
+ });
254
201
}
255
202
256
203
if ($ message = $ this ->buffer ->pop ($ this ->consumerTag )) {
257
204
return $ message ;
258
205
}
259
206
260
- try {
261
- while (true ) {
262
- $ start = microtime (true );
207
+ while (true ) {
208
+ $ start = microtime (true );
263
209
264
- $ this ->channel -> wait ( null , false , $ timeout / 1000 );
210
+ $ this ->context -> consume ( $ timeout );
265
211
266
- if ($ message = $ this ->buffer ->pop ($ this ->consumerTag )) {
267
- return $ message ;
268
- }
212
+ if ($ message = $ this ->buffer ->pop ($ this ->consumerTag )) {
213
+ return $ message ;
214
+ }
269
215
270
- // is here when consumed message is not for this consumer
216
+ // is here when consumed message is not for this consumer
271
217
272
- // as timeout is infinite have to continue consumption, but it can overflow message buffer
273
- if ($ timeout <= 0 ) {
274
- continue ;
275
- }
218
+ // as timeout is infinite have to continue consumption, but it can overflow message buffer
219
+ if ($ timeout <= 0 ) {
220
+ continue ;
221
+ }
276
222
277
- // compute remaining timeout and continue until time is up
278
- $ stop = microtime (true );
279
- $ timeout -= ($ stop - $ start ) * 1000 ;
223
+ // compute remaining timeout and continue until time is up
224
+ $ stop = microtime (true );
225
+ $ timeout -= ($ stop - $ start ) * 1000 ;
280
226
281
- if ($ timeout <= 0 ) {
282
- break ;
283
- }
227
+ if ($ timeout <= 0 ) {
228
+ break ;
284
229
}
285
- } catch (AMQPTimeoutException $ e ) {
286
230
}
287
231
}
288
232
}
0 commit comments