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

rsl1atfo  于 2023-10-15  发布在  PHP
关注(0)|答案(1)|浏览(117)

我需要一个帮助来消耗我的队列中的特定值,我下面的例子从rabbitMQ网站:http://www.rabbitmq.com/tutorials/tutorial-five-php.html
基本上,我将有一个交换(主题)与一些路由键,每个路由键指向一个队列。
交易所:SAC
队列= sac-yami

和a需要读取特定值ex:
有3条记录,

  • “一百零一”
  • “一百零二”
  • “一百零三”

我是这样插进去的。

  • php emit_log_topic.php“protocol.101”“101”
  • php emit_log_topic.php“protocol.102”“102”
  • php emit_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);
        }
vdgimpew

vdgimpew1#

由于这三条消息都被路由到同一个队列(sac-yami),因此您必须从该队列中消费才能读取消息。它们将按照出版的顺序--第101、102和103页--分发。第103条信息不可能在其他两条信息之前读到。

相关问题