RejectRedeliveredMessageMiddleware.php 2.4 KB

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