@@ -28,6 +28,7 @@ you need it, like in a controller::
28
28
// src/Controller/DefaultController.php
29
29
namespace App\Controller;
30
30
31
+ use App\Message\SendNotification;
31
32
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
32
33
use Symfony\Component\Messenger\MessageBusInterface;
33
34
@@ -91,7 +92,7 @@ the messenger component, the following configuration should have been created:
91
92
framework :
92
93
messenger :
93
94
adapters :
94
- default : " %env(MESSENGER_DSN)%"
95
+ amqp : " %env(MESSENGER_DSN)%"
95
96
96
97
.. code-block :: bash
97
98
@@ -100,17 +101,17 @@ the messenger component, the following configuration should have been created:
100
101
MESSENGER_DSN=amqp://guest:guest@localhost:5672/%2f/messages
101
102
# ##< symfony/messenger ###
102
103
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:
105
106
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.
108
109
109
110
Routing
110
111
-------
111
112
112
113
Instead 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.
114
115
You can configure which message is routed to which sender with the following
115
116
configuration:
116
117
@@ -119,40 +120,39 @@ configuration:
119
120
framework :
120
121
messenger :
121
122
routing :
122
- ' My\Message\Message ' : messenger.default_sender # Or another sender service name
123
+ ' My\Message\Message ' : amqp # The name of the defined adapter
123
124
124
125
Such configuration would only route the ``My\Message\Message `` message to be
125
126
asynchronous, the rest of the messages would still be directly handled.
126
127
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:
129
129
130
130
.. code-block :: yaml
131
131
132
132
framework :
133
133
messenger :
134
134
routing :
135
- ' My\Message\MessageAboutDoingOperationalWork ' : messenger.operations_sender
136
- ' * ' : messenger.default_sender
135
+ ' My\Message\MessageAboutDoingOperationalWork ' : another_adapter
136
+ ' * ' : amqp
137
137
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 :
139
139
140
140
.. code-block :: yaml
141
141
142
142
framework :
143
143
messenger :
144
144
routing :
145
- ' My\Message\ToBeSentToTwoSenders ' : [messenger.default_sender, messenger.audit_sender ]
145
+ ' My\Message\ToBeSentToTwoSenders ' : [amqp, audit ]
146
146
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 :
149
149
150
150
.. code-block :: yaml
151
151
152
152
framework :
153
153
messenger :
154
154
routing :
155
- ' My\Message\ThatIsGoingToBeSentAndHandledLocally ' : [messenger.default_sender , ~]
155
+ ' My\Message\ThatIsGoingToBeSentAndHandledLocally ' : [amqp , ~]
156
156
157
157
Consuming messages
158
158
------------------
@@ -163,81 +163,51 @@ like this:
163
163
164
164
.. code-block :: terminal
165
165
166
- $ bin/console messenger:consume-messages messenger.default_receiver
166
+ $ bin/console messenger:consume-messages amqp
167
167
168
168
The first argument is the receiver's service name. It might have been created by
169
169
your ``adapters `` configuration or it can be your own receiver.
170
170
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
-
186
171
Your own Adapters
187
172
-----------------
188
173
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.
192
176
193
177
Create your adapter Factory
194
178
~~~~~~~~~~~~~~~~~~~~~~~~~~~
195
179
196
180
You need to give FrameworkBundle the opportunity to create your adapter from a
197
181
DSN. You will need an adapter factory::
198
182
199
- use Symfony\Component\Messenger\Adapter\Factory\AdapterInterface;
200
183
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;
219
184
use Symfony\Component\Messenger\Transport\ReceiverInterface;
220
185
use Symfony\Component\Messenger\Transport\SenderInterface;
221
186
222
- class YourAdapter implements AdapterInterface
187
+ class YourAdapterFactory implements AdapterFactoryInterface
223
188
{
224
- public function receiver( ): ReceiverInterface
189
+ public function createReceiver(string $dsn, array $options ): ReceiverInterface
225
190
{
226
191
return new YourReceiver(/* ... */);
227
192
}
228
193
229
- public function sender( ): SenderInterface
194
+ public function createSender(string $dsn, array $options ): SenderInterface
230
195
{
231
196
return new YourSender(/* ... */);
232
197
}
198
+
199
+ public function supports(string $dsn, array $options): bool
200
+ {
201
+ return 0 === strpos($dsn, 'my-adapter://');
202
+ }
233
203
}
234
204
235
205
Register your factory
236
206
~~~~~~~~~~~~~~~~~~~~~
237
207
238
208
.. code-block :: xml
239
209
240
- <service id =" Your\Adapter\Factory " >
210
+ <service id =" Your\Adapter\YourAdapterFactory " >
241
211
<tag name =" messenger.adapter_factory" />
242
212
</service >
243
213
@@ -254,10 +224,10 @@ named adapter using your own DSN:
254
224
adapters :
255
225
yours : ' my-adapter://...'
256
226
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:
258
229
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.
262
232
263
233
.. _`enqueue's adapter` : https://github.com/sroze/enqueue-bridge
0 commit comments