YII使用beanstalk队列

本文介绍了如何在CentOS系统上通过YUM安装beanstalk,并在YII框架中集成beanstalk队列。首先,通过yum命令安装beanstalk,然后下载并配置beanstalk的PHP扩展,将其放置在extensions目录下。虽然无法提供附件,但提供了PHP版本的队列核心代码。
摘要由CSDN通过智能技术生成

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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值