婷婷综合国产,91蜜桃婷婷狠狠久久综合9色 ,九九九九九精品,国产综合av

主頁 > 知識庫 > PHP基于rabbitmq操作類的生產(chǎn)者和消費(fèi)者功能示例

PHP基于rabbitmq操作類的生產(chǎn)者和消費(fèi)者功能示例

熱門標(biāo)簽:哈爾濱400電話辦理到易號網(wǎng) 高識別電銷機(jī)器人 合肥外呼系統(tǒng)app h5 地圖標(biāo)注 電銷機(jī)器人-快迭智能 寶安400電話辦理 智能外呼電銷系統(tǒng) 沈陽人工智能電銷機(jī)器人公司 拉薩打電話機(jī)器人

本文實(shí)例講述了PHP基于rabbitmq操作類的生產(chǎn)者和消費(fèi)者功能。分享給大家供大家參考,具體如下:

注意事項(xiàng):

1、accept.php消費(fèi)者代碼需要在命令行執(zhí)行

2、'username'=>'asdf','password'=>'123456' 改成自己的帳號和密碼

RabbitMQCommand.php操作類代碼

?php
/*
 * amqp協(xié)議操作類,可以訪問rabbitMQ
 * 需先安裝php_amqp擴(kuò)展
 */
class RabbitMQCommand{
  public $configs = array();
  //交換機(jī)名稱
  public $exchange_name = '';
  //隊(duì)列名稱
  public $queue_name = '';
  //路由名稱
  public $route_key = '';
  /*
   * 持久化,默認(rèn)True
   */
  public $durable = True;
  /*
   * 自動(dòng)刪除
   * exchange is deleted when all queues have finished using it
   * queue is deleted when last consumer unsubscribes
   *
   */
  public $autodelete = False;
  /*
   * 鏡像
   * 鏡像隊(duì)列,打開后消息會(huì)在節(jié)點(diǎn)之間復(fù)制,有master和slave的概念
   */
  public $mirror = False;
  private $_conn = Null;
  private $_exchange = Null;
  private $_channel = Null;
  private $_queue = Null;
  /*
   * @configs array('host'=>$host,'port'=>5672,'username'=>$username,'password'=>$password,'vhost'=>'/')
   */
  public function __construct($configs = array(), $exchange_name = '', $queue_name = '', $route_key = '') {
    $this->setConfigs($configs);
    $this->exchange_name = $exchange_name;
    $this->queue_name = $queue_name;
    $this->route_key = $route_key;
  }
  private function setConfigs($configs) {
    if (!is_array($configs)) {
      throw new Exception('configs is not array');
    }
    if (!($configs['host']  $configs['port']  $configs['username']  $configs['password'])) {
      throw new Exception('configs is empty');
    }
    if (empty($configs['vhost'])) {
      $configs['vhost'] = '/';
    }
    $configs['login'] = $configs['username'];
    unset($configs['username']);
    $this->configs = $configs;
  }
  /*
   * 設(shè)置是否持久化,默認(rèn)為True
   */
  public function setDurable($durable) {
    $this->durable = $durable;
  }
  /*
   * 設(shè)置是否自動(dòng)刪除
   */
  public function setAutoDelete($autodelete) {
    $this->autodelete = $autodelete;
  }
  /*
   * 設(shè)置是否鏡像
   */
  public function setMirror($mirror) {
    $this->mirror = $mirror;
  }
  /*
   * 打開amqp連接
   */
  private function open() {
    if (!$this->_conn) {
      try {
        $this->_conn = new AMQPConnection($this->configs);
        $this->_conn->connect();
        $this->initConnection();
      } catch (AMQPConnectionException $ex) {
        throw new Exception('cannot connection rabbitmq',500);
      }
    }
  }
  /*
   * rabbitmq連接不變
   * 重置交換機(jī),隊(duì)列,路由等配置
   */
  public function reset($exchange_name, $queue_name, $route_key) {
    $this->exchange_name = $exchange_name;
    $this->queue_name = $queue_name;
    $this->route_key = $route_key;
    $this->initConnection();
  }
  /*
   * 初始化rabbit連接的相關(guān)配置
   */
  private function initConnection() {
    if (empty($this->exchange_name) || empty($this->queue_name) || empty($this->route_key)) {
      throw new Exception('rabbitmq exchange_name or queue_name or route_key is empty',500);
    }
    $this->_channel = new AMQPChannel($this->_conn);
    $this->_exchange = new AMQPExchange($this->_channel);
    $this->_exchange->setName($this->exchange_name);
    $this->_exchange->setType(AMQP_EX_TYPE_DIRECT);
    if ($this->durable)
      $this->_exchange->setFlags(AMQP_DURABLE);
    if ($this->autodelete)
      $this->_exchange->setFlags(AMQP_AUTODELETE);
    $this->_exchange->declare();
    $this->_queue = new AMQPQueue($this->_channel);
    $this->_queue->setName($this->queue_name);
    if ($this->durable)
      $this->_queue->setFlags(AMQP_DURABLE);
    if ($this->autodelete)
      $this->_queue->setFlags(AMQP_AUTODELETE);
    if ($this->mirror)
      $this->_queue->setArgument('x-ha-policy', 'all');
    $this->_queue->declare();
    $this->_queue->bind($this->exchange_name, $this->route_key);
  }
  public function close() {
    if ($this->_conn) {
      $this->_conn->disconnect();
    }
  }
  public function __sleep() {
    $this->close();
    return array_keys(get_object_vars($this));
  }
  public function __destruct() {
    $this->close();
  }
  /*
   * 生產(chǎn)者發(fā)送消息
   */
  public function send($msg) {
    $this->open();
    if(is_array($msg)){
      $msg = json_encode($msg);
    }else{
      $msg = trim(strval($msg));
    }
    return $this->_exchange->publish($msg, $this->route_key);
  }
  /*
   * 消費(fèi)者
   * $fun_name = array($classobj,$function) or function name string
   * $autoack 是否自動(dòng)應(yīng)答
   *
   * function processMessage($envelope, $queue) {
      $msg = $envelope->getBody();
      echo $msg."\n"; //處理消息
      $queue->ack($envelope->getDeliveryTag());//手動(dòng)應(yīng)答
    }
   */
  public function run($fun_name, $autoack = True){
    $this->open();
    if (!$fun_name || !$this->_queue) return False;
    while(True){
      if ($autoack) $this->_queue->consume($fun_name, AMQP_AUTOACK);
      else $this->_queue->consume($fun_name);
    }
  }
}

send.php生產(chǎn)者代碼

?php
set_time_limit(0);
include_once('RabbitMQCommand.php');
$configs = array('host'=>'127.0.0.1','port'=>5672,'username'=>'asdf','password'=>'123456','vhost'=>'/');
$exchange_name = 'class-e-1';
$queue_name = 'class-q-1';
$route_key = 'class-r-1';
$ra = new RabbitMQCommand($configs,$exchange_name,$queue_name,$route_key);
for($i=0;$i=100;$i++){
  $ra->send(date('Y-m-d H:i:s',time()));
}
exit();

accept.php消費(fèi)者代碼

?php
error_reporting(0);
include_once('RabbitMQCommand.php');
$configs = array('host'=>'127.0.0.1','port'=>5672,'username'=>'asdf','password'=>'123456','vhost'=>'/');
$exchange_name = 'class-e-1';
$queue_name = 'class-q-1';
$route_key = 'class-r-1';
$ra = new RabbitMQCommand($configs,$exchange_name,$queue_name,$route_key);
class A{
  function processMessage($envelope, $queue) {
    $msg = $envelope->getBody();
    $envelopeID = $envelope->getDeliveryTag();
    $pid = posix_getpid();
    file_put_contents("log{$pid}.log", $msg.'|'.$envelopeID.''."\r\n",FILE_APPEND);
    $queue->ack($envelopeID);
  }
}
$a = new A();
$s = $ra->run(array($a,'processMessage'),false);

更多關(guān)于PHP相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《PHP數(shù)據(jù)結(jié)構(gòu)與算法教程》、《php程序設(shè)計(jì)算法總結(jié)》、《php字符串(string)用法總結(jié)》、《PHP數(shù)組(Array)操作技巧大全》、《PHP常用遍歷算法與技巧總結(jié)》及《PHP數(shù)學(xué)運(yùn)算技巧總結(jié)》

希望本文所述對大家PHP程序設(shè)計(jì)有所幫助。

您可能感興趣的文章:
  • 安卓開發(fā)之mqtt協(xié)議實(shí)例代碼
  • springboot集成mqtt的實(shí)踐開發(fā)
  • vue使用stompjs實(shí)現(xiàn)mqtt消息推送通知
  • springboot實(shí)現(xiàn)rabbitmq的隊(duì)列初始化和綁定
  • docker部署rabbitmq集群的實(shí)現(xiàn)方法
  • springboot+RabbitMQ+InfluxDB+Grafara監(jiān)控實(shí)踐
  • RabbitMQ的配置與安裝教程全紀(jì)錄
  • Spring Boot RabbitMQ 延遲消息實(shí)現(xiàn)完整版示例
  • Docker MQTT安裝使用教程

標(biāo)簽:威海 梅州 山東 成都 林芝 張家口 巴中 泰州

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《PHP基于rabbitmq操作類的生產(chǎn)者和消費(fèi)者功能示例》,本文關(guān)鍵詞  PHP,基于,rabbitmq,操作,類,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《PHP基于rabbitmq操作類的生產(chǎn)者和消費(fèi)者功能示例》相關(guān)的同類信息!
  • 本頁收集關(guān)于PHP基于rabbitmq操作類的生產(chǎn)者和消費(fèi)者功能示例的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章
    主站蜘蛛池模板: 苏尼特右旗| 连南| 无锡市| 阿尔山市| 甘南县| 观塘区| 晴隆县| 锦屏县| 横山县| 东宁县| 阳新县| 淳化县| 澎湖县| 陇川县| 建德市| 陕西省| 连云港市| 吐鲁番市| 石景山区| 青铜峡市| 资兴市| 云南省| 民丰县| 房山区| 丘北县| 万载县| 罗田县| 鸡西市| 诸城市| 苍溪县| 通海县| 财经| 灵武市| 仪征市| 昭觉县| 文安县| 长子县| 江阴市| 霍州市| 台东县| 富平县|