composer包php-amqplib

Posted 张发财的博客

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了composer包php-amqplib相关的知识,希望对你有一定的参考价值。

php-amqplib官方文档

url:http://www.rabbitmq.com/tutorials/tutorial-one-php.html

#测试demo: url: http://**.**.com/openapi/1.0/function/log/*

附上一个基本的例子:

TaskWithEx.php

public function get_taskWithEx(){
    //DEMO THREE
    $connection = new AMQPStreamConnection(‘192.168.6.30‘,‘5672‘,‘dpjia‘,‘dpjia‘);
    $channel = $connection->channel();

    $channel->exchange_declare(‘logs_dpj‘,‘fanout‘,false,false,false);
//没有queue绑定的exchange 会丢失,但如果有receive正在监听,消息还是可以被获取到<不建议>
    //$channel->queue_declare(‘task_qu‘,false,true,false,false);

    $data = ‘abc bcd cde def efg‘;

    $msg = new AMQPMessage($data,
                array(‘delivery_mode‘ => AMQPMessage::DELIVERY_MODE_PERSISTENT)
            );
    $channel->basic_publish($msg,  ‘logs_dpj‘);

    echo ‘send‘.$data."\n";

    $channel->close();
    $connection->close();
    }

WorkerWithEx.php

public function get_workerWithEx(){
    //DEMO TWO
    $connection = new AMQPStreamConnection(‘192.168.6.30‘,‘5672‘,‘dpjia‘,‘dpjia‘);
    $channel = $connection->channel();

    $channel->exchange_declare(‘logs_dpj‘, ‘fanout‘, false, false, false);

    list($queue_name, ,) = $channel->queue_declare("", false, false, true, false);

    $channel->queue_bind($queue_name, ‘logs_dpj‘);

    echo ‘ [*] Waiting for logs. To exit press CTRL+C‘."\n";

    $callback = function($msg){
      echo $msg->body."\n";
    };

    $channel->basic_consume($queue_name, ‘‘, false, true, false, false, $callback);

    // while(count($channel->callbacks)) {
    //     $channel->wait();
    // }

    $channel->close();
    $connection->close();
    }

以上是关于composer包php-amqplib的主要内容,如果未能解决你的问题,请参考以下文章

简单可靠的rabbitmq组件包 - php版本

RabbitMq初探——Hello World

RabbitMQ使用场景_001_简单使用

RabbitMQ 客户端开发向导

如何在片段内部使用Compose?

使用 compose 在 AndroidView 中使用片段时崩溃