A simple to use messaging queue abstraction.
- php: ~7.0
Through Composer as saxulum/saxulum-message-queue.
<?php
namespace My\Project;
use Saxulum\MessageQueue\MessageInterface;
class SampleMessage implements MessageInterface
{
/**
* @var string
*/
private $context;
/**
* @var string
*/
private $message;
/**
* @param string $context
* @param string $message
*/
public function __construct(string $context, string $message)
{
$this->context = $context;
$this->message = $message;
}
/**
* @param string $json
*
* @return MessageInterface
*/
public static function fromJson(string $json): MessageInterface
{
$rawMessage = json_decode($json);
return new self($rawMessage->context, $rawMessage->message);
}
/**
* @return string
*/
public function toJson(): string
{
return json_encode([
'context' => $this->context,
'message' => $this->message,
]);
}
/**
* @return string
*/
public function getContext(): string
{
return $this->context;
}
/**
* @return string
*/
public function getMessage(): string
{
return $this->message;
}
}
{
"require": {
"php-amqplib/php-amqplib": "^2.6.3"
}
}
<?php
use My\Project\SampleMessage;
use PhpAmqpLib\Connection\AMQPStreamConnection;
use Saxulum\MessageQueue\Redis\RedisSend;
$connection = new AMQPStreamConnection('localhost', 5672, 'guest', 'guest');
$sender = new RabbitMQSend($connection, 'messages');
$sender->send(new SampleMessage('context', 'this is a message'));
<?php
use My\Project\SampleMessage;
use PhpAmqpLib\Connection\AMQPStreamConnection;
use Saxulum\MessageQueue\Redis\RedisReceive;
$connection = new AMQPStreamConnection('localhost', 5672, 'guest', 'guest');
$receive = new RabbitMQReceive(SampleMessage::class, $connection, 'messages');
$message = $sender->receive();
{
"require": {
"predis/predis": "^1.1.1"
}
}
<?php
use My\Project\SampleMessage;
use Predis\Client;
use Saxulum\MessageQueue\Redis\RedisSend;
$client = new Client();
$sender = new RedisSend($client, 'messages');
$sender->send(new SampleMessage('context', 'this is a message'));
<?php
use My\Project\SampleMessage;
use Predis\Client;
use Saxulum\MessageQueue\Redis\RedisReceive;
$client = new Client();
$sender = new RedisReceive(SampleMessage::class, $client, 'messages');
$message = $sender->receive();
<?php
use My\Project\SampleMessage;
use Saxulum\MessageQueue\SystemV\SystemVSend;
$sender = new SystemVSend(1);
$sender->send(new SampleMessage('context', 'this is a message'));
<?php
use My\Project\SampleMessage;
use Saxulum\MessageQueue\SystemV\SystemVReceive;
$sender = new SystemVReceive(SampleMessage::class, 1);
$message = $sender->receive();
Dominik Zogg 2016