New in Symfony 5.4: Messenger Improvements (Symfony Blog)


Symfony 5.4
is backed by Private Packagist. Private Packagist is a fast, reliable, and secure Composer repository for your private packages. It mirrors all your open-source dependencies for better availability and monitors them for security vulnerabilities.

PHP attributes are a great way of adding metadata to PHP code. In Symfony
we’re adding the option of using PHP attributes to configure most things. That’s
why in Symfony 5.4 we’re allowing to configure message handlers with attributes.

Instead of having to implement the MessageHandlerInterface, you can now add
the AsMessageHandler attribute to any PHP class and use it as a message handler:

Currently there’s no simple way to get worker metadata such as its transport
name. In Symfony 5.4 we’re improving this with the introduction of a new
WorkerMetadata class which is accessible via $worker->getWorkerMetadata().
For example, inside a method of some listener/subscriber that handles
Symfony Messenger events you could use something like this:

Container services are not reset automatically when handling messages. This can
be a problem for example with the Monolog fingers crossed handler. Since
services are not reset, if the first message triggers an error, the next messages
will log and ultimately overflow the buffer.

In Symfony 5.4 we’re improving this situation with the option to automatically
reset services after handling a message. To use this feature, set the new
reset_on_message option to true in your messenger configuration:

Sometimes, when using the Messenger component, you could handle multiple
messages at once instead of processing them one by one. In Symfony 5.4 we’ve
introduced a new BatchHandlerInterface that allows your handlers to process
messages in batches.

Handlers implementing this interface should expect a new $ack optional
argument to be provided when __invoke() is called. If you don’t provide the
$ack argument, the message is handled synchronously as usual. If you provide
$ack, __invoke() is expected to buffer the message and its $ack function,
and to return the number of pending messages in the batch. Here is what a batch
handler might look like:

The size of the batch is controlled by BatchHandlerTrait::shouldProcess()
(defaults to 10).

Help the Symfony project!

As with any Open-Source project, contributing code or documentation is the most common way to help,
but we also have a wide range of sponsoring opportunities.



Source link

Latest articles

Related articles

Leave a reply

Please enter your comment!
Please enter your name here