现在的位置: 首页 > 综合 > 正文

HTTPSQS:基于HTTP协议的轻量级开源简单队列服务(安装php客户端)

2012年12月01日 ⁄ 综合 ⁄ 共 5057字 ⁄ 字号 评论关闭

HTTPSQS 客户端

PHP 客户端 使用 httpsqs的方式:

A、PHP 客户端扩展(第三方提供,详情请访问:http://code.google.com/p/php-httpsqs-client/

 

     一 、 安装说明

       cd /usr/local

       mkdir php_httpsqs_client
      cd php_httpsqs_client

    下载地址  :  wget  http://php-httpsqs-client.googlecode.com/files/php_httpsqs_client_0.2.1.tar.gz

      tar zxvf php_httpsqs_client_0.2.1.tar.gz
     /usr/local/php/bin/phpize
  ./configure--enable-httpsqs--with-php-config=/usr/local/php/bin/php-config
  make&&make install
   #接着在php.ini中添加一行
  extension=httpsqs.so;

  重启apache

 /usr/local/apache/bin/apachectl restart

查看phpinfo(),如果存在httpsqs,则配置成功;

 

二、在程序中使用httpsqs的实例

// 取数据Daemon

//方式一: 函数调用

$hr=httpsqs_connect($host,$port);
while(1){
    $data=httpsqs_get($hr,$queuename,$charset);
   if($data===false){
       sleep(1);
   }else{
     // do something...
   }
}

//方式一: 对象调用
$hr=newHttpSQS($host,$port);
while(1){
    $data=$hr->get($queuename,$charset);
   if($data===false){
        sleep(1);
   }else{
     // do something...
   }
}

// 写数据

//方式一:函数调用

$hr=httpsqs_connect($hort,$port);
httpsqs_put($hr,$queuename,$data,$charset);

//方式一:对象调用
$hr=newHttpSQS($hort,$port);
$hr->put($queuename,$data,$charset);

 

三、函数说明

/**
 * 创建httpsqs连接
 * @param string $host 服务器地址,可以为空,默认为127.0.0.1
 * @param int    $port 服务器端口,可以为空,默认为1218
 * @return resource
 */
$hr = httpsqs_connect("127.0.0.1",1218);


/**
 * 写入队列数据
 * @param resource $hr      服务器连接句柄
 * @param string   $queue   队列名称
 * @param string   $data    写入数据
 * @param string   $charset 字符集,可以为空,默认为utf-8
 * @return boolean
 */
$putRes = httpsqs_put($hr,"testQueue","This is a test Data","UTF-8");


/**
 * 获取队列最后一条数据
 * @param resource $hr
 * @param string   $queue
 * @param boolean  $return_array 是否返回数组,可以为空,默认为false
          返回数组格式:array('pos'=>'队列插入点', 'data'=>'数据值')
 * @param string   $charset 可以为空
 * @return mixed
 */
$content = httpsqs_get($hr,"testQueue",true,"UTF-8");


/**
 * 获取队列状态
 * @param resource $hr
 * @param string   $queue
 * @param boolean  $return_json 是否返回状态的json格式,可以为空,默认为false
 * @return string
 */
$status = httpsqs_status($hr,"testQueue",true);



/**
 * 获取队列某个点数据
 * @param resource $hr
 * @param string   $queue
 * @param int      $pos 要获取的某条数据的位置
 * @param string   $charset 可以为空
 * @return string
 */
$posData = httpsqs_view($hr,"testQueue",10,"UTF-8");



/**
 * 队列重置
 * @param resource $hr
 * @param string   $queue
 * @return boolean
 */
$resetRes = httpsqs_reset($hr,"testQueue");



/**
 * 设置队列最大数据条数
 * @param resource $hr
 * @param string   $queue
 * @param int      $maxqueue 队列最大数据条数
 * @return boolean
 */
$maxqueueRes = httpsqs_maxqueue($hr,"testQueue",10000);



/**
 * 修改定时刷新内存缓冲区内容到磁盘的间隔时间
 * @param resource $hr
 * @param string   $queue
 * @param int      $synctime 间隔时间
 * @return boolean
 */
$synctimeRes = httpsqs_synctime($hr,"testQueue",10);


四、对象调用

// 参数与httpsqs_connect对应
$hr =newHttpSQS($host, $port);

// 参数与httpsqs_get对应
$hr->get($queuename, $return_array, $charset);

// 参数与httpsqs_put对应
$hr->put($queuename, $data, $charset);

// 参数与httpsqs_status对应
$hr->status($queuename, $return_json);

// 参数与httpsqs_view对应
$hr->view($queuename, $pos);

// 参数与httpsqs_reset对应
$hr->reset($queuename);

// 参数与httpsqs_maxqueue对应
$hr->maxqueue($queuename);

// 参数与httpsqs_synctime对应
$hr->synctime($queuename);


用函数和对象调用的方式都是可以的;


B、普通 PHP 客户端文件:

  查看 PHP Class 源代码:httpsqs_client.php(http://code.google.com/p/httpsqs/source/browse/trunk/client/php/httpsqs_client.php)

用法:

 

<?php

......

include_once("httpsqs_client.php");

$httpsqs = new httpsqs; 

/* -----不使用Keep-Alive的短连接方式----- */ 

 /*  1. 将文本信息放入一个队列     如果入队列成功,返回布尔值:true      如果入队列失败,返回布尔值:false  */

$result = $httpsqs->put($host, $port, $charset, $name, $data); 

/*  2. 从一个队列中取出文本信息     返回该队列的内容    

如果没有未被取出的队列,则返回文本信息:HTTPSQS_GET_END    

如果发生错误,返回布尔值:false 

*/ 

 $result = $httpsqs->get($host, $port, $charset, $name);

/*  3. 从一个队列中取出文本信息和当前队列读取点Pos     返回数组示例:array("pos" => 7, "data" =&gt; "text message")     如果没有未被取出的队列,则返回数组:array("pos" => 0, "data" =&gt; "HTTPSQS_GET_END")     如果发生错误,返回布尔值:false */  

$result = $httpsqs->gets($host, $port, $charset, $name);

/*  4. 查看队列状态(普通方式) */

$result = $httpsqs->status($host, $port, $charset, $name);

/*  5. 查看队列状态(JSON方式)     返回示例:{"name":"queue_name","maxqueue":5000000,"putpos":130,"putlap":1,"getpos":120,"getlap":1,"unread":10} */ 

$result = $httpsqs->status_json($host, $port, $charset, $name);

/*  6. 查看指定队列位置点的内容     返回指定队列位置点的内容。 */

$result = $httpsqs->view($host, $port, $charset, $name, $pos);

/*  7. 重置指定队列     如果重置队列成功,返回布尔值:true      如果重置队列失败,返回布尔值:false  */

$result = $httpsqs->reset($host, $port, $charset, $name);

/*  8. 更改指定队列的最大队列数量    如果更改成功,返回布尔值:true    如果更改操作被取消,返回布尔值:false */

$result = $httpsqs->maxqueue($host, $port, $charset, $name, $num);

/* 9. 修改定时刷新内存缓冲区内容到磁盘的间隔时间    如果更改成功,返回布尔值:true    如果更改操作被取消,返回布尔值:false */

$result = $httpsqs->synctime($host, $port, $charset, $name, $num);


/* -----使用Keep-Alive的长连接方式 (在命令行模式下速度非常快)----- */

$result = $httpsqs->pput($host, $port, $charset, $name, $data); 

$result = $httpsqs->pget($host, $port, $charset, $name);

 $result = $httpsqs->pgets($host, $port, $charset, $name);

 $result = $httpsqs->pstatus($host, $port, $charset, $name);  

$result = $httpsqs->pstatus_json($host, $port, $charset, $name); 

$result = $httpsqs->pview($host, $port, $charset, $name, $pos);

 $result = $httpsqs->preset($host, $port, $charset, $name);

 $result = $httpsqs->pmaxqueue($host, $port, $charset, $name, $num); 

$result = $httpsqs->psynctime($host, $port, $charset, $name, $num);

?>


抱歉!评论已关闭.