php接收rabbitMQ消息并执行繁重任务

php中文网
发布: 2016-06-23 13:07:03
原创
1277人浏览过

1) 建立消息队列基础类

<?php/** * @desc 消息队列 * @author caifangjie * @date 2016/05/03 */class queue{    //交换机名称    protected $_exchangename = 'ex_auto_home';        //队列名称    protected $_queuename = 'qu_auto_home';        //路由    protected $_routekey = 'ru_auto_home';        protected $_connecthandler;        protected $_channelobject;    protected $_exchangeobject;        protected $_queueobject;        //配置信息    protected $_config = array('host' => '127.0.0.1', 'port' => '5672', 'vhost' => '/', 'login' => 'guest', 'password' => 'guest');    //构造函数,依次创建通道,交换机,队列    public function __construct()    {        try{            $this->_connecthandler = new amqpconnection($this->_config);            if(!$this->_connecthandler->connect()) {                die('connect failed');            }            $this->createchannel();            $this->createexchange();            $this->createqueue();        } catch(exception $e) {            echo $e->getmessage();        }    }        //创建通道    protected function createchannel()    {        $this->_channelobject = new amqpchannel($this->_connecthandler);    }        //创建交换机    public function createexchange($exchangename='', $exchangetype=amqp_ex_type_direct)    {        $exname = $exchangename?$exchangename:$this->_exchangename;        $this->_exchangeobject = new amqpexchange($this->_channelobject);        $this->_exchangeobject->setname($exname);        $this->_exchangeobject->settype($exchangetype);        $this->_exchangeobject->setflags(amqp_durable | amqp_autodelete);        $this->_exchangeobject->declareexchange();    }        //创建队列    public function createqueue()    {        $this->_queueobject = new amqpqueue($this->_channelobject);        $this->_queueobject->setname($this->_queuename);        $this->_queueobject->setflags(amqp_durable | amqp_autodelete);        $this->_queueobject->declarequeue();        $this->_queueobject->bind($this->_exchangeobject->getname(), $this->_routekey);    }  }
登录后复制


2)有一个任务,会连续向队列中推送消息,累计起来,队列中会有大量的消息.....

3)客户端连续的接受队列中的消息,并执行相应的任务

<?phprequire_once 'execprocess.class.php';require_once 'queue/queue.class.php';class recv extends queue{    public function __construct()    {        parent::__construct();    }    //接受消息    public function recvmessage()    {        while (true) {            $this->_queueobject->consume(function(amqpenvelope $e, amqpqueue $q) {                $requesturl = $e->getbody();                if ($requesturl) {                   // var_dump($requesturl);                    $exechandler = new execprocess();                    $exechandler->start($requesturl);                    $exechandler->execsave();                    unset($exechandler);                    $q->nack($e->getdeliverytag());                } else {                    usleep(100);                }            });        }    }}$reciver = new recv();$reciver->recvmessage();
登录后复制


已知 require_once 'execprocess.class.php'; 这个类是没有问题的,单独执行可以通过,但是加到消息队列的客户端,接收消息,并执行一个繁重任务时,注:(php-cli模式下)执行时,客户端直接退出,无报错。

如果像下面这样时,则是可以正常运行,并打印队列中的消息的

<?php//require_once 'execprocess.class.php';require_once 'queue/queue.class.php';class recv extends queue{    public function __construct()    {        parent::__construct();    }    //接受消息    public function recvmessage()    {        while (true) {            $this->_queueobject->consume(function(amqpenvelope $e, amqpqueue $q) {                $requesturl = $e->getbody();                if ($requesturl) {                    var_dump($requesturl);//                    $exechandler = new execprocess();//                    $exechandler->start($requesturl);//                    $exechandler->execsave();//                    unset($exechandler);                    $q->nack($e->getdeliverytag());                } else {                    usleep(100);                }            });        }    }}$reciver = new recv();$reciver->recvmessage();
登录后复制


也就是把execprocess.class.php'加进来时,接收消息的客户端,会自动退出,而且不会报错。。。

相反,去掉require_once 'execprocess.class.php';并把处理消息的逻辑去掉,是可以把队列中的消息打印出来的.....不知道是什么鬼

因为买的书,还没来得及看。

问题因该是很明显的,谁能给我一个思路,或者提示? 3q

GAIPPT
GAIPPT

AI PPT制作和美化神器

GAIPPT 1129
查看详情 GAIPPT


回复讨论(解决方案)

我觉得我很忧伤......这是要沉贴,翻船的节奏吗?

ExecProcess是不是出问题了什么

去掉require_once 'ExecProcess.class.php';并把处理消息的逻辑去掉,是可以把队列中的消息打印出来的.....

看你描述,应该是ExecProcess.class.php中,处理消息的部分出问题了,重点检查这部分代码,看看是什么异常。

执行繁重任务才出错,
可以检查是否执行超时导致。

PHP速学教程(入门到精通)
PHP速学教程(入门到精通)

PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号