Download the PHP package leadtech/boot-rabbit-mq without Composer
On this page you can find all versions of the php package leadtech/boot-rabbit-mq. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package boot-rabbit-mq
Quickstart
This library provides an easy to way to get up and running with RabbitMQ very quickly. To implement rabbitMQ you'll first need to create an instance of the QueueTemplate class. This class represents a single line of communication between consumer(s) and producer(s). Both the producer and the consumer classes will use the same template. Once the template is available you'll have to subclass the AbstractConsumer class and implement a handle method. Incoming messages are delegated to this method. The handle method returns either true or false indicating the success status.
To implement a producer simply instantiate or subclass Boot\RabbitMQ\Producer\Producer
or Boot\RabbitMQ\Producer\BatchProducer
providing the queue template instance in the constructor. There is a command available that you can use to publish messages using the console.
Full example
Full example of a fault tolerant queue. The messages are persisted and will survive a restart. The client is configured to sent ACK/NACK signals manually.
Create worker.php
Create producer.php
Implementing queue workers as command line using boot
Boot is a minimalistic framework build upon symfony components such as DependencyInjection, EventDispatcher and Console components and relies solely on composer for package management and auto-loading. Boot was created to provide a very basic yet elegant way to bootstrap an application.
Boot makes it very easy to create a console application to run a queue worker.
This is an example of how to bootstrap a console application:
Installation
After installing RabbitMQ you'll have to setup a project. If you decide to go with boot checkout the examples
folder. You will find a ready to use console application in there. If you want to start from scratch you will need to include
the following packages in your composer.json file. If you don't want to use boot and create your implementation of symfony\console
than you
can use this package without installing boot as well.
Installing Boot
Installing Boot RabbitMQ
Requirements
System requirements
- PHP >= 5.4
- RabbitMQ Server
Required packages:
- videlalvaro/php-amqplib
- symfony/event-dispatcher
- symfony/console
- monolog/monolog
QueueTemplate
Responsibility
The responsibility of the QueueTemplate is to provide a single setup that is specific to a single line of communication between producers and consumers. Both the consumer and the producer depend on the same setup provided by the queue template.
The QueueTemplate contains:
- The connection (same configuration must be used for both the producer and the consumer).
- A message serializer.
- RabbitMQ specific options such as the queue name, exchange, passiveness, exclusive connections etcetera.
- A queue strategy.
- The event dispatcher.
What is a queue strategy?
RabbitMQ is a powerful queuing server and there are numerous ways to use it. This library provides two configurations out of the box providing either a fault tolerant solution or a basic but faster setup. Configurations may require a specific setup of both the consuming as the producing application(s).
A downside of many features is that it becomes easier to make mistakes along the way. Even more so when you have to deal with a number of teams. By providing the single point of configuration the queue template prevents queuing logic from being scattered across components and/or application(s). The strategy should both simplify the implementation of RabbitMQ and improve the reliability of the implementation amongst teams and applications.
Creating a queue in memory which performs better but will not survive a crash:
Or to create a fault tolerant queue instead we would do:
Implementing a custom strategy
To implement a custom strategy simply create an object that extends the Boot\RabbitMQ\Strategy\QueueStrategy.
Do you have an awesome strategy that might be useful to others? Feel free to share ;-)
The consumer
Responsibility
A consumer is responsible for listening and processing received messages from a particular queue. Both the consumer and producer classes are tightly coupled to the QueueTemplate class. The queue template provides a single configuration that applies to both consumers and producers.
Implementing your consumer
To get started to started simply create a subclass of the Boot\RabbitMQ\Consumer\AbstractConsumer.
You must implement the following method:
Instantiate consumer
Create the consumer object. The consumer has a dependency to the QueueTemplate class. The same template should be provided to a consumer.
Although this works it is better to use dependency injection to configure the components. If the consumer(s) are implemented as a standalone application I recommend to checkout the PHPBoot repository which implements a lightweight implementation of the Symfony2 service container and console component. I will add ready to use example of this library there as well. (by the time you read this it might already be there)
Handling incoming messages
Start listening to incoming messages.
The connect method will:
- Create a connection to the server. The consumer will get a configured connection object from queue template.
- Declare a queue as defined in the queue template and its strategy
- Declare the quality of service (qos) as defined in the queue template and its strategy
The listen method will:
- Create a channel and subscribe to a particular queue. Internally we provide a callback that will be executed for each received message. Because the AbstractConsumer implements the magic invoke method we can consumer instances as a valid callback. When the invoke method is called we will delegate the call to the handle method on the concrete consumer. The handle method must return true or false. This is especially crucial in setups where ack/nack signals are sent manually.
We are processing the messages just yet. To start accepting incoming messages we must execute:
Listen to consumer events
The customer provides additional functionality to make it easy for other components to attach additional functionality. The following events are implemented:
- ON_RECEIVE
- ON_CONSUMER_ERROR
- ON_CONSUMER_SUCCESS
Tip: checkout the symfony2 documentation for full documentation about the EventDispatcher and events. There are more ways to register listeners. (Event subscribers, using objects instead of functions etc.)
Adding listeners
Console command
This library provides an easy way to run consumers from the command line.
Instantiate the ConsumerCommand
Start consuming by executing:
php /path/to/app/console consume:my-foo-consumer
The provided solution works for any console application build upon or based on symfony2 components. If you are not using symfony2 for this project I recommend to checkout the PHPBoot repository. Boot provides a very minimalistic micro framework build solely on composer, the symfony2 service container with (optional) build in support for the console component. Boot provides a powerful builder that you can use to setup your application. Components are designed to be framework agnostic, flexible, and lightweight.
Producer
Responsibility
A producer is responsible for publishing messages to the queue. The producer uses the same queue template as the consumer. This library provides both the producer class and another producer for batch operations. There is no need to subclass the producer although you can if you need to.
Example
Using a producer to publish a message.
Using a batch producer to publish multiple messages at once.
Console
We've made it easy for you to be up and running in little time. For development/testing you can register instances of the ConsoleProducerCommand class. This command allows you to publish messages simply by running a command. The command depends on the producer object and uses the producer and the queue template to setup the connection. When you need to it is quite easy to develop your own command as well. Just extend the Boot\RabbitMQ\Command\AbstractProducerCommand class and it should almost work out of the box. The command must implement a produce method. Check the source of Boot\RabbitMQ\Command\ConsoleProducerCommand to see how it works. (just a few lines of code, I promise ;-))
Example of how the command can be created (supposing you do not use dependency injection nor symfony2 etc.
To publish the same message 10 times execute:
php /path/to/app/console produce:my-foo-producer --repeat=10 --base64=0 "This is my message"
Serializer
Responsibility
The serializer is responsible of the serialization of messages that are processed by RabbitMQ. Neither the consumer nor the producer should know how the serialization process works. The producer and consumer will automatically use the same conversion logic by requesting the serializer from the same corresponding queue template. If no serializer is defined than we will automatically use the included JsonSerializer class.
Implementing your own serializer
To implement your own serializer simply create an object that implements the 'Boot\RabbitMQ\Serializer\SerializerInterface'. Let's say we want to use encryption to secure super secret objects. We can implement this functionality simply by creating a serializer such as the one in the example below. Simply inject an instance of this serializer into the queue template and you're good to go.
For example:
While writing this example I realized that this would be a useful addition. So I added a serializer that uses encryption as well. :-)
All versions of boot-rabbit-mq with dependencies
videlalvaro/php-amqplib Version ^2.5
symfony/event-dispatcher Version ^2.7
monolog/monolog Version ^1.16
symfony/console Version ^2.7