Via Composer
$ composer require kontoulis/rabbitmq-laravel
Add the Service Provider to config/app.php
Kontoulis\RabbitMQLaravel\RabbitMQLaravelServiceProvider::class,
Add the RabbitMQ facade to config/app.php
'RabbitMQ' => Kontoulis\RabbitMQLaravel\Facades\RabbitMQ::class,
Publish the configuration file and edit it if needed in config/rabbitmq-laravel.php
$ php artisan vendor:publish
- Routing Key / Queue Name
The default routing key can be set in config file, or env("APP_NAME")."_queue") will be used.
Also most methods take argument $routingKey
which can override the default.
RabbitMQ::setRoutingKey("myRoutingKey/queueName");
- Exchange
If you don't set an exchange the default ''
exchange will be used.
If you set one, make sure the bindings are set in RabbitMQ system.
if you are not familiar with exchanges you will probably do not need to set that.
RabbitMQ::setExchange("myExchange")'
You can use the RabbitMQ facade to do anything as seen in https://github.com/php-amqplib/php-amqplib/blob/master/PhpAmqpLib/Channel/AMQPChannel.php . Basically, the RabbitMQ facade uses the Broker class which is an extension of AMQPChannel.
- Publishing
// Single message
$msg = [
"key1" => "value1",
"key2" => "value2"
];
RabbitMQ::publishMesssage($msg);
// OR
RabbitMQ::publishMessage($msg, "myRoutingKey");
// Batch publishing
$messages = [
[ "messsage_1_key1" => "value1",
"messsage_1_key2" => "value2"
],
[
"messsage_2_key1" => "value1",
"messsage_2_key2" => "value2"
]
];
RabbitMQ::publishBatch($messages);
// OR
RabbitMQ::publishBatch($messages, "myRoutingKey");
- Consuming the Queue
In order to consume the queue, you could either use the AmqpChannel through the Facade, or use a better to manage approach with QueueHandlers. A QueueHandler should extend the Kontoulis\RabbitMQLaravel\Handlers\Handler class and the built-in ListenToQueue method accepts an array of handlers to process the queue message. If more than one handler exists in array, there are some Handler return values that will use the follow up QueueHandlers in cases of failure of the previous. There is also a Kontoulis\RabbitMQLaravel\Handlers\Handler\DefaultHandler class as example and/or debug handler.
namespace Kontoulis\RabbitMQLaravel\Handlers;
use Kontoulis\RabbitMQLaravel\Message\Message;
/**
* Class DefaultHandler
* @package Kontoulis\RabbitMQLaravel\Handlers
*/
class DefaultHandler extends Handler{
/**
* Tries to process the incoming message.
* @param Message $msg
* @return int One of the possible return values defined as Handler
* constants.
*/
public function process(Message $msg)
{
return $this->handleSuccess($msg);
}
/**
* @param $msg
* @return int
*/
protected function handleSuccess($msg)
{
var_dump($msg);
/**
* For more Handler return values see the parent class
*/
return Handler::RV_SUCCEED_STOP;
}
}
In order to Listen to the Queue you have to pass an array of Handlers to the method
$handlers = ["\\App\\QueueHandlers\\MyHandler"];
\RabbitMQ::listenToQueue($handlers);
The MIT License (MIT). Please see LICENCE.md for more information.