<?php
/*
* publish-subscribe
* create by superid
*/
$queueName = 'superid';
$exchangeName = 'superid';
$routeKey = 'superid';
$conn_args = array(
'host' => '127.0.0.1',
'port' => '5672',
'login' => 'guest',
'password' => 'guest',
'vhost'=>'/'
);
$connection = new AMQPConnection($conn_args); //创建连接
$connection->connect() or die("Cannot connect to the broker!\n");
try {
$channel = new AMQPChannel($connection); // 建立一个 Channel信道
$exchange = new AMQPExchange($channel); //Create a switch object
$exchange->setName($exchangeName);
$exchange->setType(AMQP_EX_TYPE_DIRECT); //direct type
$exchange->setFlags(AMQP_DURABLE); //persistent
Change $exchange->declareExchange() ;
$queue = new AMQPQueue($channel);
$queue->setName($queueName);
$queue->setFlags(AMQP_DURABLE); //Persistent queue does not mean that the message will also persist
$queue->declareQueue() ;
$queue->bind($exchangeName, $routeKey); //Bind the exchange and queue and specify the routing key
for($i=0; $i<50000; ++$i){
$message = json_encode(array(
'id' => $i,
'name' => 'liming',
'note' => '这是一个备注',
));
$result = $exchange->publish($message,$routeKey);
var_dump("[{$result}-{$i}]");
}
} catch (AMQPConnectionException $e) {
var_dump($e);
exit();
}
$connection->disconnect();
?>
<?php
/*
* publish-subscribe
* create by superid
*/
$queueName = 'superid';
$exchangeName = 'superid';
$routeKey = 'superid';
$conn_args = array(
'host' => '127.0.0.1',
'port' => '5672',
'login' => 'guest',
'password' => 'guest',
'vhost'=>'/'
);
$connection = new AMQPConnection($conn_args);
$connection->connect() or die("Cannot connect to the broker!\n");
$channel = new AMQPChannel($connection); // Create a Channel channel
$exchange = new AMQPExchange($channel); //Create an exchange
$exchange->setName($exchangeName); //Set the name of the
exchange $exchange->setType(AMQP_EX_TYPE_DIRECT); //Direct type Exchange type: direct fanout topic
$ exchange->setFlags(AMQP_DURABLE); //Persistent
echo "Exchange Status:".$exchange->declareExchange()."\n";
$queue = new AMQPQueue($channel); //Create queue
$queue-> setName($queueName); //Set the name of the queue
$queue->setFlags(AMQP_DURABLE); //Set the persistent queue
echo "Message Total:".$queue->declareQueue()."\n"; //Queue total number of messages in
$queue->bind($exchangeName, $routeKey);
// receive messages in blocking mode
echo "Message:\n";
while(True){
$queue->consume('processMessage');
//自动ACK应答
//$queue->consume('processMessage', AMQP_AUTOACK);
}
$conn->disconnect();
/**
* Consumption callback function
* Processing message
* * Note: The queue object has two methods for fetching messages: consume and get.
*The former is blocking, it will be suspended when there is no message, and it is suitable for use in a loop; the latter is non-blocking
.
*/
function processMessage($envelope, $queue) {
$msg = $envelope->getBody();
echo $msg."\n"; //Process the message
//Manually send ACK response, if not confirmed, the message will be Accumulation more and more, resulting in serious bug
$queue->ack($envelope->getDeliveryTag());
}
?>