Esempio n. 1
0
 /**
  * 消费消息
  * @param callable $p_mCallback 回调函数
  * @param int $p_iFlags
  * @return bool
  */
 public function consume($p_mCallback, $p_iFlags = AMQP_NOPARAM)
 {
     try {
         if ($this->bAutoAck && $p_iFlags == AMQP_NOPARAM) {
             $p_iFlags = AMQP_AUTOACK;
         }
         $this->oQueue->consume($p_mCallback, $p_iFlags);
         return true;
     } catch (AMQPExchangeException $e) {
         $this->setError($e->getCode(), $e->getMessage());
         bll_rabbitmq_logger::create(array('type' => 'consume', 'error' => $e->getMessage()));
         return false;
     }
 }
Esempio n. 2
0
 /**
  * 发送一个消息
  * @param string $p_sMsg 消息体
  * @param string $p_sRoutingKey 路由
  * @param int $p_iFlags 标记
  * @param array $p_aAttributes
  * @return bool
  */
 public function publish($p_sMsg, $p_sRoutingKey = '', $p_iFlags = AMQP_NOPARAM, $p_aAttributes = array())
 {
     try {
         if ($this->bDurable && !isset($p_aAttributes['delivery_mode'])) {
             $p_aAttributes['delivery_mode'] = '2';
         }
         if ($p_sRoutingKey == '') {
             $p_sRoutingKey = $this->sRoutingKey;
         }
         $this->oExchange->publish($p_sMsg, $p_sRoutingKey, $p_iFlags, $p_aAttributes);
         return true;
     } catch (AMQPExchangeException $e) {
         $this->setError($e->getCode(), $e->getMessage());
         bll_rabbitmq_logger::create(array('type' => 'publish', 'error' => $e->getMessage()));
         return false;
     }
 }
Esempio n. 3
0
 /**
  * 创建实例的方法
  * @param string $p_sType
  * @param string $p_sProduct
  * @return object
  */
 public static function create($p_sType, $p_sProduct = 'default_producer')
 {
     $p_sType = strtolower($p_sType);
     $sKey = $p_sType . '_' . $p_sProduct;
     if (isset(self::$instances[$sKey])) {
         return self::$instances[$sKey];
     }
     if (!in_array($p_sType, array('producer', 'consumer'))) {
         bll_rabbitmq_logger::create(array('type' => 'create', 'error' => '[rabbitmq_factory] invalid rabbitmq type:' . $p_sType));
         return false;
     }
     $sClassPath = '/bll/rabbitmq/' . $p_sType;
     $sClassName = path_to_classname($sClassPath, '');
     if (!class_exists($sClassName)) {
         load_lib($sClassPath);
     }
     try {
         self::$instances[$sKey] = new $sClassName($p_sProduct);
         return self::$instances[$sKey];
     } catch (Exception $e) {
         bll_rabbitmq_logger::create(array('type' => 'create', 'error' => '[rabbitmq_factory] Class:' . $sClassName . ' init failed'));
         return false;
     }
 }
Esempio n. 4
0
 /**
  * 创建一个队列
  * @param string $p_sQueue
  * @return bool
  */
 public function createQueue($p_sQueue)
 {
     try {
         $this->oQueue = new AMQPQueue($this->oChannel);
         $this->oQueue->setName($p_sQueue);
         return $this->oQueue;
     } catch (AMQPQueueException $e) {
         $this->setError($e->getCode(), $e->getMessage());
         bll_rabbitmq_logger::create(array('type' => 'createQueue', 'error' => $e->getMessage()));
         return false;
     }
 }