8000 Symfony Messenger component documentation by sroze · Pull Request #9437 · symfony/symfony-docs · GitHub
[go: up one dir, main page]

Skip to content

Symfony Messenger component documentation #9437

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 15 commits into from
Apr 16, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Update based on comments
  • Loading branch information
sroze committed Mar 29, 2018
commit 3fb973c2612914e0dafcf2f1db16861d502aa3ae
18 changes: 9 additions & 9 deletions components/messenger.rst
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,14 @@ Bus
---

The bus is used to dispatch messages. The behaviour of the bus is in its ordered
middleware stack. The component comes with a set of middlewares that you can use.
middleware stack. The component comes with a set of middleware that you can use.

When using the message bus with Symfony's FrameworkBundle, the following middlewares
When using the message bus with Symfony's FrameworkBundle, the following middleware
are configured for you:

#. :code:`LoggingMiddleware` (logs the processing of your messages)
#. :code:`SendMessageMiddleware` (enables asynchronous processing)
#. :code:`HandleMessageMiddleware` (calls the registered handle)
#. ``LoggingMiddleware`` (logs the processing of your messages)
#. ``SendMessageMiddleware`` (enables asynchronous processing)
#. ``HandleMessageMiddleware`` (calls the registered handle)

Example::

Expand All @@ -65,7 +65,7 @@ Example::

.. note:

Every middleware need to implement the :code:`MiddlewareInterface` interface.
Every middleware need to implement the ``MiddlewareInterface`` interface.
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

need -> needs
or
need to -> must


Handlers
--------
Expand Down Expand Up @@ -183,7 +183,7 @@ Same bus received and sender
~~~~~~~~~~~~~~~~~~~~~~~~~~~~

To allow us to receive and send messages on the same bus and prevent an infinite
loop, the message bus is equipped with the :code:`WrapIntoReceivedMessage` middleware.
It will wrap the received messages into :code:`ReceivedMessage` objects and the
:code:`SendMessageMiddleware` middleware will know it should not route these
loop, the message bus is equipped with the ``WrapIntoReceivedMessage`` middleware.
It will wrap the received messages into ``ReceivedMessage`` objects and the
``SendMessageMiddleware`` middleware will know it should not route these
messages again to an adapter.
43 changes: 21 additions & 22 deletions messenger.rst
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ install messenger before using it:
Using the Messenger Service
---------------------------

Once enabled, the :code:`message_bus` service can be injected in any service where
Once enabled, the ``message_bus`` service can be injected in any service where
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will the following be enough to enable it?

# config/packages/framework.yml
framework:
    messenger: ~

And what exactly is being enabled here - is it the message_bus service (as the sentence seems to indicate) or is it the Messenger Component?

you need it, like in a controller::

// src/Controller/DefaultController.php
Expand All @@ -33,9 +33,9 @@ you need it, like in a controller::

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add something like use App\Message\SendNotification; to make clear that SendNotification is not provided by the messenger component.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added 👍

class DefaultController extends Controller
{
public function indexAction(MessageBusInterface $bus)
public function index(MessageBusInterface $bus)
{
$bus->dispatch(new MyMessage());
$bus->dispatch(new SendNotification('A string to be sent...'));
}
}

Expand Down Expand Up @@ -97,21 +97,21 @@ the messenger component, the following configuration should have been created:

# .env
###> symfony/messenger ###
AMQP_DSN=amqp://guest:guest@localhost:5672/%2f/messages
MESSENGER_DSN=amqp://guest:guest@localhost:5672/%2f/messages
###< symfony/messenger ###

This is enough to allow you to route your message to the :code:`messenger.default_adapter`
This is enough to allow you to route your message to the ``messenger.default_adapter``
adapter. This will also configure the following for you:

1. A :code:`messenger.default_sender` sender to be used when routing messages
2. A :code:`messenger.default_receiver` receiver to be used when consuming messages.
1. A ``messenger.default_sender`` sender to be used when routing messages
2. A ``messenger.default_receiver`` receiver to be used when consuming messages.
Copy link
@boite boite Apr 12, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These should be messenger.sender.default and messenger.receiver.default (there are numerous occurrences that need to be changed).


Routing
-------

Instead of calling a handler, you have the option to route your message(s) to a
sender. Part of an adapter, it is responsible of sending your message somewhere.
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

of sending -> for sending

You can configuration which message is routed to which sender with the following
You can configure which message is routed to which sender with the following
configuration:

.. code-block:: yaml
Expand All @@ -121,9 +121,8 @@ configuration:
routing:
'My\Message\Message': messenger.default_sender # Or another sender service name

Such configuration would only route the ``MessageAboutDoingOperationalWork``
message to be asynchronous, the rest of the messages would still be directly
handled.
Such configuration would only route the ``My\Message\Message`` message to be
asynchronous, the rest of the messages would still be directly handled.
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more. 67E6

Instead of two sentences, one before the config example and one after, how about just one:-

"The following configuration shows how you can route a class of messages to a sender, leaving other classes of messages to be passed to their respective handlers."

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, routing to messenger.default_sender seems to be wrong:-

In CheckExceptionOnInvalidReferenceBehaviorPass.php line 32:
                                                                               
  [Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException]   
  The service "messenger.asynchronous.middleware.send_message_to_producer" ha  
  s a dependency on a non-existent service "messenger.default_sender".

It should be messenger.sender.default.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's less clear with a single example IMHO. I've updated the sender configuration 👍


If you want to do route all the messages to a queue by default, you can use such
configuration:
Expand All @@ -146,7 +145,7 @@ Note that you can also route a message to multiple senders at the same time:
'My\Message\ToBeSentToTwoSenders': [messenger.default_sender, messenger.audit_sender]

Last but not least you can also route a message while still calling the handler
on your application by having a :code:`null` sender:
on your application by having a ``null`` sender:
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"By specifying a null sender, you can also route a class of messages to a sender while still having them passed to their respective handler:"


.. code-block:: yaml

Expand All @@ -159,23 +158,23 @@ Consuming messages
------------------

Once your messages have been routed, you will like to consume your messages in most
of the cases. Do to so, you can use the :code:`messenger:consume-messages` command
of the cases. Do to so, you can use the ``messenger:consume-messages`` command
like this:
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"Usually you will want to consume your messages once they have been routed. One way to do this is to use the ..."

I'm assuming that there are other ways to consume routed messages.


.. code-block:: terminal

$ bin/console messenger:consume-messages messenger.default_receiver

The first argument is the receiver's service name. It might have been created by
your :code:`adapters` configuration or it can be your own receiver.
your ``adapters`` configuration or it can be your own receiver.

Registering your middlewares
----------------------------
Registering your middleware
---------------------------

The message bus is based on middlewares. If you are un-familiar with the concept,
The message bus is based on a set of middleware. If you are un-familiar with the concept,
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

un-familiar -> unfamiliar

look at the :doc:`Messenger component docs </components/messenger>`.

To register your middleware, use the :code:`messenger.middleware` tag as in the
To register your middleware, use the ``messenger.middleware`` tag as in the
following example:

.. code-block:: xml
Expand Down Expand Up @@ -213,7 +212,7 @@ DSN. You will need an adapter factory::
}
}

The :code:`YourAdaper` class need to implements the :code:`AdapterInterface`. It
The ``YourAdaper`` class need to implement the ``AdapterInterface``. It
will like the following example::
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

need -> needs

or

need to -> must

and

It will be like

or

It will look like


use Symfony\Component\Messenger\Adapter\Factory\AdapterInterface;
Expand Down Expand Up @@ -245,7 +244,7 @@ Register your factory
Use your adapter
~~~~~~~~~~~~~~~~

Within the :code:`framework.messenger.adapters.*` configuration, create your
Within the ``framework.messenger.adapters.*`` configuration, create your
named adapter using your own DSN:

.. code-block:: yaml
Expand All @@ -257,8 +256,8 @@ named adapter using your own DSN:

This will give you access to the following services:

1. :code:`messenger.yours_adapter`: the instance of your adapter.
2. :code:`messenger.yours_receiver` and :code:`messenger.yours_sender`, the
1. ``messenger.yours_adapter``: the instance of your adapter.
2. ``messenger.yours_receiver`` and ``messenger.yours_sender``, the
receiver and sender created by the adapter.

.. _`enqueue's adapter`: https://github.com/sroze/enqueue-bridge
0