creatortsv/symfony-messenger-outbox-pattern

Outbox pattern for the symfony-messenger component

v1.1.0 2023-12-12 20:49 UTC

This package is auto-updated.

Last update: 2024-11-12 23:06:14 UTC


README

CI

This package is an extension for the symfony/messenger component that implements the Transactional outbox pattern. It provides the special middleware that navigates your original message to the message broker through the outbox transport

Requirements

Installation

Install with composer

composer require creatortsv/symfony-messenger-outbox-pattern

Configuration

This guide shows how the symfony messenger component should be configured

Configure transports

# config/packages/messenger.yaml

framework:
    messenger:
      
        transports:
        ### Your default async message transport for any purpose
            async: '%env(resolve:MESSENGER_TRANSPORT_DNS)%'
        ### Outbox transport, for example:
        ### outbox: '%env(resolve:MESSENGER_TRANSPORT_DNS)%'
        ### outbox: 'doctrine://%env(resolve:DATABASE_URL)%'
        ### outbox: 'doctrine://default?table_name=outbox&queue_name=custom'
            outbox: 'doctrine://default'
        ### Advance stored outbox transport
            stored: 

    ### routing: ...

Configure message bus

# config/packages/messenger.yaml

framework:
    messenger:
        buses:
        ### Configure message bus that will be used with middleware from this package
            event.bus:
            ### Default middlewares must be enabled
                default_middleware:
                    enabled: true
                    allow_no_handlers: true
          
                middleware:
                ### Add the middleware with configured outbox transport name or/and advanced names
                ### - Creatortsv\Messenger\Outbox\Middleware\SwitchToOutboxMiddleware: [ outbox, store, logs ]
                    - Creatortsv\Messenger\Outbox\Middleware\SwitchToOutboxMiddleware: [ outbox ]
    
    ### transports: ... Outbox transport configuration

    ### routing: ...

Usage

readonly class UserService
{
    public function __construct(
        private EntityManagerInterface $entityManager,
        private MessageBusInterface $eventBus,
    ) {
        // ...
    }
    
    public function register(User $user): void
    {
        $this->entityManager->wrapInTransaction(
            function () use ($user): void {
            /** Persist user in DB ... */
            
                $this->entityManager->flush();
                $this->eventBus->dispatch(new UserRegistered($user->id));
            },
        );
    }
}

Run outbox consumer

Run consumer with outbox transport name

php bin/console messenger:consume [name]

Each message which is consumed by the worker will be automatically sent to the original transport, thanks to the SwitchToOutboxMiddleware class