RabbitMQ按优先级从队列中消费消息

时间:2018-04-13 06:53:51

标签: php rabbitmq

我的想法是希望收到“link 123456”,“code 1234”等消息。

链接号必须尽可能快地消耗,而代码可以在所有当前链接号都被消耗的情况下等待。来自这里的RamibMQ文档:https://www.rabbitmq.com/priority.html非常简短干净,但对我来说根本没用。 当我按照它时,消息完全忽略优先级,并按发送顺序显示。

我尝试在Java中实现它,如示例中所示,但结果是相同的。

发件人代码:

发件人优先级为5

<?php
require_once __DIR__ . '/vendor/autoload.php';
use PhpAmqpLib\Connection\AMQPStreamConnection;
use PhpAmqpLib\Message\AMQPMessage;
use PhpAmqpLib\Wire\AMQPTable;

$connection = new AMQPStreamConnection('localhost', 5672, 'guest', 'guest');
$channel = $connection->channel();
$table = [
    'maxPriority' => ['I', 10],
];
$channel->queue_declare('queue', false, true, false, false, false, $table);

$msg = new AMQPMessage('link: '.$_SERVER['argv'][1], [
                                              'delivery_mode' => 2,
                                              'priority' => 5,
                                            ]);
$channel->basic_publish($msg, '', 'queue');
$channel->close();
$connection->close();
?>

发件人优先级为1

<?php
require_once __DIR__ . '/vendor/autoload.php';
use PhpAmqpLib\Connection\AMQPStreamConnection;
use PhpAmqpLib\Message\AMQPMessage;
use PhpAmqpLib\Wire\AMQPTable;

$connection = new AMQPStreamConnection('localhost', 5672, 'guest', 'guest');
$channel = $connection->channel();
$table = [
    'maxPriority' => ['I', 10],
];
$channel->queue_declare('queue', false, true, false, false, false, $table);

$msg = new AMQPMessage('code: '.$_SERVER['argv'][1], [
                                              'delivery_mode' => 2,
                                              'priority' => 1,
                                            ]);
$channel->basic_publish($msg, '', 'queue');
$channel->close();
$connection->close();
?>

接收者代码:

<?php
    require_once __DIR__ . '/vendor/autoload.php';
    use PhpAmqpLib\Connection\AMQPStreamConnection;
    use PhpAmqpLib\Message\AMQPMessage;
    use PhpAmqpLib\Wire\AMQPTable;
    function s($str, $charset = null) {
        return new \Delight\Str\Str($str, $charset);
    }
    $connection = new AMQPStreamConnection('localhost', 5672, 'guest', 'guest');
    $channel = $connection->channel();
    $table = [
        'maxPriority' => ['I', 10],
    ];
    $channel->queue_declare('queue', false, true, false, false, false, $table);
    echo ' [*] Waiting for messages. To exit press CTRL+C', "\n";
    $callback = function($msg){
        file_put_contents('rabbit_logs.txt', $msg->body."\r\n", FILE_APPEND);
        if( (new \Delight\Str\Str($msg->body))->startsWith('code: ')==1){
            echo " [x] Received ", $msg->body, "\n";
            sleep(1);
            echo " [x] Code sent", "\n";
        }
        else{
            if( (new \Delight\Str\Str($msg->body))->startsWith('lead: ')==1){
                echo " [x] Received ", $msg->body, "\n";
                sleep(1);
                echo " [x] lead sent", "\n";
            }
            else{
                if( (new \Delight\Str\Str($msg->body))->startsWith('link: ')==1){
                    echo " [x] Received ", $msg->body, "\n";
                    sleep(1);
                    echo " [x] link sent", "\n";
                }
                else{
                    echo " [x] Received ", $msg->body, "\n";      
                    echo " [x] Nothing sent", "\n";
                }
            }
        }
        $msg->delivery_info['channel']->basic_ack($msg->delivery_info['delivery_tag']);
    };
    $channel->basic_qos(null, 1, null);
    $channel->basic_consume('queue', '', false, false, false, false, $callback);
    while(count($channel->callbacks)) {
        $channel->wait();
    }
    $channel->close();
    $connection->close();
?>

1 个答案:

答案 0 :(得分:0)

RabbitMQ团队监控the rabbitmq-users mailing list,有时只回答StackOverflow上的问题。

您需要使用x-max-priority参数并构建参数表,如this demo所示。