我对RabbitMQ很陌生.我正在使用带codeigniter的php-amqplib库,仍然想知道我所缺少的一些知识.
>为什么使用$channel-> wait()?
>为什么它总是驻留在无尽的while循环中?
>如何/可以绕过无限while循环.
就像在我的项目的一个用户想要向100k潜在客户广播新的广告活动的情况下,如果第二个用户要发送约100封邮件,则第二个用户会受到影响;第二个用户必须等待100k邮件首先发送,然后是最后一个用户轮到他了.
我需要针对并发消费者的解决方案,他们可以顺畅地工作而不影响其他人
这是我的代码段:
public function campaign2(){
$this->load->library('mylibrary');
for( $i=1;$i<=5;$i++ ) {
$url = "http://localhost/myproject/rabbit/waiting";
$param = array('index' => $i);
$this->waiting($i);
}
}
public function waiting($i)
{
ini_set('memory_limit','400M');
ini_set('max_execution_time', 0);
ini_set('display_errors', 1);
${'conn_'.$i} = connectRabbit();
${'channel_'.$i} = ${'conn_'.$i}->channel();
${'channel_'.$i}->exchange_declare('ha-local-campaign-'.$i.'-exchange', 'fanout', false, true, false);
$q = populateQueueName('campaign-'.$i);
${'channel_'.$i}->queue_declare($q, false, true, false, false);
${'channel_'.$i}->queue_bind($q, 'ha-local-campaign-'.$i.'-exchange', 'priority.'.$i);
$consumer_tag = 'campaign_consumer' ;
function process_message($msg) {
echo 'Mail Sent';
$msg->delivery_info['channel']->basic_ack($msg->delivery_info['delivery_tag']);
}
function shutdown($channel, $conn){
echo '['.date('H:i:s').'] Campaign consumer - Shutdown!!';
}
${'channel_'.$i}->basic_consume($q, $consumer_tag, false, false, true, false,'process_message');
while(1) {
${'channel_'.$i}->wait();
}
register_shutdown_function('shutdown', ${'channel_'.$i}, ${'conn_'.$i});
}
如果有人在整个过程中友好地指导我,我将不胜感激.
解决方法:
调用$channel-> wait()时,您是:
>检查通道的队列以查看是否有待处理的消息.
>对于每条消息,您将为相应通道的回调调用已注册的回调.
从“ hello world示例”中,逐步:
// First, you define `$callback` as a function receiving
// one parameter (the _message_).
$callback = function($msg) {
echo " [x] Received ", $msg->body, "\n";
};
// Then, you assign `$callback` the the "hello" queue.
$channel->basic_consume('hello', '', false, true, false, false, $callback);
// Finally: While I have any callbacks defined for the channel,
while(count($channel->callbacks)) {
// inspect the queue and call the corresponding callbacks
//passing the message as a parameter
$channel->wait();
}
// This is an infinite loop: if there are any callbacks,
// it'll run forever unless you interrupt script's execution.
让您的第二个用户发送使用不同的队列.您可以根据需要拥有任意数量的队列.