title: Consumer Error Handling in Symfony Messenger / RabbitMQ
body: <p>I'm using the new <a href="https://symfony.com/doc/current/components/messenger.html" rel="nofollow noreferrer">Symfony Messenger Component</a> 4.1 and <a href="https://www.rabbitmq.com/" rel="nofollow noreferrer">RabbitMQ</a> 3.6.10-1 to queue and asynchronously send email and SMS notifications from my Symfony 4.1 web application. My Messenger configuration (<code>messenger.yaml</code>) looks like this:</p>
  
  <pre><code>framework:
      messenger:
          transports:
              amqp: '%env(MESSENGER_TRANSPORT_DSN_NOTIFICATIONS)%'
  
          routing:
              'App\NotificationBundle\Entity\NotificationQueueEntry': amqp
  </code></pre>
  
  <p>When a new notification is to be sent, I queue it like this:</p>
  
  <pre><code>use Symfony\Component\Messenger\MessageBusInterface;
  // ...
  $notificationQueueEntry = new NotificationQueueEntry();
  // [Set notification details such as recipients, subject, and message]
  $this-&gt;messageBus-&gt;dispatch($notificationQueueEntry);
  </code></pre>
  
  <p>Then I start the consumer like this on the command line:</p>
  
  <pre><code>$ bin/console messenger:consume-messages
  </code></pre>
  
  <p>I have implemented a <code>SendNotificationHandler</code> service where the actual delivery happens. The service configuration:</p>
  
  <pre><code>App\NotificationBundle\MessageHandler\SendNotificationHandler:
      arguments:
          - '@App\NotificationBundle\Service\NotificationQueueService'
      tags: [ messenger.message_handler ]
  </code></pre>
  
  <p>And the class:</p>
  
  <pre><code>class SendNotificationHandler
  {
      public function __invoke(NotificationQueueEntry $entry): void
      {
          $this-&gt;notificationQueueService-&gt;sendNotification($entry);
      }
  }
  </code></pre>
  
  <p><strong>Until this point, everything works smoothly and the notifications get delivered.</strong></p>
  
  <p><strong>Now my question</strong>: It may happen that an email or SMS cannot be delivered due to a (temporary) network failure. In such a case, I would like my system to retry the delivery after a specified amount of time, up to a specified maximum number of retries. <strong>What is the way to go to achieve this?</strong></p>
  
  <p>I have read about <a href="https://www.rabbitmq.com/dlx.html" rel="nofollow noreferrer">Dead Letter Exchanges</a>, however, I could not find any documentation or example on how to integrate this with the Symfony Messenger Component.</p>