一个publisher发布消息 多个个customer接受消息
1:准备工作参照:
http://www.cnblogs.com/spicy/p/7886820.html
2,:路由:
3: 方法:”
public function callFunc($msg) { echo " [x] Received ", $msg->body, "\n"; if($msg->body==2){ sleep(50); }else{ sleep($msg->body); } // sleep(substr_count($msg->body, '.')); echo " [x] Done", "\n"; } public function worker() { set_time_limit(0); $connection = new AMQPStreamConnection('localhost', 5672, 'bitch', 'bitch'); $channel = $connection->channel(); $channel->queue_declare('hello', false, true); $receiver = new self(); #下面第四个参数如果为true表示开启确认模式,也就是消费以后会告知rabbitmq服务器该条消息已经处理完毕,这样可以方式消息处理一半挂掉了,结果服务器也删除了这条未处理完毕的消息 $channel->basic_consume('hello', '', false, true, false, false, [$receiver, 'callFunc']); while(true) { $channel->wait(); } $channel->close(); $connection->close(); } public function task() { $re = input('uid'); $connection = new AMQPStreamConnection('localhost', 5672, 'bitch', 'bitch'); $channel = $connection->channel(); $channel->queue_declare('hello', false, true); for($i=1; $i<11; $i++){ $msg = new AMQPMessage($i); $channel->basic_publish($msg, '', 'hello'); echo "第".$i.'发送完毕'; } $channel->close(); $connection->close(); }