Clients/Perl

Installing

From Source

The simples way is to checkout the most recent code from the svn repository

svn co svn://softwarelivre.sapo.pt/broker/trunk/clients/perl-component

and the just build it.

cd perl-component && perl Makefile.PL

answer the questions (you must select at least one of thrift or protobuf codecs, otherwise the makefile won't be written).

Finally just make and install

make install

Dependencies

Thrift

If you chose to install  thrift support then you will need to install it. To do so you should get the  latest release version and just install the perl module.

The build process should be similar to:

wget 'http://www.apache.org/dist//incubator/thrift/XXX-incubating/thrift-XXX.tar.gz'

tar -xzf thrift-XXX.tar.gz 
cd thrift-XXX/lib/perl/
perl Makefile.PL
#you may need to install dependencies from CPAN
make
sudo make install

Protobuf

If you chose to install  protobuf support then you will need to install the protobuf development libraries. Most distributions will have these packages but in case your's doesn't you can always  compile and install them from source.

wget http://protobuf.googlecode.com/files/protobuf-XXX.tar.bz2
tar -xjf protobuf-XXX.tar.bz2
cd protobuf-XXX
./configure
make
sudo make install

Testing

The build process also runs the tests. By default tests connect to the broker in localhost. You can change this for a broker server running in another host by setting the environment variable BROKER_HOST.

If the test broker doesn't have SSL support you should define BROKER_DISABLE_SSL to 1.

Debs

If you have access to the SAPO debian repository you can just run:

sudo apt-get update
sudo apt-get install libsapo-broker-perl

If you're not trying to install in an absurdly old debian system it should just work.

Alternatively you can just use the debs attached to this page.

Usage

Example usage files can be found in the repository.

Simplest Usage

The simples production code is:

use SAPO::Broker::Clients::Simple;
        
use strict;
use warnings;
        
my $broker = SAPO::Broker::Clients::Simple->new(host=>'localhost', proto=>'tcp'); #connects to localhost using tcp by default (can also use udp or ssl)

my %options = (
        'destination_type' => 'QUEUE', #can also be TOPIC
        'destination' => '/tests/perl',
);

#to produce
$broker->publish(%options, 'payload' => "This is the payload");

#to consume
$broker->subscribe(%options, auto_acknowledge => 1); #auto_acknowledge makes life simpler
my $notification = $broker->receive;
my $payload = $notification->message->payload;

Old Perl Client Class

The SAPO::Broker class abstracts all the low-level complexity of the SAPO Broker and gives the developer a simple to use high-level API to build event consumers and producers.

A very simple producer/consumer can look like this:

           use SAPO::Broker;

           my $topic = "/test";

           my $broker = SAPO::Broker->new(
               host=> '127.0.0.1',
           );

           die "Cant connect? CAUSE: $@\n" unless $broker;

           print "SUBSCRIBING...\n";
           $broker->subscribe(
               topic   => $topic,
           );

           print "PUBLISHING...\n";
           $broker->publish(
               topic   => $topic,
               payload => 'TAU TAU',
           );

           print "RECEIVING...\n";
           while (1) {
               my $payload = $broker->receive;

               print "Message received: ", $payload, "\n";
           }

References

Attachments