@@ -51,24 +51,31 @@ serialized::
5151.. _messenger-handler :
5252
5353A message handler is a PHP callable, the recommended way to create it is to
54- create a class thatimplements :class: `Symfony\\ Component\\ Messenger\\ Handler \\ MessageHandlerInterface `
55- and has an ``__invoke() `` method that's type-hinted with the message class (or a
56- message interface)::
54+ create a class thathas the :class: `Symfony\\ Component\\ Messenger\\ Attribute \\ AsMessageHandler `
55+ attribute and has an ``__invoke() `` method that's type-hinted with the
56+ messageclass (or a message interface)::
5757
5858 // src/MessageHandler/SmsNotificationHandler.php
5959 namespace App\MessageHandler;
6060
6161 use App\Message\SmsNotification;
62- use Symfony\Component\Messenger\Handler\MessageHandlerInterface ;
62+ use Symfony\Component\Messenger\Attribute\AsMessageHandler ;
6363
64- class SmsNotificationHandler implements MessageHandlerInterface
64+ #[AsMessageHandler]
65+ class SmsNotificationHandler
6566 {
6667 public function __invoke(SmsNotification $message)
6768 {
6869 // ... do some work - like sending an SMS message!
6970 }
7071 }
7172
73+ ..note ::
74+
75+ You can also create a class without the attribute (e.g. if you're
76+ using PHP 7.4), by implementing:class: `Symfony\\ Component\\ Messenger\\ Handler\\ MessageHandlerInterface `
77+ instead.
78+
7279Thanks to:ref: `autoconfiguration <services-autoconfigure >` and the ``SmsNotification ``
7380type-hint, Symfony knows that this handler should be called when an ``SmsNotification ``
7481message is dispatched. Most of the time, this is all you need to do. But you can
@@ -349,9 +356,10 @@ Then, in your handler, you can query for a fresh object::
349356
350357 use App\Message\NewUserWelcomeEmail;
351358 use App\Repository\UserRepository;
352- use Symfony\Component\Messenger\Handler\MessageHandlerInterface ;
359+ use Symfony\Component\Messenger\Attribute\AsMessageHandler ;
353360
354- class NewUserWelcomeEmailHandler implements MessageHandlerInterface
361+ #[AsMessageHandler]
362+ class NewUserWelcomeEmailHandler
355363 {
356364 private $userRepository;
357365
@@ -1673,6 +1681,35 @@ on a case-by-case basis via the :class:`Symfony\\Component\\Messenger\\Stamp\\Se
16731681Customizing Handlers
16741682--------------------
16751683
1684+ Configuring Handlers Using Attributes
1685+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1686+
1687+ You can configure your handler by passing options to the attribute::
1688+
1689+ // src/MessageHandler/SmsNotificationHandler.php
1690+ namespace App\MessageHandler;
1691+
1692+ use App\Message\OtherSmsNotification;
1693+ use App\Message\SmsNotification;
1694+ use Symfony\Component\Messenger\Attribute\AsMessageHandler;
1695+
1696+ #[AsMessageHandler(fromTransport: 'async', priority: 10)]
1697+ class SmsNotificationHandler
1698+ {
1699+ public function __invoke(SmsNotification $message)
1700+ {
1701+ // ...
1702+ }
1703+ }
1704+
1705+ Possible options to configure with the attribute are:
1706+
1707+ * ``bus ``
1708+ * ``fromTransport ``
1709+ * ``handles ``
1710+ * ``method ``
1711+ * ``priority ``
1712+
16761713.. _messenger-handler-config :
16771714
16781715Manually Configuring Handlers