RabbitMQ for Perl mongers

Post on 10-May-2015

11,786 views 2 download

Tags:

description

A talk I held at perl mongers Wellington about RabbitMQ and Net::AMQP. A brief introduction to RabbitMQ and some basic code samples on how to use it from perl.

transcript

RabbitMQfor perl mongers

__ / \`\ __ | \ `\ /`/ \ \_/`\ \-"-/` /\ \ | | \ | (d b) \_/ / \ ,".|.'.\_/.'.|.", / /\' _|_ '/\ \ | / '-`"`-' \ | | | | | | \ \ / / | jgs \ \ \ / / / `"`\ : /'"` `""`""`

AMQPAdvanced Message Queuing Protocol

message flow in RabbitMQ

RabbitMQ

Exchange

QueueQueue

RabbitMQ

Queue

RabbitMQ

RabbitMQ

Queue

AMQP

RabbitMQ

XMPP STOMP HTTP

XMPP STOMP HTTPAMQP

AMQP

RabbitMQ

XMPP STOMP HTTP

XMPP STOMP HTTPAMQP

AMQP

RabbitMQ

XMPP STOMP HTTP

XMPP STOMP HTTPAMQP

nice pictures but how does it look like?

Net::AMQP(::Simple)

boilerplate

my $spec = { RemoteAddress => '127.0.0.1', RemotePort => { default => 5672 }, Username => { default => 'guest' }, Password => { default => 'guest' }, VirtualHost => { default => '/' },

Logger => 0, Debug => { default => {} },

Alias => { default => 'amqp_client' }, AliasTCP => { default => 'tcp_client' }, Callbacks => { default => {} }, channels => { default => {} }, is_started => { default => 0 },};

send a hash

# conect to the serverNet::AMQP::Simple::connect($spec);

# push the messageNet::AMQP::Simple::pub( $queue, to_json($hash) );

receive a message

while ( !$done ) { check( Net::AMQP::Simple::poll() );}

sub check { foreach my $_req (@_) { my $req = from_json($_req); print Dumper($req); $done = 1; }}

typical messaging flow

the “server”

Net::AMQP::Simple::connect($spec);Net::AMQP::Simple::queue("web.domaindb.up");

while (1) { process( Net::AMQP::Simple::poll() );}

sub process { foreach my $_req (@_) { my $req = from_json( $_req, { allow_nonref => 1 } ); ...}

the “client”

Net::AMQP::Simple::connect($spec);Net::AMQP::Simple::pub( $queue, to_json($hash) );Net::AMQP::Simple::queue( $uuid, 'autodelete' );

while ( !$done ) { check( Net::AMQP::Simple::poll() );}

sub check { foreach my $_req (@_) { my $req = from_json($_req); print Dumper($req); $done = 1; }}

• start a service that listens on a queue

• push a message to the service and tell the service how to reply (if needed)

• listen on a temp queue for the response (if needed)

in plain english

how to reply?

• use AMQP header fields

• roll your own protocol (JSON, XML, ...)

... roll your ownif you want to use the full power of RabbitMQ

AMQP

RabbitMQ

XMPP STOMP HTTP

XMPP STOMP HTTPAMQP

how to get started?

• http://rabbitmq.com (get rabbit here)

• http://github.com/norbu09/

• net_amqp (has the Simple.pm)

• RabbitIntro (some samples)

• Net_RabbitMQ_HTTP

thanks

• the RabbitMQ guys for an awesome product

• iWantMyName for letting me play with all that stuff

• many guys on the mailing list for helping me out when i got stuck

• Catalyst IT for the beers

credits

• http://www.ascii-art.com

• http://www.flickr.com/photos/revengingangel/

• http://www.flickr.com/photos/jeff62138/

more credits