Symfony 4 Messenger and Beanstalkd Queue Integration

Ahmet Aydoğdu
2 min readJan 22, 2019

--

I use Laradoc in development environment. I think Laradoc is initial setting. I will explain a sample application step by step.

  1. We are launching the Docker containers with the following command
docker-compose up -d workspace nginx mysql beanstalkd beanstalkd-console

2. We are connect to workspace with following command

docker-compose exec workspace bash

3. We come to directory that we set as a project directory. Create a empty project and go to the project directory

composer create-project symfony/skeleton my-project && cd my-project

4. We are installing the necessary packages for the queue and sample project

composer req --dev make
composer req annotation
composer req messenger
composer req symfony/serializer-pack
composer req enqueue/messenger-adapter
composer req enqueue/pheanstalk

Note: We always confirm the questions in the package installation. And do not seriously ignore the warnings from the parameters.

5. We get a copy of .env file in the project directory and save it as .env.local

6. We set the following parameter in the .env.local file

ENQUEUE_DSN=beanstalk://beanstalkd:11300/default

7. /config/packages/messenger.yml open this file and make it as in the code below

framework:
messenger:
transports:
test: enqueue://default

routing:
'App\Message\SmsNotification': test

8. /config/packages/enqueue.yml open this file and make it as in the code below

enqueue:
default:
transport: '%env(ENQUEUE_DSN)%'
client: ~

9. /config/packages/services.yml open this file and make it as in the code below

parameters:

services:
_defaults:
autowire: true
autoconfigure: true

App\:
resource: '../src/*'
exclude: '../src/{DependencyInjection,Entity,Migrations,Tests,Kernel.php}'

App\Controller\:
resource: '../src/Controller'
tags: ['controller.service_arguments']

App\MessageHandler\SmsNotificationHandler:
tags: [messenger.message_handler]

10. /src/Message/SmsNotification.php create this file and paste the following codes

<?php

namespace
App\Message;


class SmsNotification
{
private $content;

public function __construct(string $content)
{
$this->content = $content;
}

public function getContent(): string
{
return $this->content;
}
}

11. /src/Message/SmsNotificationHandler.php create this file and paste the following codes

<?php

namespace
App\MessageHandler;

use App\Message\SmsNotification;
use Symfony\Component\Messenger\Handler\MessageHandlerInterface;

class SmsNotificationHandler implements MessageHandlerInterface
{
public function __invoke(SmsNotification $message)
{
for ($i=0; $i<10; $i++) {
echo $i.'-Merhaba23 '.$message->getContent()."\n";
sleep(1);
}

}
}

12. /src/Controller/DefaultController.php create this file and paste the following codes

<?php

namespace
App\Controller;

use App\Message\SmsNotification;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Messenger\MessageBusInterface;
use Symfony\Component\Routing\Annotation\Route;

class DefaultController extends AbstractController
{
/**
*
@Route("/default", name="default")
*/
public function index(MessageBusInterface $bus)
{
$bus->dispatch(new SmsNotification('A string to be sent...'));

return $this->json([
'message' => 'Welcome to your new controller!',
'path' => 'src/Controller/DefaultController.php',
]);
}
}

13. We are now running the following command from the console. But we are not close.

php bin/console messenger:consume-messages

14. We have come to end. Now we open the link(for my-project) from our browser. Json data in the browser while the console 10 times will “write a string to sent…”

--

--