Skip to content

Commit d183916

Browse files
committed
Add a section about redispatching messages
1 parent 2dde019 commit d183916

File tree

1 file changed

+44
-0
lines changed

1 file changed

+44
-0
lines changed

messenger.rst

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

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

0 commit comments

Comments
 (0)