<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet href="http://feeds.feedburner.com/~d/styles/rss2full.xsl" type="text/xsl" media="screen"?><?xml-stylesheet href="http://feeds.feedburner.com/~d/styles/itemcontent.css" type="text/css" media="screen"?><!-- generator="wordpress/2.0.2" --><rss 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:feedburner="http://rssnamespace.org/feedburner/ext/1.0" version="2.0">

<channel>
	<title>Jonas Bonér</title>
	<link>http://jonasboner.com</link>
	<description>Down To The Bone</description>
	<pubDate>Tue, 25 Mar 2008 17:49:52 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.0.2</generator>
	<language>en</language>
			<atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" href="http://feeds.feedburner.com/grubbel" type="application/rss+xml" /><feedburner:browserFriendly>This is an XML content feed. It is intended to be viewed in a newsreader or syndicated to another site, subject to copyright and fair use.</feedburner:browserFriendly><item>
		<title>AOP-style Mixin Composition Stacks in Scala</title>
		<link>http://feeds.feedburner.com/~r/grubbel/~3/230372484/</link>
		<comments>http://jonasboner.com/2008/02/06/aop-style-mixin-composition-stacks-in-scala/#comments</comments>
		<pubDate>Wed, 06 Feb 2008 16:05:10 +0000</pubDate>
		<dc:creator>Jonas</dc:creator>
		
	<category>AOP</category>
	<category>Scala</category>
		<guid isPermaLink="false">http://jonasboner.com/2008/02/06/aop-style-mixin-composition-stacks-in-scala/</guid>
		<description><![CDATA[<p>Scala is one those great languages that is scalable. With scalable I mean that it is the language that grows with the user, that it makes simple things easy and hard things possible. A language that is easy to get started and to become productive in, but at the same time a deep language with [...]</p>
]]></description>
			<content:encoded><![CDATA[<p>Scala is one those great languages that is scalable. With scalable I mean that it is the language that grows with the user, that it makes simple things easy and hard things possible. A language that is easy to get started and to become productive in, but at the same time a deep language with very powerful constructs and abstractions. </p>
<p>In this blog post I will try to highlight the power of <a href="http://www.scala-lang.org/intro/mixin.html">Scala&#8217;s mixins</a> and how you can use mixin composition to get AOP/interceptor-like style of programming. </p>
<p>First let&#8217;s define our service interface, modeled as a mixin (in this case without an implementation so similar to Java&#8217;s interface):</p>
<pre>
trait Stuff {
  def doStuff
}
</pre>
<p>Now let&#8217;s define two different mixin &#8220;interceptors&#8221; that implement the service interface. The first one manages logging and the other one transaction demarcation (but for simplicity I am just using a dummy mock for TX stuff for now): </p>
<pre>
trait LoggableStuff extends Stuff {
  abstract override def doStuff {
    println("logging enter")
    super.doStuff
    println("logging exit")
  }
}

trait TransactionalStuff extends Stuff {
  abstract override def doStuff {
    println("start TX")
    try {
      super.doStuff
      println("commit TX")
    } catch {
      case e: Exception =>
        println("rollback TX")
    }
  }
}
</pre>
<p>As we can see in this example they both override the <code>Stuff.doStuff</code> method. If we look more closely we can see that they follow the same pattern:</p>
<ul>
<li>Enter method (<tt>doStuff</tt>)</li>
<li>Do something (log, start tx etc.)</li>
<li>Invoke the same method on <tt>super</tt> (<tt>super.doStuff</tt>)</li>
<li>Do something (log, commit tx etc.)</li>
</ul>
<p>The trick here is in the semantics of the call to super. Here Scala will invoke the next mixin in a stack of mixins, e.g. the same method in the &#8220;next&#8221; mixin that have been mixed in. Exactly what f.e. <a href="http://aspectj.org">AspectJ</a> does in its <tt>proceed(..)</tt> method and what Spring does in its interceptors. </p>
<p>But before we try this out, let&#8217;s create a concrete implementation of our <code>Stuff</code> service, called <code>RealStuff</code>:</p>
<pre>
class RealStuff {
  def doStuff = println("doing real stuff")
}
</pre>
<p>Now we have everything we need, so let&#8217;s fire up the Scala REPL and create a component based on the <code>RealStuff</code> class and a mixin stack with support for logging and transactionality. Scala&#8217;s mixin composition can take place when we instantiate an instance, e.g. it allows us to mix in functionality into specific instances that object creation time for specific object instances.</p>
<p>First let&#8217;s create a plain <code>RealStuff</code> instance and run it:</p>
<pre>
scala> import stacks._
import stacks._

scala> val stuff = new RealStuff
stuff: stacks.RealStuff = $anon$1@6732d42

scala> stuff.doStuff
doing real stuff
</pre>
<p>Not too exciting, but let&#8217;s do it again and this time mix in the <code>LoggableStuff</code> mixin:</p>
<pre>
scala> val stuff2 = new RealStuff with LoggableStuff
stuff2: stacks.RealStuff with stacks.LoggableStuff = $anon$1@1082d45

scala> stuff2.doStuff
logging enter
doing real stuff
logging exit
</pre>
<p>As you can see the call to <code>RealStuff.doStuff</code> is intercepted and logging is added before we are invoking this method as well as after. Let&#8217;s now add the <code>TransactionalStuff</code> mixin:</p>
<pre>
scala> val stuff3 = new RealStuff with LoggableStuff with TransactionalStuff
stuff3: stacks.RealStuff with stacks.LoggableStuff with stacks.TransactionalStuff = $anon$1@4512d65

scala> stuff3.doStuff
start TX
logging enter
doing real stuff
logging exit
commit TX
</pre>
<p>As you can see, the semantics for this mixin stack is the exact same as you would get with stacking AspectJ aspects or Spring interceptors. Another interesting aspect is that the whole composition is statically compiled with all its benefits of compile time error detection, performance, potential tool support etc.</p>
<p>This approach is similar to Rickard Oberg&#8217;s idea on using the so-called <a href="http://www.jroller.com/rickard/date/20031028">Abstract Schema</a> pattern for type-safe AOP in plain Java. </p>
<p>It is both simple and intuitive to change the order of the mixin &#8220;interceptors&#8221;, simply change the order in which they are applied to the target instance: </p>
<pre>
scala> val stuff4 = new RealStuff with TransactionalStuff with LoggableStuff
stuff4: stacks.RealStuff with stacks.TransactionalStuff with stacks.LoggableStuff = $anon$1@a20232

scala> stuff4.doStuff
logging enter
start TX
doing real stuff
commit TX
logging exit
</pre>
<p>Finally, just for fun, let&#8217;s a create a mixin that can retry failing operations. This particular one will catch any exception that the service might throw and retry it three times before giving up:</p>
<pre>
trait RetryStuff extends Stuff {
  abstract override def doStuff {
    var times = 0
    var retry = true
    while (retry) {
      try {
        super.doStuff
        retry = false
      } catch {
        case e: Exception =>
          if (times < 3) { // retry 3 times
            times += 1
            println("operation failed - retrying: " + times)
          } else {
            retry = false
            throw e
          }
      }
    }
  }
}</pre>
<p>To test this behavior (as well as the <em>rollback</em> feature in the <code>TransactionalStuff</code>) we can change the <code>RealStuff.getStuff</code> method to throw an exception: </p>
</pre>
<pre>
class RealStuff {
  def doStuff {
    println("doing real stuff")
    throw new RuntimeException("expected")
  }
}
</pre>
<p>Now we can try to add this mixin to the beginning of our our stack and run the service:</p>
<pre>
scala> val stuff5 = new RealStuff with RetryStuff with TransactionalStuff  with LoggableStuff
stuff5: stacks.RealStuff with stacks.RetryStuff with stacks.LoggableStuff with stacks.TransactionalStuff = $anon$1@a927d45

scala> stuff5.doStuff
logging enter
start TX
doing real stuff
operation failed - retrying: 1
doing real stuff
operation failed - retrying: 2
doing real stuff
operation failed - retrying: 3
rollback TX
logging exit
</pre>
<p>Pretty neat, right? </p>
<p>That&#8217;s all for now. In the next post I will cover a bunch of ways to use Scala&#8217;s language primitives to do Dependency Injection (DI).</p>
<div class="feedflare">
<a href="http://feeds.feedburner.com/~f/grubbel?a=FDXKv1E"><img src="http://feeds.feedburner.com/~f/grubbel?i=FDXKv1E" border="0"></img></a> <a href="http://feeds.feedburner.com/~f/grubbel?a=VMs4y1e"><img src="http://feeds.feedburner.com/~f/grubbel?i=VMs4y1e" border="0"></img></a> <a href="http://feeds.feedburner.com/~f/grubbel?a=q2OVpue"><img src="http://feeds.feedburner.com/~f/grubbel?i=q2OVpue" border="0"></img></a> <a href="http://feeds.feedburner.com/~f/grubbel?a=QUHt7Ie"><img src="http://feeds.feedburner.com/~f/grubbel?i=QUHt7Ie" border="0"></img></a>
</div>]]></content:encoded>
			<wfw:commentRSS>http://jonasboner.com/2008/02/06/aop-style-mixin-composition-stacks-in-scala/feed/</wfw:commentRSS>
		<feedburner:origLink>http://jonasboner.com/2008/02/06/aop-style-mixin-composition-stacks-in-scala/</feedburner:origLink></item>
		<item>
		<title>Clustering Scala Actors with Terracotta</title>
		<link>http://feeds.feedburner.com/~r/grubbel/~3/223144192/</link>
		<comments>http://jonasboner.com/2008/01/25/clustering-scala-actors-with-terracotta/#comments</comments>
		<pubDate>Fri, 25 Jan 2008 21:22:18 +0000</pubDate>
		<dc:creator>Jonas</dc:creator>
		
	<category>Distributed Computing</category>
	<category>Concurrent Computing</category>
	<category>Functional Programming</category>
	<category>Scala</category>
		<guid isPermaLink="false">http://jonasboner.com/2008/01/25/clustering-scala-actors-with-terracotta/</guid>
		<description><![CDATA[<p>Introduction
A month ago I wrote an introductory post about Scala Actors: HotSwap Code using Scala and Actors. For you who don&#8217;t know what it is I don&#8217;t want to start by reading the previous post let&#8217;s briefly recap what Actors are and what you can use them for.
An Actor is an abstraction that implements Message-Passing [...]</p>
]]></description>
			<content:encoded><![CDATA[<h2>Introduction</h2>
<p>A month ago I wrote an introductory post about <a href="http://lamp.epfl.ch/~phaller/actors.html">Scala Actors</a>: <a href="http://jonasboner.com/2007/12/19/hotswap-code-using-scala-and-actors/">HotSwap Code using Scala and Actors</a>. For you who don&#8217;t know what it is I don&#8217;t want to start by reading the previous post let&#8217;s briefly recap what Actors are and what you can use them for.</p>
<p>An Actor is an abstraction that implements <a href="http://c2.com/cgi/wiki?MessagePassingConcurrency">Message-Passing Concurrency</a>. Actors have no shared state and are communicating by sending and receiving messages. This is a paradigm that provides a very different and much simple concurrency model than <a href="http://c2.com/cgi/wiki?SharedStateConcurrency">Shared-State Concurrency</a> (the scheme adopted by C, Java, C# etc.) and is avoiding most of the latter one&#8217;s complexity and problems. This makes it possible to write code that is deterministic and side-effect-free, something that makes it easier to write, test, understand and reason about. Each Actor has a mailbox in which it receives incoming messages and normally uses pattern matching on the messages to decide if a message is interesting if action is needed.</p>
<p>Scala&#8217;s Actors are based on Doug Lea&#8217;s <a href="http://gee.cs.oswego.edu/dl/papers/fj.pdf">Fork/Join library</a> and have for example been used very effectively in the excellent <a href="http://liftweb.net"><em>lift</em></a> web framework to among other things to enable <a href="http://en.wikipedia.org/wiki/Comet_(programming)">Comet</a> style (push/streaming ajax) development. Actors allows us to, in a simple and uniformed way, parallelize applications using multiple threads, something that helps us take advantage of all the new dual/quad/&#8230; core or SMP machines that we are starting to get now days. But this also poses challenges; how can we make applications build on this &#8220;new&#8221; programming model highly available and how can we scale them out, if necessary. Would it not be cool if we could not only parallelize our application onto multiple threads but also onto multiple machines? </p>
<p>Note: <a href="http://erlang.org">Erlang</a>, the most successful implementation of Actors to date, solves the challenges in building fault-tolerant and highly available systems in an elegant way using supervisor hierarchies. Nothing prevents an implement of this strategy in Scala Actors, all the primitives (like link, trap_exit etc.) already exists. </p>
<p>I have spent some time last weeks looking into if would make sense to utilize <a href="http://terracotta.org">Terracotta</a> to cluster the Scala Actors library to give a platform on which we can both scale Actors out in a distributed fashion and ensure full fault tolerance and high-availability. The result of this exercise have been successful and I&#8217;m happy to announce that they work very nice together. I will now spend the remainder of this post on walking you through a simple example in how to cluster a Scala Actor using Terracotta.</p>
<h2>Check out the code from SVN</h2>
<p>But before we do anything, let my point you to the <a href="http://svn.terracotta.org/svn/forge/projects/labs/tim-scala-actors-2.6.1/">SVN repository</a> where you can fetch the Terracotta Integration Module (TIM) that I have implemented for Scala Actors. You can check it out anonymously by invoking:</p>
<pre>
svn co http://svn.terracotta.org/svn/forge/projects/labs/tim-scala-actors-2.6.1/
</pre>
<p>When that is done, step into <code>tim-scala-actors-2.6.1/trunk</code> and invoke <code>mvn install</code> (the last command requires that you have <a href="http://maven.apache.org/">Maven</a> installed). it&#8217;ll take a while for Maven to download all its dependencies but after a while you will have a shiny new TIM for Scala Actors installed in your local Maven repository (usually in <code>~/.m2</code>). The sample that we will discuss in the next sections is available in the <code>src/samples/scala</code> directory with the sample configuration in the <code>src/samples/resources</code> directory. </p>
<h2>Write an Actor</h2>
<p>Now, let&#8217;s write a little <code>Cart</code> actor. This actor response to two different messages <code>AddItem(item)</code> and <code>Tick</code>. The former one adds an item to the <code>Cart</code> while the latter one triggers the <code>Cart</code> to print out its content (I&#8217;ll let you know why it&#8217;s called <code>Tick</code> in a second):</p>
<pre>
// Messages
case object Tick
case class AddItem(item: String)

class Cart extends Actor {
  private[this] var items: List[String] = Nil 

  def act() {
    loop {
      react {
        case AddItem(item) =>
          items = item :: items
        case Tick =>
          println("Items: " + items)
      }
    }
  }

  def ping = ActorPing.scheduleAtFixedRate(this, Tick, 0L, 5000L)
}
</pre>
<p>As you see the state is held by a Scala <code>var</code>, which holds onto a <code>List</code> (immutable). In <code>react</code> we wait for the next incoming message and if it is of type <code>AddItem</code> then we grab the item and append it to the list with all our items, but if the message is of type <code>Tick</code> we simply print the list of items out. Simple enough. But what is this method <code>ping</code> doing? It uses an object called <code>ActorPing</code> to schedule that a <code>Tick</code> should be sent to the <code>Cart</code> every 5 seconds (<code>ActorPing</code> is shamelessly stolen from <a href="http://blog.lostlake.org/">Dave Pollak&#8217;s</a> <em>lift</em>).</p>
<h2>Configuration</h2>
<p>In order to cluster the <code>Cart</code> actor we have to write two things. First a hack, a simple configuration file in which declare which actors we want to cluster. This is something that later should be put into the regular <code>tc-config.xml</code> file, but for now we have to live with it. So let&#8217;s create a file with one single line, stating the fully qualified name of the <code>Cart</code> actor;
<pre> samples.Cart </pre>
<p> We can either name this file <code>clustered-scala-actors.conf</code> and put it in root directory of the application or name it whatever we want and feed it to Terracotta using the <code>-Dclustered.scala.actors.config=[path to the file]</code> JVM property. Second, we have to write the regular Terracotta configuration file (tc-config.xml). Here we essentially have to define three things; the TIM for Scala Actors, locks to guard our mutable state and finally which classes should be included for bytecode instrumentation.</p>
<p>Starting with the TIM for Scala Actors. Here we define the version on the module as well as the URL to our Maven repository (in a short while we will put this jar in the Terracotta Maven repository and then you would not have to point out a local one).</p>
<pre>
&lt;modules&gt;
 &lt;repository&gt;file:///Users/jonas/.m2/repository&lt;/repository&gt;
 &lt;module name="clustered-scala-actors-2.6.1" version="2.6.0.SNAPSHOT"/&gt;
&lt;/modules&gt;
</pre>
<p>Now we have to define the locks, which in Terracotta, also marks the transaction boundaries. The <code>Cart</code> has one mutable field (the <code>var</code> named <code>items</code>) that we need to ensure is guarded correctly and has transactional semantics. For each var Scala generates a setter and a getter. The getter is named the same as the field while the getter has the name suffixed with <code>_$eq</code>. That gives us the following lock definition:</p>
<pre>
&lt;locks&gt;
  &lt;named-lock&gt;
    &lt;lock-name&gt;cart_items_write&lt;/lock-name&gt;
    &lt;lock-level&gt;write&lt;/lock-level&gt;
    &lt;method-expression&gt;* samples.Cart.items_$eq(..)&lt;/method-expression&gt;
  &lt;/named-lock&gt;
  &lt;named-lock&gt;
    &lt;lock-name&gt;cart_items_read&lt;/lock-name&gt;
    &lt;lock-level&gt;read&lt;/lock-level&gt;
    &lt;method-expression&gt;* samples.Cart.items()&lt;/method-expression&gt;
  &lt;/named-lock&gt;
&lt;/locks&gt;
</pre>
<p>We have to define a pair like this for each mutable <strong>user defined</strong> field in a clustered actor (not the standard one&#8217;s that are common for all Scala Actors, those are automatically defined). </p>
<p>It important to understand the TIM automatically clusters the Actor&#8217;s mailbox, which means that no messages will ever be lost - providing full fault-tolerance.</p>
<p>Finally we have to define the classes that we need to include for instrumentation. This naturally includes our application classes, e.g. the classes that are using our <code>Cart</code> actor in one way or the other. Those are picked out by the pattern like: <code>'samples.*'</code>. We also have to include all the Scala runtime and library classes that we are referencing from the message is that we send. In our case that means the classes that are used to implement the <code>List</code> abstraction in Scala. Here is the full listing:</p>
<pre>
&lt;instrumented-classes&gt;
 &lt;include&gt;
   &lt;class-expression&gt;samples.*&lt;/class-expression&gt;
 &lt;/include&gt;
 &lt;include&gt;
   &lt;class-expression&gt;scala.List&lt;/class-expression&gt;
 &lt;/include&gt;
 &lt;include&gt;
   &lt;class-expression&gt;scala.$colon$colon&lt;/class-expression&gt;
 &lt;/include&gt;
 &lt;include&gt;
   &lt;class-expression&gt;scala.Nil$&lt;/class-expression&gt;
 &lt;/include&gt;
&lt;/instrumented-classes&gt;
</pre>
<p>I could have included these (and many more) classes in the TIM, but since Terracotta adds a tiny bit of overhead to each class that it instruments I took the decision that it would be better to let the user explicitly define the classes that needs to be instrumented and leave the other ones alone. Since you can pretty much put any valid Scala data or abstraction in an actor message, it is very likely that you will have to declare some includes, else Terracotta will throw an exception (which is expected) with a message listing the XML snippet that you have to put in the tc-config.xml file. So don&#8217;t panic if things blow up. </p>
<h2>Enable Terracotta</h2>
<p>Last but not least, we need to enable Terracotta in the Scala runtime (if you are planning to run the application in a Terracotta enabled application server, then you can skip this section - however I think it might still be useful to be able to try the application out in the Scala REPL). The simplest way of doing that is to do some minor changes to the <code>scala</code> command. First, let&#8217;s step down into the <code>scala/bin</code> directory and make a copy of the <code>scala</code> command called <code>tc-scala</code>, then scroll down all the way to the bottom. As you can see it is just a wrapper around the regular <code>java</code> command, which makes things pretty easy for us. We start by defining some environmental variables (here showing my local settings):</p>
<pre>
TC_SCALA_ACTORS_CONFIG_FILE=/Users/jonas/src/java/tc-forge/projects/tim-scala-actors-2.6.1/trunk/src/samples/resources/clustered-scala-actors.conf
TC_CONFIG_FILE=/Users/jonas/src/java/tc-forge/projects/tim-scala-actors-2.6.1/trunk/src/samples/resources/tc-config.xml
TC_INSTALL_DIR=/Users/jonas/src/java/tc/code/base/build/dist/terracotta-trunk-rev6814
TC_BOOT_JAR="$TC_INSTALL_DIR"/lib/dso-boot/dso-boot-hotspot_osx_150_13.jar
TC_TIM_SCALA_ACTORS_JAR=/Users/jonas/.m2/repository/org/terracotta/modules/clustered-scala-actors-2.6.1/2.6.0-SNAPSHOT/clustered-scala-actors-2.6.1-2.6.0-SNAPSHOT.jar
</pre>
<p>When these variables have been defined we can replace the existing invocation of <code>java</code> with the following:</p>
<pre>
${JAVACMD:=java} ${JAVA_OPTS:=-Xmx256M -Xms256M} 
 -Xbootclasspath/p:"$TC_BOOT_JAR" 
 -Dtc.install-root="$TC_INSTALL_DIR" 
 -Dtc.config="$TC_CONFIG_FILE" 
 -Dclustered.scala.actors.config="$TC_SCALA_ACTORS_CONFIG_FILE" 
 -Dscala.home="$SCALA_HOME" 
 -Denv.classpath="$CLASSPATH" 
 -Denv.emacs="$EMACS" 
 -cp "$BOOT_CLASSPATH":"$EXTENSION_CLASSPATH":"$TC_TIM_SCALA_ACTORS_JAR" 
 scala.tools.nsc.MainGenericRunner  "$@"
</pre>
<h2>Let&#8217;s run it</h2>
<p>Enough hacking. Now let&#8217;s try it out. I think that the best way of learning new things in Scala is to use its REPL, so let&#8217;s start that up, this time with Terracotta enabled. But before we do that we have to start up the Terracotta server by stepping into the bin directory in the Terracotta installation and invoke:</p>
<pre>
$ ./start-tc-server.sh
</pre>
<p><strong>Note:</strong> you need to grab Terracotta from SVN trunk to get the bits that work with the Scala TIM. See instructions on how to <a href="http://www.terracotta.org/confluence/display/wiki/Source+Repository">check out the sources</a> and <a href="http://www.terracotta.org/confluence/display/devdocs/Building+Terracotta">how to build it</a>.</p>
<p>Now, we can start up the Terracotta enabled Scala REPL:</p>
<pre>
$ tc-scala
2008-01-25 07:42:11,643 INFO - Terracotta trunk-rev6814, as of 20080124-140101 (Revision 6814 by jonas@homer from trunk)
2008-01-25 07:42:12,136 INFO - Configuration loaded from the file at '/Users/jonas/src/java/tc-forge/projects/tim-scala-actors-2.6.1/trunk/src/samples/resources/tc-config.xml'.
2008-01-25 07:42:12,325 INFO - Log file: '/Users/jonas/terracotta/client-logs/scala/actors/20080125074212303/terracotta-client.log'.
Parsing scala actors config file: /Users/jonas/src/java/tc-forge/projects/tim-scala-actors-2.6.1/trunk/src/samples/resources/clustered-scala-actors.conf
Configuring clustering for Scala Actor: samples.Cart
Welcome to Scala version 2.6.0-final.
Type in expressions to have them evaluated.
Type :help for more information.

scala&gt;
</pre>
<p>Here we can see that it has found and connected to the Terracotta server, found our <code>clustered-scala-actors.conf</code> config file and configured clustering for one Scala Actor; <code>samples.Cart</code>.</p>
<p>Let&#8217;s have some fun and start up another REPL in another terminal window. In each of these we do the following; import our classes, create a new <code>Cart</code> (Actor) and start up the Actor. </p>
<pre>
scala&gt; import samples._
import samples._

scala&gt; val cart = new Cart
cart: samples.Cart = samples.Cart@81af82

scala&gt; cart.start
res0: scala.actors.Actor = samples.Cart@81af82

scala&gt;
</pre>
<p>Now we have a distributed Actor just waiting to be fed with some messages. We don&#8217;t want to make it disappointed so let&#8217;s now add a bunch of bananas and apples to the <code>Cart</code>, and then feed it with a <code>Tick</code> message to make it print out the result:</p>
<pre>
scala&gt; cart ! AddItem("bananas")

scala&gt; cart ! AddItem("apples")

scala&gt; cart ! Tick

scala&gt; Items: List(apples, bananas)

scala&gt;
</pre>
<p>Ok, so far no news. But comes the moment of truth, let&#8217;s take the other REPL and fire of a <code>Tick</code>:</p>
<pre>
scala&gt; cart ! Tick

scala&gt; Items: List(apples, bananas)

scala&gt;
</pre>
<p>Yippee, it works. Now we can invoke the <code>ping</code> method to schedule a <code>Tick</code> (to print out status) every 5 seconds.</p>
<pre>
scala&gt; cart.ping
res2: java.util.concurrent.ScheduledFuture = java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask@3a4388

scala&gt; Items: List(apples, bananas)

scala&gt; Items: List(apples, bananas)

scala&gt; cart ! AddItem("football")

scala&gt; Items: List(football, apples, bananas)

...
</pre>
<h2>How to define scope of the clustered Actor?</h2>
<p>The Scala Actors TIM currently supports three different scopes; <code>instance</code>, <code>class</code> and <code>custom</code>. The scope is defined by appending a colon &#8216;:&#8217; and the type of scope after the FQN of the Actor in the <code>clustered-scala-actors.conf</code>. If no scope is defined then the Actor is assumed to have scope <code>instance</code>. For example: </p>
<pre>
com.biz.Foo:custom
com.biz.Bar:class
com.biz.Baz:instance
com.biz.Bam
</pre>
<p>The default scope named <code>instance</code> means that the Scala TIM is transparently intercepting the instantiation (f.e. <code>new Cart</code>) of all the Actors that you declare in the <code>clustered-scala-actors.conf</code> file. Each clustered Actor <strong>instance</strong> will have a unique identity across the cluster and each time this specific instance is created (f.e. when a new node joins the cluster) then the clustered instance with this specific identity will be handed out. The TIM distinguishes between actors of the same type but instantiated in different code paths. To take an example, let&#8217;s create one object <code>ActorFactory</code> with one single method <code>create</code>: </p>
<pre>
object ActorFactory {
  def create: Actor = new MyActor
}
</pre>
<p>If we now have two classes <code>Foo</code> and <code>Bar</code> as follows:</p>
<pre>
class Foo {
  val actor = ActorFactory.create
}

class Bar {
  val actor = ActorFactory.create
}
</pre>
<p>Then <code>Foo</code> and <code>Bar</code> will have two distinct clustered Actors each with a unique but cluster-wide identity. </p>
<p>The <code>class</code> scope lets all Actors of a the same type share Actor instance, so each time an Actor of a specific type is created the same clustered one will be handed out. </p>
<p>Finally we have the <code>custom</code> scope. Which, as it sounds, allows custom user defined scoping. </p>
<h2>How to define custom scoped Actors?</h2>
<p>If you want more control over scope and life-cycle of a specific Actor then you can define it to have <code>custom</code> scope in the <code>clustered-scala-actors.conf</code> file and create a factory in which you bind each Actor to whatever scope you wish. But now you have to create some data structure that is holding on to your Actors in the factory and explicitly define it to be a root in the <code>tc-config.xml</code> file. The factory might look something like this:</p>
<pre>
// Cart factory, allows mapping an instance to any ID
object Cart {

  // This instance is the custom Terracotta root
  private[this] val instances: Map[Any, Cart] = new HashMap

  def newInstance(id: Any): Cart = {
    instances.get(id) match {
      case Some(cart) => cart
      case None =>
        val cart = new Cart
        instances += (id -> cart)
        cart
    }
  }
}
</pre>
<p>This means that we have to add some more configuration elements to our Terracotta configuration. First we need to add the root <code>samples.Cart$.instances</code> (<code>Cart$</code> is the name of Scala&#8217;s compiled <code>Cart</code> companion object, all companion objects compiles to a class with the name of the original class suffixed with <code>$</code>):</p>
<pre>
&lt;roots&gt;
  &lt;root&gt;
    &lt;field-name&gt;samples.Cart$.instances&lt;/field-name&gt;
  &lt;/root&gt;
&lt;/roots&gt;
</pre>
<p>Then we have to add locking for the <code>Cart.newInstance(..)</code> method and finally a whole bunch of new include statements for all the Scala types that are referenced by the <code>scala.collection.mutable.HashMap</code> that we used as root:</p>
<pre>
...
&lt;named-lock&gt;
  &lt;lock-name&gt;Cart_newInstance&lt;/lock-name&gt;
  &lt;lock-level&gt;write&lt;/lock-level&gt;
  &lt;method-expression&gt;* samples.Cart$.newInstance(..)&lt;/method-expression&gt;
&lt;/named-lock&gt;

...

&lt;include&gt;
  &lt;class-expression&gt;scala.collection.mutable.HashMap&lt;/class-expression&gt;
&lt;/include&gt;
&lt;include&gt;
  &lt;class-expression&gt;scala.runtime.BoxedAnyArray&lt;/class-expression&gt;
&lt;/include&gt;
&lt;include&gt;
  &lt;class-expression&gt;scala.runtime.BoxedArray&lt;/class-expression&gt;
&lt;/include&gt;
&lt;include&gt;
  &lt;class-expression&gt;scala.collection.mutable.DefaultEntry&lt;/class-expression&gt;
&lt;/include&gt;

...
</pre>
<p>That&#8217;s pretty much all there&#8217;s to it. Check out the code, play with it and come back with feedback, bug reports, patches etc. </p>
<p>Enjoy. </p>
<div class="feedflare">
<a href="http://feeds.feedburner.com/~f/grubbel?a=7B6fRDD"><img src="http://feeds.feedburner.com/~f/grubbel?i=7B6fRDD" border="0"></img></a> <a href="http://feeds.feedburner.com/~f/grubbel?a=cyQonvd"><img src="http://feeds.feedburner.com/~f/grubbel?i=cyQonvd" border="0"></img></a> <a href="http://feeds.feedburner.com/~f/grubbel?a=SKfr1Pd"><img src="http://feeds.feedburner.com/~f/grubbel?i=SKfr1Pd" border="0"></img></a> <a href="http://feeds.feedburner.com/~f/grubbel?a=Z21oPjd"><img src="http://feeds.feedburner.com/~f/grubbel?i=Z21oPjd" border="0"></img></a>
</div>]]></content:encoded>
			<wfw:commentRSS>http://jonasboner.com/2008/01/25/clustering-scala-actors-with-terracotta/feed/</wfw:commentRSS>
		<feedburner:origLink>http://jonasboner.com/2008/01/25/clustering-scala-actors-with-terracotta/</feedburner:origLink></item>
		<item>
		<title>HotSwap Code using Scala and Actors</title>
		<link>http://feeds.feedburner.com/~r/grubbel/~3/202955462/</link>
		<comments>http://jonasboner.com/2007/12/19/hotswap-code-using-scala-and-actors/#comments</comments>
		<pubDate>Wed, 19 Dec 2007 20:59:18 +0000</pubDate>
		<dc:creator>Jonas</dc:creator>
		
	<category>Functional Programming</category>
	<category>Scala</category>
		<guid isPermaLink="false">http://jonasboner.com/2007/12/19/hotswap-code-using-scala-and-actors/</guid>
		<description><![CDATA[<p>In this post I will show you how you can do code hotswap in the same fashion as in Erlang using Scala and its Actors package. 
An actor is an abstraction that implements Message-Passing Concurrency. Actors have no shared state and are communicating by sending and receiving messages. This is a paradigm that provides a [...]</p>
]]></description>
			<content:encoded><![CDATA[<p>In this post I will show you how you can do code hotswap in the same fashion as in <a href="http://erlang.org">Erlang</a> using <a href="http://www.scala-lang.org">Scala</a> and its <a href="http://lamp.epfl.ch/~phaller/actors.html">Actors</a> package. </p>
<p>An actor is an abstraction that implements <a href="http://c2.com/cgi/wiki?MessagePassingConcurrency">Message-Passing Concurrency</a>. Actors have no shared state and are communicating by sending and receiving messages. This is a paradigm that provides a very different and much simpler concurrency model than <a href="http://c2.com/cgi/wiki?SharedStateConcurrency">Shared-State Concurrency</a> (the scheme adopted by C, Java, C# etc.) and is avoiding all of the latter one&#8217;s problems with deadlocks, live locks, thread starvation etc. This makes it possible to write code that is deterministic and <a href="http://en.wikipedia.org/wiki/Side_effect_%28computer_science%29">side-effect-free</a>, something that makes easier to write, test, understand and reason about. Each actor has a mailbox in which it receives incoming messages and can use pattern matching on the messages to decide if a message is interesting and which action to take.  The most well known and successful implementation of actors can be found in the Erlang language (and the OTP platform) where it has been used to implement extremely fault tolerant (<a href="http://ll2.ai.mit.edu/talks/armstrong.pdf ">99.9999999% reliability - 9 nines</a>) and massively concurrent systems (with hundreds of thousand simultaneous actors). </p>
<p>Let&#8217;s start by writing a little server. We implement this in the form of a <code>trait</code>, which is Scala&#8217;s <a href="http://en.wikipedia.org/wiki/Mixin">mixin</a> construct. Traits allows you to build up your components using so-called mixin composition which is something that can give you a very high grade of reuse and flexibility. This <code>trait</code> only defines a single method named <code>status</code> which prints out info about the enclosing instance. Completely useless and not much for a server, but it will give you the idea. Then we subclass this mixin and define the <code>ServerOne</code> concrete server class (with the <code>status</code> method mixed in). </p>
<pre>
// servers
trait Server {
  def status = println("current server: " + this)
}
class ServerOne extends Server
</pre>
<p>Let&#8217;s instantiate the <code>ServerOne</code> class and see what the <code>status</code> method it will print out. Here we are doing it interactively through Scala&#8217;s REPL (read-eval-print-loop).</p>
<pre>
$ scala -cp .

scala> val server = new ServerOne
server: ServerOne = ServerOne@7be75d

scala> server status
current server: ServerOne@7be75d
</pre>
<p>Now, before we write the actor we have to define the messages it responds to. Here Scala is using something called <em>case classes</em> which are similar to normal classes but with some enhancements. First you can match on them, e.g. use pattern matching similar to the one found in Erlang. They also have some syntactic sugar, f.e. you can create them without using <code>new</code>, the compiler generates getters and setters for the constructor arguments, equality is not based on object id but on meaning/content (something that makes them ideal to use for <a href="http://c2.com/cgi/wiki?ValueObject">value objects</a>, but that is another story). We define two different messages; Status and HotSwap.</p>
<pre>
// triggers the status method
case object Status

// triggers hotswap - carries the new server to be hotswapped to
case class HotSwap(s: Server)
</pre>
<p>Ok, now it is time for the actual actor. Actor is a base class in the Scala library and we can choose to either extend it explicitly or to create an anonymous actor through the <code>actor {...}</code> construct. When we subclass the actor we have to implement the method called <code>act</code> which is the callback method that is invoked when the actor is started up. </p>
<p>Scala comes with two different implementations; one that is based on Java threads in that each actor is getting its own thread, while the other one is based on events and very lightweight allowing hundreds of thousands of actors to run simultaneously. Here we will use the event-based version (which is done by using the <code>react</code> method instead of the <code>receive</code> method for receiving messages). </p>
<p>The trick to do hotswap by using actors is to loop recursively and pass on the state in each recursive call. This is a very common idiom in functional programming.  The beauty of it is that we do not update any mutual state but our execution is side effect free which makes it easier to test and reason about.  In this case our state is the actual server. We start the loop by instantiating <code>ServerOne</code>. The pattern matching is happening in <code>react</code> statement in which we have three different cases (pattern matchers). </p>
<p>The first one matches our <code>Status</code>, when we receive this message we simply invoke the <code>status</code> method on our server and then taking another round in the <code>loop</code> passing along the server. </p>
<p>The second one matches our <code>HotSwap</code> message. It is here things are starting to get interesting. Now we can take the new replacement server (here called <code>newServer</code>), which is passed to us as an argument to the <code>HotSwap</code> message, and pass it in to the call to the <code>loop</code> method. Voila, we have updated our server at runtime. Now all subsequent messages will act on our new server instance. </p>
<p>This will work since the <code>react</code> method will in fact never return but infinitely recur. Infinite recursion would have been a problem in f.e. Java since each recursion would consume a new stack frame until we run out of memory. But recursion is one of the most powerful and commonly used idioms in functional programming and the Scala compiler optimizes <a href="http://en.wikipedia.org/wiki/Tail_recursion">tail-recursive</a> algorithms and turns them into regular loops. </p>
<p>At the end we have also added a match-all pattern that does nothing, this is defined by the <code>case _ => ...</code> clause. Let&#8217;s take a look at the code.</p>
<pre>
class ServerActor extends Actor {
  def act = {
    println("starting server actor...")
    loop(new ServerOne)
  }

  def loop(server: Server) {
    react {
      case Status =>
        server.status
        loop(server)

      case HotSwap(newServer) =>
        println("hot swapping code...")
        loop(newServer)

      case _ => loop(server)
    }
  }
}
</pre>
<p>Finally we will follow one of Scala&#8217;s idioms and create a companion object for our <code>ServerActor</code> class. In this object, which is a singleton but should be seen upon as a module for functions and immutable state, we define an immutable handle to an instantiated and started actor. </p>
<p>Worth to note is that the <code>val</code> holding our actor is initialized when the enclosing object is first used, and since we are starting up the actor in the initialization block of the <code>val</code>, the actor will not be started until it is used for the first time. </p>
<pre>
// actor companion object
object ServerActor {
  val actor = {
    val a = new ServerActor
    a.start; a
  }
}
</pre>
<p>Let&#8217;s try to run it in the Scala REPL. The Scala function <em>!</em> (pronounced bang) means &#8220;send a message&#8221;. So <em>act ! msg</em> means send message <em>msg</em> to actor <em>act</em>.</p>
<pre>
$ scala -cp .  

scala> import hotswap._
import hotswap._

scala> val actor = ServerActor.actor
starting actor...
actor: hotswap.ServerActor = hotswap.ServerActor@528ed7

scala> actor ! Status
current server: hotswap.ServerOne@226445

scala> class ServerTwo extends Server {
     | override def status = println("hotswapped server: " + this)
     | }
defined class ServerTwo

scala> actor ! HotSwap(new ServerTwo)
hot swapping code...

scala> actor ! Status
hotswapped server: line5$object$$iw$$iw$$iw$ServerTwo@b556
</pre>
<p>Pretty cool, right? </p>
<p>This would be even more cool if Scala came with an SSH server that could provide this REPL remotely (like we have in Erlang OTP). Then we could connect to our application from the outside and change its behavior, fixing bugs, upgrade the server etc. Another solution would be to make use of the <a href="http://www.scala-lang.org/docu/files/api/scala/actors/remote$content.html">remote actors</a> in the Scala distribution, but that is something for another post. </p>
<div class="feedflare">
<a href="http://feeds.feedburner.com/~f/grubbel?a=L3MjieC"><img src="http://feeds.feedburner.com/~f/grubbel?i=L3MjieC" border="0"></img></a> <a href="http://feeds.feedburner.com/~f/grubbel?a=yGnY5kc"><img src="http://feeds.feedburner.com/~f/grubbel?i=yGnY5kc" border="0"></img></a> <a href="http://feeds.feedburner.com/~f/grubbel?a=Da2tiZc"><img src="http://feeds.feedburner.com/~f/grubbel?i=Da2tiZc" border="0"></img></a> <a href="http://feeds.feedburner.com/~f/grubbel?a=BfQOxRc"><img src="http://feeds.feedburner.com/~f/grubbel?i=BfQOxRc" border="0"></img></a>
</div>]]></content:encoded>
			<wfw:commentRSS>http://jonasboner.com/2007/12/19/hotswap-code-using-scala-and-actors/feed/</wfw:commentRSS>
		<feedburner:origLink>http://jonasboner.com/2007/12/19/hotswap-code-using-scala-and-actors/</feedburner:origLink></item>
		<item>
		<title>The Terracotta Book</title>
		<link>http://feeds.feedburner.com/~r/grubbel/~3/202151481/</link>
		<comments>http://jonasboner.com/2007/12/18/the-terracotta-book/#comments</comments>
		<pubDate>Tue, 18 Dec 2007 12:00:33 +0000</pubDate>
		<dc:creator>Jonas</dc:creator>
		
	<category>Writing</category>
		<guid isPermaLink="false">http://jonasboner.com/2007/12/18/the-terracotta-book/</guid>
		<description><![CDATA[<p>The last month I have been very busy writing on The Definitive Guide to Terracotta. 
It is a collaborative effort from the Terracotta team each one writing on chapters in their area of expertise. It is a very practical and will teach you how to use Terracotta with the X framework/appserver or for a Y [...]</p>
]]></description>
			<content:encoded><![CDATA[<p>The last month I have been very busy writing on <a href="http://www.apress.com/book/view/1590599861">The Definitive Guide to Terracotta</a>. </p>
<p>It is a collaborative effort from the Terracotta team each one writing on chapters in their area of expertise. It is a very practical and will teach you how to use Terracotta with the X framework/appserver or for a Y use-case, yet thorough and deep in concepts and theory. </p>
<p>It will be available sometime next year and if you&#8217;re interested in getting updates on the book can subscribe to: bookupdate AT terracottatech DOT com.</p>
<div class="feedflare">
<a href="http://feeds.feedburner.com/~f/grubbel?a=l8acPSC"><img src="http://feeds.feedburner.com/~f/grubbel?i=l8acPSC" border="0"></img></a> <a href="http://feeds.feedburner.com/~f/grubbel?a=htqzRHc"><img src="http://feeds.feedburner.com/~f/grubbel?i=htqzRHc" border="0"></img></a> <a href="http://feeds.feedburner.com/~f/grubbel?a=eUG9Trc"><img src="http://feeds.feedburner.com/~f/grubbel?i=eUG9Trc" border="0"></img></a> <a href="http://feeds.feedburner.com/~f/grubbel?a=mpXDbzc"><img src="http://feeds.feedburner.com/~f/grubbel?i=mpXDbzc" border="0"></img></a>
</div>]]></content:encoded>
			<wfw:commentRSS>http://jonasboner.com/2007/12/18/the-terracotta-book/feed/</wfw:commentRSS>
		<feedburner:origLink>http://jonasboner.com/2007/12/18/the-terracotta-book/</feedburner:origLink></item>
		<item>
		<title>Oredev 2007: BDD, LINQ, Scalability trends etc.</title>
		<link>http://feeds.feedburner.com/~r/grubbel/~3/201643900/</link>
		<comments>http://jonasboner.com/2007/11/17/oredev-2007-bdd-linq-terracotta-etc/#comments</comments>
		<pubDate>Sat, 17 Nov 2007 13:36:14 +0000</pubDate>
		<dc:creator>Jonas</dc:creator>
		
	<category>General</category>
	<category>Java</category>
	<category>Distributed Computing</category>
	<category>Functional Programming</category>
	<category>Testing</category>
		<guid isPermaLink="false">http://jonasboner.com/2007/11/17/oredev-2007-bdd-linq-terracotta-etc/</guid>
		<description><![CDATA[<p>I had the pleasure of attending Oredev last month. This was a great conference. I have attended this conference three years in a row and it&#8217;s getting better and better every year. 
This year they had a great speaker lineup and I was able to catch some very interesting talks. Among the most memorable ones [...]</p>
]]></description>
			<content:encoded><![CDATA[<p>I had the pleasure of attending <a href="http://oredev.org">Oredev</a> last month. This was a great conference. I have attended this conference three years in a row and it&#8217;s getting better and better every year. </p>
<p>This year they had a great speaker lineup and I was able to catch some very interesting talks. Among the most memorable ones were <a href="http://dannorth.net/">Dan North</a>&#8217;s keynote about why &#8220;Best Practices&#8221; in software development are neither &#8220;Best&#8221; no &#8220;Practices&#8221;. I also attended Dan&#8217;s talk on BDD (<a href="http://behaviour-driven.org/">Behavior-Driven<br />
Development</a>). Great talk. I find BDD very interesting, it feels like the natural extension of, or complement to TDD (Test-Drived Development) in that it focuses on getting complete concept coverage in the tests instead of code coverage (as in TDD). </p>
<p>Other great talks were <a href="http://blog.toolshed.com/">Andy Hunt</a>&#8217;s (Pragmatic Programmers) keynote and <a href="http://research.microsoft.com/~emeijer/">Erik Meijer</a>&#8217;s talk on <a href="http://msdn2.microsoft.com/en-us/netframework/aa904594.aspx">LINQ</a>. The latter one was fun to watch, Erik (undeliberately it felt like) turned it more or less into a praise of <a href="http://haskell.org">Haskell</a>; how they have stolen all the good stuff in LINQ from Haskell and that the world would be a better place if everyone just used Haskell. </p>
<p>My talk on <a href="http://terracotta.org">Terracotta</a> was able to attract quite a lot of attendees. One of the most interesting things was when I asked them, at the end of the talk, how many could see immediate need for something like Terracotta in their daily work roughly 80% raise their hands. I find this quite amazing and is actually something that have been consistent during last half year. I remember when I started asking this question, around 2 years ago, roughly 5-10 % raised their hands. This is quite a drastic change. Since I know that we were facing the same problems with scalability and HA a couple of years as we do now, I guess this is a sign of that the awareness of clustering, persistent and durable RAM and similar services has increased; that people have started to consider writing stateful applications with rich domain models - which implies another solution to HA and scalability than Oracle RAC and similar. </p>
<p>However, the best thing was that the conference had invited one of the best coffee shops in Malmo to serve all speakers unlimited amount of caffeine in the form of espresso, cafe latte, machiatto or whatever was asked for. I paid them 4-5 visits every day.</p>
<div class="feedflare">
<a href="http://feeds.feedburner.com/~f/grubbel?a=yZ74DOC"><img src="http://feeds.feedburner.com/~f/grubbel?i=yZ74DOC" border="0"></img></a> <a href="http://feeds.feedburner.com/~f/grubbel?a=VOlCwSc"><img src="http://feeds.feedburner.com/~f/grubbel?i=VOlCwSc" border="0"></img></a> <a href="http://feeds.feedburner.com/~f/grubbel?a=GrZ2hAc"><img src="http://feeds.feedburner.com/~f/grubbel?i=GrZ2hAc" border="0"></img></a> <a href="http://feeds.feedburner.com/~f/grubbel?a=bJOeUIc"><img src="http://feeds.feedburner.com/~f/grubbel?i=bJOeUIc" border="0"></img></a>
</div>]]></content:encoded>
			<wfw:commentRSS>http://jonasboner.com/2007/11/17/oredev-2007-bdd-linq-terracotta-etc/feed/</wfw:commentRSS>
		<feedburner:origLink>http://jonasboner.com/2007/11/17/oredev-2007-bdd-linq-terracotta-etc/</feedburner:origLink></item>
		<item>
		<title>Interview with Joe Armstrong on Erlang and more…</title>
		<link>http://feeds.feedburner.com/~r/grubbel/~3/177166120/</link>
		<comments>http://jonasboner.com/2007/10/30/interview-with-joe-armstrong-on-erlang-and-more/#comments</comments>
		<pubDate>Tue, 30 Oct 2007 12:56:49 +0000</pubDate>
		<dc:creator>Jonas</dc:creator>
		
	<category>Distributed Computing</category>
	<category>Concurrent Computing</category>
	<category>Functional Programming</category>
		<guid isPermaLink="false">http://jonasboner.com/2007/10/30/interview-with-joe-armstrong-on-erlang-and-more/</guid>
		<description><![CDATA[<p>I had the pleasure of attending Joe Armstrong&#8217;s fantastic presentation at JAOO some weeks ago. It was by far the best talk at the whole conference, both in terms of content and presentation. Joe showed deep knowledge in both hardware and software, good teaching skills all wrapped up in a dry twisted sense of British [...]</p>
]]></description>
			<content:encoded><![CDATA[<p>I had the pleasure of attending Joe Armstrong&#8217;s fantastic presentation at <a href="http://jaoo.dk/">JAOO</a> some weeks ago. It was by far the best talk at the whole conference, both in terms of content and presentation. Joe showed deep knowledge in both hardware and software, good teaching skills all wrapped up in a dry twisted sense of British humor - Joe was incredibly funny, I sometimes felt like had attended a stand-up comedian show. </p>
<p>Anyway, for all of you that missed Joe&#8217;s talk, <a href="http://channel9.msdn.com/ShowPost.aspx?PostID=351659#351659">here is a great interview</a> with him discussing Erlang&#8217;s (and a functional programmer&#8217;s) view of the world, the problem with OO, shared state concurrency and much more. Not as funny as the talk, but very interesting. </p>
<div class="feedflare">
<a href="http://feeds.feedburner.com/~f/grubbel?a=Oa6lNdA"><img src="http://feeds.feedburner.com/~f/grubbel?i=Oa6lNdA" border="0"></img></a> <a href="http://feeds.feedburner.com/~f/grubbel?a=L1udCfa"><img src="http://feeds.feedburner.com/~f/grubbel?i=L1udCfa" border="0"></img></a> <a href="http://feeds.feedburner.com/~f/grubbel?a=AxHDM3a"><img src="http://feeds.feedburner.com/~f/grubbel?i=AxHDM3a" border="0"></img></a> <a href="http://feeds.feedburner.com/~f/grubbel?a=2U6alxa"><img src="http://feeds.feedburner.com/~f/grubbel?i=2U6alxa" border="0"></img></a>
</div>]]></content:encoded>
			<wfw:commentRSS>http://jonasboner.com/2007/10/30/interview-with-joe-armstrong-on-erlang-and-more/feed/</wfw:commentRSS>
		<feedburner:origLink>http://jonasboner.com/2007/10/30/interview-with-joe-armstrong-on-erlang-and-more/</feedburner:origLink></item>
		<item>
		<title>Clojure: a Lisp-dialect for the JVM - with focus on Functional and Concurrent Programming</title>
		<link>http://feeds.feedburner.com/~r/grubbel/~3/171506319/</link>
		<comments>http://jonasboner.com/2007/10/18/clojure-a-lisp-dialect-for-the-jvm-with-focus-on-functional-and-concurrent-programming/#comments</comments>
		<pubDate>Thu, 18 Oct 2007 08:47:47 +0000</pubDate>
		<dc:creator>Jonas</dc:creator>
		
	<category>Programming Languages</category>
		<guid isPermaLink="false">http://jonasboner.com/2007/10/18/clojure-a-lisp-dialect-for-the-jvm-with-focus-on-functional-and-concurrent-programming/</guid>
		<description><![CDATA[<p>Yesterday, Rich Hickey announced the birth of Clojure - a lisp dialect for the JVM. 
After just a brief look, Clojure is perhaps the most interesting language in the (now fairly large) family of &#8216;dynamic languages for the JVM&#8217;. It brings the power of Lisp to the JVM but have made some design decisions that [...]</p>
]]></description>
			<content:encoded><![CDATA[<p>Yesterday, Rich Hickey announced the birth of <a href="http://clojure.sourceforge.net/">Clojure</a> - a lisp dialect for the JVM. </p>
<p>After just a brief look, Clojure is perhaps the most interesting language in the (now fairly large) family of &#8216;dynamic languages for the JVM&#8217;. It brings the power of Lisp to the JVM but have made some design decisions that in some ways makes it more interesting than <a href="http://en.wikipedia.org/wiki/Common_lisp">ANSI Common Lisp</a>. Here are some of the things that I find particularly interesting:</p>
<ul>
<li>Allows pure functional programming with immutable state (with immutable data-structures etc.) for side-effect-free code (possible in CL but hard to enforce).
</li>
<li>
<ul>
Great support for concurrent programming: </p>
<li>Immutable state can be freely shared across threads.
</li>
<li><a href="http://en.wikipedia.org/wiki/Software_transactional_memory">Software Transactional Memory</a> (STM) that allows atomic and isolated updates to mutable state (through &#8220;Refs&#8221;) with rollback and retry upon collision.
</li>
<li>Safe usage of mutable state through thread isolation (using &#8220;Vars&#8221;).
</li>
</ul>
</li>
<li>Full Lisp-style macro support and eval.
</li>
<li>Compiles to JVM bytecode but still fully dynamic (sounds promising but I don&#8217;t know its actual performance).
</li>
<li>Excellent integration with Java APIs, with type inference for static compilation of Java API calls.
</li>
</ul>
<p>Here are some tasters (from the <a href="http://groups.google.com/group/clojure">forum</a>).</p>
<p><strong>Java integration:</strong></p>
<pre>
(new java.util.Date)
=> Wed Oct 17 20:01:38 CEST 2007

(. (new java.util.Date) (getTime))
=> 1192644138751 

(.. System out (println "This is cool!"))
This is cool!
</pre>
<p><strong>Macros:</strong></p>
<pre>
(defmacro time [form]
  `(let [t0# (. System (currentTimeMillis))
         res# ~form
         t1# (. System (currentTimeMillis))]
    (.. System out (println (strcat "Execution took "
                                    (/ (- t1# t0#) 1000.0) " s")))
    res#))

Usage:
(defn factorial [n]
   (if (< n 2)
       1
       (* n (factorial (- n 1)))))

(time (factorial 1000))
=> Execution took 0.012 s
     40&#8230;
</pre>
<p>It is still in beta but if you want to start playing around with it yourself, dive into the <a href="http://clojure.sourceforge.net/reference/getting_started.html">docs</a>.</p>
<p><strong>Dreaming on:</strong><br />
Stuff that I would like to see (in order to make it the ultimate playground) are among other things: message-passing concurrency (I don&#8217;t fully believe in STM&#8230;yet) and declarative pattern matching (from <a href="http://erlang.org">Erlang</a>), implicit currying and laziness (as in <a href="http://haskell.org">Haskell</a>), transparent distribution (as in <a href="http://www.mozart-oz.org/">Mozart/Oz</a> and Erlang) and optional static typing. Some of these can be found in <a href="http://www.lambdassociates.org/">Qi</a>, I just would love to see them on the JVM.  </p>
<div class="feedflare">
<a href="http://feeds.feedburner.com/~f/grubbel?a=Ifg6RmA"><img src="http://feeds.feedburner.com/~f/grubbel?i=Ifg6RmA" border="0"></img></a> <a href="http://feeds.feedburner.com/~f/grubbel?a=H0FkJLa"><img src="http://feeds.feedburner.com/~f/grubbel?i=H0FkJLa" border="0"></img></a> <a href="http://feeds.feedburner.com/~f/grubbel?a=TjfLFva"><img src="http://feeds.feedburner.com/~f/grubbel?i=TjfLFva" border="0"></img></a> <a href="http://feeds.feedburner.com/~f/grubbel?a=3otCASa"><img src="http://feeds.feedburner.com/~f/grubbel?i=3otCASa" border="0"></img></a>
</div>]]></content:encoded>
			<wfw:commentRSS>http://jonasboner.com/2007/10/18/clojure-a-lisp-dialect-for-the-jvm-with-focus-on-functional-and-concurrent-programming/feed/</wfw:commentRSS>
		<feedburner:origLink>http://jonasboner.com/2007/10/18/clojure-a-lisp-dialect-for-the-jvm-with-focus-on-functional-and-concurrent-programming/</feedburner:origLink></item>
		<item>
		<title>Interviewed by Xebia: Part 2</title>
		<link>http://feeds.feedburner.com/~r/grubbel/~3/167839181/</link>
		<comments>http://jonasboner.com/2007/10/10/terracotta-podcast-part-2/#comments</comments>
		<pubDate>Wed, 10 Oct 2007 08:51:27 +0000</pubDate>
		<dc:creator>Jonas</dc:creator>
		
	<category>Java</category>
	<category>Distributed Computing</category>
	<category>Concurrent Computing</category>
		<guid isPermaLink="false">http://jonasboner.com/2007/10/10/terracotta-podcast-part-2/</guid>
		<description><![CDATA[<p>The second half of an interview that Xebia did with me some months ago have been published.
In this half which we discuss Terracotta in more detail, covering Network-Attached Memory and JVM-level clustering in depth (some stuff that we cover is slightly outdated, but most of it still holds). 
You can find it here.
If you missed [...]</p>
]]></description>
			<content:encoded><![CDATA[<p>The second half of an interview that Xebia did with me some months ago have been published.</p>
<p>In this half which we discuss <a href="http://terracotta.org">Terracotta</a> in more detail, covering Network-Attached Memory and JVM-level clustering in depth (some stuff that we cover is slightly outdated, but most of it still holds). </p>
<p>You can find it <a href="http://podcast.xebia.com/Xebia_Audio_and_Video_Podcast/Entries/2007/10/10_Episode_13_-_Terracotta_Part_2.html">here</a>.</p>
<p>If you missed the first part you can read more it <a href="http://jonasboner.com/2007/09/18/terracotta-podcast-part-1/">here</a> (which gives more of an overview).</p>
<div class="feedflare">
<a href="http://feeds.feedburner.com/~f/grubbel?a=hb1NrF5G"><img src="http://feeds.feedburner.com/~f/grubbel?i=hb1NrF5G" border="0"></img></a> <a href="http://feeds.feedburner.com/~f/grubbel?a=y6pCZAt5"><img src="http://feeds.feedburner.com/~f/grubbel?i=y6pCZAt5" border="0"></img></a> <a href="http://feeds.feedburner.com/~f/grubbel?a=73qwnX26"><img src="http://feeds.feedburner.com/~f/grubbel?i=73qwnX26" border="0"></img></a> <a href="http://feeds.feedburner.com/~f/grubbel?a=Ai94gkN0"><img src="http://feeds.feedburner.com/~f/grubbel?i=Ai94gkN0" border="0"></img></a>
</div>]]></content:encoded>
			<wfw:commentRSS>http://jonasboner.com/2007/10/10/terracotta-podcast-part-2/feed/</wfw:commentRSS>
		<feedburner:origLink>http://jonasboner.com/2007/10/10/terracotta-podcast-part-2/</feedburner:origLink></item>
		<item>
		<title>Interviewed by Xebia: Part 1</title>
		<link>http://feeds.feedburner.com/~r/grubbel/~3/158419295/</link>
		<comments>http://jonasboner.com/2007/09/18/terracotta-podcast-part-1/#comments</comments>
		<pubDate>Wed, 19 Sep 2007 06:46:18 +0000</pubDate>
		<dc:creator>Jonas</dc:creator>
		
	<category>Java</category>
	<category>Distributed Computing</category>
	<category>Web</category>
	<category>Concurrent Computing</category>
		<guid isPermaLink="false">http://jonasboner.com/2007/09/18/terracotta-podcast-part-1/</guid>
		<description><![CDATA[<p>Xebia did an interview with me some months ago in which we discuss things like Terracotta, scale-out and high-availability for Java, JVM-level clustering, network-attached memory, distributed and concurrent programming, JEE best practices etc. 
The interview is split up in two parts, the first one being more of an overview while the second one discusses things [...]</p>
]]></description>
			<content:encoded><![CDATA[<p><a href="http://xebia.com/">Xebia</a> did an interview with me some months ago in which we discuss things like <a href="http://terracotta.org/">Terracotta</a>, scale-out and high-availability for Java, JVM-level clustering, network-attached memory, distributed and concurrent programming, JEE best practices etc. </p>
<p>The interview is split up in two parts, the first one being more of an overview while the second one discusses things in more detail.</p>
<p>You can find the first part <a href="http://podcast.xebia.com/Xebia_Audio_and_Video_Podcast/Entries/2007/9/19_Episode_11_-_Terracotta_Part_1.html">here</a>. </p>
<p>Second part will be published in 2 weeks.</p>
<div class="feedflare">
<a href="http://feeds.feedburner.com/~f/grubbel?a=iQhgXXeJ"><img src="http://feeds.feedburner.com/~f/grubbel?i=iQhgXXeJ" border="0"></img></a> <a href="http://feeds.feedburner.com/~f/grubbel?a=OOon6lSS"><img src="http://feeds.feedburner.com/~f/grubbel?i=OOon6lSS" border="0"></img></a> <a href="http://feeds.feedburner.com/~f/grubbel?a=6P9aXThi"><img src="http://feeds.feedburner.com/~f/grubbel?i=6P9aXThi" border="0"></img></a> <a href="http://feeds.feedburner.com/~f/grubbel?a=ryZ1sKgL"><img src="http://feeds.feedburner.com/~f/grubbel?i=ryZ1sKgL" border="0"></img></a>
</div>]]></content:encoded>
			<wfw:commentRSS>http://jonasboner.com/2007/09/18/terracotta-podcast-part-1/feed/</wfw:commentRSS>
		<feedburner:origLink>http://jonasboner.com/2007/09/18/terracotta-podcast-part-1/</feedburner:origLink></item>
		<item>
		<title>Article: Distributed Web Continuations with RIFE and Terracotta</title>
		<link>http://feeds.feedburner.com/~r/grubbel/~3/142105582/</link>
		<comments>http://jonasboner.com/2007/08/08/article-distributed-web-continuations-with-rife-and-terracotta/#comments</comments>
		<pubDate>Wed, 08 Aug 2007 19:52:13 +0000</pubDate>
		<dc:creator>Jonas</dc:creator>
		
	<category>Java</category>
	<category>Distributed Computing</category>
	<category>Web</category>
	<category>Open Source</category>
		<guid isPermaLink="false">http://jonasboner.com/2007/08/08/article-distributed-web-continuations-with-rife-and-terracotta/</guid>
		<description><![CDATA[<p>My and Geert Bevin&#8217;s article about how to cluster RIFE&#8217;s Web Continuations with Terracotta has just been published on Artima. 
Here is the abstract:
In this article, we discuss how the RIFE Web framework helps you become productive and efficient in building conversational Web applications. Productivity with RIFE is in large part due to RIFE&#8217;s unique [...]</p>
]]></description>
			<content:encoded><![CDATA[<p>My and <a href="http://rifers.org/blogs/gbevin">Geert Bevin&#8217;s</a> article about how to cluster <a href="http://rifers.org">RIFE&#8217;s</a> Web Continuations with <a href="http://terracotta.org">Terracotta</a> has just been <a href="http://www.artima.com/lejava/articles/distributed_continuations.html">published on Artima</a>. </p>
<p>Here is the abstract:</p>
<blockquote><p>In this article, we discuss how the RIFE Web framework helps you become productive and efficient in building conversational Web applications. Productivity with RIFE is in large part due to RIFE&#8217;s unique approach to Web development—its use of continuations for conversational logic, and complete integration of meta-programming to minimize boilerplate code.</p>
<p>We also introduce you to Terracotta and it&#8217;s JVM-level clustering technology, and show you how Terracotta and RIFE can work together to create an application stack that allows you to scale out and ensure high-availability for your applications, but without sacrificing simplicity and productivity. This means working with POJOs, and minimal boilerplate and infrastructure code.</p></blockquote>
<p>It tries to not only explain but to show you in a pragmatic way how RIFE&#8217;s continuations and Terracotta is a perfect match with their common goal of power with simplicity. But don&#8217;t take my word for it, go on and <a href="http://www.artima.com/lejava/articles/distributed_continuations.html">read it yourself</a>. </p>
<div class="feedflare">
<a href="http://feeds.feedburner.com/~f/grubbel?a=qU3suuCT"><img src="http://feeds.feedburner.com/~f/grubbel?i=qU3suuCT" border="0"></img></a> <a href="http://feeds.feedburner.com/~f/grubbel?a=C2Q6vbUW"><img src="http://feeds.feedburner.com/~f/grubbel?i=C2Q6vbUW" border="0"></img></a> <a href="http://feeds.feedburner.com/~f/grubbel?a=dQzbpCIR"><img src="http://feeds.feedburner.com/~f/grubbel?i=dQzbpCIR" border="0"></img></a> <a href="http://feeds.feedburner.com/~f/grubbel?a=LZ62yuUf"><img src="http://feeds.feedburner.com/~f/grubbel?i=LZ62yuUf" border="0"></img></a>
</div>]]></content:encoded>
			<wfw:commentRSS>http://jonasboner.com/2007/08/08/article-distributed-web-continuations-with-rife-and-terracotta/feed/</wfw:commentRSS>
		<feedburner:origLink>http://jonasboner.com/2007/08/08/article-distributed-web-continuations-with-rife-and-terracotta/</feedburner:origLink></item>
	</channel>
</rss>
