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 extension=amqp.so 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

MESSENGER_TRANSPORT_DSN=amqp://guest:guest@127.0.0.1:5672/%2f/sf-messages  

Create your first Message with the related Handler.

<?php

namespace App\Message;

class MyFirstMessage  
{
    protected $message;

    public function __construct($message = 'Hello World!')
    {
        $this->message = $message;
    }
}
<?php

namespace App\MessageHandler;

use App\Message\HelloWorldMessage;

class MyFirstHandler  
{
    public function __invoke(MyFirstMessage $message)
    {
        var_dump($message);
    }
}

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

use Symfony\Component\Messenger\MessageBusInterface;

//...

public function executeAsync(MessageBusInterface $bus)  
{
    $bus->dispatch(
        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 (http://127.0.0.1:15672/#/queues/%2F/sf-messages)

sf-queue

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) {  
  ["message":protected]=>
  string(12) "Hello Max!"
}

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