Redoing RabbitMQ’s tutorial – part 1

I am experimenting with RabbitMQ and Perl for a pet project of mine: gravity.

The Perl module Net::RabbitMQ seems to be usable, finally. I decided to give it a go, and reproduce with Perl the python scripts shown in the official RabbitMQ tutorial.

Tutorial 1 starts, unsurprisingly, with an "Hello world!" example:

let's send a message, receive it and print it on the screen. To do so we need two programs: one that sends a message and one that receives and prints it.

I am not going to redo the whole tutorial, just show the finished code. Please refer to the tutorial and to Net::RabbitMQ's documentation for the details. …send.pl

#!/usr/bin/perl

use strict ;
use warnings ;

use Net::RabbitMQ ;

{
    # closure to return a new channel ID every time we call nextchan
    my $nextchan = 1 ;
    sub nextchan { return $nextchan++ } ;
}

### BEGIN CONFIGURABLE PARAMETERS ######################################
my $qserver = q{localhost} ;
my %qparms = () ;

my $qname   = q{gravity.checks} ;
my $message = q{Test injection} ;
### NO CONFIGURABLE PARAMETERS BELOW THIS LINE #########################


my $mq      = Net::RabbitMQ->new() ;
my $chanID  = nextchan() ;
$message   .= " ".scalar(localtime) ;

print STDERR qq{Will try to send message "$message" through channel $chanIDn} ;

$mq->connect($qserver, %qparms) ;
$mq->channel_open($chanID) ;
$mq->queue_declare($chanID,$qname,,) ;
$mq->publish($chanID,$qname,$message,{ exchange => "" },) ;

print STDERR qq{Message "$message" sent to queue $qnamen} ;

$mq->disconnect ;

receive.pl

#!/usr/bin/perl

use strict ;
use warnings ;

use Net::RabbitMQ ;

{
    # closure to return a new channel ID every time we call nextchan
    my $nextchan = 1 ;
    sub nextchan { return $nextchan++ } ;
}

### BEGIN CONFIGURABLE PARAMETERS ######################################
my $qserver = q{localhost} ;
my %qparms = () ;

my $qname   = q{gravity.checks} ;
### NO CONFIGURABLE PARAMETERS BELOW THIS LINE #########################

my $mq      = Net::RabbitMQ->new() ;
my $chanID  = nextchan() ;

$mq->connect($qserver, %qparms) ;
$mq->channel_open($chanID) ;
$mq->queue_declare($chanID,$qname,,) ;

for (my $message = $mq->get($chanID,$qname,) ;
     defined $message ;
     $message = $mq->get($chanID,$qname,) ) {
    print STDERR qq{Received from queue $qname:n}.$message->{body}.qq{nn} ;
}

print STDERR qq{No more messages in queuen} ;
$mq->disconnect ;

output

bronto@gravity:/gravity/tutorial-1$ ./send.pl 
Will try to send message "Test injection Sun Jan 13 12:52:45 2013" through channel 1
Message "Test injection Sun Jan 13 12:52:45 2013" sent to queue gravity.checks
bronto@gravity:/gravity/tutorial-1$ ./receive.pl 
Received from queue gravity.checks:
Test injection Sun Jan 13 12:52:45 2013

No more messages in queue

Advertisements

One thought on “Redoing RabbitMQ’s tutorial – part 1

  1. Pingback: Porting RabbitMQ Tutorials to Perl Net::RabbitMQ | crashingdaily

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s