<?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>jBlog &#187; mailbox</title>
	<atom:link href="http://www.blog.wordaligned.com/tag/mailbox/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.blog.wordaligned.com</link>
	<description>//TODO: add tagline</description>
	<lastBuildDate>Fri, 19 Feb 2010 15:31:59 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>concurrency models in jruby using jetlang&#8230;</title>
		<link>http://www.blog.wordaligned.com/2010/02/17/concurrency-models-in-jruby-using-jetlang/</link>
		<comments>http://www.blog.wordaligned.com/2010/02/17/concurrency-models-in-jruby-using-jetlang/#comments</comments>
		<pubDate>Thu, 18 Feb 2010 00:37:04 +0000</pubDate>
		<dc:creator>Joel</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[concurrency]]></category>
		<category><![CDATA[jretlang]]></category>
		<category><![CDATA[jruby]]></category>
		<category><![CDATA[mailbox]]></category>
		<category><![CDATA[software]]></category>

		<guid isPermaLink="false">http://www.blog.wordaligned.com/?p=29</guid>
		<description><![CDATA[Note: This is in response to a post I saw the other morning on #dev-test-ops called &#8220;Jruby + Jetlang =&#62; Awesome Message Passing Concurrency&#8220;, written by Sai Venkatarishan.
As Sai&#8217;s post states, jetlang (or if you&#8217;re in C#, retlang) makes for awesome message passing concurrency. I have become familiar with jetlang/retlang in my past few years [...]]]></description>
			<content:encoded><![CDATA[<p>Note: This is in response to a post I saw the other morning on #dev-test-ops called &#8220;<a href="http://developer-in-test.blogspot.com/2010/02/jruby-jetlang-awesome-message-passing.html" target="_blank">Jruby + Jetlang =&gt; Awesome Message Passing Concurrency</a>&#8220;, written by Sai Venkatarishan.</p>
<p>As Sai&#8217;s post states, jetlang (or if you&#8217;re in C#, retlang) makes for awesome message passing concurrency. I have become familiar with jetlang/retlang in my past few years of working at <a href="http://drw.com" target="_blank">DRW Trading</a>; many times writing code very similar to that within the examples provided by Sai but in C#. However, over the past few months my team has started using jruby more frequently so naturally we used jetlang as stated in Sai&#8217;s post. Shortly after adopting jruby as our JVM based language we started using jetlang via a gem written by Gareth Reeves, called <a href="http://github.com/reevesg/jretlang">jretlang</a>, which provides the benefits of jetlang in jruby with less of the ceremony. To illustrate this, I&#8217;ve rewritten Sai&#8217;s first example using jretlang as opposed to jetlang. You&#8217;ll see that essentially all it does is clean up some of the setup of the file.</p>
<pre class="brush: ruby;">
require &quot;rubygems&quot;
require &quot;jretlang&quot;

class Arnie
  def initialize
    @channel = JRL::Channel.new
    @consumer = JRL::Fiber.new
  end

  def start
    @consumer.start
    @channel.subscribe_on_fiber( @consumer ) do |message|
      case message
      when &quot;The End&quot;
        puts &quot;I will be back...&quot;
        @consumer.dispose
        @consumer.join
      when &quot;Terminate&quot;
       puts &quot;Hastala vista baby!!!&quot;
      else
       puts &quot;You are terminated******&quot;
      end
    end
  end

  def ^(message)
    @channel.publish(message)
  end
end

terminator = Arnie.new
terminator.start
terminator ^ &quot;Terminate&quot;
terminator ^ &quot;Buy me ice cream&quot;
terminator ^ &quot;The End&quot;
terminator ^ &quot;Terminate&quot; #Will not puts anything, as you've stopped the fiber
</pre>
<p>Now upon seeing this, it does not really seem to need the channel for the message passing. Typically we&#8217;ll use channels when the object publishing a message and the object(s) receiving the message are distinct and don&#8217;t know about each other. Notice I used &#8220;(s)&#8221; on object, this is because the publisher does not need to worry itself with he details of how many objects, if any, are subscribed on its channel.  This is commonly referred to as a <a href="http://en.wikipedia.org/wiki/Process_calculus">process calculi</a> approach of concurrency. The Actor-model approach, which Sai speaks of,  is more about an object having a reference to another object, their actor, which will act on the delivery of a message to its inbox. Jetlang/Retlang&#8217;s fibers support this concept without having the overhead of a channel. Fibers contain an execute method that can simply execute a lambda on that fiber. Using this concept, we can clean this example up even more and move to a truer actor-model based approach.</p>
<pre class="brush: ruby;">

require &quot;rubygems&quot;
require &quot;jretlang&quot;

class Arnie
  def initialize
    @consumer = JRL::Fiber.new
  end

  def start
    @consumer.start
  end

  def ^(message)
    @consumer.execute do
      case message
      when &quot;The End&quot;
        puts &quot;I will be back...&quot;
        @consumer.dispose
        @consumer.join
      when &quot;Terminate&quot;
        puts &quot;Hastala vista baby!!!&quot;
      else
        puts &quot;You are terminated******&quot;
      end
    end
  end
end

terminator = Arnie.new
terminator.start
terminator ^ &quot;Terminate&quot;
terminator ^ &quot;Buy me ice cream&quot;
terminator ^ &quot;The End&quot;
terminator ^ &quot;Terminate&quot; #Will not puts, fiber has been disposed
</pre>
<p>Having used jetlang/retlang as my main source of concurrency for a while, I&#8217;ve seen myself writing very similar code to that above every time I introduce a new thread (fiber). But when I look at the class above I think, what really is the main job of the class? I find the answer in the body of the fiber&#8217;s execute. It is the only thing that differs from Arnie to any other class that has similar functionality, say JohnConner. After putting up with implementing this duplication for far too long, my co-worker Pat Farley and I  decided that we were tired of wasting our &#8220;valuable&#8221; time and effort typing most of this over and over again. This lead us to create  <a href="http://github.com/joelash/mailbox" target="_blank">mailbox</a> (see <a href="http://www.klankboomklang.com/2009/10/31/ruby-in-a-multicore-world/" target="_blank">Pat&#8217;s blog post</a>), a gem to help remove most of the structural redundancies involved in jruby concurrency models using jetlang. So, using mailbox our example code becomes:</p>
<pre class="brush: ruby;">
require &quot;rubygems&quot;
require &quot;mailbox&quot;

class Arnie
  include Mailbox

  mailslot
  def ^(message)
    case message
    when &quot;The End&quot;
      puts &quot;I will be back...&quot;
      # cannot dispose from here
    when &quot;Terminate&quot;
      puts &quot;Hastala vista baby!!!&quot;
    else
      puts &quot;You are terminated******&quot;
    end
  end
end

terminator = Arnie.new
terminator ^ &quot;Terminate&quot;
terminator ^ &quot;Buy me ice cream&quot;
terminator ^ &quot;The End&quot;
terminator ^ &quot;Terminate&quot; # THIS STILL PRINTS
</pre>
<p>This separates the message handling logic from how the object achieves actor-model based concurrency. In addition to actor-model based concurrency, mailbox supports the use of channels, but this example is better shown without channels.</p>
<p>All the above code is available on <a href="http://gist.github.com/305954" target="_blank">github as a gist</a>. I&#8217;ll try later this week to do the ping-pong example using mailbox and put it up either as a Gist or just an example in Mailbox; please look in the comments for another link to the ping-pong example.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.blog.wordaligned.com/2010/02/17/concurrency-models-in-jruby-using-jetlang/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

