10000 minor #10664 Enhance messenger doc (ogizanagi) · symfony/symfony-docs@2191d35 · GitHub
[go: up one dir, main page]

Skip to content

Commit 2191d35

Browse files
committed
minor #10664 Enhance messenger doc (ogizanagi)
This PR was squashed before being merged into the 4.1 branch (closes #10664). Discussion ---------- Enhance messenger doc Backporting some changes made in #10644 + some new changes. Commits ------- a3691a1 Enhance messenger doc
2 parents 7c8641e + a3691a1 commit 2191d35

File tree

2 files changed

+86
-55
lines changed

2 files changed

+86
-55
lines changed

components/messenger.rst

Lines changed: 39 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,33 @@ Concepts
3838
something can be a message broker or a third party API for example.
3939

4040
**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.
4343

4444
**Handler**:
4545
Responsible for handling messages using the business logic applicable to the messages.
46+
Handlers are called by the ``HandleMessageMiddleware`` middleware.
47+
48+
**Middleware**:
49+
Middleware can access the message and its wrapper (the envelope) while it is
50+
dispatched through the bus.
51+
Literally *"the software in the middle"*, those are not about core concerns
52+
(business logic) of an application. Instead, they are cross cutting concerns
53+
applicable throughout the application and affecting the entire message bus.
54+
For instance: logging, validating a message, starting a transaction, ...
55+
They are also responsible for calling the next middleware in the chain,
56+
which means they can tweak the envelope, by adding items to it or even
57+
replacing it, as well as interrupt the middleware chain.
58+
59+
**Envelope**
60+
Messenger specific concept, it gives full flexibility inside the message bus,
61+
by wrapping the messages into it, allowing to add useful information inside
62+
through *envelope items*.
63+
64+
**Envelope Items**
65+
Piece of information you need to attach to your message: serializer context
66+
to use for transport, markers identifying a received message or any sort of
67+
metadata your middleware or transport layer may use.
4668

4769
Bus
4870
---
@@ -53,9 +75,9 @@ middleware stack. The component comes with a set of middleware that you can use.
5375
When using the message bus with Symfony's FrameworkBundle, the following middleware
5476
are configured for you:
5577

56-
#. ``LoggingMiddleware`` (logs the processing of your messages)
57-
#. ``SendMessageMiddleware`` (enables asynchronous processing)
58-
#. ``HandleMessageMiddleware`` (calls the registered handle)
78+
#. :class:`Symfony\\Component\\Messenger\\Middleware\\LoggingMiddleware` (logs the processing of your messages)
79+
#. :class:`Symfony\\Component\\Messenger\\Asynchronous\\Middleware\\SendMessageMiddleware` (enables asynchronous processing)
80+
#. :class:`Symfony\\Component\\Messenger\\Middleware\\HandleMessageMiddleware` (calls the registered handler(s))
5981

6082
Example::
6183

@@ -74,7 +96,7 @@ Example::
7496

7597
.. note::
7698

77-
Every middleware needs to implement the ``MiddlewareInterface``.
99+
Every middleware needs to implement the :class:`Symfony\\Component\\Messenger\\Middleware\\MiddlewareInterface`.
78100

79101
Handlers
80102
--------
@@ -112,7 +134,7 @@ the ``SerializerConfiguration`` envelope::
112134
]))
113135
);
114136

115-
At the moment, the Symfony Messenger has the following built-in envelopes:
137+
At the moment, the Symfony Messenger has the following built-in envelope items:
116138

117139
#. :class:`Symfony\\Component\\Messenger\\Transport\\Serialization\\SerializerConfiguration`,
118140
to configure the serialization groups used by the transport.
@@ -151,6 +173,12 @@ The above example will forward the message to the next middleware with an additi
151173
envelope item *if* the message has just been received (i.e. has the `ReceivedMessage` item).
152174
You can create your own items by implementing :class:`Symfony\\Component\\Messenger\\EnvelopeAwareInterface`.
153175

176+
.. note::
177+
178+
Any envelope item must be php serializable if going through transport using
179+
the :class:`Symfony\\Component\\Messenger\\Transport\\Serialization\\Serializer`
180+
base serializer.
181+
154182
Transports
155183
----------
156184

@@ -160,7 +188,8 @@ transport will be responsible for communicating with your message broker or 3rd
160188
Your own Sender
161189
~~~~~~~~~~~~~~~
162190

163-
Using the ``SenderInterface``, you can create your own message sender.
191+
Using the :class:`Symfony\\Component\\Messenger\\Transport\\SenderInterface`,
192+
you can create your own message sender.
164193
Imagine that you already have an ``ImportantAction`` message going through the
165194
message bus and being handled by a handler. Now, you also want to send this
166195
message as an email.
@@ -206,7 +235,7 @@ First, create your sender::
206235
Your own Receiver
207236
~~~~~~~~~~~~~~~~~
208237

209-
A receiver is responsible for receiving messages from a source and dispatching
238+
A receiver is responsible for getting messages from a source and dispatching
210239
them to the application.
211240

212241
Imagine you already processed some "orders" in your application using a
@@ -226,7 +255,7 @@ First, create your receiver::
226255
use Symfony\Component\Serializer\SerializerInterface;
227256
use Symfony\Component\Messenger\Envelope;
228257

229-
class NewOrdersFromCsvFile implements ReceiverInterface
258+
class NewOrdersFromCsvFileReceiver implements ReceiverInterface
230259
{
231260
private $serializer;
232261
private $filePath;

messenger.rst

Lines changed: 47 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,9 @@ message handler. It's a class with an ``__invoke`` method::
8484

8585
Message handlers must be registered as services and :doc:`tagged </service_container/tags>`
8686
with the ``messenger.message_handler`` tag. If you're using the
87-
:ref:`default services.yaml configuration <service-container-services-load-example>`,
87+
:ref:`default services.yaml configuration <service-container-services-load-example>` and implement
88+
:class:`Symfony\\Component\\Messenger\\Handler\\MessageHandlerInterface`
89+
or :class:`Symfony\\Component\\Messenger\\Handler\\MessageSubscriberInterface`,
8890
this is already done for you, thanks to :ref:`autoconfiguration <services-autoconfigure>`.
8991

9092
If you're not using service autoconfiguration, then you need to add this config:
@@ -543,11 +545,16 @@ for each bus looks like this:
543545
#. ``call_message_handler`` middleware. Will call the message handler(s) for the
544546
given message.
545547

546-
Adding your own Middleware
547-
~~~~~~~~~~~~~~~~~~~~~~~~~~
548+
.. note::
548549

549-
As described in the component documentation, you can add your own middleware
550-
within the buses to add some extra capabilities like this:
550+
These middleware names are actually shortcuts working by convention.
551+
The real service ids are prefixed with the ``messenger.middleware.`` namespace.
552+
553+
Disabling default Middleware
554+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
555+
556+
If you don't want the default collection of middleware to be present on your bus,
557+
you can disable them like this:
551558

552559
.. configuration-block::
553560

@@ -558,9 +565,7 @@ within the buses to add some extra capabilities like this:
558565
messenger:
559566
buses:
560567
messenger.bus.default:
561-
middleware:
562-
- 'App\Middleware\MyMiddleware'
563-
- 'App\Middleware\AnotherMiddleware'
568+
default_middleware: false
564569
565570
.. code-block:: xml
566571
@@ -576,10 +581,7 @@ within the buses to add some extra capabilities like this:
576581
577582
<framework:config>
578583
<framework:messenger>
579-
<framework:bus name="messenger.bus.default">
580-
<framework:middleware id="App\Middleware\MyMiddleware E377 " />
581-
<framework:middleware id="App\Middleware\AnotherMiddleware" />
582-
</framework:bus>
584+
<framework:bus name="messenger.bus.default" default-middleware="false" />
583585
</framework:messenger>
584586
</framework:config>
585587
</container>
@@ -591,23 +593,17 @@ within the buses to add some extra capabilities like this:
591593
'messenger' => array(
592594
'buses' => array(
593595
'messenger.bus.default' => array(
594-
'middleware' => array(
595-
'App\Middleware\MyMiddleware',
596-
'App\Middleware\AnotherMiddleware',
597-
),
596+
'default_middleware' => false,
598597
),
599598
),
600599
),
601600
));
602601
603-
Note that if the service is abstract, a different instance of service will be
604-
created per bus.
605-
606-
Disabling default Middleware
607-
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
602+
Adding your own Middleware
603+
~~~~~~~~~~~~~~~~~~~~~~~~~~
608604

609-
If you don't want the default collection of middleware to be present on your bus,
610-
you can disable them like this:
605+
As described in the component documentation, you can add your own middleware
606+
within the buses to add some extra capabilities like this:
611607

612608
.. configuration-block::
613609

@@ -618,7 +614,9 @@ you can disable them like this:
618614
messenger:
619615
buses:
620616
messenger.bus.default:
621-
default_middleware: false
617+
middleware:
618+
- 'App\Middleware\MyMiddleware'
619+
- 'App\Middleware\AnotherMiddleware'
622620
623621
.. code-block:: xml
624622
@@ -634,7 +632,10 @@ you can disable them like this:
634632
635633
<framework:config>
636634
<framework:messenger>
637-
<framework:bus name="messenger.bus.default" default-middleware="false" />
635+
<framework:bus name="messenger.bus.default">
636+
<framework:middleware id="App\Middleware\MyMiddleware" />
637+
<framework:middleware id="App\Middleware\AnotherMiddleware" />
638+
</framework:bus>
638639
</framework:messenger>
639640
</framework:config>
640641
</container>
@@ -646,12 +647,18 @@ you can disable them like this:
646647
'messenger' => array(
647648
'buses' => array(
648649
'messenger.bus.default' => array(
649-
'default_middleware' => false,
650+
'middleware' => array(
651+
'App\Middleware\MyMiddleware',
652+
'App\Middleware\AnotherMiddleware',
653+
),
650654
),
651655
),
652656
),
653657
));
654658
659+
Note that if the service is abstract, a different instance of the service will
660+
be created per bus.
661+
655662
Using Middleware Factories
656663
~~~~~~~~~~~~~~~~~~~~~~~~~~
657664

@@ -674,13 +681,13 @@ factories. Defining such requires a two-step configuration based on Symfony's
674681
675682
# Step 2: an abstract definition that will call the factory with default
676683
# arguments or the ones provided in the middleware config
677-
messenger.middleware.doctrine_transaction_middleware:
684+
messenger.middleware.doctrine_transaction:
678685
class: Symfony\Bridge\Doctrine\Messenger\DoctrineTransactionMiddleware
679686
factory: 'doctrine.orm.messenger.middleware_factory.transaction:createMiddleware'
680687
abstract: true
681688
# the default arguments to use when none provided from config. Example:
682689
# middleware:
683-
# - doctrine_transaction_middleware: ~
690+
# - doctrine_transaction: ~
684691
arguments: ['default']
685692
686693
.. code-block:: xml
@@ -703,7 +710,7 @@ factories. Defining such requires a two-step configuration based on Symfony's
703710
704711
<!-- Step 2: an abstract definition that will call the factory with default
705712
arguments or the ones provided in the middleware config -->
706-
<service id="messenger.middleware.doctrine_transaction_middleware"
713+
<service id="messenger.middleware.doctrine_transaction"
707714
class="Symfony\Bridge\Doctrine\Messenger\DoctrineTransactionMiddleware"
708715
abstract="true">
709716
@@ -729,7 +736,7 @@ factories. Defining such requires a two-step configuration based on Symfony's
729736
730737
// Step 2: an abstract definition that will call the factory with default
731738
// arguments or the ones provided in the middleware config
732-
$container->register('messenger.middleware.doctrine_transaction_middleware', DoctrineTransactionMiddleware::class)
739+
$container->register('messenger.middleware.doctrine_transaction', DoctrineTransactionMiddleware::class)
733740
->setFactory(array(
734741
new Reference('doctrine.orm.messenger.middleware_factory.transaction'),
735742
'createMiddleware'
@@ -742,7 +749,7 @@ which is the argument expected by the
742749
``Symfony\Bridge\Doctrine\Messenger\DoctrineTransactionMiddlewareFactory::createMiddleware`` method.
743750

744751
Then you can reference and configure the
745-
``messenger.middleware.doctrine_transaction_middleware`` service as a middleware:
752+
``messenger.middleware.doctrine_transaction`` service as a middleware:
746753

747754
.. configuration-block::
748755

@@ -755,9 +762,9 @@ Then you can reference and configure the
755762
command_bus:
756763
middleware:
757764
# Using defaults
758-
- doctrine_transaction_middleware
765+
- doctrine_transaction
759766
# Using another entity manager
760-
- doctrine_transaction_middleware: ['custom']
767+
- doctrine_transaction: ['custom']
761768
762769
.. code-block:: xml
763770
@@ -775,9 +782,9 @@ Then you can reference and configure the
775782
<framework:messenger>
776783
<framework:bus name="command_bus">
777784
<!-- Using defaults -->
778-
<framework:middleware id="doctrine_transaction_middleware" />
785+
<framework:middleware id="doctrine_transaction" />
779786
<!-- Using another entity manager -->
780-
<framework:middleware id="doctrine_transaction_middleware">
787+
<framework:middleware id="doctrine_transaction">
781788
<framework:argument>custom</framework:argument>
782789
</framework:middleware>
783790
</framework:bus>
@@ -794,20 +801,15 @@ Then you can reference and configure the
794801
'command_bus' => array(
795802
'middleware' => array(
796803
// Using defaults
797-
'doctrine_transaction_middleware',
804+
'doctrine_transaction',
798805
// Using another entity manager
799-
array('id' => 'doctrine_transaction_middleware', 'arguments' => array('custom')),
806+
array('id' => 'doctrine_transaction', 'arguments' => array('custom')),
800807
),
801808
),
802809
),
803810
),
804811
));
805812
806-
.. note::
807-
808-
The ``doctrine_transaction_middleware`` shortcut is a convention. The real
809-
service id is prefixed with the ``messenger.middleware.`` namespace.
810-
811813
.. note::
812814

813815
Middleware factories only allow scalar and array arguments in config (no
@@ -816,9 +818,9 @@ Then you can reference and configure the
816818

817819
.. tip::
818820

819-
The ``doctrine_transaction_middleware`` is a built-in middleware wired
820-
automatically when the DoctrineBundle and the Messenger component are
821-
installed and enabled.
821+
The ``messenger.middleware.doctrine_transaction`` middleware is a built-in
822+
middleware wired automatically when the DoctrineBundle and the Messenger
823+
component are installed and enabled.
822824

823825
Your own Transport
824826
------------------

0 commit comments

Comments
 (0)
0