PHP code example of xbyter / amqp-rabbitmq

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

    

xbyter / amqp-rabbitmq example snippets


/** 以下是示例,根据实际情况可使用依赖注入,supervisor等方式优化 **/

$configs = nnectionManage = \Examples\ConnectionManageBuilder::buildFromConnections($connections);

//创建exchange和queue并绑定他们之间的关系
$declarer = new \Xbyter\Amqp\Declarer($connectionManage);
foreach ($connections as $connName => $connConfig) {
    $declarer->setExchanges($connName, $connConfig['declarer']['exchanges'] ?? []);
    $declarer->setQueues($connName, $connConfig['declarer']['queues'] ?? []);
    $declarer->setBinds($connName, $connConfig['declarer']['binds'] ?? []);
}
$declarer->createAndBind();

//发布消息
$producer = new \Xbyter\Amqp\Producer($connectionManage);
$producer->publish(new \Examples\DefaultConn\Producers\DemoProducer('消息参数1', '消息参数2', '...'));

//消费指定消费者消息
$consumer = new \Xbyter\Amqp\Consumer($connectionManage);
$consumer->consume(new \Examples\DefaultConn\Consumers\DemoConsumer());

//启动消费服务(建议使用supervisor等进程管理工具)
$consumerServer = new \Examples\ConsumerServer($consumer);
$consumerServer->run($consumers);