saxulum/saxulum-message-queue

This package is abandoned and no longer maintained. The author suggests using the enqueue/enqueue-dev package instead.

Saxulum Message Queue

1.0.0-beta1 2016-09-18 15:11 UTC

README

Build Status Total Downloads Latest Stable Version Scrutinizer Code Quality

Description

A simple to use messaging queue abstraction.

Requirements

  • php: ~7.0

Installation

Through Composer as saxulum/saxulum-message-queue.

Usage

Message

<?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;
    }
}

RabbitMQ

Additional Requirements

{
    "require": {
        "php-amqplib/php-amqplib": "^2.6.3"
    }
}

Send

<?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'));

Receive

<?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();

Redis

Additional Requirements

{
    "require": {
        "predis/predis": "^1.1.1"
    }
}

Send

<?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'));

Receive

<?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();

SystemV

Send

<?php

use My\Project\SampleMessage;
use Saxulum\MessageQueue\SystemV\SystemVSend;

$sender = new SystemVSend(1);
$sender->send(new SampleMessage('context', 'this is a message'));

Receive

<?php

use My\Project\SampleMessage;
use Saxulum\MessageQueue\SystemV\SystemVReceive;

$sender = new SystemVReceive(SampleMessage::class, 1);
$message = $sender->receive();

Copyright

Dominik Zogg 2016