Monday, September 24, 2007

Apache ActiveMQ and Perl

I write an awful lot of Perl code as part of my day job. In fact, I've been writing Perl for quite a while, now (about ten years). Perl is one of those languages that does a lot of what it says it does. It's Perl; no more, no less. I tend to use Perl as if it were very strict, strongly typed, object oriented and don't get very Perl-ly with it. I like encapsulation, accessor methods, design patterns, and other things of that sort. I suppose, in the end, I'm not really a Perl guy.

Recently, I've been working on a project that aims to decouple systems via messaging. Not wanting to necessarily build a messaging infrastructure from the ground up, I went in search of something to do the work for me. I'm a fan of the JMS feature set and wanted to find a way to bring such a thing to Perl. The Apache project has developed a JMS compliant message broker called ActiveMQ. ActiveMQ, as you might expect of a JMS broker, is implemented in Java and supports a few different wire protocols. One of these protocols is called the Streaming Text Oriented Messaging Protocol, or more succinctly, Stomp. The idea behind Stomp seems to be to provide an interoperable wire format so that any of the available Stomp Clients can communicate with any Stomp Message Broker to provide easy and widespread messaging interop among languages, platforms and brokers. That's truth in advertising.

As with most things, someone else has thought of talking to JMS brokers from Perl long before I have and has manifested this as the Net::Stomp Perl module on CPAN. I, for one, am grateful.

For Perl monkeys, it's about as straight forward as it gets.

  use strict;
  use Net::Stomp;

  my ($stomp);

  $stomp = Net::Stomp->new({
    hostname     => $host,
    port         => $port,
  });

  $stomp->connect({
    login        => $username,
    passcode     => $password,
  });

  # Sending a message to a topic
  $stomp->send({
    destination  => '/topic/MagicDoSomethingBucket',
    body         => 'Hello from Perl land.',
  });

  $stomp->disconnect();

A rather contrived example, but you get the idea. In my case, I opted to use a binary message body (mostly due to time constraints) and used Storable's nfreeze() function to serialize a data structure for transport. If you're interested in doing content based routing or filtering, XML is a better way to go. Either way, I found this to be fast (about 20k messages per second when talking to a local message broker and posting to a publish / subscribe topic with one consumer), flexible, simple, and reliable. In its final home, I will be using ActiveMQ's store and forward functionality with a series of brokers configured in a grid. It really is neat stuff.

As always, I'm interested in any experience people have with ActiveMQ and other messaging solutions, especially as a method of communication between different languages. If you're not familiar with messaging solutions, take a few moments to read up.

You'll be glad you did.

5 comments:

Anonymous said...

I am new to ActiveMQ and I can't find a step by step tutorial.

Therefore, please help me.

How to send a "message" to my perl script on the same system?
I need the "message" to test my perl script

Please advice.

Thanks.

E. Sammer said...

Anonymous:

That's kind of vague. I'm not sure I can help with a step by step tutorial. The code above shows how to write a simple STOMP client in perl to connect and send a message to ActiveMQ. It should be pretty self explanatory.

If what you're looking to do is to have a Perl app message itself, you're going to have to deal with forking or Perl's threads (hint: don't use Perl's threading support). This is probably more complicated than just using traditional IPC strategies and won't be worth it, in the end. Unless, of course, you're planning on having two classes that are meant to be run in separate processes in the future and you want to test in a single program. Either way, write two programs when testing: a producer program and a consumer program.

As for step by step, the best I can give you (other than this article and all of the links within it) is:

1. Install Net::Stomp
2. Read 'perldoc Net::Stomp'
3. Install ActiveMQ and configure the STOMP connector.
4. Write perl code to send messages and a daemon to listen (consume) messages.

Honestly, all the information you need is in this article, on the ActiveMQ site / wiki, or in the Net::Stomp perldoc. You just need to read through it.

Good luck!

Anonymous said...

Dear E.Sammer,

A big thank you to you.

You have given me the best step by step tutorial.

Again, thank you very much.

Sunil M said...

Hi,

When I try to connect to ActiveMQ using Net::Stomp its giving the following error.
Error reading command: at Net/Stomp/Frame.pm line 38.

I use the basic code
# send a message to the queue 'foo'
use Net::Stomp;
my $stomp = Net::Stomp->new( { hostname => 'localhost', port => '61613' } );
$stomp->connect( { login => 'hello', passcode => 'there' } );
$stomp->send(
{ destination => '/queue/foo', body => 'test message' } );
$stomp->disconnect;

Could you please help me with that.

Thanks.

Anonymous said...

[url=http://community.bsu.edu/members/buy+online+Viagra.aspx]buying Viagra online without prescription[/url]

[url=http://eterporno.ru/seks-znakomstva-v-tobolske.php]секс знакомства в тобольске[/url]
[url=http://eterporno.ru/prostitutki-mulatki-negrityanki.php]проститутки мулатки негритянки[/url]

[url=http://pc.eterporno.ru/prostitutki-saratova-engelsa.php]проститутки саратова энгельса[/url]
[url=http://pc.eterporno.ru/prostitutki-g-voronezha.php]проститутки г воронежа[/url]

[url=http://pv.eterporno.ru/gei-prostitutki-ostavili-soobscheniy.php]геи проститутки оставили сообщений[/url]
[url=http://pv.eterporno.ru/dosug-almaty-transseksual.php]досуг алматы транссексуал[/url]
[url=http://px.eterporno.ru/index.php]интим услуги барнаул[/url]
[url=http://px.eterporno.ru/mamba-sayt-znakomstv-samyy-populyarnyy.php]мамба сайт знакомств самый популярный[/url]
[url=http://px.eterporno.ru/telefon-seks-znakomstva.php]телефон секс знакомства[/url]

[url=http://pz.eterporno.ru/mama-doch-prostitutki.php]мама дочь проститутки[/url]
[url=http://pz.eterporno.ru/gde-poznakomitsya-s-geem.php]где познакомиться с геем[/url]