[rabbitmq-discuss] Persistent message in perl

Emile Joubert emile at rabbitmq.com
Mon Dec 20 16:42:22 GMT 2010


Hi Fab,

If you ran publish.pl before consume.pl then the messages will be 
discarded, because there is no queue for messages to be delivered to and 
no binding for messages to get there.

As Alexandru explained, the binding and queue have to exist first. You 
could either launch the consumer in the background before starting the 
publisher, or declare the queue and binding in the publisher as well.

Regards

Emile



Op 20/12/2010 16:11, het Alexandru Scvorţov geskryf:
> Hi,
>
> Queues and exchanges don't work like that.
>
> An exchange is just a router, it doesn't store messages.  That's the
> queues' job.
>
> What you want to do is this:
>    - publisher declares a durable queue with a known name and binds it to an
>      exchange (by default it's bound the default exchange with its name
>      as routing key; the default exchange is "amq.default" or "")
>    - publisher publishes a persistent message to the exchange, which
>      routes it to the queue (the routing key should be the queue's name,
>      or whatever you bound the queue with)
>
>    - consumer declares a durable queue with the same name (this is just
>      to make sure that it exists; it's effectively a no-op)
>    - consumer consumes or gets from the queue.
>
> Hope this helps.
>
> Cheers,
> Alex
>
> On Mon, Dec 20, 2010 at 03:38:11PM +0000, underattack7 wrote:
>> Hi,
>>
>> 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 :
>>
>> #!/usr/bin/perl
>> 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->channel_open(1);
>> $mq->publish(1, $rkey, $text, {exchange =>  $myexchange}, { content_type =>
>> 'text/plain', delivery_mode =>  2});
>> mqdisconnect;
>> 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 :
>> #!/usr/bin/perl
>> 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" });
>> $mq->channel_open(1);
>> 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";
>>      }
>>      sleep(1);
>> }
>> 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 ?
>>
>> Thanks,
>>
>> Fab


More information about the rabbitmq-discuss mailing list