johanv
johanv

Reputation: 1042

Restrict message handler to multiple buses with symfony messenger

As explained in the documentation of the Symfony messenger component, by default message handlers handle messages from all message buses. However, one can restrict a message handler to a specific bus like this:

# config/services.yaml
services:
    App\MessageHandler\SomeCommandHandler:
        tags: [{ name: messenger.message_handler, bus: command.bus }]
        # prevent handlers from being registered twice (or you can remove
        # the MessageHandlerInterface that autoconfigure uses to find handlers)
        autoconfigure: false

Now for a project I am working on, I have three buses, and I would like a handler to only handle messages on two of those buses.

Is anybody aware of a way to achieve this?

Upvotes: 0

Views: 1321

Answers (1)

virtualize
virtualize

Reputation: 2507

It's just a wild guess, but as tags is an array, could you try to add multiple tags?

# config/services.yaml
services:
    App\MessageHandler\SomeCommandHandler:
        tags: 
            - { name: messenger.message_handler, bus: command.bus }
            - { name: messenger.message_handler, bus: command.bus_2 }

Upvotes: 1

Related Questions