为什么我的Perl的RabbitMQ消费者会出现404错误?

时间:2011-09-28 15:23:23

标签: perl rabbitmq

设置RabbitMQ,我可以使用示例脚本:

use Net::RabbitMQ;
my $mq = Net::RabbitMQ->new();
$mq->connect("localhost", { user => "guest", password => "guest" });
$mq->channel_open(1);
$mq->publish(1, "queuename", "Hi there!");
$mq->disconnect();

它发布消息(我假设)。我尝试了以下方法,从队列中简单地抓取一条消息,然后得到404:

my $mq = Net::RabbitMQ->new();
$mq->connect("localhost", { user => "guest", password => "guest" });
$mq->channel_open(1);
print $mq->get(1, "queuename");

2 个答案:

答案 0 :(得分:3)

错误消息的全文是:

basic_get: server channel error 404, message: NOT_FOUND - no queue 'queuename' in vhost '/' ...

您需要使用auto_delete => 0创建队列 - 否则在第一个进程终止时它将消失。看看queue_declare方法。

答案 1 :(得分:0)

我查看queue_declare并将其添加到侦听器并更改了一些代码,如下所示:

#!/usr/bin/perl

use strict;
use Data::Dumper;
use Net::RabbitMQ;
my $channel = 1;
my $queue = "MyQueue.q";
my $exchange = "MyExchange.x";
my $routing_key = "foobar";
my $mq = Net::RabbitMQ->new();
$mq->connect("localhost", { user => "guest", password => "guest" });
$mq->channel_open($channel);
$mq->exchange_declare( $channel, $exchange, { auto_delete => 0, });
$mq->queue_declare( $channel, $queue, { auto_delete => 0, });
$mq->queue_bind( $channel, $queue, $exchange, $routing_key);

while(1){
    my $hashref = $mq->get($channel, $queue);
    next if (! defined($hashref));
    print Dumper($hashref);
}

我关闭了侦听器脚本,然后,当我执行以下操作时,它会发布消息:

#!/usr/bin/perl
my $channel = 1;
my $queue = "MyQueue.q";
my $exchange = "MyExchange.x";
my $routing_key = "foobar";
use Net::RabbitMQ;
my $mq = Net::RabbitMQ->new();
$mq->connect("localhost", { user => "guest", password => "guest" });
$mq->channel_open(1);
$mq->publish($channel, $queue, "Message Here");
$mq->disconnect();