1414use Interop \Queue \PsrMessage ;
1515use Interop \Queue \PsrProcessor ;
1616use Interop \Queue \PsrQueue ;
17+ use Interop \Queue \PsrSubscriptionConsumerAwareContext ;
1718use Psr \Log \LoggerInterface ;
1819use Psr \Log \NullLogger ;
1920
@@ -58,6 +59,13 @@ class QueueConsumer
5859 */
5960 private $ logger ;
6061
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+
6169 /**
6270 * @param PsrContext $psrContext
6371 * @param ExtensionInterface|ChainExtension|null $extension
@@ -77,6 +85,8 @@ public function __construct(
7785
7886 $ this ->boundProcessors = [];
7987 $ this ->logger = new NullLogger ();
88+
89+ $ this ->enableSubscriptionConsumer = false ;
8090 }
8191
8292 /**
@@ -187,7 +197,45 @@ public function consume(ExtensionInterface $runtimeExtension = null)
187197
188198 $ this ->logger ->info ('Start consuming ' );
189199
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) {
191239 $ callback = function (AmqpMessage $ message , AmqpConsumer $ consumer ) use (&$ context ) {
192240 $ currentProcessor = null ;
193241
@@ -223,7 +271,18 @@ public function consume(ExtensionInterface $runtimeExtension = null)
223271
224272 while (true ) {
225273 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) {
227286 $ this ->extension ->onBeforeReceive ($ context );
228287
229288 if ($ context ->isExecutionInterrupted ()) {
@@ -251,7 +310,13 @@ public function consume(ExtensionInterface $runtimeExtension = null)
251310 } catch (ConsumptionInterruptedException $ e ) {
252311 $ this ->logger ->info (sprintf ('Consuming interrupted ' ));
253312
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) {
255320 foreach ($ consumers as $ consumer ) {
256321 /* @var AmqpConsumer $consumer */
257322
@@ -279,6 +344,14 @@ public function consume(ExtensionInterface $runtimeExtension = null)
279344 }
280345 }
281346
347+ /**
348+ * @param bool $enableSubscriptionConsumer
349+ */
350+ public function enableSubscriptionConsumer (bool $ enableSubscriptionConsumer )
351+ {
352+ $ this ->enableSubscriptionConsumer = $ enableSubscriptionConsumer ;
353+ }
354+
282355 /**
283356 * @param ExtensionInterface $extension
284357 * @param Context $context
0 commit comments