基于amqp的PHP封装类

只对比较简单的direct模式进行了封装,队列支持持久化、镜像以及auto-delete。

(但是不支持交换机的auto-delete,不知道扩展中为什么没与支持)

先执行消费者,创建对交换机、队列并与路由键绑定,并监听消息队列;

再执行生产者,向已创建的交换机中发送消息。

直接上代码了:

  1. /** 
  2.  * 消费者类 
  3.  */  
  4. class AsynMessageConsumer{  
  5.     private $config = array();  
  6.     private $durable = True;  
  7.     private $mirror = False;  
  8.     private $autodelete = False;  
  9.     private $conn = Null;  
  10.     private $channel = Null;  
  11.     private $queue = Null;  
  12.     public $is_ready = False;  
  13.   
  14.     /** 
  15.      * 创建连接、交换机、队列,并绑定 
  16.      * @param array $config RabbitMQ服务器信息 
  17.      * @param string $e_name 交换机名称 
  18.      * @param string $k_route 路由键 
  19.      * @param string $q_name 队列名称 
  20.      * @param bool $durable 队列是否持久化 
  21.      * @param bool $mirror 队列是否镜像 
  22.      * @return void  
  23.      */  
  24.     public function __construct($config$e_name$k_route$q_name$durable = True, $mirror = False, $autodelete = False){  
  25.         if (!($config && $e_name && $q_name && $k_route)) return False;  
  26.         $this->config = $config;  
  27.         if (!self::connect()) return False;  
  28.         $this->channel = new AMQPChannel($this->conn);    
  29.         $this->durable = (bool)$durable;  
  30.         $this->mirror = (bool)$mirror;  
  31.         $this->autodelete = (bool)$autodelete;  
  32.         $this->establishExchange($e_name);  
  33.         $this->establishQueue($q_name$e_name$k_route);  
  34.         $this->is_ready = True;  
  35.     }  
  36.   
  37.     /** 
  38.      * 循环阻塞方式接收消息 
  39.      * @param string $fun_name 自定义处理函数的函数名 
  40.      * @param bool $autoack 是否自动发送ACK应答,否则需要在自定义处理函数中手动发送 
  41.      * @return bool  
  42.      */  
  43.     public function run($fun_name$autoack = True){  
  44.         $fun_name = strval($fun_name);  
  45.         if (!$fun_name || !$this->queue) return False;  
  46.         while(True){  
  47.             if ($autoack$this->queue->consume($fun_name, AMQP_AUTOACK);   
  48.             else $this->queue->consume($fun_name);    
  49.         }  
  50.     }  
  51.   
  52.     // 以下为私有方法,无需手动调用  
  53.   
  54.     /** 
  55.      * 创建链接 
  56.      * 无法链接时则会自动选择下一个配置项(IP不通的情况下会有5秒等待) 
  57.      * @param int $i 配置项索引 
  58.      * @return bool 
  59.      */  
  60.     private function connect($i = 0){  
  61.         if (array_key_exists($i$this->config)){  
  62.             try{  
  63.                 $this->conn = new AMQPConnection($this->config[$i]);  
  64.                 $this->conn->connect();  
  65.                 $ret = True;      
  66.             }catch(AMQPConnectionException $e){  
  67.                 $ret = $this->connect(++$i);  
  68.             }  
  69.         } else {  
  70.             $ret = False;      
  71.         }  
  72.         return $ret;  
  73.     }  
  74.   
  75.     /** 
  76.      * 创建交换机 
  77.      * @param string $name 名称 
  78.      * @return int 
  79.      */  
  80.     private function establishExchange($name){  
  81.         $ex = new AMQPExchange($this->channel);    
  82.         $ex->setName($name);  
  83.         $ex->setType(AMQP_EX_TYPE_DIRECT); //direct类型   
  84.         if ($this->durable) $ex->setFlags(AMQP_DURABLE); //持久化  
  85.         return $ex->declare();    
  86.     }  
  87.       
  88.     /** 
  89.      * 创建队列 
  90.      * @param string $name 名称 
  91.      * @param string $e_name 交换机名称 
  92.      * @param string $k_route 路由键 
  93.      * @return int 
  94.      */  
  95.     private function establishQueue($name$e_name$k_route){  
  96.         $this->queue = new AMQPQueue($this->channel);    
  97.         $this->queue->setName($name);  
  98.         if ($this->durable) $this->queue->setFlags(AMQP_DURABLE); //持久化  
  99.         if ($this->mirror) $this->queue->setArgument('x-ha-policy''all'); //镜像  
  100.         if ($this->autodelete) $this->queue->setFlags(AMQP_AUTODELETE);  //auto-delete  
  101.         $this->queue->declare();    
  102.         $ret = $this->queue->bind($e_name$k_route);  
  103.         return $ret;  
  104.     }  
  105.   
  106.   
  107.     public function __destruct(){  
  108.         if ($this->conn){  
  109.             $this->conn->disconnect();  
  110.         }  
  111.     }  
  112. }  


  1. /** 
  2.  * 生产者类 
  3.  */  
  4. class AsynMessagePublisher{  
  5.     private $config = array();  
  6.     private $conn = Null;  
  7.     private $channel = Null;  
  8.     private $exchange = Null;  
  9.     public $is_ready = False;  
  10.   
  11.     /** 
  12.      * 创建连接,并指定交换机 
  13.      * @param array $config RabbitMQ服务器信息 
  14.      * @param string $e_name 交换机名称 
  15.      * @return void  
  16.      */  
  17.     public function __construct($config$e_name){  
  18.         if (!($config && $e_name)) return False;  
  19.         $this->config = $config;  
  20.         if (!self::connect()) return False;  
  21.         $this->channel = new AMQPChannel($this->conn);    
  22.         $this->establishExchange($e_name);  
  23.         $this->is_ready = True;  
  24.     }  
  25.   
  26.     /** 
  27.      * 发送消息 
  28.      * @param string $msg 消息体 
  29.      * @param string $k_route 路由键 
  30.      * @return int / False  
  31.      */  
  32.     public function send($msg$k_route){  
  33.         $msg = trim(strval($msg));  
  34.         if (!$this->exchange || $msg==='' || !$k_routereturn False;  
  35.         $ret = $this->exchange->publish($msg$k_route);  
  36.         return $ret;  
  37.     }  
  38.   
  39.     // 以下为私有方法,无需手动调用  
  40.   
  41.     /** 
  42.      * 创建链接 
  43.      * 无法链接时则会自动选择下一个配置项(IP不通的情况下会有5秒等待) 
  44.      * @param int $i 配置项索引 
  45.      * @return bool 
  46.      */  
  47.     private function connect($i = 0){  
  48.         if (array_key_exists($i$this->config)){  
  49.             try{  
  50.                 $this->conn = new AMQPConnection($this->config[$i]);  
  51.                 $this->conn->connect();  
  52.                 $ret = True;      
  53.             }catch(AMQPConnectionException $e){  
  54.                 $ret = $this->connect(++$i);  
  55.             }  
  56.         } else {  
  57.             $ret = False;      
  58.         }  
  59.         return $ret;  
  60.     }  
  61.   
  62.     /** 
  63.      * 创建交换机 
  64.      * @param string $name 名称 
  65.      * @return void 
  66.      */  
  67.     private function establishExchange($name){  
  68.         $this->exchange = new AMQPExchange($this->channel);    
  69.         $this->exchange->setName($name);  
  70.     }  
  71.   
  72.     public function __destruct(){  
  73.         if ($this->conn){  
  74.             $this->conn->disconnect();  
  75.         }  
  76.     }  
  77.   
  78. }  

使用比较简单,demo例子就不给了。

实际应用中也可以再用工厂模式进行改造。

posted @ 2013-02-02 12:27  孤火  阅读(1622)  评论(1编辑  收藏  举报