PHP receives RABBITMQ messages and performs heavy tasks

Source: Internet
Author: User
Tags usleep rabbitmq
1) Establishing the message Queue base class
  ' 127.0.0.1 ', ' port ' = ' 5672 ', ' vhost ' = '/', ' login ' = ' guest ', ' Password ' + ' guest '; constructor, create channel, switch, queue public function __construct () {try{$this->_connecthandler = new Amqpconn            Ection ($this->_config);            if (! $this->_connecthandler->connect ()) {die (' Connect failed ');            } $this->createchannel ();            $this->createexchange ();        $this->createqueue ();        } catch (Exception $e) {echo $e->getmessage (); }}//create channel protected function CreateChannel () {$this->_channelobject = new Amqpchannel ($this-&G    T;_connecthandler); }//Create switch Public function createexchange ($exchangeName = ", $exchangeType =amqp_ex_type_direct) {$exNam        E = $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 (); }//Create queue Public Function Createqueue () {$this->_queueobject = new Amqpqueue ($this->_channelobje        CT);        $this->_queueobject->setname ($this->_queuename); $this->_queueobject->setflags (amqp_durable |        Amqp_autodelete);        $this->_queueobject->declarequeue ();    $this->_queueobject->bind ($this->_exchangeobject->getname (), $this->_routekey); }  }


2) There is a task, will continuously push the message to the queue, accumulate, there will be a large number of messages in the queue ....

3) The client continuously accepts messages in the queue and performs the corresponding tasks

 
  _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;                }}            );    }}} $reciver = new Recv (); $reciver->recvmessage ();


Known require_once ' ExecProcess.class.php '; This class is no problem, individual execution can pass, but add to Message Queuing client, receive messages, and perform a heavy task when note: (in PHP-CLI mode) when executed, the client exits directly without error.

If this is the case, then it is OK to run and print the messages in the queue

 
  _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;                }}            );    }}} $reciver = new Recv (); $reciver->recvmessage ();


That is, the ExecProcess.class.php ' add in, receive the message client, will automatically exit, and will not error ...

Instead, remove the require_once ' ExecProcess.class.php ' and remove the logic that handles the message so that the messages in the queue can be printed .... I don't know what the hell.

Because I bought the book, I haven't had time to see it.

The problem is obvious, who can give me a clue, or a hint? 3Q


Reply to discussion (solution)

I think I am very sad ... Is this the rhythm of the sinking, the capsized?

Execprocess, is something wrong?

Remove the require_once ' ExecProcess.class.php ' and remove the logic from the processing message to print out the messages in the queue ....

Look what you describe, it should be ExecProcess.class.php, the part that handles the message is in trouble, focus on checking this part of the code to see what is abnormal.

There's a heavy task to go wrong.
You can check whether the execution time-out is causing.

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    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.