php接收rabbitMQ訊息並執行繁重任務

來源:互聯網
上載者:User
1) 建立訊息佇列基礎類
 '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)用戶端連續的接受隊列中的訊息,並執行相應的任務

_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模式下)執行時,用戶端直接退出,無報錯。

如果像下面這樣時,則是可以正常運行,並列印佇列中的訊息的

_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


回複討論(解決方案)

我覺得我很憂傷......這是要沉貼,翻船的節奏嗎?

ExecProcess是不是出問題了什麼

去掉require_once 'ExecProcess.class.php';並把處理訊息的邏輯去掉,是可以把隊列中的訊息列印出來的.....

看你描述,應該是ExecProcess.class.php中,處理訊息的部分出問題了,重點檢查這部分代碼,看看是什麼異常。

執行繁重任務才出錯,
可以檢查是否執行逾時導致。

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.