PHP code example of everlution / message-bus

1. Go to this page and download the library: Download everlution/message-bus library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

everlution / message-bus example snippets


use PhpAmqpLib\Connection\AMQPStreamConnection;
use Everlution\MessageBus\Transport\RabbitMqTransport;

$connection = new AMQPStreamConnection('rabbitmq_host', 5672, 'user', 'password');
$exchange = 'MessageBus';
$consumerName = 'consumer1';

$transport = new RabbitMqTransport($connection, $exchange, $consumerName);



class SmsSendMessage implements MessageDefinitionInterface
{
    public static function getName(): string
    {
        return 'sms_send';
    }

    public function getJsonSchema(): string
    {
        $schema = [
            'type' => 'object',
            'properties' => [
                'messageName' => [
                    'type' => 'string',
                    'enum' => [self::getName()],
                ],
                'number' => [
                    'type' => 'string',
                    'regex' => '$\d*^'
                ],
                'text' => [
                    'type' => 'string',
                    'maxLength' => 255,
                ],
            ],
            'additionalProperties' => false,
            '

use Everlution\MessageBus\Protocol\DefaultProtocol;

$protocol = new DefaultProtocol();
$protocol->addMessageDefinition(new SmsSendMessage());

use Everlution\MessageBus\Validator\DefaultValidator;
use Everlution\MessageBus\Validator\ValidatorException;

$validator = new DefaultValidator();

$message = [
    'messageName' => 'sms_send',
    'number' => '0777777777',
    'text' => 'This is my message,
];

try {
    $validator->validate($protocol, $message);
} catch (ValidatorException $e) {
    var_dump($e->getJsonSchema());
    var_dump($e->getData());
    var_dump($e->getErrors());
}

use Everlution\MessageBus\Publisher\DefaultPublisher;

$publisher = new DefaultPublisher($protocol, $validator, $serializer, $transport);

try {
    $publisher->publish($message);
} catch (\Everlution\MessageBus\Validator\ValidatorException $e) {
    var_dump($e->getJsonSchema());
    var_dump($e->getData());
    var_dump($e->getErrors());
}

use Everlution\MessageBus\Consumer\EchoConsumer;

$consumer = new EchoConsumer($validator, $protocol, $serializer, $transport);

try {
    $consumer->consume($numberOfMessagesToConsumeAntThenExit = 1);
} catch (\Everlution\MessageBus\Validator\ValidatorException $e) {
    var_dump($e->getJsonSchema());
    var_dump($e->getData());
    var_dump($e->getErrors());
}