php的rabbitmq队列的消费代码

2014-03-13 10:26 来源:www.chinab4c.com 作者:ecshop专家

    上一章节我们讲述了php的rabbitmq的持久化。这次我们讨论php的rabbitmq消费

$ename = "ename15";
$qname = "queue15";
$con   = new AMQPConnection();
$con->connect();
$ch    = new AMQPChannel($con);

$q     = new AMQPQueue($ch);
$q->setName($qname);
$q->setFlags(2);
$q->declare();

$ex    = new AMQPExchange($ch);
$ex->setName($ename);
$ex->setType(AMQP_EX_TYPE_DIRECT);
$ex->setFlags(2);
$ex->declare();
$q->bind($ename,$qname);
$message = $q->get(AMQP_AUTOACK);

if ( $message == false ) {
    printf("no messages in queue $queueName");
}
else{   
    echo $message->getBody(); // print Hello World!   
}

来源:http://www.chinab4c.com