@@ -51,17 +51,19 @@ serialized::
51
51
.. _messenger-handler :
52
52
53
53
A message handler is a PHP callable, the recommended way to create it is to
54
- create a class that implements :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 using :class: `Symfony\\ Component\\ Messenger\\ Attribute\\ AsMessageHandler `
55
+ attribute which has an ``__invoke() `` method that's type-hinted with the
56
+ message class (or a message interface) or you can create a class without the attribute, by implementing
57
+ :class: `Symfony\\ Component\\ Messenger\\ Handler\\ MessageHandlerInterface `::
57
58
58
59
// src/MessageHandler/SmsNotificationHandler.php
59
60
namespace App\MessageHandler;
60
61
61
62
use App\Message\SmsNotification;
62
- use Symfony\Component\Messenger\Handler\MessageHandlerInterface ;
63
+ use Symfony\Component\Messenger\Attribute\AsMessageHandler ;
63
64
64
- class SmsNotificationHandler implements MessageHandlerInterface
65
+ #[AsMessageHandler]
66
+ class SmsNotificationHandler
65
67
{
66
68
public function __invoke(SmsNotification $message)
67
69
{
@@ -349,9 +351,10 @@ Then, in your handler, you can query for a fresh object::
349
351
350
352
use App\Message\NewUserWelcomeEmail;
351
353
use App\Repository\UserRepository;
352
- use Symfony\Component\Messenger\Handler\MessageHandlerInterface ;
354
+ use Symfony\Component\Messenger\Attribute\AsMessageHandler ;
353
355
354
- class NewUserWelcomeEmailHandler implements MessageHandlerInterface
356
+ #[AsMessageHandler]
357
+ class NewUserWelcomeEmailHandler
355
358
{
356
359
private $userRepository;
357
360
@@ -1769,6 +1772,36 @@ Customizing Handlers
1769
1772
1770
1773
.. _messenger-handler-config :
1771
1774
1775
+ Configuring Handlers Using Attribute
1776
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1777
+
1778
+ You can configure your handler easily by passing options to the attribute::
1779
+
1780
+ // src/MessageHandler/SmsNotificationHandler.php
1781
+ namespace App\MessageHandler;
1782
+
1783
+ use App\Message\OtherSmsNotification;
1784
+ use App\Message\SmsNotification;
1785
+ use Symfony\Component\Messenger\Attribute\AsMessageHandler;
1786
+
1787
+ #[AsMessageHandler(fromTransport: 'async', priority: 10)]
1788
+ class SmsNotificationHandler
1789
+ {
1790
+ public function __invoke(SmsNotification $message)
1791
+ {
1792
+ // ...
1793
+ }
1794
+ }
1795
+
1796
+
1797
+ Possible options to configure with the attribute are:
1798
+
1799
+ * ``bus ``
1800
+ * ``fromTransport ``
1801
+ * ``handles ``
1802
+ * ``method ``
1803
+ * ``priority ``
1804
+
1772
1805
Manually Configuring Handlers
1773
1806
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1774
1807
0 commit comments