PHP code example of ryaremenko / amqp

1. Go to this page and download the library: Download ryaremenko/amqp 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/ */

    

ryaremenko / amqp example snippets


class BaseConnection extends AmqpConnection
{
    protected function setConnectionOptions(): AmqpConnectionOptions
    {
        return  (new AmqpConnectionOptions())
            ->setHost('127.0.0.1')
            ->setPort(5672)
            ->setLogin('guest')
            ->setPassword('guest');
    }
}

/*
|--------------------------------------------------------------------------
|  Laravel example
|--------------------------------------------------------------------------
*/

//...

$this->app->singleton(BaseConnection::class);
$this->app->bind(AMQPConnectionInterface::class, BaseConnection::class);

//...

    (new AmqpProducer)->publish(['data'], 'queue_name');

class AMQPHandlersService 
{
    private const HANDLERS = [
        'queue_name' => TestHandler::class
    ];
    
    private const PRIORITY_HANDLERS = [
        'queue_name'
    ];
    
    private $amqpConsumer;
    
    public function __construct(AmqpConsumer $amqpConsumer) {
        $this->amqpConsumer = $amqpConsumer;
    }
    
    public function handle(string $queueName) {
        $properties = [];
        if (in_array($queueName, self::PRIORITY_HANDLERS, true)){
            $properties['priority'] = true;
        }
    
        $handler = app(self::HANDLERS[$queueName]);
        $this->amqpConsumer->consume($queueName, function ($message) use ($handler) {
            try {
                $handler->handle($message->body);
            } catch (\Exception $exception) {
                // exception handler
            }
        },
        $properties);
    }
}

$ php composer