@@ -8,32 +8,46 @@ The Message Component
88 The Message component helps application to send and receive messages
99 to/from other applications or via
1010
11+ Installation
12+ ------------
13+
14+ ..code-block ::terminal
15+
16+ $ composer require symfony/message
17+
18+ Alternatively, you can clone the `<https://github.com/symfony/message >`_ repository.
19+
20+ ..include ::/components/require_autoload.rst.inc
21+
1122Concepts
1223--------
1324
1425..image ::/_images/components/message/overview.png
1526
16- 1. **Sender **
17- Responsible for serializing and sending the message to _something_. This something can be a message broker or a 3rd
18- party API for example.
27+ **Sender **:
28+ Responsible for serializing and sending the message to _something_. This
29+ something can be a message broker or a third party API for example.
1930
20- 2. **Receiver **
21- Responsible for deserializing and forwarding the messages to handler(s). This can be a message queue puller or an API
22- endpoint for example.
31+ **Receiver **:
32+ Responsible for deserializing and forwarding the messages to handler(s). This
33+ can be a message queue puller or an API endpoint for example.
2334
24- 3. **Handler **
25- Given a received message, contains the user business logic related to the message. In practice, that is just a PHP
26- callable.
35+ **Handler **:
36+ Given a received message, contains the user business logic related to the
37+ message. In practice, that is just a PHP callable.
2738
2839Bus
2940---
3041
31- The bus is used to dispatch messages. MessageBus' behaviour is in its ordered middleware stack. When using
32- the message bus with Symfony's FrameworkBundle, the following middlewares are configured for you:
42+ The bus is used to dispatch messages. MessageBus' behavior is in its ordered
43+ middleware stack. When using the message bus with Symfony's FrameworkBundle, the
44+ following middlewares are configured for you:
45+
46+ #. ``LoggingMiddleware `` (logs the processing of your messages)
47+ #. ``SendMessageMiddleware `` (enables asynchronous processing)
48+ #. ``HandleMessageMiddleware `` (calls the registered handle)
3349
34- 1. `LoggingMiddleware ` (log the processing of your messages)
35- 2. `SendMessageMiddleware ` (enable asynchronous processing)
36- 3. `HandleMessageMiddleware ` (call the registered handle)
50+ Example::
3751
3852 use App\Message\MyMessage;
3953
@@ -42,9 +56,10 @@ the message bus with Symfony's FrameworkBundle, the following middlewares are co
4256Handlers
4357--------
4458
45- Once dispatched to the bus, messages will be handled by a "message handler". A message handler is a PHP callable
46- (i.e. a function or an instance of a class) that will do the required processing for your message. It _might_ return a
47- result.
59+ Once dispatched to the bus, messages will be handled by a "message handler". A
60+ message handler is a PHP callable (i.e. a function or an instance of a class)
61+ that will do the required processing for your message. It _might_ return a
62+ result::
4863
4964 namespace App\MessageHandler;
5065
@@ -58,43 +73,57 @@ result.
5873 }
5974 }
6075
76+ ..code-block ::xml
6177
6278 <service id =" App\Handler\MyMessageHandler" >
6379 <tag name =" message_handler" />
6480 </service >
6581
66- ** Note: ** If the message cannot be guessed from the handler's type-hint, use the ` handles ` attribute on the tag.
82+ .. note ::
6783
68- ### Asynchronous messages
84+ If the message cannot be guessed from the handler's type-hint, use the
85+ ``handles `` attribute on the tag.
6986
70- Using the Message Component is useful to decouple your application but it also very useful when you want to do some
71- asychronous processing. This means that your application will produce a message to a queuing system and consume this
87+ Asynchronous messages
88+ ~~~~~~~~~~~~~~~~~~~~~
89+
90+ Using the Message Component is useful to decouple your application but it also
91+ very useful when you want to do some asynchronous processing. This means that
92+ your application will produce a message to a queuing system and consume this
7293message later in the background, using a _worker_.
7394
74- #### Adapters
95+ Adapters
96+ ~~~~~~~~
7597
76- The communication with queuing system or3rd parties is for delegated to libraries for now. You can use one of the
77- following adapters:
98+ The communication with queuing system orthird parties is for delegated to
99+ libraries for now. You can use one of the following adapters:
78100
79- - [ PHP Enqueue bridge](https://github.com/sroze/enqueue-bridge) to use one of their 10+ compatible queues such as
80- RabbitMq, Amazon SQS or Google Pub/Sub.
101+ #. ` PHP Enqueue bridge `_ to use one of their 10+ compatible queues such as
102+ RabbitMq, Amazon SQS or Google Pub/Sub.
81103
82104Routing
83105-------
84106
85- When doing asynchronous processing, the key is to route the message to the right sender. As the routing is
86- application-specific and not message-specific, the configuration can be made within the `framework.yaml `
87- configuration file as well:
107+ When doing asynchronous processing, the key is to route the message to the right
108+ sender. As the routing is application-specific and not message-specific, the
109+ configuration can be made within the ``framework.yaml `` configuration file as
110+ well:
111+
112+ ..code-block ::yaml
88113
89114framework :
90115message :
91116routing :
92117' My\Message\MessageAboutDoingOperationalWork ' :my_operations_queue_sender
93118
94- Such configuration would only route the `MessageAboutDoingOperationalWork ` message to be asynchronous, the rest of the
95- messages would still be directly handled.
119+ Such configuration would only route the ``MessageAboutDoingOperationalWork ``
120+ message to be asynchronous, the rest of the messages would still be directly
121+ handled.
96122
97- If you want to do route all the messages to a queue by default, you can use such configuration:
123+ If you want to do route all the messages to a queue by default, you can use such
124+ configuration:
125+
126+ ..code-block ::yaml
98127
99128framework :
100129message :
@@ -104,6 +133,8 @@ If you want to do route all the messages to a queue by default, you can use such
104133
105134 Note that you can also route a message to multiple senders at the same time:
106135
136+ ..code-block ::yaml
137+
107138framework :
108139message :
109140routing :
@@ -112,18 +143,20 @@ Note that you can also route a message to multiple senders at the same time:
112143 Same bus received and sender
113144~~~~~~~~~~~~~~~~~~~~~~~~~~~~
114145
115- To allow us to receive and send messages on the same bus and prevent a loop, the message bus is equipped with the
116- `WrapIntoReceivedMessage ` received. It will wrap the received messages into `ReceivedMessage ` objects and the
117- `SendMessageMiddleware ` middleware will know it should not send these messages.
146+ To allow us to receive and send messages on the same bus and prevent a loop, the
147+ message bus is equipped with the ``WrapIntoReceivedMessage `` received. It will
148+ wrap the received messages into ``ReceivedMessage `` objects and the
149+ ``SendMessageMiddleware `` middleware will know it should not send these messages.
118150
119151Your own sender
120152---------------
121153
122- Using the `SenderInterface `, you can easily create your own message sender. Let's say you already have an
123- `ImportantAction ` message going through the message bus and handled by a handler. Now, you also want to send this
124- message as an email.
154+ Using the ``SenderInterface ``, you can easily create your own message sender.
155+ Let's say you already have an ``ImportantAction `` message going through the
156+ message bus and handled by a handler. Now, you also want to send this message as
157+ an email.
125158
126- 1. Create your sender
159+ First, create your sender::
127160
128161 namespace App\MessageSender;
129162
@@ -158,7 +191,9 @@ message as an email.
158191 }
159192 }
160193
161- 2. Register your sender service
194+ Then, register your sender service:
195+
196+ ..code-block ::yaml
162197
163198services :
164199App\MessageSender\ImportantActionToEmailSender :
@@ -169,27 +204,35 @@ message as an email.
169204tags :
170205 -message.sender
171206
172- 3. Route your important message to the sender
207+ Finally, route your important message to the sender:
208+
209+ ..code-block ::yaml
173210
174211framework :
175212message :
176213routing :
177214' App\Message\ImportantAction ' :[App\MessageSender\ImportantActionToEmailSender, ~]
178215
179- **Note: ** this example shows you how you can at the same time send your message and directly handle it using a `null `
180- (`~ `) sender.
216+ ..note ::
217+
218+ This example shows you how you can at the same time send your message and
219+ directly handle it using a ``null `` (``~ ``) sender.
181220
182221Your own receiver
183222-----------------
184223
185- A consumer is responsible of receiving messages from a source and dispatching them to the application.
224+ A consumer is responsible of receiving messages from a source and dispatching
225+ them to the application.
186226
187- Let's say you already proceed some "orders" on your application using a `NewOrder ` message. Now you want to integrate with
188- a 3rd party or a legacy application but you can't use an API and need to use a shared CSV file with new orders.
227+ Let's say you already proceed some "orders" on your application using a
228+ ``NewOrder `` message. Now you want to integrate with a 3rd party or a legacy
229+ application but you can't use an API and need to use a shared CSV file with new
230+ orders.
189231
190- You will read this CSV file and dispatch a `NewOrder ` message. All you need to do is your custom CSV consumer and Symfony will do the rest.
232+ You will read this CSV file and dispatch a ``NewOrder `` message. All you need to
233+ do is your custom CSV consumer and Symfony will do the rest.
191234
192- 1. Create your receiver
235+ First, create your receiver::
193236
194237 namespace App\MessageReceiver;
195238
@@ -219,7 +262,9 @@ You will read this CSV file and dispatch a `NewOrder` message. All you need to d
219262 }
220263 }
221264
222- 2. Register your receiver service
265+ Then, register your receiver service:
266+
267+ ..code-block ::yaml
223268
224269services :
225270App\MessageReceiver\NewOrdersFromCsvFile :
@@ -230,6 +275,10 @@ You will read this CSV file and dispatch a `NewOrder` message. All you need to d
230275tags :
231276 -message.receiver
232277
233- 3. Use your consumer
278+ Finally, use your consumer:
279+
280+ ..code-block ::terminal
234281
235282 $ bin/console message:consume App\MessageReceived\NewOrdersFromCsvFile
283+
284+ .. _`PHP Enqueue bridge` :https://github.com/sroze/enqueue-bridge