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.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package oo-amqp-client

Latest Stable Version Total Downloads Latest Unstable Version License Scrutinizer Code Quality Build Status Coverage Status

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.

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 Build Version
Package Version
Requires php Version >=7.0
php-amqplib/php-amqplib Version ^2.6.3
psr/log Version ^1
mouf/utils.log.psr.errorlog_logger Version ^2
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package mouf/oo-amqp-client contains the following files

Loading the files please wait ....