Skip to content

Commit 32403ea

Browse files
committed
Update the documentation to reflect the latest changes
1 parent 3fb973c commit 32403ea

File tree

2 files changed

+48
-75
lines changed

2 files changed

+48
-75
lines changed

components/messenger.rst

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -24,16 +24,15 @@ Concepts
2424
.. image:: /_images/components/messenger/overview.png
2525

2626
**Sender**:
27-
Responsible for serializing and sending the message to _something_. This
27+
Responsible for serializing and sending messages to _something_. This
2828
something can be a message broker or a third party API for example.
2929

3030
**Receiver**:
31-
Responsible for deserializing and forwarding the messages to handler(s). This
31+
Responsible for deserializing and forwarding messages to handler(s). This
3232
can be a message queue puller or an API endpoint for example.
3333

3434
**Handler**:
35-
Given a received message, contains the user business logic related to the
36-
message. In practice, that is just a PHP callable.
35+
Responsible for handling messages using the business logic applicable to the messages.
3736

3837
Bus
3938
---
@@ -65,15 +64,14 @@ Example::
6564

6665
.. note:
6766
68-
Every middleware need to implement the ``MiddlewareInterface`` interface.
67+
Every middleware needs to implement the ``MiddlewareInterface`` interface.
6968
7069
Handlers
7170
--------
7271

7372
Once dispatched to the bus, messages will be handled by a "message handler". A
7473
message handler is a PHP callable (i.e. a function or an instance of a class)
75-
that will do the required processing for your message. It _might_ return a
76-
result::
74+
that will do the required processing for your message::
7775

7876
namespace App\MessageHandler;
7977

@@ -90,8 +88,8 @@ result::
9088
Adapters
9189
--------
9290

93-
The communication with queuing system or third parties is delegated to
94-
libraries for now.
91+
In order to send and receive messages, you will have to configure an adapter. An
92+
adapter will be responsible of communicating with your message broker or 3rd parties.
9593

9694
Your own sender
9795
~~~~~~~~~~~~~~~
@@ -169,18 +167,23 @@ First, create your receiver::
169167
$this->filePath = $filePath;
170168
}
171169

172-
public function receive() : \Generator
170+
public function receive(callable $handler) : void
173171
{
174172
$ordersFromCsv = $this->serializer->deserialize(file_get_contents($this->filePath), 'csv');
175173

176174
foreach ($ordersFromCsv as $orderFromCsv) {
177-
yield new NewOrder($orderFromCsv['id'], $orderFromCsv['account_id'], $orderFromCsv['amount']);
175+
$handler(new NewOrder($orderFromCsv['id'], $orderFromCsv['account_id'], $orderFromCsv['amount']));
178176
}
179177
}
178+
179+
public function stop(): void
180+
{
181+
// noop
182+
}
180183
}
181184

182-
Same bus received and sender
183-
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
185+
Receiver and Sender on the same bus
186+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
184187

185188
To allow us to receive and send messages on the same bus and prevent an infinite
186189
loop, the message bus is equipped with the ``WrapIntoReceivedMessage`` middleware.

messenger.rst

Lines changed: 32 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -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

109110
Routing
110111
-------
111112

112113
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.
114115
You can configure which message is routed to which sender with the following
115116
configuration:
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
125126
asynchronous, 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
169169
your ``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-
186171
Your 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

193177
Create your adapter Factory
194178
~~~~~~~~~~~~~~~~~~~~~~~~~~~
195179

196180
You need to give FrameworkBundle the opportunity to create your adapter from a
197181
DSN. 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

235205
Register 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

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy