How to configure Symfony Messenger to work with topic exchange?

Viewed 1247

We have a project that partially uses DDD. When changing / creating an entity, we throw an event inside the entity. Next, the Doctrine Event Subscriber takes all the events and forwards them to the EventBus.

Let's say we have a MissionCreatedEvent event. I want to make routing key mission.event.created for it and tie it into 2 queues: new_mission_notification_to_part_time_employee and new_mission_area_notification_to_manager.

This is what the config looks like:

transports:
    async_new_mission_notification_to_part_time_employee:
        dsn: '%env(MESSENGER_TRANSPORT_DSN)%'
        options:
            exchange:
                name: mission
                type: topic
            queues:
                new_mission_notification_to_part_time_employee:
                    binding_keys: ['mission.event.created']
        retry_strategy:
            max_retries: 0

    async_new_mission_area_notification_to_manager:
        dsn: '%env(MESSENGER_TRANSPORT_DSN)%'
        options:
            exchange:
                name: mission
                type: topic
            queues:
                new_mission_area_notification_to_manager:
                    binding_keys: ['mission.event.created']
        retry_strategy:
            max_retries: 0

routing:
    'App\Domain\Mission\Event\MissionCreatedEvent':
        - async_new_mission_notification_to_part_time_employee
        - async_new_mission_area_notification_to_manager

The problem is that Symfony Messenger first runs on transports and only then pushes the message into exchange. Since we have 2 transports, it turns out that the event with routing key mission.event.created is triggered twice. Because of this, my Message handlers run 2 times.

There is an option not to use routing key mission.event.created. But use similar to mission.cmd.notify_part_time_employee_about_new_mission and mission.cmd.notify_manager_about_new_mission_in_area for each transport with direct exchange.

This will help solve duplication problems, but I can’t use all the features that RabbitMQ gives me :(

0 Answers
Related