14
14
use Interop \Queue \PsrMessage ;
15
15
use Interop \Queue \PsrProcessor ;
16
16
use Interop \Queue \PsrQueue ;
17
+ use Interop \Queue \PsrSubscriptionConsumerAwareContext ;
17
18
use Psr \Log \LoggerInterface ;
18
19
use Psr \Log \NullLogger ;
19
20
@@ -58,6 +59,13 @@ class QueueConsumer
58
59
*/
59
60
private $ logger ;
60
61
62
+ /**
63
+ * @deprecated added as BC layer, will be a default behavior in 0.9 version.
64
+ *
65
+ * @var bool
66
+ */
67
+ private $ enableSubscriptionConsumer ;
68
+
61
69
/**
62
70
* @param PsrContext $psrContext
63
71
* @param ExtensionInterface|ChainExtension|null $extension
@@ -77,6 +85,8 @@ public function __construct(
77
85
78
86
$ this ->boundProcessors = [];
79
87
$ this ->logger = new NullLogger ();
88
+
89
+ $ this ->enableSubscriptionConsumer = false ;
80
90
}
81
91
82
92
/**
@@ -187,7 +197,45 @@ public function consume(ExtensionInterface $runtimeExtension = null)
187
197
188
198
$ this ->logger ->info ('Start consuming ' );
189
199
190
- if ($ this ->psrContext instanceof AmqpContext) {
200
+ $ subscriptionConsumer = null ;
201
+ if ($ this ->enableSubscriptionConsumer ) {
202
+ $ subscriptionConsumer = new FallbackSubscriptionConsumer ();
203
+ if ($ context instanceof PsrSubscriptionConsumerAwareContext) {
204
+ $ subscriptionConsumer = $ context ->createSubscriptionConsumer ();
205
+ }
206
+
207
+ $ callback = function (PsrMessage $ message , PsrConsumer $ consumer ) use (&$ context ) {
208
+ $ currentProcessor = null ;
209
+
210
+ /** @var PsrQueue $queue */
211
+ foreach ($ this ->boundProcessors as list ($ queue , $ processor )) {
212
+ if ($ queue ->getQueueName () === $ consumer ->getQueue ()->getQueueName ()) {
213
+ $ currentProcessor = $ processor ;
214
+ }
215
+ }
216
+
217
+ if (false == $ currentProcessor ) {
218
+ throw new \LogicException (sprintf ('The processor for the queue "%s" could not be found. ' , $ consumer ->getQueue ()->getQueueName ()));
219
+ }
220
+
221
+ $ context = new Context ($ this ->psrContext );
222
+ $ context ->setLogger ($ this ->logger );
223
+ $ context ->setPsrQueue ($ consumer ->getQueue ());
224
+ $ context ->setPsrConsumer ($ consumer );
225
+ $ context ->setPsrProcessor ($ currentProcessor );
226
+ $ context ->setPsrMessage ($ message );
227
+
228
+ $ this ->doConsume ($ this ->extension , $ context );
229
+
230
+ return true ;
231
+ };
232
+
233
+ foreach ($ consumers as $ consumer ) {
234
+ /* @var AmqpConsumer $consumer */
235
+
236
+ $ subscriptionConsumer ->subscribe ($ consumer , $ callback );
237
+ }
238
+ } elseif ($ this ->psrContext instanceof AmqpContext) {
191
239
$ callback = function (AmqpMessage $ message , AmqpConsumer $ consumer ) use (&$ context ) {
192
240
$ currentProcessor = null ;
193
241
@@ -223,7 +271,18 @@ public function consume(ExtensionInterface $runtimeExtension = null)
223
271
224
272
while (true ) {
225
273
try {
226
- if ($ this ->psrContext instanceof AmqpContext) {
274
+ if ($ this ->enableSubscriptionConsumer ) {
275
+ $ this ->extension ->onBeforeReceive ($ context );
276
+
277
+ if ($ context ->isExecutionInterrupted ()) {
278
+ throw new ConsumptionInterruptedException ();
279
+ }
280
+
281
+ $ subscriptionConsumer ->consume ($ this ->receiveTimeout );
282
+
283
+ usleep ($ this ->idleTimeout * 1000 );
284
+ $ this ->extension ->onIdle ($ context );
285
+ } elseif ($ this ->psrContext instanceof AmqpContext) {
227
286
$ this ->extension ->onBeforeReceive ($ context );
228
287
229
288
if ($ context ->isExecutionInterrupted ()) {
@@ -251,7 +310,13 @@ public function consume(ExtensionInterface $runtimeExtension = null)
251
310
} catch (ConsumptionInterruptedException $ e ) {
252
311
$ this ->logger ->info (sprintf ('Consuming interrupted ' ));
253
312
254
- if ($ this ->psrContext instanceof AmqpContext) {
313
+ if ($ this ->enableSubscriptionConsumer ) {
314
+ foreach ($ consumers as $ consumer ) {
315
+ /* @var PsrConsumer $consumer */
316
+
317
+ $ subscriptionConsumer ->unsubscribe ($ consumer );
318
+ }
319
+ } elseif ($ this ->psrContext instanceof AmqpContext) {
255
320
foreach ($ consumers as $ consumer ) {
256
321
/* @var AmqpConsumer $consumer */
257
322
@@ -279,6 +344,14 @@ public function consume(ExtensionInterface $runtimeExtension = null)
279
344
}
280
345
}
281
346
347
+ /**
348
+ * @param bool $enableSubscriptionConsumer
349
+ */
350
+ public function enableSubscriptionConsumer (bool $ enableSubscriptionConsumer )
351
+ {
352
+ $ this ->enableSubscriptionConsumer = $ enableSubscriptionConsumer ;
353
+ }
354
+
282
355
/**
283
356
* @param ExtensionInterface $extension
284
357
* @param Context $context
0 commit comments