How do I use Symfony Messenger for queues?

Symfony Messenger, PHP, queues, message handling, asynchronous processing
Learn how to utilize Symfony Messenger for efficient queue management and asynchronous message processing in your PHP applications.

use Symfony\Component\Messenger\MessageBusInterface;
use Symfony\Component\Messenger\Handler\MessageHandlerInterface;
use Symfony\Component\Messenger\Transport\TransportInterface;

// Message Class
class MyMessage {
    private $content;

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

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

// Handler
class MyMessageHandler implements MessageHandlerInterface {
    public function __invoke(MyMessage $message) {
        echo 'Received message: ' . $message->getContent();
    }
}

// Sending a message
$bus = // ... get your message bus from the container
$message = new MyMessage('Hello, Symfony Messenger!');
$bus->dispatch($message);
    

Symfony Messenger PHP queues message handling asynchronous processing