@@ -8,32 +8,46 @@ The Message Component
8
8
The Message component helps application to send and receive messages
9
9
to/from other applications or via
10
10
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
+
11
22
Concepts
12
23
--------
13
24
14
25
.. image :: /_images/components/message/overview.png
15
26
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.
19
30
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.
23
34
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.
27
38
28
39
Bus
29
40
---
30
41
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)
33
49
34
- 1. `LoggingMiddleware ` (log the processing of your messages)
35
- 2. `SendMessageMiddleware ` (enable asynchronous processing)
36
- 3. `HandleMessageMiddleware ` (call the registered handle)
50
+ Example::
37
51
38
52
use App\Message\MyMessage;
39
53
@@ -42,9 +56,10 @@ the message bus with Symfony's FrameworkBundle, the following middlewares are co
42
56
Handlers
43
57
--------
44
58
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::
48
63
49
64
namespace App\MessageHandler;
50
65
@@ -58,43 +73,57 @@ result.
58
73
}
59
74
}
60
75
76
+ .. code-block :: xml
61
77
62
78
<service id =" App\Handler\MyMessageHandler" >
63
79
<tag name =" message_handler" />
64
80
</service >
65
81
66
- ** Note: ** If the message cannot be guessed from the handler's type-hint, use the ` handles ` attribute on the tag.
82
+ .. note ::
67
83
68
- ### Asynchronous messages
84
+ If the message cannot be guessed from the handler's type-hint, use the
85
+ ``handles `` attribute on the tag.
69
86
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
72
93
message later in the background, using a _worker_.
73
94
74
- #### Adapters
95
+ Adapters
96
+ ~~~~~~~~
75
97
76
- The communication with queuing system or 3rd parties is for delegated to libraries for now. You can use one of the
77
- following adapters:
98
+ The communication with queuing system or third parties is for delegated to
99
+ libraries for now. You can use one of the following adapters:
78
100
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.
81
103
82
104
Routing
83
105
-------
84
106
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
88
113
89
114
framework :
90
115
message :
91
116
routing :
92
117
' My\Message\MessageAboutDoingOperationalWork ' : my_operations_queue_sender
93
118
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.
96
122
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
98
127
99
128
framework :
100
129
message :
@@ -104,6 +133,8 @@ If you want to do route all the messages to a queue by default, you can use such
104
133
105
134
Note that you can also route a message to multiple senders at the same time:
106
135
136
+ .. code-block :: yaml
137
+
107
138
framework :
108
139
message :
109
140
routing :
@@ -112,18 +143,20 @@ Note that you can also route a message to multiple senders at the same time:
112
143
Same bus received and sender
113
144
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
114
145
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.
118
150
119
151
Your own sender
120
152
---------------
121
153
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.
125
158
126
- 1. Create your sender
159
+ First, create your sender::
127
160
128
161
namespace App\MessageSender;
129
162
@@ -158,7 +191,9 @@ message as an email.
158
191
}
159
192
}
160
193
161
- 2. Register your sender service
194
+ Then, register your sender service:
195
+
196
+ .. code-block :: yaml
162
197
163
198
services :
164
199
App\MessageSender\ImportantActionToEmailSender :
@@ -169,27 +204,35 @@ message as an email.
169
204
tags :
170
205
- message.sender
171
206
172
- 3. Route your important message to the sender
207
+ Finally, route your important message to the sender:
208
+
209
+ .. code-block :: yaml
173
210
174
211
framework :
175
212
message :
176
213
routing :
177
214
' App\Message\ImportantAction ' : [App\MessageSender\ImportantActionToEmailSender, ~]
178
215
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.
181
220
182
221
Your own receiver
183
222
-----------------
184
223
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.
186
226
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.
189
231
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.
191
234
192
- 1. Create your receiver
235
+ First, create your receiver::
193
236
194
237
namespace App\MessageReceiver;
195
238
@@ -219,7 +262,9 @@ You will read this CSV file and dispatch a `NewOrder` message. All you need to d
219
262
}
220
263
}
221
264
222
- 2. Register your receiver service
265
+ Then, register your receiver service:
266
+
267
+ .. code-block :: yaml
223
268
224
269
services :
225
270
App\MessageReceiver\NewOrdersFromCsvFile :
@@ -230,6 +275,10 @@ You will read this CSV file and dispatch a `NewOrder` message. All you need to d
230
275
tags :
231
276
- message.receiver
232
277
233
- 3. Use your consumer
278
+ Finally, use your consumer:
279
+
280
+ .. code-block :: terminal
234
281
235
282
$ bin/console message:consume App\MessageReceived\NewOrdersFromCsvFile
283
+
284
+ .. _`PHP Enqueue bridge` : https://github.com/sroze/enqueue-bridge
0 commit comments