Skip to content

Commit 30fd704

Browse files
committed
[consumption] queue consumer should use amqp basic consume if amqp context is used.
1 parent fcee9a0 commit 30fd704

1 file changed

Lines changed: 52 additions & 12 deletions

File tree

‎pkg/enqueue/Consumption/QueueConsumer.php‎

Lines changed: 52 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,13 @@
22

33
namespace Enqueue\Consumption;
44

5+
use Enqueue\AmqpExt\AmqpConsumer;
6+
use Enqueue\AmqpExt\AmqpContext;
57
use Enqueue\Consumption\Exception\ConsumptionInterruptedException;
68
use Enqueue\Consumption\Exception\InvalidArgumentException;
79
use Enqueue\Consumption\Exception\LogicException;
810
use Enqueue\Util\VarExport;
11+
use Interop\Amqp\AmqpMessage;
912
use Interop\Queue\PsrConsumer;
1013
use Interop\Queue\PsrContext;
1114
use Interop\Queue\PsrProcessor;
@@ -150,6 +153,12 @@ public function consume(ExtensionInterface $runtimeExtension = null)
150153
$consumers[$queue->getQueueName()] = $this->psrContext->createConsumer($queue);
151154
}
152155

156+
foreach ($consumers as $consumer) {
157+
if ($consumer instanceof AmqpConsumer) {
158+
$consumer->basicConsume($this->receiveTimeout, null);
159+
}
160+
}
161+
153162
$extension = $this->extension ?: new ChainExtension([]);
154163
if ($runtimeExtension) {
155164
$extension = new ChainExtension([$extension, $runtimeExtension]);
@@ -163,17 +172,48 @@ public function consume(ExtensionInterface $runtimeExtension = null)
163172

164173
while (true) {
165174
try {
166-
/** @var PsrQueue $queue */
167-
foreach ($this->boundProcessors as list($queue, $processor)) {
168-
$consumer = $consumers[$queue->getQueueName()];
169-
170-
$context = new Context($this->psrContext);
171-
$context->setLogger($logger);
172-
$context->setPsrQueue($queue);
173-
$context->setPsrConsumer($consumer);
174-
$context->setPsrProcessor($processor);
175-
176-
$this->doConsume($extension, $context);
175+
if ($this->psrContext instanceof AmqpContext) {
176+
reset($consumers);
177+
/** @var AmqpConsumer $consumer */
178+
$consumer = current($consumers);
179+
$consumer->basicConsume($this->receiveTimeout, function (AmqpMessage $message, AmqpConsumer $consumer) use ($extension, $logger) {
180+
$currentProcessor = null;
181+
182+
/** @var PsrQueue $queue */
183+
foreach ($this->boundProcessors as list($queue, $processor)) {
184+
if ($queue->getQueueName() === $consumer->getQueue()->getQueueName()) {
185+
$currentProcessor = $processor;
186+
}
187+
}
188+
189+
if (false == $currentProcessor) {
190+
throw new \LogicException(sprintf('The processor for the queue "%s" could not be found.', $consumer->getQueue()->getQueueName()));
191+
}
192+
193+
$context = new Context($this->psrContext);
194+
$context->setLogger($logger);
195+
$context->setPsrQueue($consumer->getQueue());
196+
$context->setPsrConsumer($consumer);
197+
$context->setPsrProcessor($currentProcessor);
198+
$context->setPsrMessage($message);
199+
200+
$this->doConsume($extension, $context);
201+
202+
return true;
203+
});
204+
} else {
205+
/** @var PsrQueue $queue */
206+
foreach ($this->boundProcessors as list($queue, $processor)) {
207+
$consumer = $consumers[$queue->getQueueName()];
208+
209+
$context = new Context($this->psrContext);
210+
$context->setLogger($logger);
211+
$context->setPsrQueue($queue);
212+
$context->setPsrConsumer($consumer);
213+
$context->setPsrProcessor($processor);
214+
215+
$this->doConsume($extension, $context);
216+
}
177217
}
178218
} catch (ConsumptionInterruptedException $e) {
179219
$logger->info(sprintf('Consuming interrupted'));
@@ -218,7 +258,7 @@ protected function doConsume(ExtensionInterface $extension, Context $context)
218258
throw new ConsumptionInterruptedException();
219259
}
220260

221-
if ($message = $consumer->receive($this->receiveTimeout)) {
261+
if ($context->getPsrMessage() || $message = $consumer->receive($this->receiveTimeout)) {
222262
$logger->info('Message received from the queue: '.$context->getPsrQueue()->getQueueName());
223263
$logger->debug('Headers: {headers}', ['headers' => new VarExport($message->getHeaders())]);
224264
$logger->debug('Properties: {properties}', ['properties' => new VarExport($message->getProperties())]);

0 commit comments

Comments
 (0)