<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Andrew Birkett's blog &#187; hack</title>
	<atom:link href="http://www.nobugs.org/blog/archives/tag/hack/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.nobugs.org/blog</link>
	<description>Thoughts of a software engineer</description>
	<lastBuildDate>Thu, 15 Jul 2010 22:25:15 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Squawk (simple queues using awk)</title>
		<link>http://www.nobugs.org/blog/archives/2008/05/11/squawk-simple-queues-using-awk/</link>
		<comments>http://www.nobugs.org/blog/archives/2008/05/11/squawk-simple-queues-using-awk/#comments</comments>
		<pubDate>Sun, 11 May 2008 06:08:33 +0000</pubDate>
		<dc:creator>Andrew</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[awk]]></category>
		<category><![CDATA[hack]]></category>
		<category><![CDATA[mq]]></category>
		<category><![CDATA[unix]]></category>

		<guid isPermaLink="false">http://www.nobugs.org/blog/archives/2008/05/11/squawk-simple-queues-using-awk/</guid>
		<description><![CDATA[If you are easily offended, look away now &#8230;
Reliable message queues (ActiveMQ in particular) are pretty handy things.  They make it a lot easier to build reliable systems which are able to network problems, hardware trouble and temporary weirdness.  However, they always feel pretty heavyweight; suitable for &#8220;enterprise systems&#8221; but not quick shell [...]]]></description>
			<content:encoded><![CDATA[<p>If you are easily offended, look away now &#8230;</p>
<p>Reliable message queues (<a href="http://activemq.apache.org/">ActiveMQ</a> in particular) are pretty handy things.  They make it a lot easier to build reliable systems which are able to <a href="http://lists.jammed.com/ISN/2006/01/0065.html">network problems</a>, <a href="http://www.acadweb.wwu.edu/dbrunner/cbefire/">hardware trouble</a> and <a href="http://www.ida.liu.se/~abdmo/SNDFT/docs/ram-soft.html">temporary weirdness</a>.  However, they always feel pretty heavyweight; suitable for &#8220;enterprise systems&#8221; but not quick shell scripts.</p>
<p>Well, let&#8217;s fix that.  My aim is publish and receive messages to an ActiveMQ broker from the unix shell with a minimum of overhead.  I want to have a &#8216;consume&#8217; script which reads messages from a queue and pipes them to a handler.  If the handler script succeeds, the message is acknowledged and we win.  If the handler script fails, the message is returned back to the queue, and can be re-tried later (possibly by a different host).</p>
<p><a href="http://stomp.codehaus.org/Protocol">STOMP</a> is what makes this easy.  It&#8217;s a &#8217;simple text-oriented message protocol&#8217; which is supported directly by ActiveMQ.  So we won&#8217;t need to mess around with weighty client libraries.  A good start.</p>
<p>But we still need to write a &#8216;consume&#8217; program which will speak STOMP and invoke the message handler script.  There are existing STOMP bindings for perl and ruby, but I&#8217;m pitching for a pure unix solution.</p>
<p>In STOMP, messages are NUL separated which made me wonder if it&#8217;d be possible to use <a href="http://en.wikipedia.org/wiki/Awk">awk</a>, by setting its &#8216;record separator&#8217; to NUL.  The short answer is: yes, awk can do reliable messaging &#8211; win!</p>
<p>We&#8217;ll need some network glue.  Recent versions of awk have builtin network support, but I&#8217;m going to use netcat because it&#8217;s more common than bleeding-edge awks.</p>
<p>I also want to keep &#8216;consume&#8217; to be a single file, but I don&#8217;t want to pull my hair out trying to escape everything properly.  So, I&#8217;ll use a bash <a href="http://tldp.org/LDP/abs/html/here-docs.html">here document</a> to write the awk script out to a temporary file before invoking awk.  (is there a nicer way to do this?)</p>
<p>There&#8217;s not much more to say except here&#8217;s the scripts: <a href="http://www.nobugs.org/developer/squawk/consume">consume</a> and <a href="http://www.nobugs.org/developer/squawk/produce">produce</a>.</p>
<p>To try it out, you&#8217;ll need to <a href="http://activemq.apache.org/activemq-510-release.html">download ActiveMQ</a> and start it up; just do ./bin/activemq and you&#8217;ll get a broker which has a stomp listener on port 61613.</p>
<p>To publish to a queue, run: echo &#8216;my message&#8217; | ./produce localhost 61613 /queue/a</p>
<p>To consume, first write a message handler, such as:</p>
<pre>
#!/bin/bash
echo Handling a message at $(date).  Message follows:
cat
echo '(message ends)'
exit 0
</pre>
<p>and then run: ./consume localhost 61613 /queue/a ./myhandler.</p>
<p>To simulate failure, change the handler to &#8220;exit 1&#8243;.  The message will be returned to the queue.  By default, the consumer will then immediately try again, so I added in a &#8217;sleep 1&#8242; to slow things down a bit.  ActiveMQ has many tweakable settings to control backoff, redelivery attempts and dead-letter queue behaviour.</p>
<p>I&#8217;m done.</p>
<p>If you want to learn more about awk, check out the awk book on <a href="http://astore.amazon.com/nobugs-20">my amazon.com bookshelf</a>.</p>
<p>Y&#8217;know, come the apocalypse, the cockroaches&#8217;s programming language of choice is probably going to be awk.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.nobugs.org/blog/archives/2008/05/11/squawk-simple-queues-using-awk/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
