最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

 更新時(shí)間:2018年06月16日 08:50:12   作者:愛代碼也愛生活  
這篇文章主要介紹了PHP基于rabbitmq操作類的生產(chǎn)者和消費(fèi)者功能,結(jié)合實(shí)例形式分析了基于rabbitmq操作類的生產(chǎn)者和消費(fèi)者定義與使用方法,需要的朋友可以參考下

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

注意事項(xiàng):

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

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

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é)

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

相關(guān)文章

  • 一個(gè)簡(jiǎn)單安全的PHP驗(yàn)證碼類 附調(diào)用方法

    一個(gè)簡(jiǎn)單安全的PHP驗(yàn)證碼類 附調(diào)用方法

    這篇文章主要為大家分享了一個(gè)簡(jiǎn)單安全的PHP驗(yàn)證碼類,附調(diào)用方法,感興趣的小伙伴們可以參考一下
    2016-06-06
  • PHP實(shí)現(xiàn)多圖片上傳類實(shí)例

    PHP實(shí)現(xiàn)多圖片上傳類實(shí)例

    這篇文章主要介紹了PHP實(shí)現(xiàn)多圖片上傳類實(shí)例,該類文件注釋詳細(xì)功能實(shí)用且具有一定的擴(kuò)展性,需要的朋友可以參考下
    2014-07-07
  • PHP連接MySQL查詢結(jié)果中文顯示亂碼解決方法

    PHP連接MySQL查詢結(jié)果中文顯示亂碼解決方法

    MySQL和Apache兩個(gè)系統(tǒng)的編碼(字符集)問題讓我費(fèi)勁腦筋,吃盡苦頭。網(wǎng)上對(duì)這些問題的解決比較零散,比較片面,大部分是提供解決方法,卻不說為什么。于是我將這幾天收獲總結(jié)一下,避免后來者再走彎路
    2013-10-10
  • php獲取是星期幾的的一些常用姿勢(shì)

    php獲取是星期幾的的一些常用姿勢(shì)

    這篇文章主要給大家總結(jié)介紹了利用php獲取是星期幾的一些姿勢(shì),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用php具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-12-12
  • php empty,isset,is_null判斷比較(差異與異同)

    php empty,isset,is_null判斷比較(差異與異同)

    做php開發(fā)時(shí)候,想必在使用:empty,isset,is_null 這幾個(gè)函數(shù)時(shí)候,遇到一些問題。甚至給自己的程序帶來一些安全隱患的bug。很多時(shí)候,對(duì)于isset,empty都認(rèn)為差不多。因此開發(fā)時(shí)候,就沒有注意,一段作為流程判斷時(shí)候,就出現(xiàn)bug問題了。
    2010-10-10
  • php基于session實(shí)現(xiàn)數(shù)據(jù)庫交互的類實(shí)例

    php基于session實(shí)現(xiàn)數(shù)據(jù)庫交互的類實(shí)例

    這篇文章主要介紹了php基于session實(shí)現(xiàn)數(shù)據(jù)庫交互的類,實(shí)例分析了php結(jié)合session操作數(shù)據(jù)庫的相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下
    2015-08-08
  • thinkphp5使用bootstrapvalidator進(jìn)行異步驗(yàn)證郵箱的示例

    thinkphp5使用bootstrapvalidator進(jìn)行異步驗(yàn)證郵箱的示例

    這篇文章主要介紹了thinkphp5使用bootstrapvalidator進(jìn)行異步驗(yàn)證郵箱的示例,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2017-10-10
  • 使用PHP函數(shù)進(jìn)行網(wǎng)站性能監(jiān)控和優(yōu)化的代碼示例

    使用PHP函數(shù)進(jìn)行網(wǎng)站性能監(jiān)控和優(yōu)化的代碼示例

    在 PHP 中,有許多內(nèi)置函數(shù)和工具可以幫助我們實(shí)現(xiàn)網(wǎng)站的性能監(jiān)控和優(yōu)化,本文將介紹幾種常用的 PHP 函數(shù),并提供相應(yīng)的代碼示例,來幫助您更好地進(jìn)行網(wǎng)站性能監(jiān)控和優(yōu)化,需要的朋友可以參考下
    2024-06-06
  • php的命名空間與自動(dòng)加載實(shí)現(xiàn)方法

    php的命名空間與自動(dòng)加載實(shí)現(xiàn)方法

    這篇文章主要給大家介紹了關(guān)于php的命名空間與自動(dòng)加載實(shí)現(xiàn)方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用php具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-08-08
  • PHP6 中可能會(huì)出現(xiàn)的新特性預(yù)覽

    PHP6 中可能會(huì)出現(xiàn)的新特性預(yù)覽

    PHP核心團(tuán)隊(duì)開發(fā)者Pierre Joye近日在官網(wǎng)上貼出了PHP6中可能會(huì)出現(xiàn)的新特性,之所以說是“可能會(huì)出現(xiàn)”,是因?yàn)槟壳癙HP6項(xiàng)目還處于階段,這些特性還只是一個(gè)想法,還需要討論,隨時(shí)都有可能變更
    2014-04-04

最新評(píng)論

九寨沟县| 青州市| 太谷县| 车险| 泰宁县| 沅陵县| 高台县| 博白县| 绥化市| 昌图县| 九台市| 桐乡市| 安图县| 灵寿县| 华池县| 久治县| 神池县| 武威市| 历史| 阳西县| 龙胜| 合山市| 安龙县| 青冈县| 万山特区| 新丰县| 芮城县| 富蕴县| 兴化市| 乌鲁木齐县| 嘉祥县| 仁寿县| 福清市| 昭通市| 庆安县| 应城市| 荣成市| 西林县| 丹凤县| 绥芬河市| 湘乡市|