1. 程式人生 > >composer包php-amqplib

composer包php-amqplib

composer pub 基本 .com bcd demo pan pos 官方文檔

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