New in Symfony 4.4: Messenger Middleware to Clear Doctrine Entity Manager
October 15, 2019 • Published by Javier Eguiluz
Warning: This post is about an unsupported Symfony version. Some of this information may be out of date. Read the most recent Symfony Docs.
UPDATE: this feature was finally removed from Symfony before Symfony 4.4 release. Instead, DoctrineBundle now does the same thing as explained in this blog post but without you having to configure anything.
In the Messenger component, middleware is used to configure what happens when you dispatch a message to a message bus. In Symfony 4.4 we've added a new middleware to clear Doctrine's entity manager after each message is consumed.
Enable it by adding messenger.middleware.doctrine_clear_entity_manager
to
the middleware of your buses:
1 2 3 4 5 6 7 8 9
# config/packages/messenger.yaml
framework:
messenger:
buses:
messenger.bus.default:
default_middleware: false
middleware:
# ...
- 'doctrine_clear_entity_manager'
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
<!-- config/packages/messenger.xml -->
<?xml version="1.0" encoding="UTF-8" ?>
<container xmlns="http://symfony.com/schema/dic/services"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:framework="http://symfony.com/schema/dic/symfony"
xsi:schemaLocation="http://symfony.com/schema/dic/services
https://symfony.com/schema/dic/services/services-1.0.xsd
http://symfony.com/schema/dic/symfony
https://symfony.com/schema/dic/symfony/symfony-1.0.xsd">
<framework:config>
<framework:messenger>
<!-- ... -->
<framework:middleware id="doctrine_clear_entity_manager"/>
<framework:bus name="messenger.bus.default" default-middleware="false"/>
</framework:messenger>
</framework:config>
</container>
1 2 3 4 5 6 7 8 9 10 11 12 13 14
// config/packages/messenger.php
$container->loadFromExtension('framework', [
'messenger' => [
'buses' => [
'messenger.bus.default' => [
'middleware' => [
// ...
'doctrine_clear_entity_manager',
],
'default_middleware' => false,
],
],
],
]);
The first advantage of this middleware is that it reduces the memory consumption when handling messages in long-running processes. The second advantage is that it prevents unexpected side-effects. For example, in the case of an user account recovery process (where an email is sent asynchronously with Messenger and AMQP), if the email address is updated after the first try, the second email is sent to the old email address. Using this middleware will solve that problem.
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.
Comments are closed.
To ensure that comments stay relevant, they are closed for old posts.
Nice one!
If I am not mistaken one will be able to add it via a shorter
doctrine_clear_entity_manager
id.@Valentin you are right! I've fixed it. Thanks.
Please note that this blog post has been updated because this feature was finally removed from Symfony 4.4 and moved to DoctrineBundle.