Skip to content

[Messenger] Consume a queue from fanout exchange #38630

Closed
@redayoub

Description

@redayoub

Description
I have a microservice 1 from where I need to dispatch a command and then consume it from different microservices 2, 3, ... using different handler logic. The most convinient way to achive this is to use a rabbitmq exchange of type fanout that will dispatch the message to every queue linked to that exchange. This is my config for this:

framework:
    messenger:
        default_bus: command.bus
        buses:
            command.bus: ~
            query.bus: ~
            event.bus:
                default_middleware: allow_no_handlers

        failure_transport: failed

        transports:
            ms:
                dsn: '%env(MESSENGER_TRANSPORT_DSN)%'
                options:
                    exchange:
                        name: ms
                    queues:
                        ms1: ~
                        ms2: ~
                        ms3: ~                        
            failed: '%env(MESSENGER_TRANSPORT_DSN)%?queue_name=failed'
            # sync: 'sync://'

        routing:
            'App\Message\Command\PublishMessageCommand': ms

The problem is that symfony messenger only allow us to consume from a transport using this command:

php bin/console messenger:consume ms

Running this will consume all messages in the queues (ms1, ms2 and ms3). It there a way to consume from a specific queue inside an exchange using messenger because I know this is possible in other languages like nodejs.

The only solution for this is to create a transport for every microservice using this config:

framework:
    messenger:
        default_bus: command.bus
        buses:
            command.bus: ~
            query.bus: ~
            event.bus:
                default_middleware: allow_no_handlers

        failure_transport: failed

        transports:
            ms1: '%env(MESSENGER_TRANSPORT_DSN)%?queue_name=ms1'                      
            ms2: '%env(MESSENGER_TRANSPORT_DSN)%?queue_name=ms2'                      
            ms3: '%env(MESSENGER_TRANSPORT_DSN)%?queue_name=ms3'                      
            failed: '%env(MESSENGER_TRANSPORT_DSN)%?queue_name=failed'
            # sync: 'sync://'

        routing:
            'App\Message\Command\PublishMessageCommand': [ms1, ms2, ms3]

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions