Example of using Yandex Message Queue with Symfony
The Symfony
Installing
You will need the following tools:
Install Symfony using this guide
Getting started
- Create a service account.
- Assign the editor role to the service account.
- Create a static access key.
Create a queue in Message Queue and copy its URL.
Step-by-step guide
In this example, you will create:
- Demo message that stores the source numbers.
- Message handler that sums up two numbers from the message.
- Command that adds a task to the Message Queue queue.
To use Message Queue with Symfony Messenger, follow these steps:
-
Create a test request named
mq_example
:symfony new --webapp mq_example
-
Install dependencies to work with Amazon SQS. Message Queue uses the SQS-compatible format:
composer require symfony/amazon-sqs-messenger async-aws/sqs ^1.9
-
Create a message and a handler:
php bin/console make:message Sum
While running, the command will ask
Which transport do you want to route your message to? [[no transport]]
.
Enter the number that stands for theasync
option. -
Create a command for delivering messages to a queue:
php bin/console make:command app:create
-
Open the
src/Command/SumCommand.php
file and change it as follows:<?php namespace App\Command; use App\Message\Sum; use Symfony\Component\Console\Attribute\AsCommand; use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Output\OutputInterface; use Symfony\Component\Messenger\MessageBusInterface; #[AsCommand( name: 'app:create', description: 'Add a short description for your command', )] class CreateCommand extends Command { public function __construct(private readonly MessageBusInterface $messageBus) { parent::__construct(); } protected function execute(InputInterface $input, OutputInterface $output): int { $this->messageBus->dispatch(new Sum(4,2)); return Command::SUCCESS; } }
-
Open the
src\Message\Sum.php
file and change it as follows:<?php namespace App\Message; final class Sum { public function __construct(private readonly int $a, private readonly int $b) { } public function getA(): int { return $this->a; } public function getB(): int { return $this->b; } }
-
Open the
src\MessageHandler\SumHandler.php
file and change it as follows:<?php namespace App\MessageHandler; use App\Message\Sum; use Symfony\Component\Messenger\Attribute\AsMessageHandler; #[AsMessageHandler] final class SumHandler { public function __invoke(Sum $message): void { printf('Sum of %d and %d is %d', $message->getA(), $message->getB(), $message->getA() + $message->getB() ); } }
-
Open the
.env
file and find theMESSENGER_TRANSPORT_DSN=doctrine://default?auto_setup=0
line in it. Edit it as follows:MESSENGER_TRANSPORT_DSN=sqs://message-queue.api.cloud.yandex.net/b1gvlrnlei4l********/dj6000000000********/symfony-test?access_key=KEY&secret_key=SECRET®ion=ru-central1
Replace
b1gvlrnlei4l********/dj6000000000********/symfony-test
with the path copied in the Yandex Cloud console.In the
access_key=KEY
andsecret_key=SECRET
parameters, replace theKEY
andSECRET
values with the value of the Message Queue static access key. -
Open the
config/packages/messenger.yaml
file and change it as follows:framework: messenger: failure_transport: failed transports: # https://symfony.com/doc/current/messenger.html#transport-configuration async: dsn: '%env(MESSENGER_TRANSPORT_DSN)%' retry_strategy: max_retries: 3 multiplier: 2 failed: 'doctrine://default?queue_name=failed' # sync: 'sync://' routing: Symfony\Component\Mailer\Messenger\SendEmailMessage: async Symfony\Component\Notifier\Message\ChatMessage: async Symfony\Component\Notifier\Message\SmsMessage: async App\Message\Sum: async # Route your messages to the transports # 'App\Message\YourMessage': async
-
Run the command for delivering messages to the queue:
php bin/console app:create
-
Run the command for processing the queue:
php bin/console messenger:consume async