Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

[Messenger] Fix documentation after 4.2 BC Breaks#10644

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
xabbuh merged 1 commit intosymfony:masterfromogizanagi:4.2/messenger_bc_breaks
Nov 11, 2018
Merged
Show file tree
Hide file tree
Changes fromall commits
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
67 changes: 33 additions & 34 deletionscomponents/messenger.rst
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -38,8 +38,8 @@ Concepts
something can be a message broker or a third party API for example.

**Receiver**:
Responsible for deserializing and forwarding messages to handler(s). This
can be a message queue puller or an API endpoint for example.
Responsible forretrieving,deserializing and forwarding messages to handler(s).
Thiscan be a message queue puller or an API endpoint for example.

**Handler**:
Responsible for handling messages using the business logic applicable to the messages.
Expand All@@ -55,7 +55,7 @@ are configured for you:

#. ``LoggingMiddleware`` (logs the processing of your messages)
#. ``SendMessageMiddleware`` (enables asynchronous processing)
#. ``HandleMessageMiddleware`` (calls the registeredhandle)
#. ``HandleMessageMiddleware`` (calls the registeredhandler)

Example::

Expand DownExpand Up@@ -99,57 +99,54 @@ Adding Metadata to Messages (Envelopes)
---------------------------------------

If you need to add metadata or some configuration to a message, wrap it with the
:class:`Symfony\\Component\\Messenger\\Envelope` class. For example, to set the
serialization groups used when the message goes through the transport layer, use
the``SerializerConfiguration``envelope::
:class:`Symfony\\Component\\Messenger\\Envelope` class and add stamps.
For example, to set theserialization groups used when the message goes
throughthetransport layer, use the ``SerializerStamp``stamp::

use Symfony\Component\Messenger\Envelope;
use Symfony\Component\Messenger\Transport\Serialization\SerializerConfiguration;
use Symfony\Component\Messenger\Stamp\SerializerStamp;

$bus->dispatch(
(new Envelope($message))->with(newSerializerConfiguration([
(new Envelope($message))->with(newSerializerStamp([
'groups' => ['my_serialization_groups'],
]))
);

At the moment, the Symfony Messenger has the following built-inenvelopes:
At the moment, the Symfony Messenger has the following built-inenvelope stamps:

#. :class:`Symfony\\Component\\Messenger\\Transport\\Serialization\\SerializerConfiguration`,
#. :class:`Symfony\\Component\\Messenger\\Stamp\\SerializerStamp`,
to configure the serialization groups used by the transport.
#. :class:`Symfony\\Component\\Messenger\\Middleware\\Configuration\\ValidationConfiguration`,
#. :class:`Symfony\\Component\\Messenger\\Stamp\\ValidationStamp`,
to configure the validation groups used when the validation middleware is enabled.
#. :class:`Symfony\\Component\\Messenger\\Asynchronous\\Transport\\ReceivedMessage`,
#. :class:`Symfony\\Component\\Messenger\\Stamp\\ReceivedStamp`,
an internal item that marks the message as received from a transport.

Instead of dealing directly with the messages in the middleware you can receive the
envelope by implementing the :class:`Symfony\\Component\\Messenger\\EnvelopeAwareInterface`
marker, like this::
Instead of dealing directly with the messages in the middleware you receive the envelope.
Hence you can inspect the envelope content and its stamps, or add any::

use Symfony\Component\Messenger\Asynchronous\Transport\ReceivedMessage;
use App\Message\Stamp\AnotherStamp;
use Symfony\Component\Messenger\Stamp\ReceivedStamp;
use Symfony\Component\Messenger\Middleware\MiddlewareInterface;
use Symfony\Component\Messenger\EnvelopeAwareInterface;
use Symfony\Component\Messenger\Middleware\StackInterface;

class MyOwnMiddleware implements MiddlewareInterface, EnvelopeAwareInterface
class MyOwnMiddleware implements MiddlewareInterface
{
public function handle($envelope,callable $next)
public function handle(Envelope$envelope,StackInterface $stack): Envelope
{
// $envelope here is an `Envelope` object, because this middleware
// implements the EnvelopeAwareInterface interface.

if (null !== $envelope->get(ReceivedMessage::class)) {
if (null !== $envelope->get(ReceivedStamp::class)) {
// Message just has been received...

// You could for example add another item.
$envelope = $envelope->with(newAnotherEnvelopeItem(/* ... */));
$envelope = $envelope->with(newAnotherStamp(/* ... */));
}

return $next($envelope);
return $stack->next()->handle($envelope, $stack);
}
}

The above example will forward the message to the next middleware with an additional
envelope item*if* the message has just been received (i.e. has the `ReceivedMessage` item).
You can create your own items by implementing :class:`Symfony\\Component\\Messenger\\EnvelopeAwareInterface`.
stamp*if* the message has just been received (i.e. has the `ReceivedStamp` stamp).
You can create your own items by implementing :class:`Symfony\\Component\\Messenger\\Stamp\\StampInterface`.

Transports
----------
Expand All@@ -170,7 +167,7 @@ First, create your sender::
namespace App\MessageSender;

use App\Message\ImportantAction;
use Symfony\Component\Messenger\Transport\SenderInterface;
use Symfony\Component\Messenger\Transport\Sender\SenderInterface;
use Symfony\Component\Messenger\Envelope;

class ImportantActionToEmailSender implements SenderInterface
Expand All@@ -184,7 +181,7 @@ First, create your sender::
$this->toEmail = $toEmail;
}

public function send(Envelope $envelope)
public function send(Envelope $envelope): Envelope
{
$message = $envelope->getMessage();

Expand All@@ -200,13 +197,15 @@ First, create your sender::
'text/html'
)
);

return $envelope;
}
}

Your own Receiver
~~~~~~~~~~~~~~~~~

A receiver is responsible forreceiving messages from a source and dispatching
A receiver is responsible forgetting messages from a source and dispatching
them to the application.

Imagine you already processed some "orders" in your application using a
Expand All@@ -222,11 +221,11 @@ First, create your receiver::
namespace App\MessageReceiver;

use App\Message\NewOrder;
use Symfony\Component\Messenger\Transport\ReceiverInterface;
use Symfony\Component\Messenger\Transport\Receiver\ReceiverInterface;
use Symfony\Component\Serializer\SerializerInterface;
use Symfony\Component\Messenger\Envelope;

classNewOrdersFromCsvFile implements ReceiverInterface
classNewOrdersFromCsvFileReceiver implements ReceiverInterface
{
private $serializer;
private $filePath;
Expand DownExpand Up@@ -258,8 +257,8 @@ Receiver and Sender on the same Bus
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

To allow sending and receiving messages on the same bus and prevent an infinite
loop, the message bus will add a :class:`Symfony\\Component\\Messenger\\Asynchronous\\Transport\\ReceivedMessage`
envelope itemto the message envelopes and the :class:`Symfony\\Component\\Messenger\\Asynchronous\\Middleware\\SendMessageMiddleware`
loop, the message bus will add a :class:`Symfony\\Component\\Messenger\\Stamp\\ReceivedStamp`
stampto the message envelopes and the :class:`Symfony\\Component\\Messenger\\Middleware\\SendMessageMiddleware`
middleware will know it should not route these messages again to a transport.

.. _blog posts about command buses: https://matthiasnoback.nl/tags/command%20bus/
Expand Down
Loading

[8]ページ先頭

©2009-2025 Movatter.jp