@@ -38,8 +38,8 @@ Concepts
38
38
something can be a message broker or a third party API for example.
39
39
40
40
**Receiver **:
41
- Responsible for deserializing and forwarding messages to handler(s). This
42
- can be a message queue puller or an API endpoint for example.
41
+ Responsible for retrieving, deserializing and forwarding messages to handler(s).
42
+ This can be a message queue puller or an API endpoint for example.
43
43
44
44
**Handler **:
45
45
Responsible for handling messages using the business logic applicable to the messages.
@@ -55,7 +55,7 @@ are configured for you:
55
55
56
56
#. ``LoggingMiddleware `` (logs the processing of your messages)
57
57
#. ``SendMessageMiddleware `` (enables asynchronous processing)
58
- #. ``HandleMessageMiddleware `` (calls the registered handle )
58
+ #. ``HandleMessageMiddleware `` (calls the registered handler )
59
59
60
60
Example::
61
61
@@ -99,57 +99,54 @@ Adding Metadata to Messages (Envelopes)
99
99
---------------------------------------
100
100
101
101
If you need to add metadata or some configuration to a message, wrap it with the
102
- :class: `Symfony\\ Component\\ Messenger\\ Envelope ` class. For example, to set the
103
- serialization groups used when the message goes through the transport layer, use
104
- the `` SerializerConfiguration `` envelope ::
102
+ :class: `Symfony\\ Component\\ Messenger\\ Envelope ` class and add stamps.
103
+ For example, to set the serialization groups used when the message goes
104
+ through the transport layer, use the `` SerializerStamp `` stamp ::
105
105
106
106
use Symfony\Component\Messenger\Envelope;
107
- use Symfony\Component\Messenger\Transport\Serialization\SerializerConfiguration ;
107
+ use Symfony\Component\Messenger\Stamp\SerializerStamp ;
108
108
109
109
$bus->dispatch(
110
- (new Envelope($message))->with(new SerializerConfiguration ([
110
+ (new Envelope($message))->with(new SerializerStamp ([
111
111
'groups' => ['my_serialization_groups'],
112
112
]))
113
113
);
114
114
115
- At the moment, the Symfony Messenger has the following built-in envelopes :
115
+ At the moment, the Symfony Messenger has the following built-in envelope stamps :
116
116
117
- #. :class: `Symfony\\ Component\\ Messenger\\ Transport \\ Serialization \\ SerializerConfiguration `,
117
+ #. :class: `Symfony\\ Component\\ Messenger\\ Stamp \\ SerializerStamp `,
118
118
to configure the serialization groups used by the transport.
119
- #. :class: `Symfony\\ Component\\ Messenger\\ Middleware \\ Configuration \\ ValidationConfiguration `,
119
+ #. :class: `Symfony\\ Component\\ Messenger\\ Stamp \\ ValidationStamp `,
120
120
to configure the validation groups used when the validation middleware is enabled.
121
- #. :class: `Symfony\\ Component\\ Messenger\\ Asynchronous \\ Transport \\ ReceivedMessage `,
121
+ #. :class: `Symfony\\ Component\\ Messenger\\ Stamp \\ ReceivedStamp `,
122
122
an internal item that marks the message as received from a transport.
123
123
124
- Instead of dealing directly with the messages in the middleware you can receive the
125
- envelope by implementing the :class: `Symfony\\ Component\\ Messenger\\ EnvelopeAwareInterface `
126
- marker, like this::
124
+ Instead of dealing directly with the messages in the middleware you receive the envelope.
125
+ Hence you can inspect the envelope content and its stamps, or add any::
127
126
128
- use Symfony\Component\Messenger\Asynchronous\Transport\ReceivedMessage;
127
+ use App\Message\Stamp\AnotherStamp;
128
+ use Symfony\Component\Messenger\Stamp\ReceivedStamp;
129
129
use Symfony\Component\Messenger\Middleware\MiddlewareInterface;
130
- use Symfony\Component\Messenger\EnvelopeAwareInterface ;
130
+ use Symfony\Component\Messenger\Middleware\StackInterface ;
131
131
132
- class MyOwnMiddleware implements MiddlewareInterface, EnvelopeAwareInterface
132
+ class MyOwnMiddleware implements MiddlewareInterface
133
133
{
134
- public function handle($envelope, callable $next)
134
+ public function handle(Envelope $envelope, StackInterface $stack): Envelope
135
135
{
136
- // $envelope here is an `Envelope` object, because this middleware
137
- // implements the EnvelopeAwareInterface interface.
138
-
139
- if (null !== $envelope->get(ReceivedMessage::class)) {
136
+ if (null !== $envelope->get(ReceivedStamp::class)) {
140
137
// Message just has been received...
141
138
142
139
// You could for example add another item.
143
- $envelope = $envelope->with(new AnotherEnvelopeItem (/* ... */));
140
+ $envelope = $envelope->with(new AnotherStamp (/* ... */));
144
141
}
145
142
146
- return $next($envelope);
143
+ return $stack-> next()->handle( $envelope, $stack );
147
144
}
148
145
}
149
146
150
147
The above example will forward the message to the next middleware with an additional
151
- envelope item *if * the message has just been received (i.e. has the `ReceivedMessage ` item ).
152
- You can create your own items by implementing :class: `Symfony\\ Component\\ Messenger\\ EnvelopeAwareInterface `.
148
+ stamp *if * the message has just been received (i.e. has the `ReceivedStamp ` stamp ).
149
+ You can create your own items by implementing :class: `Symfony\\ Component\\ Messenger\\ Stamp \\ StampInterface `.
153
150
154
151
Transports
155
152
----------
@@ -170,7 +167,7 @@ First, create your sender::
170
167
namespace App\MessageSender;
171
168
172
169
use App\Message\ImportantAction;
173
- use Symfony\Component\Messenger\Transport\SenderInterface;
170
+ use Symfony\Component\Messenger\Transport\Sender\ SenderInterface;
174
171
use Symfony\Component\Messenger\Envelope;
175
172
176
173
class ImportantActionToEmailSender implements SenderInterface
@@ -184,7 +181,7 @@ First, create your sender::
184
181
$this->toEmail = $toEmail;
185
182
}
186
183
187
- public function send(Envelope $envelope)
184
+ public function send(Envelope $envelope): Envelope
188
185
{
189
186
$message = $envelope->getMessage();
190
187
@@ -200,13 +197,15 @@ First, create your sender::
200
197
'text/html'
201
198
)
202
199
);
200
+
201
+ return $envelope;
203
202
}
204
203
}
205
204
206
205
Your own Receiver
207
206
~~~~~~~~~~~~~~~~~
208
207
209
- A receiver is responsible for receiving messages from a source and dispatching
208
+ A receiver is responsible for getting messages from a source and dispatching
210
209
them to the application.
211
210
212
211
Imagine you already processed some "orders" in your application using a
@@ -222,11 +221,11 @@ First, create your receiver::
222
221
namespace App\MessageReceiver;
223
222
224
223
use App\Message\NewOrder;
225
- use Symfony\Component\Messenger\Transport\ReceiverInterface;
224
+ use Symfony\Component\Messenger\Transport\Receiver\ ReceiverInterface;
226
225
use Symfony\Component\Serializer\SerializerInterface;
227
226
use Symfony\Component\Messenger\Envelope;
228
227
229
- class NewOrdersFromCsvFile implements ReceiverInterface
228
+ class NewOrdersFromCsvFileReceiver implements ReceiverInterface
230
229
{
231
230
private $serializer;
232
231
private $filePath;
@@ -258,8 +257,8 @@ Receiver and Sender on the same Bus
258
257
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
259
258
260
259
To allow sending and receiving messages on the same bus and prevent an infinite
261
- loop, the message bus will add a :class: `Symfony\\ Component\\ Messenger\\ Asynchronous \\ Transport \\ ReceivedMessage `
262
- envelope item to the message envelopes and the :class: `Symfony\\ Component\\ Messenger\\ Asynchronous \\ Middleware\\ SendMessageMiddleware `
260
+ loop, the message bus will add a :class: `Symfony\\ Component\\ Messenger\\ Stamp \\ ReceivedStamp `
261
+ stamp to the message envelopes and the :class: `Symfony\\ Component\\ Messenger\\ Middleware\\ SendMessageMiddleware `
263
262
middleware will know it should not route these messages again to a transport.
264
263
265
264
.. _blog posts about command buses : https://matthiasnoback.nl/tags/command%20bus/
0 commit comments