KamiOrz / lumen-amqp by KamiOrz

AMQP wrapper for Lumen to publish and consume messages
25
0
2
Package Data
Maintainer Username: KamiOrz
Maintainer Contact: 68152283@qq.com (XiaoGai)
Package Create Date: 2017-01-10
Package Last Update: 2017-01-10
Language: PHP
License: MIT
Last Refreshed: 2024-11-23 03:12:01
Package Statistics
Total Downloads: 25
Monthly Downloads: 0
Daily Downloads: 0
Total Stars: 0
Total Watchers: 2
Total Forks: 0
Total Open Issues: 1

kamiorz/lumen-amqp

AMQP wrapper for Lumen to publish and consume messages especially from RabbitMQ

Features

  • Advanced queue configuration
  • Add message to queues easily
  • Listen queues with useful options

Installation

Composer

Add the following to your require part within the composer.json:

"kamiorz/lumen-amqp": "1.*"
$ php composer update

or

$ php composer require kamiorz/lumen-amqp

Integration

Lumen

Create a config folder in the root directory of your Lumen application and copy the content from vendor/kami/lumen-amqp/config/amqp.php to config/amqp.php.

Adjust the properties to your needs.

return [

    'use' => 'production',

    'properties' => [

        'production' => [
            'host'                => 'localhost',
            'port'                => 5672,
            'username'            => 'username',
            'password'            => 'password',
            'vhost'               => '/',
            'exchange'            => 'amq.topic',
            'exchange_type'       => 'topic',
            'consumer_tag'        => 'consumer',
            'ssl_options'         => [], // See https://secure.php.net/manual/en/context.ssl.php
            'connect_options'     => [], // See https://github.com/php-amqplib/php-amqplib/blob/master/PhpAmqpLib/Connection/AMQPSSLConnection.php
            'queue_properties'    => ['x-ha-policy' => ['S', 'all']],
            'exchange_properties' => [],
            'timeout'             => 0
        ],

    ],

];

Register the Lumen Service Provider in bootstrap/app.php:

/*
|--------------------------------------------------------------------------
| Register Service Providers
|--------------------------------------------------------------------------
*/

//...

$app->configure('amqp');
$app->register(KamiOrz\Amqp\LumenServiceProvider::class);

//...

Add Facade Support for Lumen 5.2+

//...
$app->withFacades();
class_alias(\Illuminate\Support\Facades\App::class, 'App');
//...

Publishing a message

Push message with routing key

    Amqp::publish('routing-key', 'message');

Push message with routing key and create queue

    Amqp::publish('routing-key', 'message' , ['queue' => 'queue-name']);

Push message with routing key and overwrite properties

    Amqp::publish('routing-key', 'message' , ['exchange' => 'amq.direct']);

Consuming messages

Consume messages, acknowledge and stop when no message is left

Amqp::consume('queue-name', function ($message, $resolver) {
        
   var_dump($message->body);

   $resolver->acknowledge($message);

   $resolver->stopWhenProcessed();
        
});

Consume messages forever

Amqp::consume('queue-name', function ($message, $resolver) {
        
   var_dump($message->body);

   $resolver->acknowledge($message);
        
});

Consume messages, with custom settings

Amqp::consume('queue-name', function ($message, $resolver) {
        
   var_dump($message->body);

   $resolver->acknowledge($message);
      
}, [
  'timeout' => 2,
  'vhost'   => 'vhost3'
]);

Fanout example

Publishing a message

\Amqp::publish('', 'message' , [
    'exchange_type' => 'fanout',
    'exchange' => 'amq.fanout',
]);

Consuming messages

\Amqp::consume('', function ($message, $resolver) {
    var_dump($message->body);
    $resolver->acknowledge($message);
}, [
    'exchange' => 'amq.fanout',
    'exchange_type' => 'fanout',
    'queue_force_declare' => true,
    'queue_exclusive' => true,
    'persistent' => true// required if you want to listen forever
]);

License

This package is open-sourced software licensed under the MIT license