RabbitMQ學習之(五)_一個基於PHP的RabbitMQ操作類

OldBoy~發表於2018-01-09
//amqp.php類檔案
<?php

class Amqp
{
    public $e_name;
    public $q_name;
    public $k_route;
    public $channel;

    public function __construct($config,$e_name,$q_name,$k_route)
    {
        $this->e_name = $e_name;
        $this->q_name = $q_name;
        $this->k_route = $k_route;

        //建立連線和channel 
        $this->conn = new AMQPConnection($config);    
        if (!$this->conn->connect()) {    
            return array('error_code' => 1,'msg'=>'Cannot connect to the broker!' );
        }
        $this->channel = new AMQPChannel($this->conn);
        $this->CreateExchange();
        $this->CreateQueue();
    }

    //建立交換機
    public function CreateExchange()
    {
        $ex = new AMQPExchange($this->channel);    
        $ex->setName($this->e_name);  
        $ex->setType(AMQP_EX_TYPE_DIRECT); //direct型別   
        $ex->setFlags(AMQP_DURABLE | AMQP_AUTODELETE); //持久化  
        //echo "Exchange Status:".$ex->declare()."\n";   //佇列內容總數
        $ex->declare();
        $this->ex = $ex;
    }
    
    //建立佇列   
    public function CreateQueue()
    {
        $q = new AMQPQueue($this->channel);  
        $q->setName($this->q_name);    
        $q->setFlags(AMQP_DURABLE | AMQP_AUTODELETE); //持久化   
        //echo "Message Total:".$this->q->declare()."\n";  
        //繫結交換機與佇列,並指定路由鍵  
        //echo "queue status: ".$q->declare();
        //echo "\n";
        //echo 'Queue Bind: '.$q->bind($this->e_name, $this->k_route)."\n"; 
        //echo "\n";
         
         $q->bind($this->e_name, $this->k_route);
    }
    
    //傳送訊息
    public function send($msg)
    { 
        //$this->CreateExchange();
        //$this->CreateQueue();
        $message=json_encode($msg);
        $this->channel->startTransaction();
        //echo "send: ".$this->ex->publish($message, $this->k_route); //將你的訊息通過制定routingKey傳送
        $status = $this->ex->publish($message, $this->k_route);
        $this->channel->commitTransaction();
        $this->conn->disconnect();
        return array('status'=>$status);
    }
    
    //獲取訊息
    public function get()
    {
        $q = new AMQPQueue($this->channel);  
        $q->setName($this->q_name);  
        $q->setFlags(AMQP_DURABLE | AMQP_AUTODELETE);  
        
        //$q->delete();刪除佇列
        $return=array();
        while($a=$q->declare())
        {
            //echo "queue status: ".$a;
            //echo "==========\n";

            $messages = $q->get(AMQP_AUTOACK);
            $return[]=json_decode($messages->getBody(),true);  
            //echo "\n";  
        }
        $this->conn->disconnect();
        return $return;
    }

}
//config.php配置檔案
return array(
    'amqp'=>array(    
                array(    
                        'host' => '127.0.0.1',
                        'port' => '5672',
                        'vhost' => '/',
                        'user' => 'admin',
                        'password' => 'admin'
                 )
            ),
);
//send.php(加入佇列檔案|生產者)
require_once('amqp.php');
        $e_name = 'e_guest'; //交換機名  
        $k_route = 'k_route_feedpush'; //路由key
        $q_name = 'q_guest_feedpush'; //佇列名
        $config = config('amqp');
        
        $amqp = new Amqp(config('amqp'),$e_name,$q_name,$k_route);

  $msg = array('test','123');

 $re = $amqp->send($msg);
//get.php(接收並處理檔案|消費者)

require_once('amqp.php');
$config = require('config.php');
$config_qmqp = $config['amqp'];

$e_name = 'e_guest'; //交換機名  
$k_route = 'k_route_sendemail'; //路由key 
$q_name = 'q_guest_sendemail'; //佇列名
$amqp = new Amqp($config_qmqp,$e_name,$q_name,$k_route);
$re = $amqp->get();

 

相關文章