Download the PHP package mouf/oo-amqp-client without Composer
On this page you can find all versions of the php package mouf/oo-amqp-client. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download mouf/oo-amqp-client
More information about mouf/oo-amqp-client
Files in mouf/oo-amqp-client
Package oo-amqp-client
Short Description An object oriented wrapper on top of php-amqplib helping work with RabbitMQ in a more object oriented way.
License MIT
Homepage http://mouf-php.com/packages/mouf/oo-amqp-client
Informations about the package oo-amqp-client
About Object Oriented AMQP Client
This package contains an object oriented wrapper on top of php-amqplib helping work with RabbitMQ in a more object oriented way.
Using this package, exchanges, bindings and queues are represented as objects. This is useful, especially if you want to inject those objects in your dependency injection container.
Installation
Usage
Before using this library, you should be accustomed to the AMQP concepts. If you are not, we strongly advise you to start reading the "AMQP 0-9-1 Model Explained" document from the RabbitMQ documentation.
Done? Let's get started.
Creating a client
The first thing you want to create is a Client
object. A Client
represents a connection to RabbitMQ (for those of you used to php-amqplib, it is both a connection AND a channel).
Note: the Client
class exposes a number of useful configuration methods (you do not need to use those if you don't know what they do):
Creating an exchange
In AMQP, exchanges are the objects that receive messages and are in charge of forwarding those messages to queues.
You must therefore define an Exchange
objects to send messages.
When creating an exchange, you pass to the constructor the Client
object, the exchange name, and the exchange type.
Note: the exchange will self-register in the client.
You can apply advanced configuration using configuration methods:
Creating a queue and a binding
Messages arriving to an exchange are forwarded to a queue through a binding.
We will now create a queue to store our messages.
When creating a client, you pass to the constructor the Client
object, the client name, and an array of Consumer
objects (actually an array of objects implementing the ConsumerInterface
).
A Consumer
object is an object that contains code that will be called each time a message is received.
Note: the queue will self-register in the client.
You can apply advanced configuration to your queue using those configuration methods:
You will certainly want to use the setDurable
method if you want your queue to store messages in case of outage of the receiver.
At this point, we have an exchange, we have a queue, but both are not linked together. We need to bind those, using a Binding
object.
A Binding
links an exchange to a queue.
Important: unlike the Exchange
and the Queue
, a Binding
does not self-register in the client. You have to declare it in the client yourself, using the Client::register
method.
Done? Let's send and receive messages!
Sending a message
In order to send a message, you simply use the Exchange::publish
method:
You may still want to configure a bit more the sending of your message. The Exchange::publish
method accepts a number of optional arguments:
Also, the Message
class can be tweaked with one of those methods:
Receiving messages
As we already saw, the first step to receiving message is creating a queue and adding Consumer
objects to that queue.
We still need to tell PHP to start listening, otherwise, the callbacks in the Consumer
will never be called.
This can be done using the ConsumerService
class.
The ConsumerService
constructor takes the client in parameter, and the array of queues that must be listened to.
The ConsumerService::run
method will start listening on arriving messages, in an infinite loop.
Notice that you can use $consumerService->run(true);
if you want to listen to one message only and return afterward.
Acknowledgements and error handling
When you receive a message, an acknowledgement will not be sent before the Consumer
has finished consuming the message.
If an exception is triggered in the Consumer
, a nack
will be sent instead to RabbitMQ.
Note: if your consumer callback throws an exception implementing the RetryableExceptionInterface
interface, the nack
message will be sent with the "requeue" flag. The message will be requeued.
Note: if your consumer callback throws an exception implementing the FatalExceptionInterface
interface, the exception will be propagated by the consumer (hence leading to the crash of the consumer script). Otherwise, consumer will continue processing messages.
Exceptions are logged by default using the error_log function. You can override this behaviour by passing a PSR-3 compliant logger to the AbstractConsumer
constructor.
Writing your consumer as a class
So far, to create a consumer, we used the Consumer
class that takes a callback as first constructor parameter.
As an alternative, you can extend the AbstractConsumer
class and implement the onMessageReceived
method:
Sending a message to a given queue
If you want to target a special queue and send a message to it directly, you have 2 options.
Option 1: create a DefaultExchange
object and pass the queue name as the key of the message.
Option 2: use the publish
method of the Queue
object:
Note: these are RabbitMQ specific features and might not work with other AMQP buses.
Symfony console integration
This package comes with 2 Symfony commands that you can use to send and receive messages.
Mouf\AmqpClient\Commands\PublishCommand
(amqp:publish
) allows you to send an arbitrary message on an exchange (read from a file or from STDIN)Mouf\AmqpClient\Commands\ConsumeCommand
(amqp:consume
) listen to all configured queues
Running the unit tests
This package uses PHPUnit for unit tests.
To run the tests:
Obviously, you need a running RabbitMQ server to test this package. If you use Docker, you can start one using:
All versions of oo-amqp-client with dependencies
php-amqplib/php-amqplib Version ^2.6.3
psr/log Version ^1
mouf/utils.log.psr.errorlog_logger Version ^2