ZF3 Module for RabbitMQ
composer require jiromm/zf3-rabbitmq
create configuration with rabbitmq
key as described below:
<?php
return [
'rabbitmq' => [
'host' => 'localhost',
'port' => 5672,
'login' => 'guest',
'password' => 'guest',
],
]
Make sure the module enabled and try an example below.
<?php
/**
* @var \RabbitMQ\Service\RabbitMQ $rabbitmqService
* @var \Interop\Container\ContainerInterface $container
*/
$rabbitMQ = $container->get(\RabbitMQ\Service\RabbitMQ::class);
$publisher = new \RabbitMQ\Publisher\WorkQueuePublisher('test_work_queue');
$job = new \RabbitMQ\Job\Job(['some' => 'data']);
$rabbitMQ->setPublisher($publisher);
$rabbitMQ->push($job);
<?php
/**
* @var \RabbitMQ\Service\RabbitMQ $rabbitmqService
* @var \Interop\Container\ContainerInterface $container
*/
$rabbitMQ = $container->get(\RabbitMQ\Service\RabbitMQ::class);
$consumer = new \RabbitMQ\Consumer\WorkQueueConsumer('test_work_queue');
$rabbitMQ->setConsumer($consumer);
$rabbitMQ->receive(function (\RabbitMQ\Consumer\Message $message) {
echo $message->getBody();
$message->ack();
});