PHP code example of minms / rabbitmq

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

    

minms / rabbitmq example snippets


# 创建消息结构

class TestMessage extends \Minms\RabbitMQ\Message
{
    public function __construct($index, $pushTime)
    {
        parent::__construct(compact('index', 'pushTime'), TestHandler::class);
    }
}

# 创建消息对应消费方法

class TestHandler extends \Minms\RabbitMQ\Handler
{
    public function run()
    {
        echo date('Y-m-d H:i:s')." 队列消息消费: pushTime: ". $this->getBody("pushTime") . ", index: ".$this->getBody("index")." \n";
    }
}

# 配置RabbitMQ连接信息
\Minms\RabbitMQ\Config::setConfig('192.168.2.186', 5672, 'guest', 'guest', '/');

# 监听队列默认有个Listen帮助类, 也可以参考进行重写, !! 需要在callback里面处理异常情况
\Minms\RabbitMQ\Listen::callback(function (\PhpAmqpLib\Message\AMQPMessage $message){
    $data = json_decode($message->getBody(), true);

    //todo, 自定义处理

    /** @var \Minms\RabbitMQ\Handler $handlerClassInstance */
    $handlerClassInstance = new $data['handler']($data['params'], $data['message']);

    /** 执行任务 */
    $handlerClassInstance->run();
});