graphaware / php-simplemq
Github Events API Sync module
Requires
- symfony/filesystem: ^2.7
- symfony/finder: ^2.7
- symfony/yaml: ^2.7
- videlalvaro/php-amqplib: ^2.5
Requires (Dev)
- behat/behat: ^3.0
- bossa/phpspec2-expect: ^1.0
- phpspec/phpspec: ^2.2
- phpunit/phpunit: ^4.7
This package is not auto-updated.
Last update: 2024-12-21 19:16:08 UTC
README
GraphAware's PHP Simple Message Queue for RabbitMQ
RabbitMQ's Rapid Application Development.
This library makes possible to create channels, queues, bindings, producers, consumers, .. on the fly by only providing a simple YAML configuration file.
Usage
Require the library dependency :
composer require graphaware/php-simplemq
Define the connections, exchanges, producers and consumers, eg:
simple_mq: connections: default: host: 192.168.59.103 port: 5672 user: admin password: error vhost: "/" exchanges: logs: connection: default type: fanout durable: true error-logs: connection: default type: direct durable: true producers: logs: exchange: logs errors: exchange: error-logs routing_key: error consumers: logs-printer: exchange: logs ack: true queue: name: my-app-all-logs durable: true qos: prefetch_count: 1 error-logs-recorder: exchange: error-logs queue: name: my-app-error-logs durable: true qos: prefetch_size: 1 bindings: - queue: my-app-error-logs routing_key: error
Bootstrap the library by providing your configuration file location :
require_once(__DIR__.'/vendor/autoload.php'); use GraphAware\SimpleMQ\SimpleMQ; $smq = SimpleMQ::withYAMLConfigFile(__DIR__.'/path_to_your_config_file.yml');
Based on the example configuration, producers named logs
and errors
as well as consumers named logs-printer
and
error-logs-recorder
are available through the library.
To retrieve and start consuming queues, you can get the consumer with the following method :
$consumer = $smq->getConsumer('logs-printer'); $callback = function($message) { print_r($message->body); }; $consumer->consume($callback);
Getting a single message :
$message = $consumer->getMessage(); // Returns a AMQPMessage instance
Getting more than one message :
$messages = $consumer->getMessageBatch(10); // Returns an array of AMQPMessage
Sometimes, there can be a latency between a message is sent and this message to be seen by the producers (for eg in CI suites).
You can define a maxAttempts to reach the batchSize before stopping reading queues :
$messages = $consumer->getMessageBatch(10, 20);
And to start sending messages to exchanges, it is pretty much the same :
$producer = $smq->getProducer('errors'); $message = json_encode(array('id' => 1234, 'text' => 'Hello world')); $producer->sendMessage($message);
The producer and consumers knows exactly, based on the configuration, which routing key to use for direct and topic exchanges and also which binding keys to use for binding queues to exchanges.
License: MIT
Author: Christophe Willemsen