Skip to content

Commit 55d687e

Browse files
[Messenger] Mention RedispatchMessage and RedispatchMessageHandler
1 parent d573187 commit 55d687e

File tree

1 file changed

+48
-0
lines changed

1 file changed

+48
-0
lines changed

messenger.rst

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2605,6 +2605,54 @@ Messenger gives you a single message bus service by default. But, you can config
26052605
as many as you want, creating "command", "query" or "event" buses and controlling
26062606
their middleware. See :doc:`/messenger/multiple_buses`.
26072607

2608+
Redispatching a Message
2609+
-----------------------
2610+
2611+
It may occur that you dispatch a message and for some reason, wants to
2612+
redispatch it through the same transport with the same envelope. To do so, you
2613+
can create a new
2614+
:class:`Symfony\\Component\\Messenger\\Message\\RedispatchMessage` and dispatch
2615+
it through your bus. Let's do this with the ``SmsNotification`` seen earlier::
2616+
2617+
// src/MessageHandler/SmsNotificationHandler.php
2618+
namespace App\MessageHandler;
2619+
2620+
use App\Message\SmsNotification;
2621+
use Symfony\Component\Messenger\Attribute\AsMessageHandler;
2622+
use Symfony\Component\Messenger\MessageBusInterface;
2623+
use Symfony\Component\Messenger\Message\RedispatchMessage;
2624+
2625+
#[AsMessageHandler]
2626+
class SmsNotificationHandler
2627+
{
2628+
public function __construct(private MessageBusInterface $bus)
2629+
{
2630+
}
2631+
2632+
public function __invoke(SmsNotification $message): void
2633+
{
2634+
// do something with the message
2635+
// then redispatch it based on your own logic
2636+
2637+
if ($needsRedispatch) {
2638+
$this->bus->dispatch(new RedispatchMessage($message));
2639+
}
2640+
}
2641+
}
2642+
2643+
The built-in
2644+
:class:`Symfony\\Component\\Messenger\\Handler\\RedispatchMessageHandler` will
2645+
take care of this message and redispatch it through the same bus it's been
2646+
dispatched at first. You can also use the second argument of the
2647+
``RedispatchMessage`` constructor to provide transports to use when
2648+
redispatching the message.
2649+
2650+
.. versionadded:: 6.3
2651+
2652+
The :class:`Symfony\\Component\\Messenger\\Message\\RedispatchMessage`
2653+
and :class:`Symfony\\Component\\Messenger\\Handler\\RedispatchMessageHandler`
2654+
classes were introduced in Symfony 6.3.
2655+
26082656
Learn more
26092657
----------
26102658

0 commit comments

Comments
 (0)