PHP code example of limingxinleo / swoft-amqplib

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

    

limingxinleo / swoft-amqplib example snippets



use yii\base\StaticInstanceTrait;
use Swoftx\Amqplib\Connection;
use Swoftx\Amqplib\Message\Publisher;

class YiiConnection extends \Swoftx\Amqplib\Connection
{
    use StaticInstanceTrait;


use Swoftx\Amqplib\Message\Publisher;
use Swoftx\Amqplib\SwoftConnection;
use Swoftx\Amqplib\Pool\RabbitMQPool;
use Swoft\Pool\ConnectionInterface;

class DemoMessage extends Publisher
{
    protected $exchange = 'demo';

    protected $queue = 'demo.queue';

    protected $routingKey = 'test';
    
    /** @var ConnectionInterface SwoftConnection */
    protected $swoftConnection;

    public function getConnection(): \Swoftx\Amqplib\Connection
    {
        $pool = \Swoft\App::getPool(RabbitMQPool::class);
        /** @var SwoftConnection $connection */
        $this->swoftConnection = $pool->getConnection();
        return $this->swoftConnection->getConnection();
    }
    
    public function publish()
    {
        parent::publish();
        // 释放资源 【很重要】
        $this->swoftConnection->release(true);
    }