|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Symfony package. |
| 5 | + * |
| 6 | + * (c) Fabien Potencier <[email protected]> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +namespace Symfony\Component\Messenger\Bridge\Amqp\Middleware; |
| 13 | + |
| 14 | +use Symfony\Component\Messenger\Bridge\Amqp\Transport\AmqpReceivedStamp; |
| 15 | +use Symfony\Component\Messenger\Envelope; |
| 16 | +use Symfony\Component\Messenger\Exception\RejectRedeliveredMessageException; |
| 17 | +use Symfony\Component\Messenger\Middleware\MiddlewareInterface; |
| 18 | +use Symfony\Component\Messenger\Middleware\StackInterface; |
| 19 | +use Symfony\Component\Messenger\Transport\AmqpExt\AmqpReceivedStamp as LegacyAmqpReceivedStamp; |
| 20 | + |
| 21 | +/** |
| 22 | + * Middleware that throws a RejectRedeliveredMessageException when a message is detected that has been redelivered by AMQP. |
| 23 | + * |
| 24 | + * The middleware runs before the HandleMessageMiddleware and prevents redelivered messages from being handled directly. |
| 25 | + * The thrown exception is caught by the worker and will trigger the retry logic according to the retry strategy. |
| 26 | + * |
| 27 | + * AMQP redelivers messages when they do not get acknowledged or rejected. This can happen when the connection times out |
| 28 | + * or an exception is thrown before acknowledging or rejecting. When such errors happen again while handling the |
| 29 | + * redelivered message, the message would get redelivered again and again. The purpose of this middleware is to prevent |
| 30 | + * infinite redelivery loops and to unblock the queue by republishing the redelivered messages as retries with a retry |
| 31 | + * limit and potential delay. |
| 32 | + * |
| 33 | + * @author Tobias Schultze <http://tobion.de> |
| 34 | + */ |
| 35 | +class RejectRedeliveredMessageMiddleware implements MiddlewareInterface |
| 36 | +{ |
| 37 | + public function handle(Envelope $envelope, StackInterface $stack): Envelope |
| 38 | + { |
| 39 | + $amqpReceivedStamp = $envelope->last(AmqpReceivedStamp::class); |
| 40 | + if ($amqpReceivedStamp instanceof AmqpReceivedStamp && $amqpReceivedStamp->getAmqpEnvelope()->isRedelivery()) { |
| 41 | + throw new RejectRedeliveredMessageException('Redelivered message from AMQP detected that will be rejected and trigger the retry logic.'); |
| 42 | + } |
| 43 | + |
| 44 | + // Legacy code to support symfony/messenger < 5.1 |
| 45 | + $amqpReceivedStamp = $envelope->last(LegacyAmqpReceivedStamp::class); |
| 46 | + if ($amqpReceivedStamp instanceof LegacyAmqpReceivedStamp && $amqpReceivedStamp->getAmqpEnvelope()->isRedelivery()) { |
| 47 | + throw new RejectRedeliveredMessageException('Redelivered message from AMQP detected that will be rejected and trigger the retry logic.'); |
| 48 | + } |
| 49 | + |
| 50 | + return $stack->next()->handle($envelope, $stack); |
| 51 | + } |
| 52 | +} |
0 commit comments