Benjamin W. Smith

Benjamin W. Smith

Benjamin W. Smith  //  Sysadmin by trade, Pythonista by passion. Dad to two boys and a girl. Guitarist. I like my coffee black, just like my metal.

Sep 19 / 8:29pm

Python STOMP Updates

Due to a flurry of activity(like, you know, a couple people) on bitbucket and twitter, it became apparent to me that people were actually using the library I created for working with message brokers that utilize the STOMP protocol. This is exciting news for me! ;-)

In response I decided it would be a good idea to, you know, maintain the software and polish it up a bit. There were three forks of the python-stomp repository on
bitbucket, so I waded through diffs to see what ideas/suggestions/improvements others had. Everything I saw looked great, so I pulled in and merged all the changes in to the main repository. Thankfully this was a trivial task because mercurial is FREAKING AWESOME. The next step was to polish it all up. So, I went to PEP8ifying where needed(thankfully others had done a great deal of that work for me), and unit testing new functionality introduced by the other developers.

There are many improvements/changes in the latest iteration of this library, so I’d like to outline some of them here.

  • Renamed to stompy
  • Fully documented with Sphinx
  • Simplified client interface with automatic transaction support (abstracted stompy.stomp as stompy.simple)
  • Intermediate message queue layer
  • Simplified authentication
  • More resilient and informative error handling
  • Polling/Non-blocking support
  • Now able to subscribe to multiple destinations at a time
  • Tested and known to work well with ActiveMQ and RabbitMQ
  • Now available on pypi!

This is just a high level overview of the major changes to stompy. For more detailed information, have a look at the source and documentation.

I’d like to thank a few people that have submitted patches and new functionality.

In closing, I’d like to point out that this “project” was an exercise in TDD(Test Driven Development) for me. I wrote the tests first, then the actual code to make the tests pass. This was also the first time I had used the mocking library Dingus. This has been a pleasurable and enlightening development experience for me!

Thanks for reading!

Filed under  //  activemq   bitbucket   etc   hg   mercurial   python   rabbitmq   stomp  
Apr 3 / 5:42am

Python STOMP Implementation

Everyone else is doing it!

Quite seriously though… In my dealings with JMS Message Queuing, specifically with ActiveMQ, I felt the lack of a simple, straight-forward and easy to use Python implementation of the protocol that was also well documented and hackable.

More often than not, I found myself using Perl’s Net::Stomp module because it was so much quicker to get going. Gasp you say, a Perl module that is easier to use than a Python module?! BLASPHEMY! I implore you to believe me!

I don’t think I’ve reinvented the wheel, merely rounded it to my specifications. So, if you choose, check out my implementation of STOMP in Python over at Bitbucket: python-stomp.

It is feature-complete according to the STOMP Protocol, 100% Unit test covered, documented, comes with example usage and is simple to use.

Feedback is appreciated in the form of issues on the Bitbucket issue tracker or this blog if you want to say something nice, or mean about it!

Documentation:

Code:

Filed under  //  activemq   etc   python   stomp  
Sep 3 / 1:07am

ActiveMQ stomp end to end test for nagios

I recently found myself needing to test producing and consuming messages from activemq via stomp. It’s a pretty trivial task thanks to Perl and nagios! Here is a quick script to test the production and consumption of messages and the command you’ll need to put into core.cfg (nagios v2). It’s slim on libs to cut down on excess fat. Enjoy!

Prerequisites..

Class::Accessor (Net::Stomp wants it)
Net::Stomp
$USER3$ = custom location of nagios check scripts..
check_mq = the name of the script.
$ARG1$ = host to check
$ARG1$ = number of messages to produce/consume
$ARG2$ = name of queue to test with

The command for core.cfg:

define command{
        command_name    check_mq
        command_line    $USER3$/check_mq $ARG1$ $ARG2$ $ARG3$
        }

The code:

#!/usr/bin/perl -w

use strict;
use Net::Stomp;

my $host = $ARGV[0];
my $nummsgs = $ARGV[1];
my $queue = $ARGV[2];

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

$stomp->connect();

for (my $i=0; $i <$nummsgs; $i++) {
    $stomp->send({ destination => "/queue/$queue",
                   body => "$queue$i",
                   persistent => 'true'} );
}

$stomp->subscribe({ destination => "/queue/$queue",
                    'ack' => 'client',
                    'activemq.prefetchSize' => 1});

my $count = 0;
for (my $i=0; $i <$nummsgs; $i++) {
    my $frame = $stomp->receive_frame;
    my $body = $frame->body;
    if ($body eq "$queue$i") {
        $count++;
    }
    $stomp->ack( { frame => $frame } );
}

$stomp->disconnect();

print "Produced: $nummsgs Consumed $count\n";

if ($count == $nummsgs) {
    exit(0);
}
elsif (($count != $nummsgs) && ($count != 0)) {
    exit(1);
}
elsif ($count == 0) {
    exit(2);
}

Oh so simple..

Filed under  //  activemq   nagios   perl   stomp