Bridge with symfony/messenger
Refer to the official documentation on Symfony’s website.
This bridge provides a way launch job using message bus.
Along with ways to interact with messenger inside jobs.
Launch Job through Messenger dispatcher
The DispatchMessageJobLauncher execute jobs via a symfony command message dispatch.
A LaunchJobMessage message will be dispatched and handled by the LaunchJobMessageHandler will be called with that message after being routed.
How to configure an async transport for the launcher?
You first need to configure an async transport, like explained in Symfony’s doc.
Then you will have to route the message to this async transport you created, like explained in Symfony’s doc.
You will end with something like:
# config/packages/messenger.yaml
framework:
messenger:
transports:
async: "%env(MESSENGER_TRANSPORT_DSN)%"
routing:
'Yokai\Batch\Bridge\Symfony\Messenger\LaunchJobMessage': async
How to configure different transport for your jobs?
On some projects, you will end with different messenger transports, and will not want to run all jobs on the same transport.
Because we are using the same message class for all jobs, there is no way to configure this in Symfony.
Instead, you will have to configure a job name to transport routing, in our side of the configuration.
It is very likely to the messenger routing configuration, but you will use the job name instead of the message class.
# config/packages/yokai_batch.yaml
yokai_batch:
launchers:
messenger:
routing:
export_job_name: async_with_low_priority
import_job_name: async_with_high_priority
Dispatch item with messenger writer
This item writer will dispatches each item through Symfony’s messenger bus.
Items must be objects that messenger can handle.
<?php
declare(strict_types=1);
use Symfony\Component\Messenger\MessageBusInterface;
use Yokai\Batch\Bridge\Symfony\Messenger\Writer\DispatchEachItemAsMessageWriter;
/** @var MessageBusInterface $messageBus */
new DispatchEachItemAsMessageWriter(
messageBus: $messageBus,
);
See also