1.系统centos 我是直接使用yum install beanstalk安装的
2.下载beanstalk的php扩展包 放在extensions
//控制器往队列里面塞任务 @author yao
Yii::import('application.extensions.SendSmsBeanstalk');
class AController extends CController{
public function actionIndex(){
for($i=0;$i<1000;$i++){
SendSmsBeanstalk::sendSms('sendsms_'.$i);
}
}
public function actionA(){
SendSmsBeanstalk::handleMessage();
}
}
</pre><pre code_snippet_id="546591" snippet_file_name="blog_20141209_4_7698245" name="code" class="php">//发送短信 @author yao
Yii::import('application.extensions.beanstalk.*');
class SendSmsBeanstalk extends CComponent{
//建立发短信任务
public static function sendSms($body){
//实例化beanstalk
$beanstalk = new Socket_Beanstalk();
if (!$beanstalk->connect()) {
exit(current($beanstalk->errors()));
}
//选择使用的tube
$beanstalk->useTube('hube_send_smss');
//往tube中增加数据
$put = $beanstalk->put(
23, // 任务的优先级.
0, // 不等待直接放到ready队列中.
60, // 处理任务的时间.
$body // 任务内容
);
if (!$put) {
return false;
}
$beanstalk->disconnect();
}
//处理消息
public static function handleMessage(){
//实例化beanstalk
$beanstalk = new Socket_Beanstalk();
if (!$beanstalk->connect()) {
exit(current($beanstalk->errors()));
}
$beanstalk->useTube('hube_send_smss');
//设置要监听的tube
$beanstalk->watch('hube_send_smss');
//取消对默认tube的监听,可以省略
$beanstalk->ignore('default');
while($job = $beanstalk->reserve(2)){//这里我写了单个任务只执行2秒。防止超时。本地测试的时候 没写超时会一直执行下去,哪怕队列里面已经没有任务
//处理任务
$result = $job['body'];
echo $job['id'];echo '<br>';//打印任务ID
if ($result) {<span style="font-family: Arial, Helvetica, sans-serif;">//这里可以写逻辑 todo</span>
//删除任务
$beanstalk->delete($job['id']);
} else {
//休眠任务
$beanstalk->bury($job['id'],'');
}
}
$beanstalk->disconnect();
}
下方提供我找到的队列PHP版本core,不能上传附件。代码在下面
/**
* beanstalk: A minimalistic PHP beanstalk client.
*
* Copyright (c) 2009-2012 David Persson
*
* Distributed under the terms of the MIT License.
* Redistributions of files must retain the above copyright notice.
*
* @copyright 2009-2012 David Persson <nperson@gmx.de>
* @license http://www.opensource.org/licenses/mit-license.php The MIT License
* @link http://github.com/davidpersson/beanstalk
*/
/**
* An interface to the beanstalk queue service. Implements the beanstalk
* protocol spec 1.2. Where appropriate the documentation from the protcol has
* been added to the docblocks in this class.
*
* @link https://github.com/kr/beanstalkd/blob/master/doc/protocol.txt
*/
class Socket_Beanstalk {
/**
* Holds a boolean indicating whether a connection to the server is
* currently established or not.
*
* @var boolean
*/
public $connected = false;
/**
* Holds configuration values.
*
* @var array
*/
protected $_config = array();
/**
* The current connection resource handle (if any).
*
* @var resource
*/
protected $_connection;
/**
* Generated errors. Will hold a maximum of 200 error messages at any time
* to prevent pilling up messages and using more and more memory. This is
* especially important if this class is used in long-running workers.
*
* @see Socket_Beanstalk::errors()
* @see Socket_Beanstalk::_error()
* @var array
*/
protected $_errors = array();
/**
* Constructor.
*
* @param array $config An array of configuration values:
* - `'persistent'` Whether to make the connection persistent or
* not, defaults to `true` as the FAQ recommends
* persistent connections.
* - `'host'` The beanstalk server hostname or IP address to
* connect to, defaults to `127.0.0.1`.
* - `'port'` The port of the server to connect to, defaults
* to `11300`.
* - `'t