Last active
September 11, 2019 22:36
-
-
Save mareksuscak/33d1ff7968fa594a2d724824d5912b6f to your computer and use it in GitHub Desktop.
Symfony 4.4 Messenger - Different middleware on the consumer and producer side
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Messenger\Middleware; | |
use Symfony\Component\Messenger\Middleware\MiddlewareInterface; | |
use Symfony\Component\Messenger\Middleware\StackInterface; | |
use Symfony\Component\Messenger\Envelope; | |
use Symfony\Component\Messenger\Stamp\BusNameStamp; | |
# This is sort of a workaround, read more here: | |
# https://github.com/symfony/symfony/issues/32436#issuecomment-530565499 | |
final class AddConsumerBusNameStampMiddleware implements MiddlewareInterface | |
{ | |
public function handle(Envelope $envelope, StackInterface $stack): Envelope | |
{ | |
$envelope = $envelope->with(new BusNameStamp('consumer.bus')); | |
return $stack->next()->handle($envelope, $stack); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
framework: | |
messenger: | |
# ... | |
default_bus: command.bus | |
buses: | |
command.bus: | |
# Will be run in the producer context (message sender) | |
middleware: | |
# Override the bus name that should receive and handle messages (bus name: consumer.bus) | |
- App\Messenger\Middleware\AddConsumerBusNameStampMiddleware | |
consumer.bus: | |
# Will be run in the consumer context (message receiver -> worker) | |
middleware: | |
- doctrine_ping_connection | |
- doctrine_transaction |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment