@@ -28,6 +28,7 @@ you need it, like in a controller::
2828 // src/Controller/DefaultController.php
2929 namespace App\Controller;
3030
31+ use App\Message\SendNotification;
3132 use Symfony\Bundle\FrameworkBundle\Controller\Controller;
3233 use Symfony\Component\Messenger\MessageBusInterface;
3334
@@ -91,7 +92,7 @@ the messenger component, the following configuration should have been created:
9192 framework :
9293 messenger :
9394 adapters :
94- default : " %env(MESSENGER_DSN)%"
95+ amqp : " %env(MESSENGER_DSN)%"
9596
9697 .. code-block :: bash
9798
@@ -100,17 +101,17 @@ the messenger component, the following configuration should have been created:
100101 MESSENGER_DSN=amqp://guest:guest@localhost:5672/%2f/messages
101102 # ##< symfony/messenger ###
102103
103- This is enough to allow you to route your message to the ``messenger.default_adapter ``
104- adapter. This will also configure the following for you:
104+ This is enough to allow you to route your message to the ``amqp ``. This will also
105+ configure the following services for you:
105106
106- 1. A ``messenger.default_sender `` sender to be used when routing messages
107- 2. A ``messenger.default_receiver `` receiver to be used when consuming messages.
107+ 1. A ``messenger.sender.amqp `` sender to be used when routing messages.
108+ 2. A ``messenger.receiver.amqp `` receiver to be used when consuming messages.
108109
109110Routing
110111-------
111112
112113Instead of calling a handler, you have the option to route your message(s) to a
113- sender. Part of an adapter, it is responsible of sending your message somewhere.
114+ sender. Part of an adapter, it is responsible for sending your message somewhere.
114115You can configure which message is routed to which sender with the following
115116configuration:
116117
@@ -119,40 +120,39 @@ configuration:
119120 framework :
120121 messenger :
121122 routing :
122- ' My\Message\Message ' : messenger.default_sender # Or another sender service name
123+ ' My\Message\Message ' : amqp # The name of the defined adapter
123124
124125 Such configuration would only route the ``My\Message\Message `` message to be
125126asynchronous, the rest of the messages would still be directly handled.
126127
127- If you want to do route all the messages to a queue by default, you can use such
128- configuration:
128+ You can route all classes of message to a sender using an asterisk instead of a class name:
129129
130130.. code-block :: yaml
131131
132132 framework :
133133 messenger :
134134 routing :
135- ' My\Message\MessageAboutDoingOperationalWork ' : messenger.operations_sender
136- ' * ' : messenger.default_sender
135+ ' My\Message\MessageAboutDoingOperationalWork ' : another_adapter
136+ ' * ' : amqp
137137
138- Note that you can also route a message to multiple senders at the same time :
138+ A class of message can also be routed to a multiple senders by specifying a list :
139139
140140.. code-block :: yaml
141141
142142 framework :
143143 messenger :
144144 routing :
145- ' My\Message\ToBeSentToTwoSenders ' : [messenger.default_sender, messenger.audit_sender ]
145+ ' My\Message\ToBeSentToTwoSenders ' : [amqp, audit ]
146146
147- Last but not least you can also route a message while still calling the handler
148- on your application by having a `` null `` sender :
147+ By specifying a `` null `` sender, you can also route a class of messages to a sender
148+ while still having them passed to their respective handler :
149149
150150.. code-block :: yaml
151151
152152 framework :
153153 messenger :
154154 routing :
155- ' My\Message\ThatIsGoingToBeSentAndHandledLocally ' : [messenger.default_sender , ~]
155+ ' My\Message\ThatIsGoingToBeSentAndHandledLocally ' : [amqp , ~]
156156
157157 Consuming messages
158158------------------
@@ -163,81 +163,51 @@ like this:
163163
164164.. code-block :: terminal
165165
166- $ bin/console messenger:consume-messages messenger.default_receiver
166+ $ bin/console messenger:consume-messages amqp
167167
168168 The first argument is the receiver's service name. It might have been created by
169169your ``adapters `` configuration or it can be your own receiver.
170170
171- Registering your middleware
172- ---------------------------
173-
174- The message bus is based on a set of middleware. If you are un-familiar with the concept,
175- look at the :doc: `Messenger component docs </components/messenger >`.
176-
177- To register your middleware, use the ``messenger.middleware `` tag as in the
178- following example:
179-
180- .. code-block :: xml
181-
182- <service id =" Your\Own\Middleware" >
183- <tag name =" messenger.middleware" />
184- </service >
185-
186171Your own Adapters
187172-----------------
188173
189- Learn how to build your own adapters within the Component's documentation. Once
190- you have built your classes, you can register your adapter factory to be able to
191- use it via a DSN in the Symfony application.
174+ Once you have written your adapter's sender and receiver, you can register your
175+ adapter factory to be able to use it via a DSN in the Symfony application.
192176
193177Create your adapter Factory
194178~~~~~~~~~~~~~~~~~~~~~~~~~~~
195179
196180You need to give FrameworkBundle the opportunity to create your adapter from a
197181DSN. You will need an adapter factory::
198182
199- use Symfony\Component\Messenger\Adapter\Factory\AdapterInterface;
200183 use Symfony\Component\Messenger\Adapter\Factory\AdapterFactoryInterface;
201-
202- class YourAdapterFactory implements AdapterFactoryInterface
203- {
204- public function create(string $dsn): AdapterInterface
205- {
206- return new YourAdapter(/* ... */);
207- }
208-
209- public function supports(string $dsn): bool
210- {
211- return 0 === strpos($dsn, 'my-adapter://');
212- }
213- }
214-
215- The ``YourAdaper `` class need to implement the ``AdapterInterface ``. It
216- will like the following example::
217-
218- use Symfony\Component\Messenger\Adapter\Factory\AdapterInterface;
219184 use Symfony\Component\Messenger\Transport\ReceiverInterface;
220185 use Symfony\Component\Messenger\Transport\SenderInterface;
221186
222- class YourAdapter implements AdapterInterface
187+ class YourAdapterFactory implements AdapterFactoryInterface
223188 {
224- public function receiver( ): ReceiverInterface
189+ public function createReceiver(string $dsn, array $options ): ReceiverInterface
225190 {
226191 return new YourReceiver(/* ... */);
227192 }
228193
229- public function sender( ): SenderInterface
194+ public function createSender(string $dsn, array $options ): SenderInterface
230195 {
231196 return new YourSender(/* ... */);
232197 }
198+
199+ public function supports(string $dsn, array $options): bool
200+ {
201+ return 0 === strpos($dsn, 'my-adapter://');
202+ }
233203 }
234204
235205Register your factory
236206~~~~~~~~~~~~~~~~~~~~~
237207
238208.. code-block :: xml
239209
240- <service id =" Your\Adapter\Factory " >
210+ <service id =" Your\Adapter\YourAdapterFactory " >
241211 <tag name =" messenger.adapter_factory" />
242212 </service >
243213
@@ -254,10 +224,10 @@ named adapter using your own DSN:
254224 adapters :
255225 yours : ' my-adapter://...'
256226
257- This will give you access to the following services:
227+ In addition of being able to route your messages to the ``yours `` sender, this
228+ will give you access to the following services:
258229
259- 1. ``messenger.yours_adapter ``: the instance of your adapter.
260- 2. ``messenger.yours_receiver `` and ``messenger.yours_sender ``, the
261- receiver and sender created by the adapter.
230+ 1. ``messenger.sender.hours ``: the sender.
231+ 2. ``messenger.receiver.hours ``: the receiver.
262232
263233.. _`enqueue's adapter` : https://github.com/sroze/enqueue-bridge
0 commit comments