Run your async job with symfony/messenger

Configure your PHP env

apt-get install rabbitmq-server  
apt-get install librabbitmq-dev  
pecl install amqp

## When you receive:
## Set the path to librabbitmq install prefix [autodetect] : [Press enter]

Finally you must add in your php.ini

Install the component

composer require messenger  

Now you can edit the new env var MESSENGER_TRANSPORT_DSN with a configuration like this:

# .env


Create your first Message with the related Handler.


namespace App\Message;

class MyFirstMessage  
    protected $message;

    public function __construct($message = 'Hello World!')
        $this->message = $message;

namespace App\MessageHandler;

use App\Message\HelloWorldMessage;

class MyFirstHandler  
    public function __invoke(MyFirstMessage $message)

In your code, now you can add some like this:

use Symfony\Component\Messenger\MessageBusInterface;


public function executeAsync(MessageBusInterface $bus)  
        new MyFirstMessage('Hello, Max!')

Processing the messages

For processing your messages you must run this command:

/etc/init.d/rabbitmq-server start

bin/console messenger:consume-messages amqp  

The component will create immediately the new queue in RabbitMQ (


The final result is:

(sf-message {1} U:6 ?:4 ✗) sf-messenger bin/console messenger:consume-messages amqp -vvv
2018-07-07T21:44:41+02:00 [debug] Notified event "console.command" to listener "Symfony\Component\HttpKernel\EventListener\DebugHandlersListener::configure".  
2018-07-07T21:51:30+02:00 [debug] Starting handling message App\Message\MyFirstMessage  
object(App\Message\HelloWorldMessage)#190 (1) {  
  string(12) "Hello Max!"

2018-07-07T21:51:30+02:00 [debug] Finished handling message App\Message\MyFirstMessage