1. Go to this page and download the library: Download xervice/rabbitmq library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
xervice / rabbitmq example snippets
namespace App\MyModule\Business\Model\Exchange;
use DataProvider\RabbitMqExchangeDataProvider;
use Xervice\RabbitMQ\Business\Dependency\Exchange\ExchangeInterface;
use Xervice\RabbitMQ\Business\Model\Core\ExchangeProviderInterface;
class MyExchange implements ExchangeInterface
{
/**
* @param \Xervice\RabbitMQ\Core\ExchangeProviderInterface $exchangeProvider
*/
public function declareExchange(ExchangeProviderInterface $exchangeProvider)
{
$myExchange = new RabbitMqExchangeDataProvider();
$myExchange
->setName('MyExchangeName')
->setType('direct')
->setAutoDelete(false);
$exchangeProvider->declare($myExchange);
}
}
namespace App\MyModule\Business\Model\Queue;
use DataProvider\RabbitMqExchangeDataProvider;
use DataProvider\RabbitMqQueueBindDataProvider;
use DataProvider\RabbitMqQueueDataProvider;
use Xervice\RabbitMQ\Business\Dependency\Queue\QueueInterface;
use Xervice\RabbitMQ\Business\Model\Core\QueueProviderInterface;
class MyQueue implements QueueInterface
{
public function declareQueue(QueueProviderInterface $queueProvider)
{
$queue = new RabbitMqQueueDataProvider();
$queue
->setName('MyQueueName')
->setAutoDelete(false)
->setArgument([]);
$exchangeToBind = new RabbitMqExchangeDataProvider();
$exchangeToBind->setName('MyExchangeName');
$bind = new RabbitMqQueueBindDataProvider();
$bind
->setExchange($exchangeToBind)
->setQueue($queue);
$queueProvider->declare($queue);
$queueProvider->bind($bind);
}
}
namespace App\MyModule\Business\Model\Listener;
use DataProvider\RabbitMqMessageCollectionDataProvider;
use PhpAmqpLib\Channel\AMQPChannel;
use Xervice\RabbitMQ\Business\Model\Worker\Listener\AbstractListener;
class MyListener extends AbstractListener
{
public function handleMessage(
RabbitMqMessageCollectionDataProvider $collectionDataProvider,
AMQPChannel $channel
): void {
foreach ($collectionDataProvider->getMessages() as $message) {
echo $message->getMessage()->getMessage();
$this->sendAck($channel, $message);
}
}
/**
* @return int
*/
public function getChunkSize(): int
{
return 500;
}
/**
* @return string
*/
public function getQueueName(): string
{
return 'MyQueueName';
}
}
namespace App\RabbitMQ;
use Xervice\RabbitMQ\RabbitMQDependencyProvider as XerviceRabbitMQDependencyProvider;
use XerviceTest\RabbitMQ\Exchange\TestExchange;
use XerviceTest\RabbitMQ\Listener\TestListener;
use XerviceTest\RabbitMQ\Queue\TestQueue;
class RabbitMQDependencyProvider extends XerviceRabbitMQDependencyProvider
{
/**
* @return \Xervice\RabbitMQ\Business\Dependency\Worker\Listener\ListenerInterface[]
*/
protected function getListener(): array
{
return [
new MyListener()
];
}
/**
* @return \Xervice\RabbitMQ\Business\Dependency\Queue\QueueInterface[]
*/
protected function getQueues(): array
{
return [
new MyQueue()
];
}
/**
* @return \Xervice\RabbitMQ\Business\Dependency\Exchange\ExchangeInterface[]
*/
protected function getExchanges(): array
{
return [
new MyExchange()
];
}
}
$rabbitMQFacade->init();
$exchange = new RabbitMqExchangeDataProvider();
$exchange->setName('UnitTest');
$simpleMessage = new SimpleMessageDataProvider();
$simpleMessage->setMessage('TestMessage');
$testMessage = new RabbitMqMessageDataProvider();
$testMessage
->setExchange($exchange)
->setMessage($simpleMessage);
$rabbitMQFacade->sendMessage($message);
$rabbitMQFacade->runWorker();
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.