PHP Rabbit MQ从单个队列读取特定消息

时间:2018-11-07 11:10:53

标签: php rabbitmq

我需要帮助来消耗队列中的特定值,我正在跟踪RabbitMQ网站上的示例:http://www.rabbitmq.com/tutorials/tutorial-five-php.html

基本上,我将与一些路由键进行交换(主题),每个路由键都指向一个队列。

交换:SAC

队列=萨卡亚米(sac-yami)

enter image description here

还有需要读取特定值的信息,例如:

排队sac-yami有3条记录,

  • “ 101”
  • “ 102”
  • “ 103”

我是通过这种方式插入的。

  • php generate_log_topic.php“ protocol.101”“ 101”
  • php generate_log_topic.php“ protocol.102”“ 102”
  • php generate_log_topic.php“ protocol.103”“ 103”

我需要从队列中删除103,该怎么做?

这是我的代码:发出..

<?php

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

$exchange = "sac";
$queue = "sac-yami";
$type = 'topic';
$message = implode(' ', array_slice($argv, 2));

$connection = new AMQPStreamConnection('MYHOST', PORT, 'USER', 'PASS','/');
$channel = $connection->channel();
$routing_key = isset($argv[1]) && !empty($argv[1]) ? $argv[1] : 'anonymous.info';
$channel->queue_declare($queue, false, true, false, false);
$channel->exchange_declare($exchange, $type, false, true, false);
$channel->queue_bind($queue, $exchange, $routing_key);

$msg = new AMQPMessage($message);
$return = $channel->basic_publish($msg, $exchange, $routing_key);
$channel->close();
$connection->close();

阅读:

$rabbit->connection = new AMQPStreamConnection($rabbit->host, $rabbit->port, $rabbit->user, $rabbit->pass, $rabbit->vhost);
$rabbit->channel = $rabbit->connection->channel();

$rabbit->channel->queue_declare($queue, false, true, false, false);
$rabbit->channel->exchange_declare($queue, 'topic', false, true, false);
$rabbit->channel->queue_bind($queue, $routingKey);
$rabbit->channel->basic_consume($queue, $consumerTag, false, false, false, false, array($rabbit, 'processMessage'));

    function processMessage($message)
        {
            $message->delivery_info['channel']->basic_ack($message->delivery_info['delivery_tag']);
    print_r($message);
        }

1 个答案:

答案 0 :(得分:1)

由于所有三个消息都路由到同一个队列(sac-yami),因此您必须从该队列进行读取才能读取消息。它们将按照其发布的顺序发送-101、102和103。在其他两个消息之前,无法读取消息103。


注意: RabbitMQ团队监视the rabbitmq-users mailing list,并且有时仅在StackOverflow上回答问题。