Why do I get a 404 error with Perl's RabbitMQ consumer?
RabbitMQ is setup and I can use the sample script:
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();
It posts messages (I assume). I tried the following for a simple grab of a message off of the queue and I get a 404:
my $mq = Net::RabbitMQ->new();
$mq->connect("localhost", { user => "guest", password => "guest" });
$mq->channel_open(1);
print $mq->get(开发者_开发百科1, "queuename");
The full text of the error message is:
basic_get: server channel error 404, message: NOT_FOUND - no queue 'queuename' in vhost '/' ...
You need to create the queue with auto_delete => 0
- otherwise it will go away when the first process terminates. Have a look at the queue_declare
method.
I looked at queue_declare
and added it to the listener and changed some code as follows:
#!/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);
}
I kick the listener script off, then, when I execute the following, it posts messages:
#!/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();
精彩评论