如果处理结果不适合我,如何将消息返回队列。只找到有关消息确认的信息,但我认为它不适合我。我需要,如果作为处理结果我得到参数RETRY消息被添加回队列。然后这个工人或另一个工人再次拿起它并试图处理它。
例如:
<?php
use PhpAmqpLib\Connection\AMQPStreamConnection;
echo ' [*] Waiting for messages. To exit press CTRL+C', "\n";
$connection = new AMQPStreamConnection($AMQP);
$channel = $connection->channel();
$channel->queue_declare('test', false, false, false, false);
$callback = function($msg) {
$condition = json_decode($msg->body);
if (!$condition) {
# return to the queue
}
};
$channel->basic_consume('test', '', false, true, false, false, $callback);
while(count($channel->callbacks)) {
$channel->wait();
}
$channel->close();
$connection->close();
?>
答案 0 :(得分:0)
解决方案比我想象的更容易,事实证明任务不是专门针对RabbitMQ,而是关于变量的范围。如果有人对解决方案感兴趣,请点击此处:
<?php
use PhpAmqpLib\Connection\AMQPStreamConnection;
use PhpAmqpLib\Message\AMQPMessage;
echo ' [*] Waiting for messages. To exit press CTRL+C', "\n";
$connection = new AMQPStreamConnection($AMQP);
$channel = $connection->channel();
$channel->queue_declare('test', false, false, false, false);
$callback = function($msg) {
global $channel;
$condition = json_decode($msg->body);
if (!$condition) {
$msg = new AMQPMessage(json_encode(array(
'condition' => false
)));
$channel->basic_publish($msg, '', 'test');
}
};
$channel->basic_consume('test', '', false, true, false, false, $callback);
while(count($channel->callbacks)) {
$channel->wait();
}
$channel->close();
$connection->close();
?>
答案 1 :(得分:0)
将自动 no_ack 标志设为false
queue:从哪里获取消息的队列
consumer_tag:消费者标识符
no_local:不接收此消费者发布的消息 no_ack:告诉服务器消费者是否会确认消息 exclusive:请求独占的消费者访问权限,这意味着只有这个消费者可以访问队列
NOWAIT:
回调:PHP回调
$ channel-&gt; basic_consume('test','',false,false,false,false,$ callback);
你必须使用确认,如果你的过程不起作用,你可以忽略确认
<?php
use PhpAmqpLib\Connection\AMQPStreamConnection;
echo ' [*] Waiting for messages. To exit press CTRL+C', "\n";
$connection = new AMQPStreamConnection($AMQP);
$channel = $connection->channel();
$channel->queue_declare('test', false, false, false, false);
$callback = function($msg) {
$condition = json_decode($msg->body);
if (!$condition) {
// return to the queue
$message->delivery_info['channel']->basic_nack($message->delivery_info['delivery_tag']);
}else{
// send ack , remove from queue
$message->delivery_info['channel']->basic_ack($message->delivery_info['delivery_tag']);
}
};
$channel->basic_consume('test', '', false, false, false, false, $callback);
while(count($channel->callbacks)) {
$channel->wait();
}
$channel->close();
$connection->close();
?>