我有一个生产者向主题交换发送消息。每条消息都包含一个路由键。(为原始图道歉)
P
|
X
/| |\
/ | | \
/ | | \
Q1 Q2 Q3 Q4
| / / /
| / / /
| / / /
|/ / /
C
我正在使用php-amqplib并尝试使用多个队列。我想要实现的是按顺序测试每个队列,看看它是否有消息,如果有,处理它,否则,继续下一个队列。此外,如果找到消息,请从 Q1 重新开始检查过程。以下代码不起作用,但将演示我想要做的逻辑。
$connection = new AMQPConnection(HOST, PORT, USER, PASS, VHOST);
$channel = $connection->channel();
$channel->exchange_declare('myexchange', 'topic', false, false, false);
$channel->queue_declare("Q1", false, true, false, false);
$channel->queue_bind("Q1", 'myexchange', 'priority.1');
$channel->queue_declare("Q2", false, true, false, false);
$channel->queue_bind("Q2", 'myexchange', 'priority.2');
$channel->queue_declare("Q3", false, true, false, false);
$channel->queue_bind("Q3", 'myexchange', 'priority.3');
$channel->queue_declare("DFQ4", false, true, false, false);
$channel->queue_bind("DFQ4", 'myexchange', 'priority.4');
$queues = array('Q1','Q2','Q3','Q4');
$priority = 0;
while (1) {
$priority = ($priority<4)? $priority+1 : 0;
$msg = $channel->basic_consume($queues[$priority], $consumer_tag, false, false, false, false);
if(isset($msg->body)) {
echo ' [x] ',$msg->delivery_info['routing_key'], "\n";
$channel->basic_ack($msg->delivery_info['delivery_tag']);
$priority = 0;
}
}
$channel->close();
$connection->close();