Skip to content

[Messenger] drop the MessageSubscriberInterface in favor of the #AsMessageHandler attribute #17350

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 15, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 17 additions & 41 deletions messenger.rst
Original file line number Diff line number Diff line change
Expand Up @@ -667,7 +667,7 @@ of some or all transports:
$ php bin/console messenger:stats my_transport_name other_transport_name

.. note::

In order for this command to work, the configured transport's receiver must implement
:class:`Symfony\\Component\\Messenger\\Transport\\Receiver\\MessageCountAwareInterface`.

Expand Down Expand Up @@ -1061,7 +1061,7 @@ to retry them:

# see all messages in the failure transport with a default limit of 50
$ php bin/console messenger:failed:show

# see the 10 first messages
$ php bin/console messenger:failed:show --max=10

Expand Down Expand Up @@ -1935,49 +1935,38 @@ Possible options to configure with tags are:
* ``method``
* ``priority``

Handler Subscriber & Options
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
.. _handler-subscriber-options:

Handling Multiple Messages
~~~~~~~~~~~~~~~~~~~~~~~~~~

A handler class can handle multiple messages or configure itself by implementing
:class:`Symfony\\Component\\Messenger\\Handler\\MessageSubscriberInterface`::
A handler class can handle multiple messages. For that add the ``#AsMessageHandler`` attribute to the handling methods::

// src/MessageHandler/SmsNotificationHandler.php
namespace App\MessageHandler;

use App\Message\OtherSmsNotification;
use App\Message\SmsNotification;
use Symfony\Component\Messenger\Handler\MessageSubscriberInterface;

class SmsNotificationHandler implements MessageSubscriberInterface
class SmsNotificationHandler
{
public function __invoke(SmsNotification $message)
#[AsMessageHandler]
public function handleSmsNotification(SmsNotification $message)
{
// ...
}

#[AsMessageHandler]
public function handleOtherSmsNotification(OtherSmsNotification $message)
{
// ...
}

public static function getHandledMessages(): iterable
{
// handle this message on __invoke
yield SmsNotification::class;

// also handle this message on handleOtherSmsNotification
yield OtherSmsNotification::class => [
'method' => 'handleOtherSmsNotification',
//'priority' => 0,
//'bus' => 'messenger.bus.default',
];
}
}

.. deprecated:: 6.2

:class:`Symfony\\Component\\Messenger\\Handler\\MessageSubscriberInterface` was deprecated in Symfony 6.2.
Use :class:`#[AsMessageHandler] <Symfony\\Component\\Messenger\\Attribute\\AsMessageHandler>` attribute instead.
Implementing the :class:`Symfony\\Component\\Messenger\\Handler\\MessageSubscriberInterface` is another way to
handle multiple messages with one handler class. This interface was deprecated in Symfony 6.2.

Binding Handlers to Different Transports
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Expand All @@ -2002,38 +1991,25 @@ To do this, add the ``from_transport`` option to each handler. For example::
namespace App\MessageHandler;

use App\Message\UploadedImage;
use Symfony\Component\Messenger\Handler\MessageSubscriberInterface;

class ThumbnailUploadedImageHandler implements MessageSubscriberInterface
#[AsMessageHandler(from_transport: 'image_transport')]
class ThumbnailUploadedImageHandler
{
public function __invoke(UploadedImage $uploadedImage)
{
// do some thumbnailing
}

public static function getHandledMessages(): iterable
{
yield UploadedImage::class => [
'from_transport' => 'image_transport',
];
}
}

And similarly::

// src/MessageHandler/NotifyAboutNewUploadedImageHandler.php
// ...

class NotifyAboutNewUploadedImageHandler implements MessageSubscriberInterface
#[AsMessageHandler(from_transport: 'async_priority_normal')]
class NotifyAboutNewUploadedImageHandler
{
// ...

public static function getHandledMessages(): iterable
{
yield UploadedImage::class => [
'from_transport' => 'async_priority_normal',
];
}
}

Then, make sure to "route" your message to *both* transports:
Expand Down