定制 vrnvgasu/php-rabbit-handler 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

vrnvgasu/php-rabbit-handler

Composer 安装命令:

composer require vrnvgasu/php-rabbit-handler

包简介

Handle queues

README 文档

README

composer require vrnvgasu/php-rabbit-handler

Look examples in ./Examples.

Connection

Create new connection.

$connection = new Connection('localhost', 5672, 'guest', 'guest');

Queue

Create new queue.

$queue = new Queue('hello', false, false, false, false);

Pass '' as the first variable if you need a unique queue from rabbit.

Exchange

Create new exchange.

$exchange = new Exchange('direct_logs', 'direct', false, false, false);

Bind queue and exchange

Create new binding.

$binding = new Binding();

You can create pass routing key in your binding.

$binding = new Binding('key');

Then you must to create helper and pass him $connection, $queue, $exchange and $binding.

$helper = new AMQPHelper($connection, $queue, $exchange, $binding);

Consuming

Create new consume.

$consume = new Consume('', false, false,false, false);

You can pass callback function in Consume constructor to handle message from rabbit

$consume = new Consume('', false, false, false, false, function($msg) {
    print_r('Message: ' . $msg->body);
});

Or you can extend your consumer class from Consumer and declare callback method.

class TestConsumer extends Consumer
{
    /**
     * @param AMQPMessage $msg
     */
    public function callback(AMQPMessage $msg): void
    {
        print_r(' [x] Received ' . $msg->body . "\n");

        if (!$this->consume->getNoAck()) {
            $msg->delivery_info['channel']->basic_ack($msg->delivery_info['delivery_tag']);
        }
    }
}

Then you must to create consumer and pass him $helper and $consume.

$consumer = new Consumer($helper, $publication);

Then call method execute.

$consumer->execute();

If you want to interrupt the execution of the handler after a while, then use:

$seconds = 60;
$consumer->execute(true, null, false, $seconds);

This will interrupt execution after the specified time after processing the last message.

Producing

Create new publication.

$publication = new Publication([], 'hello');

Then you must to create producer and pass him $helper and $publication.

$producer = new Producer($helper, $consume);

You need payload for your producer. Producer can get payload from objects which implements JobInterface.

You can pass your job object to Producer object

$producer->execute(TestJob::dispatch($param));

Or you can pass producer to the method execute.

TestJob::dispatch($param)->execute($producer);

Job

Prepare payload for producer.

Dispatch

At the first you need to transfer data for preparation to dispatch.

use ATC\Jobs\TestJob;

TestJob::dispatch($param1, $param2, $param3);

Class declaration

Your TestJob must extends from Job

class TestJob extends BaseJob
{
    //
}

Handling

In TestJob you must to declare method handle with params which you pass in dispatch. handle will execute in dispatch.

    protected function handle($param1, $param2, $param3)
    {
        // Parameters will be passed to this class automatically
        // You can do something with them and get some $data
        
        // In the same method, you need to transfer the payload to the method `payload` for `producer`
        $this->setPayload($data);
    }

统计信息

  • 总下载量: 7.6k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 2
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 2
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2020-02-05

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固