[rabbitmq-discuss] Persistent message in perl

underattack7 underattack7 at googlemail.com
Mon Dec 20 15:38:11 GMT 2010


I am struggling with persistent message in perl.

What I am trying to achieve is :
- publish message to a direct exchange
- the exchange stores the message
- a consumer creates a queue, binds it to the exchange, and get all the
stored messages

I have a durable exchange, and apparently I would need to publish message
using "delivery_mode = 2" for the exchange to store the message until a
queue is bound to it....is that right ?

The problem I have is that when my consumer creates a queue and bind it to
the exchange, it does not get all the message sent.

My publisher code :

use Net::RabbitMQ;

my $myexchange=$ARGV[0];
my $rkey=$ARGV[1];
my $text=$ARGV[2];

my $mq = Net::RabbitMQ->new();
$mq->connect("rabbitmqserver", { vhost=>"chat", user => "guest", password =>
"guest" });
$mq->publish(1, $rkey, $text, {exchange => $myexchange}, { content_type =>
'text/plain', delivery_mode => 2});
exit 0;

I execute it with for example :
./publish.pl "exchangechat" "toto" "hi toto1"
./publish.pl "exchangechat" "toto" "hi toto2"
./publish.pl "exchangechat" "toto" "hi toto3"

The consumer code is :
use Net::RabbitMQ;

my $exchange=$ARGV[0];
my $rkey=$ARGV[1];

my $mq = Net::RabbitMQ->new();
$mq->connect("rabbitmqserver", { vhost => "chat", user => "guest", password
=> "guest" });
my $queuename = $mq->queue_declare(1, '', {exclusive => 1, durable => 0,
auto_delete => 1});
$mq->queue_bind(1, "$queue", "$exchange", "$rkey");

while(1) {
    my $message = $mq->get(1, $queuename);
    if ( defined($message) ) {
        print $message->{'routing_key'} . "\n";
        print $message->{'body'} . "\n";
        print "\n\n";
exit 0;

And I run it with :
./consume.pl exchangechat "toto"

but I do not get the message "hi toto1"

Any idea what's wrong here ?


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.rabbitmq.com/pipermail/rabbitmq-discuss/attachments/20101220/deec112b/attachment.htm>

More information about the rabbitmq-discuss mailing list