Skip to content

Commit 71476e4

Browse files
committed
minor #18852 [Messenger] Mention RedispatchMessage and RedispatchMessageHandler (alexandre-daubois)
This PR was merged into the 6.4 branch. Discussion ---------- [Messenger] Mention `RedispatchMessage` and `RedispatchMessageHandler` Fix #18098 Commits ------- 55d687e [Messenger] Mention `RedispatchMessage` and `RedispatchMessageHandler`
2 parents 47a236f + 55d687e commit 71476e4

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
@@ -2738,6 +2738,54 @@ Messenger gives you a single message bus service by default. But, you can config
27382738
as many as you want, creating "command", "query" or "event" buses and controlling
27392739
their middleware. See :doc:`/messenger/multiple_buses`.
27402740

2741+
Redispatching a Message
2742+
-----------------------
2743+
2744+
It may occur that you dispatch a message and for some reason, wants to
2745+
redispatch it through the same transport with the same envelope. To do so, you
2746+
can create a new
2747+
:class:`Symfony\\Component\\Messenger\\Message\\RedispatchMessage` and dispatch
2748+
it through your bus. Let's do this with the ``SmsNotification`` seen earlier::
2749+
2750+
// src/MessageHandler/SmsNotificationHandler.php
2751+
namespace App\MessageHandler;
2752+
2753+
use App\Message\SmsNotification;
2754+
use Symfony\Component\Messenger\Attribute\AsMessageHandler;
2755+
use Symfony\Component\Messenger\MessageBusInterface;
2756+
use Symfony\Component\Messenger\Message\RedispatchMessage;
2757+
2758+
#[AsMessageHandler]
2759+
class SmsNotificationHandler
2760+
{
2761+
public function __construct(private MessageBusInterface $bus)
2762+
{
2763+
}
2764+
2765+
public function __invoke(SmsNotification $message): void
2766+
{
2767+
// do something with the message
2768+
// then redispatch it based on your own logic
2769+
2770+
if ($needsRedispatch) {
2771+
$this->bus->dispatch(new RedispatchMessage($message));
2772+
}
2773+
}
2774+
}
2775+
2776+
The built-in
2777+
:class:`Symfony\\Component\\Messenger\\Handler\\RedispatchMessageHandler` will
2778+
take care of this message and redispatch it through the same bus it's been
2779+
dispatched at first. You can also use the second argument of the
2780+
``RedispatchMessage`` constructor to provide transports to use when
2781+
redispatching the message.
2782+
2783+
.. versionadded:: 6.3
2784+
2785+
The :class:`Symfony\\Component\\Messenger\\Message\\RedispatchMessage`
2786+
and :class:`Symfony\\Component\\Messenger\\Handler\\RedispatchMessageHandler`
2787+
classes were introduced in Symfony 6.3.
2788+
27412789
Learn more
27422790
----------
27432791

0 commit comments

Comments
 (0)