<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
 
 <title>Jonas Bonér</title>
 <link href="http://jboner.github.com/atom.xml" rel="self"/>
 <link href="http://jboner.github.com"/>
 <updated>2015-12-31T08:15:27+00:00</updated>
 <id>http://jboner.github.com</id>
 <author>
   <name>Jonas Bonér</name>
   <email>jonas@jonasboner.com</email>
 </author>

 
 <entry>
   <title>Introducing Akka - Simpler Scalability, Fault-Tolerance, Concurrency &amp; Remoting through Actors</title>
   <link href="http://jboner.github.com/2010/01/04/introducing-akka"/>
   <updated>2010-01-04T00:00:00+00:00</updated>
   <id>hhttp://jboner.github.com/2010/01/04/introducing-akka</id>
   <content type="html">&lt;h1&gt;Introducing Akka &amp;#8211; Simpler Scalability, Fault-Tolerance, Concurrency &amp;amp; Remoting through Actors&lt;/h1&gt;
&lt;p class=&quot;meta&quot;&gt;4 Jan 2010&lt;/p&gt;
&lt;h1&gt;Introduction&lt;/h1&gt;
&lt;p&gt;Writing correct concurrent, fault-tolerant and scalable applications is too hard. Most of the time it&amp;#8217;s because we are using the wrong tools and the wrong level of abstraction.&lt;/p&gt;
&lt;p&gt;&lt;a href=&quot;http://akkasource.org&quot;&gt;Akka&lt;/a&gt; is an attempt to change that.&lt;/p&gt;
&lt;p&gt;Akka uses the Actor Model together with Software Transactional Memory to raise the abstraction level and provide a better platform to build correct concurrent and scalable applications.&lt;/p&gt;
&lt;p&gt;For fault-tolerance Akka adopts the &amp;#8220;Let it crash&amp;#8221;, also called &amp;#8220;Embrace failure&amp;#8221;, model which have been used with great success in the telecom industry to build applications that self-heals, systems that never stop.&lt;/p&gt;
&lt;p&gt;Actors also provides the abstraction for transparent distribution and the basis for truly scalable and fault-tolerant applications.&lt;/p&gt;
&lt;p&gt;Akka is Open Source and available under the Apache 2 License.&lt;/p&gt;
&lt;p&gt;In this article we will introduce you to Akka and see how we can utilize it to build a highly concurrent, scalable and fault-tolerant network server.&lt;/p&gt;
&lt;p&gt;But first let&amp;#8217;s take a step back and discuss what Actors really are and what they are useful for.&lt;/p&gt;
&lt;h1&gt;Actors&lt;/h1&gt;
&lt;p&gt;&lt;a href=&quot;http://en.wikipedia.org/wiki/Actor_model&quot;&gt;The Actor Model&lt;/a&gt; provides a higher level of abstraction for writing concurrent and distributed systems. It alleviates the developer from having to deal with explicit locking and thread management. It makes it easier to write correct concurrent and parallel systems. Actors are really nothing new, they were defined in the 1963 paper by Carl Hewitt and have been popularized by the Erlang language which emerged in the mid 80s. It has been used by for example at Ericsson with great success to build highly concurrent and extremely reliable (99.9999999 % availability &amp;#8211; 31 ms/year downtime) telecom systems.&lt;/p&gt;
&lt;p&gt;Actors encapsulates state and behavior into a lightweight process/thread. In a sense they are like OO objects but with a major semantic difference; they &lt;strong&gt;do not&lt;/strong&gt; share state with any other Actor. Each Actor have their own view of the world and can only have impact on other Actors by sending messages to them. Messages are sent asynchronously and non-blocking in a so-called &amp;#8220;fire-and-forget&amp;#8221; manner where the Actor sends off a message to some other Actor and then do not wait for a reply but goes off doing other things or are suspended by the runtime. Each Actor has a mailbox (ordered message queue) in which incoming messages are processed one by one. Since all processing is done asynchronously and Actors do not block and consume any resources while waiting for messages, Actors tend to  give very good concurrency and scalability characteristics and are excellent for building event-based systems.&lt;/p&gt;
&lt;h1&gt;Creating Actors&lt;/h1&gt;
&lt;p&gt;Akka has both a &lt;a href=&quot;http://doc.akkasource.org/actors&quot;&gt;Scala &lt;span class=&quot;caps&quot;&gt;API&lt;/span&gt;&lt;/a&gt; and a &lt;a href=&quot;http://doc.akkasource.org/active-objects&quot;&gt;Java &lt;span class=&quot;caps&quot;&gt;API&lt;/span&gt;&lt;/a&gt;. In this article we will only look at the Scala &lt;span class=&quot;caps&quot;&gt;API&lt;/span&gt; since that is the most expressive one. The article assumes some basic Scala knowledge, but even if you don&amp;#8217;t know Scala I don&amp;#8217;t think it will not be too hard to follow along anyway.&lt;/p&gt;
&lt;p&gt;Akka has adopted the same style of writing Actors as Erlang in which each Actor has an explicit message handler which does pattern matching to match on the incoming messages.&lt;/p&gt;
&lt;p&gt;Actors can be created either by:&lt;/p&gt;
&lt;ul&gt;
	&lt;li&gt;Extending the &amp;#8216;Actor&amp;#8217; class and implementing the &amp;#8216;receive&amp;#8217; method.&lt;/li&gt;
	&lt;li&gt;Create an anonymous Actor using one of the &amp;#8216;actor&amp;#8217; methods.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Here is a little example before we dive into a more interesting one.&lt;/p&gt;
&lt;pre name=&quot;code&quot; class=&quot;scala&quot;&gt;
class MyActor extends Actor {
  def receive = {
    case &quot;test&quot; =&amp;gt; println(&quot;received test&quot;)
    case _ =&amp;gt;      println(&quot;received unknown message&quot;)
  }
}

val myActor = new MyActor
myActor.start
&lt;/pre&gt;
&lt;p&gt;Here is the same Actor with the anonymous syntax. Anonymous Actors are implicitly started:&lt;/p&gt;
&lt;pre name=&quot;code&quot; class=&quot;scala&quot;&gt;
val myActor = actor { 
  case &quot;test&quot; =&amp;gt; println(&quot;received test&quot;)
  case _ =&amp;gt;      println(&quot;received unknown message&quot;)
}
&lt;/pre&gt;
&lt;p&gt;Akka Actors are extremely lightweight. Each Actor consume ~600 bytes, which means that you can create 6.5 million on 4 G &lt;span class=&quot;caps&quot;&gt;RAM&lt;/span&gt;.&lt;/p&gt;
&lt;p&gt;Messages are sent using the &amp;#8216;!&amp;#8217; operator:&lt;/p&gt;
&lt;pre name=&quot;code&quot; class=&quot;scala&quot;&gt;
myActor ! &quot;test&quot;
&lt;/pre&gt;
&lt;h1&gt;Sample application&lt;/h1&gt;
&lt;p&gt;We will try to write a simple chat/IM system. It is client-server based and uses remote Actors to implement remote clients. Even if it is not likely that you will ever write a chat system I think that it can be a useful exercise since it uses patterns and idioms found in many other use-cases and domains.&lt;/p&gt;
&lt;p&gt;We will use many of the features of Akka along the way. In particular; Actors, fault-tolerance using Actor supervision, remote Actors, Software Transactional Memory (&lt;span class=&quot;caps&quot;&gt;STM&lt;/span&gt;) and persistence.&lt;/p&gt;
&lt;p&gt;But let&amp;#8217;s start by defining the messages that will flow in our system.&lt;/p&gt;
&lt;h1&gt;Creating messages&lt;/h1&gt;
&lt;p&gt;It is very important that all messages that will be sent around in the system are immutable. The Actor model relies on the simple fact that no state is shared between Actors and the only way to guarantee that is to make sure we don&amp;#8217;t pass mutable state around as part of the messages.&lt;/p&gt;
&lt;p&gt;In Scala we have something called &lt;a href=&quot;http://www.scala-lang.org/node/107&quot;&gt;case classes&lt;/a&gt;. These make excellent messages since they are both immutable and great to pattern match on.&lt;/p&gt;
&lt;p&gt;Let&amp;#8217;s now start by creating the messages that will flow in our system.&lt;/p&gt;
&lt;pre name=&quot;code&quot; class=&quot;scala&quot;&gt;
/**
 * ChatServer&#39;s internal events.
 */
sealed trait Event

case class Login(username: String) extends Event
case class Logout(username: String) extends Event

case class ChatMessage(fromUser: String, message: String) extends Event

case class GetChatLog(fromUser: String) extends Event
case class ChatLog(messages: List[String]) extends Event
&lt;/pre&gt;
&lt;p&gt;As you can see with these messages we can log in and out, send a chat message and ask for and get a reply with all the messages in the chat log so far.&lt;/p&gt;
&lt;h1&gt;Client: Sending messages&lt;/h1&gt;
&lt;p&gt;Our client wraps each message send in a function, making it a bit easier to use. Here we assume that we have a reference to the chat service so we can communicate with it by sending messages. Messages are sent with the &amp;#8216;!&amp;#8217; operator (pronounced &amp;#8220;bang&amp;#8221;). This sends a message of asynchronously and do not wait for a reply.&lt;/p&gt;
&lt;p&gt;Sometimes however, there is a need for sequential logic, sending a message and wait for the reply before doing anything else. In Akka we can achieve that using the &amp;#8216;!!&amp;#8217; (&amp;#8220;bangbang&amp;#8221;) operator. When sending a message with &amp;#8216;!!&amp;#8217; we do not return immediately but wait for a reply using a &lt;a href=&quot;http://en.wikipedia.org/wiki/Futures_and_promises&quot;&gt;Future&lt;/a&gt;. A &amp;#8216;Future&amp;#8217; is a promise that we will get a result later but with the difference from regular method dispatch that the OS thread we are running on is put to sleep while waiting and that we can set a time-out for how long we wait before bailing out, retrying or doing something else. The &amp;#8216;!!&amp;#8217; function returns a &lt;a href=&quot;http://www.codecommit.com/blog/scala/the-option-pattern&quot;&gt;scala.Option&lt;/a&gt; which implements the &lt;a href=&quot;http://en.wikipedia.org/wiki/Null_Object_pattern&quot;&gt;Null Object pattern&lt;/a&gt;. It has two subclasses; &amp;#8216;None&amp;#8217; which means no result and &amp;#8216;Some(value)&amp;#8217; which means that we got a reply. The &amp;#8216;Option&amp;#8217; class has a lot of great methods to work with the case of not getting a defined result. F.e. as you can see below we are using the &amp;#8216;getOrElse&amp;#8217; method which will try to return the result and if there is no result defined invoke the &amp;#8220;&amp;#8230;OrElse&amp;#8221; statement.&lt;/p&gt;
&lt;pre name=&quot;code&quot; class=&quot;scala&quot;&gt;
/**
 * Chat client.
 */
class ChatClient(val name: String) { 
  import Actor.Sender.Self
  def login =                 ChatService ! Login(name) 
  def logout =                ChatService ! Logout(name)  
  def post(message: String) = ChatService ! ChatMessage(name, name + &quot;: &quot; + message)  
  def chatLog: ChatLog = {
    val option = ChatService !! (GetChatLog(name), 1000) // timeout 1000 ms
    option.getOrElse(throw new Exception(&quot;Couldn&#39;t get the chat log&quot;))	
  }
}
&lt;/pre&gt;
&lt;h1&gt;Session: Receiving messages&lt;/h1&gt;
&lt;p&gt;Now we are done with the client side and let&amp;#8217;s dig into the server code. We start by creating a user session. The session is an Actor and is defined by extending the &amp;#8216;Actor&amp;#8217; trait. This trait has one abstract method that we have to define; &amp;#8216;receive&amp;#8217; which implements the message handler for the Actor.&lt;/p&gt;
&lt;p&gt;In our example the session has state in the form of a &amp;#8216;List&amp;#8217; with all the messages sent by the user during the session. In takes two parameters in its constructor; the user name and a reference to an Actor implementing the persistent message storage. For both of the messages it responds to, &amp;#8216;ChatMessage&amp;#8217; and &amp;#8216;GetChatLog&amp;#8217;, it passes them on to the storage Actor.&lt;/p&gt;
&lt;p&gt;If you look closely (in the code below) you will see that when passing on the &amp;#8216;GetChatLog&amp;#8217; message we are not using &amp;#8216;!&amp;#8217; but &amp;#8216;forward&amp;#8217;. This is similar to &amp;#8216;!&amp;#8217; but with the important difference that it passes the original sender reference, in this case to the storage Actor. This means that the storage can use this reference to reply to the original sender (our client) directly.&lt;/p&gt;
&lt;pre name=&quot;code&quot; class=&quot;scala&quot;&gt;
/**
 * Internal chat client session.
 */
class Session(user: String, storage: Actor) extends Actor {
  private val loginTime = System.currentTimeMillis
  private var userLog: List[String] = Nil

  log.info(&quot;New session for user [%s] has been created at [%s]&quot;, user, loginTime)

  def receive = {
    case event: ChatMessage =&amp;gt; 
      userLog ::= event.message
      storage ! event

    case event: GetChatLog =&amp;gt; 
      storage forward event
  }
}
&lt;/pre&gt;
&lt;h1&gt;Let it crash: Implementing fault-tolerance&lt;/h1&gt;
&lt;p&gt;Akka&amp;#8217;s &lt;a href=&quot;http://doc.akkasource.org/fault-management&quot;&gt;approach to fault-tolerance&lt;/a&gt;; the &amp;#8220;let it crash&amp;#8221; model, is implemented by linking Actors. It is very different to what Java and most non-concurrency oriented languages/frameworks have adopted. It’s a way of dealing with failure that is designed for concurrent and distributed systems.&lt;/p&gt;
&lt;p&gt;If we look at concurrency first. Now let’s assume we are using non-linked Actors. Throwing an exception in concurrent code, will just simply blow up the thread that currently executes the Actor. There is no way to find out that things went wrong (apart from see the stack trace in the log). There is nothing you can do about it. Here linked Actors provide a clean way of both getting notification of the error so you know what happened, as well as the Actor that crashed, so you can do something about it.&lt;/p&gt;
&lt;p&gt;Linking Actors allow you to create sets of Actors where you can be sure that either:&lt;/p&gt;
&lt;ul&gt;
	&lt;li&gt;All are dead&lt;/li&gt;
	&lt;li&gt;All are alive&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;This is very useful when you have hundreds of thousands of concurrent Actors. Some Actors might have implicit dependencies and together implement a service, computation, user session etc. for these being able to group them is very nice.&lt;/p&gt;
&lt;p&gt;Akka encourages non-defensive programming. Don’t try to prevent things from go wrong, because they will, whether you want it or not. Instead; expect failure as a natural state in the life-cycle of your app, crash early and let someone else (that sees the whole picture), deal with it.&lt;/p&gt;
&lt;p&gt;Now let’s look at distributed Actors. As you probably know, you can’t build a fault-tolerant system with just one single node, but you need at least two. Also, you (usually) need to know if one node is down and/or the service you are talking to on the other node is down. Here Actor supervision/linking is a critical tool for not only monitoring the health of remote services, but to actually manage the service, do something about the problem if the Actor or node is down. This could be restarting him on the same node or on another node.&lt;/p&gt;
&lt;p&gt;To sum things up, it is a very different way of thinking but a way that is very useful (if not critical) to building fault-tolerant highly concurrent and distributed applications.&lt;/p&gt;
&lt;h1&gt;Supervisor hierarchies&lt;/h1&gt;
&lt;p&gt;A supervisor is a regular Actor that is responsible for starting, stopping and monitoring its child Actors. The basic idea of a supervisor is that it should keep its child Actors alive by restarting them when necessary. This makes for a completely different view on how to write fault-tolerant servers. Instead of trying all things possible to prevent an error from happening, this approach embraces failure. It shifts the view to look at errors as something natural and something that will happen and instead of trying to prevent it; embrace it. Just &amp;#8220;let it crash&amp;#8221; and reset the service to a stable state through restart.&lt;/p&gt;
&lt;p&gt;Akka has two different restart strategies; All-For-One and One-For-One.&lt;/p&gt;
&lt;ul&gt;
	&lt;li&gt;OneForOne: Restart only the component that has crashed.&lt;/li&gt;
	&lt;li&gt;AllForOne: Restart all the components that the supervisor is managing, including the one that have crashed.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The latter strategy should be used when you have a certain set of components that are coupled in some way that if one is crashing they all need to be reset to a stable state before continuing.&lt;/p&gt;
&lt;h1&gt;Chat server: Supervision, Traits and more&lt;/h1&gt;
&lt;p&gt;There are two ways you can define an Actor to be a supervisor; declaratively and dynamically. In this example we use the dynamic approach. There are two things we have to do:&lt;/p&gt;
&lt;ul&gt;
	&lt;li&gt;Define the fault handler by setting the &amp;#8216;faultHandler&amp;#8217; member field to the strategy we want.&lt;/li&gt;
	&lt;li&gt;Define the exceptions we want to &amp;#8220;trap&amp;#8221;, e.g. which exceptions should be handled according to the fault handling strategy we have defined. This in done by setting the &amp;#8216;trapExit&amp;#8217; member field to a &amp;#8216;List&amp;#8217; with all exceptions we want to trap.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The last thing we have to do to supervise Actors (in our example the storage Actor) is to &amp;#8216;link&amp;#8217; the Actor. Invoking &amp;#8216;link(actor)&amp;#8217; will create a link between the Actor passed as argument into &amp;#8216;link&amp;#8217; and ourselves. This means that we will now get a notification if the linked Actor is crashing and if the cause of the crash, the exception, matches one of the exceptions in our &amp;#8216;trapExit&amp;#8217; list then the crashed Actor is restarted according the the fault handling strategy defined in our &amp;#8216;faultHandler&amp;#8217;. We also have the &amp;#8216;unlink(actor)&amp;#8217; function which disconnects the linked Actor from the supervisor.&lt;/p&gt;
&lt;p&gt;In our example we are using a method called &amp;#8216;startLink(actor)&amp;#8217; which starts the Actor and links him in an atomic operation. The linking and unlinking is done in &amp;#8216;init&amp;#8217; and &amp;#8216;shutdown&amp;#8217; callback methods which are invoked by the runtime when the Actor is started and shut down (shutting down is done by invoking &amp;#8216;actor.stop&amp;#8217;). In these methods we initialize our Actor, by starting and linking the storage Actor and clean up after ourselves by shutting down all the user session Actors and the storage Actor.&lt;/p&gt;
&lt;p&gt;That is it. Now we have implemented the supervising part of the fault-tolerance for the storage Actor. But before we dive into the &amp;#8216;ChatServer&amp;#8217; code there are some more things worth mentioning about its implementation.&lt;/p&gt;
&lt;p&gt;It defines an abstract member field holding the &amp;#8216;ChatStorage&amp;#8217; implementation the server wants to use. We do not define that in the &amp;#8216;ChatServer&amp;#8217; directly since we want to decouple it from the actual storage implementation.&lt;/p&gt;
&lt;p&gt;The &amp;#8216;ChatServer&amp;#8217; is a &amp;#8216;trait&amp;#8217;, which is Scala&amp;#8217;s version of mixins. A mixin can be seen as an interface with an implementation and is a very powerful tool in Object-Oriented design that makes it possible to design the system into small, reusable, highly cohesive, loosely coupled parts that can be composed into larger object and components structures.&lt;/p&gt;
&lt;p&gt;I&amp;#8217;ll try to show you how we can make use Scala&amp;#8217;s mixins to decouple the Actor implementation from the business logic of managing the user sessions, routing the chat messages and storing them in the persistent storage. Each of these separate parts of the server logic will be represented by its own trait; giving us four different isolated mixins; &amp;#8216;Actor&amp;#8217;, &amp;#8216;SessionManagement&amp;#8217;, &amp;#8216;ChatManagement&amp;#8217; and &amp;#8216;ChatStorageFactory&amp;#8217; This will give us as loosely coupled system with high cohesion and reusability. At the end of the article I&amp;#8217;ll show you how you can compose these mixins into a the complete runtime component we like.&lt;/p&gt;
&lt;pre name=&quot;code&quot; class=&quot;scala&quot;&gt;
/**
 * Chat server. Manages sessions and redirects all 
 * other messages to the Session for the client.
 */
trait ChatServer extends Actor {
  faultHandler = Some(OneForOneStrategy(5, 5000))
  trapExit = List(classOf[Exception])

  val storage: ChatStorage

  log.info(&quot;Chat service is starting up...&quot;)

  // actor message handler
  def receive = sessionManagement orElse chatManagement

  // abstract methods to be defined somewhere else
  protected def chatManagement: PartialFunction[Any, Unit]
  protected def sessionManagement: PartialFunction[Any, Unit]   
  protected def shutdownSessions: Unit

  override def init = startLink(storage)

  override def shutdown = { 
    log.info(&quot;Chat server is shutting down...&quot;)
    shutdownSessions
    unlink(storage)
    storage.stop
  }
}
&lt;/pre&gt;
&lt;p&gt;If you look at the &amp;#8216;receive&amp;#8217; message handler function you can see that we have defined it but instead of adding our logic there we are delegating to two different functions; &amp;#8216;sessionManagement&amp;#8217; and &amp;#8216;chatManagement&amp;#8217;, chaining them with &amp;#8216;orElse&amp;#8217;. These two functions are defined as abstract in our &amp;#8216;ChatServer&amp;#8217; which means that they have to be provided by some another mixin or class when we instantiate our &amp;#8216;ChatServer&amp;#8217;. Naturally we will put the &amp;#8216;sessionManagement&amp;#8217; implementation in the &amp;#8216;SessionManagement&amp;#8217; trait and the &amp;#8216;chatManagement&amp;#8217; implementation in the &amp;#8216;ChatManagement&amp;#8217; trait. First let&amp;#8217;s create the &amp;#8216;SessionManagement&amp;#8217; trait.&lt;/p&gt;
&lt;p&gt;Chaining partial functions like this is a great way of composing functionality in Actors. You can for example put define one default message handle handling generic messages in the base Actor and then let deriving Actors extend that functionality by defining additional message handlers. There is a section on how that is done &lt;a href=&quot;http://doc.akkasource.org/actors&quot;&gt;here&lt;/a&gt;.&lt;/p&gt;
&lt;h1&gt;Session management&lt;/h1&gt;
&lt;p&gt;The session management is defined in the &amp;#8216;SessionManagement&amp;#8217; trait in which we implement the two abstract methods in the &amp;#8216;ChatServer&amp;#8217;; &amp;#8216;sessionManagement&amp;#8217; and &amp;#8216;shutdownSessions&amp;#8217;.&lt;/p&gt;
&lt;p&gt;The &amp;#8216;SessionManagement&amp;#8217; trait holds a &amp;#8216;HashMap&amp;#8217; with all the session Actors mapped by user name as well as a reference to the storage (to be able to pass it in to each newly created &amp;#8216;Session&amp;#8217;).&lt;/p&gt;
&lt;p&gt;The &amp;#8216;sessionManagement&amp;#8217; function performs session management by responding to the &amp;#8216;Login&amp;#8217; and &amp;#8216;Logout&amp;#8217; messages. For each &amp;#8216;Login&amp;#8217; message it creates a new &amp;#8216;Session&amp;#8217; Actor, starts it and puts it in the &amp;#8216;sessions&amp;#8217; Map and for each &amp;#8216;Logout&amp;#8217; message it does the opposite; shuts down the user&amp;#8217;s session and removes it from the &amp;#8216;sessions&amp;#8217; Map.&lt;/p&gt;
&lt;p&gt;The &amp;#8216;shutdownSessions&amp;#8217; function simply shuts all the sessions Actors down. That completes the user session management.&lt;/p&gt;
&lt;pre name=&quot;code&quot; class=&quot;scala&quot;&gt;
/**
 * Implements user session management.
 * &amp;lt;p/&amp;gt;
 * Uses self-type annotation &#39;this: Actor =&amp;gt;&#39;
 * to declare that it needs to be mixed in with an Actor.
 */
trait SessionManagement { this: Actor =&amp;gt; 

  val storage: ChatStorage // needs someone to provide the ChatStorage
  val sessions = new HashMap[String, Actor]

  protected def sessionManagement: PartialFunction[Any, Unit] = {
    case Login(username) =&amp;gt; 
      log.info(&quot;User [%s] has logged in&quot;, username)
      val session = new Session(username, storage)
      session.start
      sessions += (username -&amp;gt; session)

    case Logout(username) =&amp;gt;        
      log.info(&quot;User [%s] has logged out&quot;, username)
      val session = sessions(username)
      session.stop
      sessions -= username 
  }  

  protected def shutdownSessions = 
    sessions.foreach { case (_, session) =&amp;gt; session.stop }  
}
&lt;/pre&gt;
&lt;h1&gt;Chat message management&lt;/h1&gt;
&lt;p&gt;Chat message management is implemented by the &amp;#8216;ChatManagement&amp;#8217; trait. It has an abstract &amp;#8216;HashMap&amp;#8217; session member field with all the sessions. Since it is abstract it needs to be mixed in with someone that can provide this reference. If this dependency is not resolved when composing the final component, you will get a compilation error.&lt;/p&gt;
&lt;p&gt;It implements the &amp;#8216;chatManagement&amp;#8217; function which responds to two different messages; &amp;#8216;ChatMessage&amp;#8217; and &amp;#8216;GetChatLog&amp;#8217;. It simply gets the session for the user (the sender of the message) and routes the message to this session. Here we also use the &amp;#8216;forward&amp;#8217; function to make sure the original sender reference is passed along to allow the end receiver to reply back directly.&lt;/p&gt;
&lt;pre name=&quot;code&quot; class=&quot;scala&quot;&gt;
/**
 * Implements chat management, e.g. chat message dispatch.
 * &amp;lt;p/&amp;gt;
 * Uses self-type annotation &#39;this: Actor =&amp;gt;&#39;
 * to declare that it needs to be mixed in with an Actor.
 */
trait ChatManagement { this: Actor =&amp;gt;
  val sessions: HashMap[String, Actor] // someone needs to provide the Session map

  protected def chatManagement: PartialFunction[Any, Unit] = {
    case msg @ ChatMessage(from, _) =&amp;gt; sessions(from) ! msg
    case msg @ GetChatLog(from) =&amp;gt;     sessions(from) forward msg
  }
}
&lt;/pre&gt;
&lt;p&gt;Using an Actor as a message broker, as in this example, is a very common pattern with many variations; load-balancing, master/worker, map/reduce, replication, logging etc. It becomes even more useful with remote Actors when we can use it to route messages to different nodes.&lt;/p&gt;
&lt;h1&gt;&lt;span class=&quot;caps&quot;&gt;STM&lt;/span&gt; and Transactors&lt;/h1&gt;
&lt;p&gt;Actors are excellent for solving problems where you have many independent processes that can work in isolation and only interact with other Actors through message passing. This model fits many problems. But the Actor model is unfortunately a terrible model for implementing truly shared state. E.g. when you need to have consensus and a stable view of state across many components. The classic example is the bank account where clients can deposit and withdraw, in which each operation needs to be atomic. For detailed discussion on the topic see this &lt;a href=&quot;http://www.slideshare.net/jboner/state-youre-doing-it-wrong-javaone-2009&quot;&gt;presentation&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;&lt;a href=&quot;http://en.wikipedia.org/wiki/Software_transactional_memory&quot;&gt;Software Transactional Memory&lt;/a&gt; (&lt;span class=&quot;caps&quot;&gt;STM&lt;/span&gt;) on the other hand is excellent for problems where you need consensus and a stable view of the state by providing compositional transactional shared state. Some of the really nice traits of &lt;span class=&quot;caps&quot;&gt;STM&lt;/span&gt; are that transactions compose and that it raises the abstraction level from lock-based concurrency.&lt;/p&gt;
&lt;p&gt;Akka has a &lt;a href=&quot;http://doc.akkasource.org/stm&quot;&gt;&lt;span class=&quot;caps&quot;&gt;STM&lt;/span&gt; implementation&lt;/a&gt; that is based on the same ideas as found in the &lt;a href=&quot;http://clojure.org/&quot;&gt;Clojure language&lt;/a&gt;; Managed References working with immutable data.&lt;/p&gt;
&lt;p&gt;Akka allows you to combine Actors and &lt;span class=&quot;caps&quot;&gt;STM&lt;/span&gt; into what we call &lt;a href=&quot;http://doc.akkasource.org/transactors&quot;&gt;Transactors&lt;/a&gt; (short for Transactional Actors), these allow you to optionally combine Actors and &lt;span class=&quot;caps&quot;&gt;STM&lt;/span&gt; provides &lt;span class=&quot;caps&quot;&gt;IMHO&lt;/span&gt; the best of the Actor model (simple concurrency and asynchronous event-based programming) and &lt;span class=&quot;caps&quot;&gt;STM&lt;/span&gt; (compositional transactional shared state) by providing transactional, compositional, asynchronous, event-based message flows. You don&amp;#8217;t need Transactors all the time but when you do need them then you &lt;strong&gt;really need&lt;/strong&gt; them.&lt;/p&gt;
&lt;p&gt;Akka currently provides three different transactional abstractions; &amp;#8216;Map&amp;#8217;, &amp;#8216;Vector&amp;#8217; and &amp;#8216;Ref&amp;#8217;. They can be shared between multiple Actors and they are managed by the &lt;span class=&quot;caps&quot;&gt;STM&lt;/span&gt;. You are not allowed to modify them outside a transaction, if you do so, an exception will be thrown.&lt;/p&gt;
&lt;p&gt;What you get is transactional memory in which multiple Actors are allowed to read and write to the same memory concurrently and if there is a clash between two transactions then both of them are aborted and retried. Aborting a transaction means that the memory is rolled back to the state it were in when the transaction was started.&lt;/p&gt;
&lt;p&gt;In database terms &lt;span class=&quot;caps&quot;&gt;STM&lt;/span&gt; gives you &amp;#8216;&lt;span class=&quot;caps&quot;&gt;ACI&lt;/span&gt;&amp;#8217; semantics; &amp;#8216;Atomicity&amp;#8217;, &amp;#8216;Consistency&amp;#8217; and &amp;#8216;Isolation&amp;#8217;. The &amp;#8216;D&amp;#8217; in &amp;#8216;&lt;span class=&quot;caps&quot;&gt;ACID&lt;/span&gt;&amp;#8217;; &amp;#8216;Durability&amp;#8217;, you can&amp;#8217;t get with an &lt;span class=&quot;caps&quot;&gt;STM&lt;/span&gt; since it is in memory. This however is addressed by the persistence module in Akka.&lt;/p&gt;
&lt;h1&gt;Persistence: Storing the chat log&lt;/h1&gt;
&lt;p&gt;Akka provides the possibility of taking the transactional data structures we discussed above and making them persistent. It is an extension to the &lt;span class=&quot;caps&quot;&gt;STM&lt;/span&gt; which guarantees that it has the same semantics.&lt;/p&gt;
&lt;p&gt;The &lt;a href=&quot;http://doc.akkasource.org/persistence&quot;&gt;persistence module&lt;/a&gt; has pluggable storage back-ends. At the time of the writing it has three different storage back-ends:&lt;/p&gt;
&lt;ul&gt;
	&lt;li&gt;&lt;a href=&quot;http://wiki.apache.org/cassandra/&quot;&gt;Cassandra&lt;/a&gt; &amp;#8211; A distributed structured storage database.&lt;/li&gt;
	&lt;li&gt;&lt;a href=&quot;http://www.mongodb.org/display/DOCS/Home&quot;&gt;MongoDB&lt;/a&gt; &amp;#8211; A high performance schema-free, document oriented data store with &lt;span class=&quot;caps&quot;&gt;SQL&lt;/span&gt; like query facilities.&lt;/li&gt;
	&lt;li&gt;&lt;a href=&quot;http://code.google.com/p/redis/&quot;&gt;Redis&lt;/a&gt; &amp;#8211; An advanced key-value store, also called a data structure server, with lists, ordered sets etc.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;They all implement persistent &amp;#8216;Map&amp;#8217;, &amp;#8216;Vector&amp;#8217; and &amp;#8216;Ref&amp;#8217;. Which can be created and retrieved by id through one of the storage modules.&lt;/p&gt;
&lt;pre name=&quot;code&quot; class=&quot;scala&quot;&gt;
val map =    RedisStorage.newMap(id)
val vector = CassandraStorage.newVector(id)
val ref =    MongoStorage.newRef(id)
&lt;/pre&gt;
&lt;h1&gt;Chat storage: Backed by Redis&lt;/h1&gt;
&lt;p&gt;Now let&amp;#8217;s implement the persistent storage. We start by creating a &amp;#8216;ChatStorage&amp;#8217; trait allowing us to have multiple different storage backend. For example one in-memory and one persistent.&lt;/p&gt;
&lt;pre name=&quot;code&quot; class=&quot;scala&quot;&gt;
/**
 * Abstraction of chat storage holding the chat log.
 */
trait ChatStorage extends Actor
&lt;/pre&gt;
&lt;p&gt;Let&amp;#8217;s use Redis to implementation the persistent storage. Redis is an excellent storage backend, blazingly fast with a rich data model.&lt;/p&gt;
&lt;p&gt;Our &amp;#8216;RedisChatStorage&amp;#8217; extends the &amp;#8216;ChatStorage&amp;#8217; trait. The only state it holds is the &amp;#8216;chatLog&amp;#8217; which is a &amp;#8216;Vector&amp;#8217; managed by Redis. We give it an explicit id (the String &amp;#8220;akka.chat.log&amp;#8221;) to be able to retrieve the same vector across remote nodes and/or through server restarts.&lt;/p&gt;
&lt;p&gt;It responds to two different messages; &amp;#8216;ChatMessage&amp;#8217; and &amp;#8216;GetChatLog&amp;#8217;. The &amp;#8216;ChatMessage&amp;#8217; message handler takes the &amp;#8216;message&amp;#8217; attribute and appends it to the &amp;#8216;chatLog&amp;#8217; vector. Here you can see  that we are using the &amp;#8216;atomic { &amp;#8230; }&amp;#8217; block to run the vector operation in a transaction. Redis works with binary data so we need to convert the message into a binary representation. Since we are using Strings we just have to invoke &amp;#8216;message.getBytes(&amp;#8220;&lt;span class=&quot;caps&quot;&gt;UTF&lt;/span&gt;-8&amp;#8221;)&amp;#8217;, but if we would have had a richer message that we wanted to persist then we would have had to use one of the Akka&amp;#8217;s serialization traits or serializers. You can read more about that &lt;a href=&quot;http://doc.akkasource.org/serialization&quot;&gt;here&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;The &amp;#8216;GetChatLog&amp;#8217; message handler retrieves all the messages in the chat log storage inside an atomic block, iterates over them using the &amp;#8216;map&amp;#8217; combinator transforming them from &amp;#8216;Array[Byte] to &amp;#8217;String&amp;#8217;. Then it invokes the &amp;#8216;reply(message)&amp;#8217; function that will send the chat log to the original sender; the &amp;#8216;ChatClient&amp;#8217;.&lt;/p&gt;
&lt;p&gt;You might rememeber that the &amp;#8216;ChatServer&amp;#8217; was supervising the &amp;#8216;ChatStorage&amp;#8217; actor. When we discussed that we showed you the supervising Actor&amp;#8217;s view. Now is the time for the supervised Actor&amp;#8217;s side of things. First, a supervised Actor need to define a life-cycle in which it declares if it should be seen as a:&lt;/p&gt;
&lt;ul&gt;
	&lt;li&gt;&amp;#8216;Permanent&amp;#8217;: which means that the actor will always be restarted.&lt;/li&gt;
	&lt;li&gt;&amp;#8216;Temporary&amp;#8217;: which means that the actor will not be restarted, but it will be shut down through the regular shutdown process so the &amp;#8216;shutdown&amp;#8217; callback function will called.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;We define the &amp;#8216;RedisChatStorage&amp;#8217; as &amp;#8216;Permanent&amp;#8217; by setting the &amp;#8216;lifeCycle&amp;#8217; member field to &amp;#8216;Some(LifeCycle(Permanent))&amp;#8217;.&lt;/p&gt;
&lt;p&gt;The idea with this crash early style of designing your system is that the services should just crash and then they should be restarted and reset into a stable state and continue from there. The definition of &amp;#8220;stable state&amp;#8221; is domain specific and up to the application developer to define. Akka provides two callback functions; &amp;#8216;preRestart&amp;#8217; and &amp;#8216;postRestart&amp;#8217; that are called right &lt;strong&gt;before&lt;/strong&gt; and right &lt;strong&gt;after&lt;/strong&gt; the Actor is restarted. Both of these functions take a &amp;#8216;Throwable&amp;#8217;, the reason for the crash, as argument. In our case we just need to implement the &amp;#8216;postRestart&amp;#8217; hook and there re-initialize the &amp;#8216;chatLog&amp;#8217; member field with a fresh persistent &amp;#8216;Vector&amp;#8217; from Redis.&lt;/p&gt;
&lt;pre name=&quot;code&quot; class=&quot;scala&quot;&gt;
/**
 * Redis-backed chat storage implementation.
 */
class RedisChatStorage extends ChatStorage {
  lifeCycle = Some(LifeCycle(Permanent))    

  private var chatLog = RedisStorage.getVector(&quot;akka.chat.log&quot;)

  log.info(&quot;Redis-based chat storage is starting up...&quot;)

  def receive = {
    case msg @ ChatMessage(from, message) =&amp;gt; 
      log.debug(&quot;New chat message [%s]&quot;, message)
      atomic { 
        chatLog + message.getBytes(&quot;UTF-8&quot;)
      }

    case GetChatLog(_) =&amp;gt; 
      val messageList = atomic {
       chatLog.map(bytes =&amp;gt; new String(bytes, &quot;UTF-8&quot;)).toList
      }
      reply(ChatLog(messageList))
  }

  override def postRestart(reason: Throwable) = 
    chatLog = RedisStorage.getVector(&quot;akka.chat.log&quot;)  
}
&lt;/pre&gt;
&lt;p&gt;The last thing we need to do in terms of persistence is to create a &amp;#8216;RedisChatStorageFactory&amp;#8217; that will take care of instantiating and resolving the &amp;#8216;val storage: ChatStorage&amp;#8217; field in the &amp;#8216;ChatServer&amp;#8217; with a concrete implementation of our persistence Actor.&lt;/p&gt;
&lt;pre name=&quot;code&quot; class=&quot;scala&quot;&gt;
/**
 * Creates and a RedisChatStorage.
 */
trait RedisChatStorageFactory {
  val storage: ChatStorage = new RedisChatStorage
}
&lt;/pre&gt;
&lt;h1&gt;Composing the full Chat Service&lt;/h1&gt;
&lt;p&gt;We have now created the full functionality for the chat server, all nicely decoupled into isolated and well-defined traits. Now let&amp;#8217;s bring all these traits together and compose the complete concrete &amp;#8216;ChatService&amp;#8217;.&lt;/p&gt;
&lt;pre name=&quot;code&quot; class=&quot;scala&quot;&gt;
/**
 * Object encapsulating the full Chat Service.
 */
object ChatService extends 
  ChatServer with 
  SessionManagement with 
  ChatManagement with 
  RedisChatStorageFactory
&lt;/pre&gt;
&lt;h1&gt;Making the ChatService remote&lt;/h1&gt;
&lt;p&gt;Now that we have the &amp;#8216;ChatService&amp;#8217; object how do we make it into a remote service that we can use from different nodes?&lt;/p&gt;
&lt;p&gt;It is very simple. We only need to do two things. First we need to start up a remote server to run the &amp;#8216;ChatService&amp;#8217;. Then for each client that wants to use the &amp;#8216;ChatService&amp;#8217; we just need to invoke &amp;#8216;ChatService.makeRemote&amp;#8217; to get a handle to the remote &amp;#8216;ChatService&amp;#8217;.&lt;/p&gt;
&lt;p&gt;Starting the first step. We have two options on how we can start up a remote server. Either start up the &amp;#8216;RemoteNode&amp;#8217; in some part of the code that runs on the machine you want to run the server on (can just be a simple class with a &amp;#8216;main&amp;#8217; method).&lt;/p&gt;
&lt;p&gt;We start the &amp;#8216;RemoteNode&amp;#8217; by invoking &amp;#8216;start&amp;#8217; and passing in the host name and port.&lt;/p&gt;
&lt;pre name=&quot;code&quot; class=&quot;scala&quot;&gt;
RemoteNode.start(&quot;darkstar&quot;, 9999)
&lt;/pre&gt;
&lt;p&gt;You can also choose to use the version of &amp;#8216;start&amp;#8217; that takes a &amp;#8216;ClassLoader&amp;#8217; as argument if you want to be explicit on through which class loader you want to load the class of the Actor that you want to run as remote service.&lt;/p&gt;
&lt;p&gt;The second option is to put your application in a &lt;span class=&quot;caps&quot;&gt;JAR&lt;/span&gt; file and drop it into the &amp;#8216;AKKA_HOME/deploy&amp;#8217; directory and then start up the Akka microkernel. This will deploy your application and start the &amp;#8216;RemoteNode&amp;#8217; for you. Then you use the &amp;#8216;AKKA_HOME/config/akka.conf&amp;#8217; configuration file to configure the remote server (among many other things). The microkernel is started up like this:&lt;/p&gt;
&lt;pre name=&quot;code&quot; class=&quot;terminal&quot;&gt;
export AKKA_HOME=...
cd $AKKA_HOME
java -jar $AKKA_HOME/dist/akka-0.6.jar
&lt;/pre&gt;
&lt;p&gt;That was the server part. The client part is just as simple. We only need to tell the runtime system that we want to use the &amp;#8216;ChatService&amp;#8217; as a remote Actor by invoking the &amp;#8216;makeRemote(hostname, port)&amp;#8217; function on it. This will instantiate the Actor on the remote host and turn the local Actor instance into a proxy or handle through which we can use the remote Actor transparently with the exact same semantics as if it was a regular local Actor.&lt;/p&gt;
&lt;p&gt;That&amp;#8217;s it. Now let&amp;#8217;s run a sample client session.&lt;/p&gt;
&lt;pre name=&quot;code&quot; class=&quot;scala&quot;&gt;
ChatService.makeRemote(&quot;darkstar&quot;, 9999)
ChatService.start
&lt;/pre&gt;
&lt;p&gt;That&amp;#8217;s it. Were done. Now we have a, very simple, but scalable, fault-tolerant, event-driven, persistent chat server that can without problem serve a million concurrent users on a regular workstation.&lt;/p&gt;
&lt;p&gt;Let&amp;#8217;s use it.&lt;/p&gt;
&lt;h1&gt;Sample client chat session&lt;/h1&gt;
&lt;p&gt;Now let&amp;#8217;s create a simple test runner that logs in posts some messages and logs out.&lt;/p&gt;
&lt;pre name=&quot;code&quot; class=&quot;scala&quot;&gt;
import se.scalablesolutions.akka.sample.chat._

/**
 * Test runner emulating a chat session.
 */
object Runner {
  // create a handle to the remote ChatService 
  ChatService.makeRemote(&quot;localhost&quot;, 9999)
  ChatService.start

  def run = {
    val client = new ChatClient(&quot;jonas&quot;)

    client.login

    client.post(&quot;Hi there&quot;)
    println(&quot;CHAT LOG:\n\t&quot; + client.chatLog.log.mkString(&quot;\n\t&quot;))

    client.post(&quot;Hi again&quot;)
    println(&quot;CHAT LOG:\n\t&quot; + client.chatLog.log.mkString(&quot;\n\t&quot;))

    client.logout
  }
}
&lt;/pre&gt;
&lt;h1&gt;Sample code&lt;/h1&gt;
&lt;p&gt;All this code is available as part of the Akka distribution. It resides in the &amp;#8216;./akka-samples/akka-sample-chat&amp;#8217; module and have a &amp;#8216;&lt;span class=&quot;caps&quot;&gt;README&lt;/span&gt;&amp;#8217; file explaining how to run it as well as a Maven &amp;#8216;pom.xml&amp;#8217; build file so it is easy to build, run, hack, rebuild, run etc. You can also just read the next section for instructions on how to run it.&lt;/p&gt;
&lt;p&gt;Or if you rather browse it &lt;a href=&quot;http://github.com/jboner/akka/tree/master/akka-samples-chat/&quot;&gt;online&lt;/a&gt;.&lt;/p&gt;
&lt;h1&gt;Run it&lt;/h1&gt;
&lt;p&gt;First we need to start up Redis.&lt;/p&gt;
&lt;ol&gt;
	&lt;li&gt;Download Redis from &lt;a href=&quot;http://code.google.com/p/redis/downloads/list&quot;&gt;here&lt;/a&gt;.&lt;/li&gt;
	&lt;li&gt;Step into the distribution.&lt;/li&gt;
	&lt;li&gt;Build: &amp;#8216;make install&amp;#8217;.&lt;/li&gt;
	&lt;li&gt;Run: &amp;#8216;./redis-server&amp;#8217;.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;For details on how to set up Redis server have a look &lt;a href=&quot;http://code.google.com/p/redis/wiki/QuickStart&quot;&gt;here&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;Download and build Akka&lt;/p&gt;
&lt;ol&gt;
	&lt;li&gt;Download Akka from &lt;a href=&quot;http://github.com/jboner/akka/downloads&quot;&gt;http://github.com/jboner/akka/downloads&lt;/a&gt;.&lt;/li&gt;
	&lt;li&gt;Set &amp;#8216;AKKA_HOME&amp;#8217; environment variable to the root of the Akka distribution.&lt;/li&gt;
	&lt;li&gt;Open up a shell and step into the Akka distribution root folder.&lt;/li&gt;
	&lt;li&gt;Build Akka by invoking &amp;#8216;mvn install -Dmaven.test.skip=true&amp;#8217;. This will also bulid the sample application and deploy it to the &amp;#8216;$AKKA_HOME/deploy&amp;#8217; directory.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;Run the microkernel&lt;/p&gt;
&lt;pre name=&quot;code&quot; class=&quot;terminal&quot;&gt;
export AKKA_HOME=...
cd $AKKA_HOME
java -jar ./dist/akka-0.6.jar
&lt;/pre&gt;
&lt;p&gt;Run a sample chat session&lt;/p&gt;
&lt;ol&gt;
	&lt;li&gt;Now start up a new shell and go down into the &amp;#8216;./akka-samples/akka-sample-chat&amp;#8217; directory.&lt;/li&gt;
	&lt;li&gt;Invoke &amp;#8216;mvn scala:console -o&amp;#8217;. This will give you a Scala &lt;span class=&quot;caps&quot;&gt;REPL&lt;/span&gt; (interpreter) with the chat application and all its dependency JARs on the classpath.&lt;/li&gt;
	&lt;li&gt;Simply paste in the whole code block with the &amp;#8216;Runner&amp;#8217; object above and invoke &amp;#8216;Runner.run&amp;#8217;. This run a simulated client session that will connect to the running server in the microkernel.&lt;/li&gt;
	&lt;li&gt;Invoke &amp;#8216;Runner.run&amp;#8217; again and again&amp;#8230;&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;Now you could test client reconnect by killing the running microkernel and start it up again. See the client reconnect take place in the &lt;span class=&quot;caps&quot;&gt;REPL&lt;/span&gt; shell.&lt;/p&gt;
&lt;p&gt;That&amp;#8217;s it. Have fun.&lt;/p&gt;
&lt;h1&gt;Onward&lt;/h1&gt;
&lt;p&gt;There is much much more to Akka than what we have covered in this article. For example &lt;a href=&quot;http://doc.akkasource.org/active-objects&quot;&gt;Active Objects&lt;/a&gt;, &lt;a href=&quot;http://doc.akkasource.org/cluster-membership&quot;&gt;Cluster Membership &lt;span class=&quot;caps&quot;&gt;API&lt;/span&gt;&lt;/a&gt;, a &lt;a href=&quot;http://doc.akkasource.org/comet&quot;&gt;Comet module&lt;/a&gt;, &lt;a href=&quot;http://doc.akkasource.org/rest&quot;&gt;&lt;span class=&quot;caps&quot;&gt;REST&lt;/span&gt; (&lt;span class=&quot;caps&quot;&gt;JAX&lt;/span&gt;-RS) integration&lt;/a&gt;, a &lt;a href=&quot;http://doc.akkasource.org/security&quot;&gt;Security module&lt;/a&gt;, &lt;a href=&quot;http://doc.akkasource.org/amqp&quot;&gt;&lt;span class=&quot;caps&quot;&gt;AMQP&lt;/span&gt; integration&lt;/a&gt;, &lt;a href=&quot;http://doc.akkasource.org/spring-integration&quot;&gt;Spring integration&lt;/a&gt;, &lt;a href=&quot;http://doc.akkasource.org/guice-integration&quot;&gt;Google Guice integration&lt;/a&gt;, &lt;a href=&quot;http://github.com/jboner/akka/tree/master/akka-samples-lift/&quot;&gt;Lift integration&lt;/a&gt;, a rich &lt;a href=&quot;http://doc.akkasource.org/stm&quot;&gt;Transaction &lt;span class=&quot;caps&quot;&gt;API&lt;/span&gt;&lt;/a&gt;, tons of &lt;a href=&quot;http://doc.akkasource.org/configuration&quot;&gt;configuration possibilities&lt;/a&gt; etc.&lt;/p&gt;</content>
 </entry>
 
 <entry>
   <title>Asynchronous Event Sourcing using Actors</title>
   <link href="http://jboner.github.com/2009/02/12/event-sourcing-using-actors"/>
   <updated>2009-02-12T00:00:00+00:00</updated>
   <id>hhttp://jboner.github.com/2009/02/12/event-sourcing-using-actors</id>
   <content type="html">&lt;h1&gt;Asynchronous Event Sourcing using Actors&lt;/h1&gt;
&lt;p class=&quot;meta&quot;&gt;12 Feb 2009&lt;/p&gt;
&lt;h1&gt;Introduction&lt;/h1&gt;
&lt;p&gt;There has been some discussions lately about Event Sourcing. For example, Greg Young &lt;a href=&quot;http://www.infoq.com/interviews/greg-young-ddd&quot;&gt;recently discussed&lt;/a&gt; how they were using Event Sourcing and explicit state transitions together with Domain-Driven Design (&lt;span class=&quot;caps&quot;&gt;DDD&lt;/span&gt;) to build a highly scalable and loosely coupled system.&lt;/p&gt;
&lt;p&gt;So what is Event Sourcing? Martin Fowler wrote an &lt;a href=&quot;http://martinfowler.com/eaaDev/EventSourcing.html&quot;&gt;excellent article&lt;/a&gt; about some years ago and there is no use repeating it here, so please read (or at least skim) that article before reading further.&lt;/p&gt;
&lt;p&gt;What I will do in this article is to show you how you can implement Event Sourcing using asynchronous message-passing based on actors. Actors are generally an excellent paradigm to implement asynchronous event-based systems and they allow you to easily get explicit state transitions working nicely together with an immutable domain model. This gives a concurrent system that scales very well, with the side-effect/feature of &lt;a href=&quot;http://www.allthingsdistributed.com/2008/12/eventually_consistent.html&quot;&gt;Eventual Consistency&lt;/a&gt;.&lt;/p&gt;
&lt;h1&gt;Domain model&lt;/h1&gt;
&lt;p&gt;I will reuse the example Martin Fowler used in his article but rewrite it using Scala Actors. So without further ado let&amp;#8217;s start hacking. Martin&amp;#8217;s example implements a simple Ship management system.&lt;/p&gt;
&lt;p&gt;First, let&amp;#8217;s define the simplistic domain model; Ship, Port and Country.&lt;/p&gt;
&lt;p&gt;The Ship class is worth discussing a bit. It is an actor, which means that it is an isolated &amp;#8216;lightweight process&amp;#8217; with its own state, which is only accessible and modifiable using messages (in our case, events). The Ship actor responds to four different events; set arrival and departure, query for current port and finally reset the state.&lt;/p&gt;
&lt;pre name=&quot;code&quot; class=&quot;scala&quot;&gt;
class Ship(val name: String, val home: Port) extends Actor {

  def act = loop(home)

  private def loop(current: Port) {
    react {
      case ArrivalEvent(time, port, _) =&amp;gt; 
        println(toString + &quot; ARRIVED  at port   &quot; + port + &quot; @ &quot; + time)
        loop(port)

      case DepartureEvent(time, port, _) =&amp;gt; 
        println(toString + &quot; DEPARTED from port &quot; + port  + &quot; @ &quot; + time)
        loop(Port.AT_SEA)

      case Reset =&amp;gt; 
        println(toString + &quot; has been reset&quot;)
        loop(home)

      case CurrentPort =&amp;gt; 
        reply(current)
        loop(current)

      case unknown =&amp;gt; 
        error(&quot;Unknown event: &quot; + unknown)
    }
  }

  override def toString = &quot;Ship(&quot; + name + &quot;)&quot;
}

class Port(val city: String, val country: Country) {
  override def toString = &quot;Port(&quot; + city + &quot;)&quot;  
}
object Port {
  val AT_SEA = new Port(&quot;AT SEA&quot;, Country.AT_SEA)
}

case class Country(val name: String)
object Country {
  val US = new Country(&quot;US&quot;)
  val CANADA = new Country(&quot;CANADA&quot;)
  val AT_SEA = new Country(&quot;AT_SEA&quot;)
}
&lt;/pre&gt;
&lt;p&gt;Note: In this example I have been managing the state in the actors (Ship and EventProcessor) by passing it on in the recursive &amp;#8216;loop&amp;#8217;, using stack-confinement. This is a slick technique but not possible if you need to persist the state in some way, either using something like Terracotta or store it in a database. Then you would have to put the state in private field(s) in the actor, something that will &lt;b&gt;not&lt;/b&gt; affect the correctness or performance.&lt;/p&gt;
&lt;h1&gt;Events&lt;/h1&gt;
&lt;p&gt;Now let&amp;#8217;s define our events, implementing the explicit state transitions DepartureEvent and ArrivalEvent. In Scala these are best defined as &amp;#8216;case classes&amp;#8217; which supports pattern matching and attribute destructing. These two events encapsulate their state transition in the &amp;#8216;process&amp;#8217; method. We also define one event for asking the Ship for its current port and one for resetting its state to its &amp;#8220;home&amp;#8221; port.&lt;/p&gt;
&lt;pre name=&quot;code&quot; class=&quot;scala&quot;&gt;
sealed abstract case class Event

abstract case class StateTransitionEvent(val occurred: Date) 
  extends Event {
  val recorded = new Date
  def process: Unit
}

case class DepartureEvent(val time: Date, val port: Port, val ship: Ship) 
  extends StateTransitionEvent(time) {
  override def process = ship ! this
}

case class ArrivalEvent(val time: Date, val port: Port, val ship: Ship) 
  extends StateTransitionEvent(time) {
  override def process = ship ! this
}

case object Reset extends Event

case object CurrentPort extends Event
&lt;/pre&gt;
&lt;h1&gt;Event processor&lt;/h1&gt;
&lt;p&gt;Finally, let&amp;#8217;s define the event processor. This class is an actor which responds to any event that is a subtype of StateTransitionEvent, e.g. either DepartureEvent or ArrivalEvent. It also holds a history list (&amp;#8216;log&amp;#8217;) with all events that it has processed. Something that we will make use of later on.&lt;/p&gt;
&lt;pre name=&quot;code&quot; class=&quot;scala&quot;&gt;
class EventProcessor extends Actor {
  def act = loop(Nil)

  private def loop(log: List[StateTransitionEvent]) {
    react {
      case event: StateTransitionEvent =&amp;gt; 
        event.process
        loop(event :: log)

      case unknown =&amp;gt; 
        error(&quot;Unknown event: &quot; + unknown)
    }
  }
}
&lt;/pre&gt;
&lt;h2&gt;Test run 1&lt;/h2&gt;
&lt;p&gt;Now we have the basis for our Ship Management Event Sourcing framework. Let&amp;#8217;s create some tests to drive the thing. Since each event submission is processed asynchronously we have to interleave them with calls to &amp;#8216;Thread.sleep(500)&amp;#8217; in order to see what is going on.&lt;/p&gt;
&lt;pre name=&quot;code&quot; class=&quot;scala&quot;&gt;
class ActorBasedEventSourcingTest {

  private var shipKR: Ship = _
  private var portSFO, portLA, portYYV: Port = _
  private var processor: EventProcessor = _

  def setup = {
    processor = new EventProcessor
    processor.start
    
    portSFO = new Port(&quot;San Francisco&quot;, Country.US)
    portLA = new Port(&quot;Los Angeles&quot;, Country.US)
    portYYV = new Port(&quot;Vancouver&quot;, Country.CANADA)

    shipKR = new Ship(&quot;King Roy&quot;, portYYV)
    shipKR.start

    this
  }

  def tearDown = { 
    processor.exit
    this
  }

  def arrivalSetsShipsLocation = {
    println(&quot;\n===&amp;gt; arrivalSetsShipsLocation&quot;)

    processor ! DepartureEvent(new Date(2009, 2, 1), portSFO, shipKR)
    Thread.sleep(500)

    processor ! ArrivalEvent(new Date(2009, 2, 3), portSFO, shipKR)
    Thread.sleep(500)

    assert(portSFO == (shipKR !? CurrentPort))
    this
  }

  def departurePutsShipOutToSea = {
    println(&quot;\n===&amp;gt; departurePutsShipOutToSea&quot;)

    processor ! DepartureEvent(new Date(2009, 2, 4), portLA, shipKR)
    Thread.sleep(500)

    assert(Port.AT_SEA == (shipKR !? CurrentPort))
    this
  }

  def smallTrip = {
    println(&quot;\n===&amp;gt; smallTrip&quot;)

    processor ! ArrivalEvent(new Date(2009, 2, 5), portLA, shipKR)
    Thread.sleep(500)

    processor ! DepartureEvent(new Date(2009, 2, 6), portYYV, shipKR)
    Thread.sleep(500)

    processor ! ArrivalEvent(new Date(2009, 2, 8), portYYV, shipKR)
    Thread.sleep(500)

    processor ! DepartureEvent(new Date(2009, 2, 9), portSFO, shipKR)
    Thread.sleep(500)

    processor ! ArrivalEvent(new Date(2009, 2, 11), portSFO, shipKR)
    Thread.sleep(500)

    assert(portSFO == (shipKR !? CurrentPort))
    this
  }
}

(new ActorBasedEventSourcingTest)
  .setup
  .arrivalSetsShipsLocation
  .departurePutsShipOutToSea
  .smallTrip
  .tearDown
&lt;/pre&gt;
&lt;p&gt;Which gives us the following output:&lt;/p&gt;
&lt;pre name=&quot;code&quot; class=&quot;scala&quot;&gt;
===&amp;gt; arrivalSetsShipsLocation
Ship(King Roy) DEPARTED from port Port(San Francisco) @ Mon Mar 01 00:00:00 CET 3909
Ship(King Roy) ARRIVED  at port   Port(San Francisco) @ Wed Mar 03 00:00:00 CET 3909

===&amp;gt; departurePutsShipOutToSea
Ship(King Roy) DEPARTED from port Port(Los Angeles) @ Thu Mar 04 00:00:00 CET 3909

===&amp;gt; smallTrip
Ship(King Roy) ARRIVED  at port   Port(Los Angeles) @ Fri Mar 05 00:00:00 CET 3909
Ship(King Roy) DEPARTED from port Port(Vancouver) @ Sat Mar 06 00:00:00 CET 3909
Ship(King Roy) ARRIVED  at port   Port(Vancouver) @ Mon Mar 08 00:00:00 CET 3909
Ship(King Roy) DEPARTED from port Port(San Francisco) @ Tue Mar 09 00:00:00 CET 3909
Ship(King Roy) ARRIVED  at port   Port(San Francisco) @ Thu Mar 11 00:00:00 CET 3909
&lt;/pre&gt;
&lt;p&gt;Pretty nice.&lt;/p&gt;
&lt;p&gt;But now, let&amp;#8217;s start to take advantage of the event persistence. Let&amp;#8217;s implement event replay.&lt;/p&gt;
&lt;h1&gt;Replay&lt;/h1&gt;
&lt;p&gt;Implementing replay is actually very simple now when we have an event log. First we define a Replay event.&lt;/p&gt;
&lt;pre name=&quot;code&quot; class=&quot;scala&quot;&gt;
case object Replay extends Event
&lt;/pre&gt;
&lt;p&gt;Then we need the EventProcessor to respond to this new event by first reversing the order of the event log (since functional lists are concatenated in reverse order) and then for each event invoke &amp;#8216;process&amp;#8217;.&lt;/p&gt;
&lt;pre name=&quot;code&quot; class=&quot;scala&quot;&gt;
class EventProcessor extends Actor {
  def act = loop(Nil)

  private def loop(log: List[DomainEvent]) {
    react {
       ...

      case Replay =&amp;gt; 
        log.reverse.foreach(_.process) 
        loop(log)
    }
  }
}
&lt;/pre&gt;
&lt;p&gt;Done deal.&lt;/p&gt;
&lt;h2&gt;Test run 2&lt;/h2&gt;
&lt;p&gt;Let&amp;#8217;s try it out by adding a new test method to our suite. Here we make use of the Reset event which resets the ship to its initial state before replaying all state transitions.&lt;/p&gt;
&lt;pre name=&quot;code&quot; class=&quot;scala&quot;&gt;
def resetAndReplayEventLog = {
  println(&quot;\n===&amp;gt; resetAndReplayEventLog&quot;)

  shipKR ! Reset

  processor ! Replay
  Thread.sleep(500)

  assert(portSFO == (shipKR !? CurrentPort))
  this
}

(new ActorBasedEventSourcingTest)
  .setup
  .arrivalSetsShipsLocation
  .departurePutsShipOutToSea
  .smallTrip
  .resetAndReplayEventLog // new test method
  .tearDown
&lt;/pre&gt;
&lt;p&gt;This yields the following output:&lt;/p&gt;
&lt;pre name=&quot;code&quot; class=&quot;scala&quot;&gt;
===&amp;gt; arrivalSetsShipsLocation
Ship(King Roy) DEPARTED from port Port(San Francisco) @ Mon Mar 01 00:00:00 CET 3909
Ship(King Roy) ARRIVED  at port   Port(San Francisco) @ Wed Mar 03 00:00:00 CET 3909

===&amp;gt; departurePutsShipOutToSea
Ship(King Roy) DEPARTED from port Port(Los Angeles) @ Thu Mar 04 00:00:00 CET 3909

===&amp;gt; smallTrip
Ship(King Roy) ARRIVED  at port   Port(Los Angeles) @ Fri Mar 05 00:00:00 CET 3909
Ship(King Roy) DEPARTED from port Port(Vancouver) @ Sat Mar 06 00:00:00 CET 3909
Ship(King Roy) ARRIVED  at port   Port(Vancouver) @ Mon Mar 08 00:00:00 CET 3909
Ship(King Roy) DEPARTED from port Port(San Francisco) @ Tue Mar 09 00:00:00 CET 3909
Ship(King Roy) ARRIVED  at port   Port(San Francisco) @ Thu Mar 11 00:00:00 CET 3909

===&amp;gt; resetAndReplayEventLog
Ship(King Roy) has been reset
Ship(King Roy) DEPARTED from port Port(San Francisco) @ Mon Mar 01 00:00:00 CET 3909
Ship(King Roy) ARRIVED  at port   Port(San Francisco) @ Wed Mar 03 00:00:00 CET 3909
Ship(King Roy) DEPARTED from port Port(Los Angeles) @ Thu Mar 04 00:00:00 CET 3909
Ship(King Roy) ARRIVED  at port   Port(Los Angeles) @ Fri Mar 05 00:00:00 CET 3909
Ship(King Roy) DEPARTED from port Port(Vancouver) @ Sat Mar 06 00:00:00 CET 3909
Ship(King Roy) ARRIVED  at port   Port(Vancouver) @ Mon Mar 08 00:00:00 CET 3909
Ship(King Roy) DEPARTED from port Port(San Francisco) @ Tue Mar 09 00:00:00 CET 3909
Ship(King Roy) ARRIVED  at port   Port(San Francisco) @ Thu Mar 11 00:00:00 CET 3909
&lt;/pre&gt;
&lt;h1&gt;Replay up to a specific point in time&lt;/h1&gt;
&lt;p&gt;Finally, (my last example, I promise) let&amp;#8217;s add the possibility of replaying the event log up to a specific date to get a snapshot of the system&amp;#8217;s state at a particular point in time.&lt;/p&gt;
&lt;p&gt;You know the drill by now, first define a new event; ReplayUpTo, holding the date.&lt;/p&gt;
&lt;pre name=&quot;code&quot; class=&quot;scala&quot;&gt;
case class ReplayUpTo(date: Date) extends Event
&lt;/pre&gt;
&lt;p&gt;Here the event processor first reverses the log, then it applies a filter to the list which filters out all events that has been created after the date specified and finally run &amp;#8216;process&amp;#8217; on all events in the resulting filtered list.&lt;/p&gt;
&lt;pre name=&quot;code&quot; class=&quot;scala&quot;&gt;
class EventProcessor extends Actor {
  def act = loop(Nil)

  private def loop(log: List[DomainEvent]) {
    react {
       ...

      case ReplayUpTo(date) =&amp;gt; 
        log.reverse.filter(_.occurred.getTime &amp;lt;= date.getTime).foreach(_.process) 
        loop(log)
    }
  }
}
&lt;/pre&gt;
&lt;h2&gt;Test run 3&lt;/h2&gt;
&lt;p&gt;So we add a last test method to our suite, one that replays all events created in earlier tests up to the date &amp;#8216;2009/2/4&amp;#8217;.&lt;/p&gt;
&lt;pre name=&quot;code&quot; class=&quot;scala&quot;&gt;
def resetAndReplayEventLogUpToDate = {
  println(&quot;\n===&amp;gt; resetAndReplayEventLogUpToDate&quot;)

  shipKR ! Reset

  processor ! ReplayUpTo(new Date(2009, 2, 4))
  Thread.sleep(500)

  assert(Port.AT_SEA == (shipKR !? CurrentPort))
  this
} 

(new EventSourcingWithActorsTest)
  .setup
  .arrivalSetsShipsLocation
  .departurePutsShipOutToSea
  .smallTrip
  .resetAndReplayEventLog
  .resetAndReplayEventLogUpToDate // new test method
  .tearDown
&lt;/pre&gt;
&lt;p&gt;This yield the following output.&lt;/p&gt;
&lt;pre name=&quot;code&quot; class=&quot;scala&quot;&gt;
===&amp;gt; arrivalSetsShipsLocation
Ship(King Roy) DEPARTED from port Port(San Francisco) @ Mon Mar 01 00:00:00 CET 3909
Ship(King Roy) ARRIVED  at port   Port(San Francisco) @ Wed Mar 03 00:00:00 CET 3909

===&amp;gt; departurePutsShipOutToSea
Ship(King Roy) DEPARTED from port Port(Los Angeles) @ Thu Mar 04 00:00:00 CET 3909

===&amp;gt; smallTrip
Ship(King Roy) ARRIVED  at port   Port(Los Angeles) @ Fri Mar 05 00:00:00 CET 3909
Ship(King Roy) DEPARTED from port Port(Vancouver) @ Sat Mar 06 00:00:00 CET 3909
Ship(King Roy) ARRIVED  at port   Port(Vancouver) @ Mon Mar 08 00:00:00 CET 3909
Ship(King Roy) DEPARTED from port Port(San Francisco) @ Tue Mar 09 00:00:00 CET 3909
Ship(King Roy) ARRIVED  at port   Port(San Francisco) @ Thu Mar 11 00:00:00 CET 3909

===&amp;gt; resetAndReplayEventLog
Ship(King Roy) has been reset
Ship(King Roy) DEPARTED from port Port(San Francisco) @ Mon Mar 01 00:00:00 CET 3909
Ship(King Roy) ARRIVED  at port   Port(San Francisco) @ Wed Mar 03 00:00:00 CET 3909
Ship(King Roy) DEPARTED from port Port(Los Angeles) @ Thu Mar 04 00:00:00 CET 3909
Ship(King Roy) ARRIVED  at port   Port(Los Angeles) @ Fri Mar 05 00:00:00 CET 3909
Ship(King Roy) DEPARTED from port Port(Vancouver) @ Sat Mar 06 00:00:00 CET 3909
Ship(King Roy) ARRIVED  at port   Port(Vancouver) @ Mon Mar 08 00:00:00 CET 3909
Ship(King Roy) DEPARTED from port Port(San Francisco) @ Tue Mar 09 00:00:00 CET 3909
Ship(King Roy) ARRIVED  at port   Port(San Francisco) @ Thu Mar 11 00:00:00 CET 3909

===&amp;gt; resetAndReplayEventLogUpToDate
Ship(King Roy) has been reset
Ship(King Roy) DEPARTED from port Port(San Francisco) @ Mon Mar 01 00:00:00 CET 3909
Ship(King Roy) ARRIVED  at port   Port(San Francisco) @ Wed Mar 03 00:00:00 CET 3909
Ship(King Roy) DEPARTED from port Port(Los Angeles) @ Thu Mar 04 00:00:00 CET 3909
&lt;/pre&gt;
&lt;p&gt;That&amp;#8217;s all there&amp;#8217;s to it. We have only scratched the surface on what can be done with asynchronous Event Sourcing, and as in all these kind of articles, the example is almost too simplistic to fully understand the power and flexibility of the solution. But I hope that you have understood the underlying principle enough to be able to apply it to a real-world enterprise system.&lt;/p&gt;</content>
 </entry>
 
 <entry>
   <title>Slides for my Pragmatic Real-World Scala presentation</title>
   <link href="http://jboner.github.com/2009/01/30/slides-pragmatic-real-world-scala"/>
   <updated>2009-01-30T00:00:00+00:00</updated>
   <id>hhttp://jboner.github.com/2009/01/30/slides-pragmatic-real-world-scala</id>
   <content type="html">&lt;h1&gt;Slides for my Pragmatic Real-World Scala presentation&lt;/h1&gt;
&lt;p class=&quot;meta&quot;&gt;30 Jan 2009&lt;/p&gt;
&lt;p&gt;I have uploaded the slides for my &amp;#8216;Pragmatic Real-World Scala&amp;#8217; talk that I gave at &lt;a href=&quot;http://jfokus.se&quot;&gt;Jfokus&lt;/a&gt; yesterday to &lt;a href=&quot;http://www.slideshare.net/jboner/pragmatic-real-world-scala-45-min-presentation&quot;&gt;slideshare&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;Enjoy.&lt;/p&gt;
&lt;div style=&quot;width:425px;text-align:left&quot; id=&quot;__ss_966972&quot;&gt;
&lt;a style=&quot;font:14px Helvetica,Arial,Sans-serif;display:block;margin:12px 0 3px 0;text-decoration:underline;&quot; href=&quot;http://www.slideshare.net/jboner/pragmatic-real-world-scala-45-min-presentation?type=powerpoint&quot; title=&quot;Pragmatic Real-World Scala (short version)&quot;&gt;Pragmatic Real-World Scala (short version)&lt;/a&gt;&lt;object style=&quot;margin:0px&quot; width=&quot;425&quot; height=&quot;355&quot;&gt;&lt;param name=&quot;movie&quot; value=&quot;http://static.slideshare.net/swf/ssplayer2.swf?doc=pragmaticrealworldscalajfokus2009-1233251076441384-2&amp;rel=0&amp;stripped_title=pragmatic-real-world-scala-45-min-presentation&quot; /&gt;&lt;param name=&quot;allowFullScreen&quot; value=&quot;true&quot;/&gt;&lt;param name=&quot;allowScriptAccess&quot; value=&quot;always&quot;/&gt;&lt;embed src=&quot;http://static.slideshare.net/swf/ssplayer2.swf?doc=pragmaticrealworldscalajfokus2009-1233251076441384-2&amp;rel=0&amp;stripped_title=pragmatic-real-world-scala-45-min-presentation&quot; type=&quot;application/x-shockwave-flash&quot; allowscriptaccess=&quot;always&quot; allowfullscreen=&quot;true&quot; width=&quot;425&quot; height=&quot;355&quot;&gt;&lt;/embed&gt;&lt;/object&gt;
&lt;/div&gt;</content>
 </entry>
 
 <entry>
   <title>Added Comments to Blog</title>
   <link href="http://jboner.github.com/2009/01/30/added-comments-to-blog"/>
   <updated>2009-01-30T00:00:00+00:00</updated>
   <id>hhttp://jboner.github.com/2009/01/30/added-comments-to-blog</id>
   <content type="html">&lt;h1&gt;Added Comments to Blog&lt;/h1&gt;
&lt;p class=&quot;meta&quot;&gt;30 Jan 2009&lt;/p&gt;
&lt;p&gt;Many people have asked for it, so I have decided to add comments to my blog again.&lt;/p&gt;
&lt;p&gt;Thank you &lt;a href=&quot;http://disqus.com&quot;&gt;Disqus&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;Let the discussion &lt;a href=&quot;http://jonasboner.com&quot;&gt;continue&lt;/a&gt;.&lt;/p&gt;</content>
 </entry>
 
 <entry>
   <title>Back To Consulting</title>
   <link href="http://jboner.github.com/2009/01/10/back-to-consulting"/>
   <updated>2009-01-10T00:00:00+00:00</updated>
   <id>hhttp://jboner.github.com/2009/01/10/back-to-consulting</id>
   <content type="html">&lt;h1&gt;Back To Consulting&lt;/h1&gt;
&lt;p class=&quot;meta&quot;&gt;10 Jan 2009&lt;/p&gt;
&lt;p&gt;The last year has been a ride.&lt;/p&gt;
&lt;p&gt;&lt;a href=&quot;http://triental.com&quot;&gt;Startup&lt;/a&gt; in finance business, green field development, challenging and interesting problem domain, been pushing &lt;a href=&quot;http://scala-lang.org&quot;&gt;Scala&lt;/a&gt; to its limits in a real-world production setting. Its been a lot of fun and a great learning experience. (I have written some about the way we have used Scala in the &amp;#8220;Real-World Scala&amp;#8221; series of blog posts, check the &lt;a href=&quot;/archives&quot;&gt;archives&lt;/a&gt; if you are curious.)&lt;/p&gt;
&lt;p&gt;But now its all over. Zero cash flow in. Knocked out by the financial crisis. To bad, we had a great product coming. But not much to do.&lt;/p&gt;
&lt;p&gt;The good news is that I am back in the consulting business. Rewrote and redesigned my company site to mark the beginnning of a new era&amp;#8230;well, at least a &amp;#8220;new&amp;#8221; job.&lt;/p&gt;
&lt;p&gt;Check it out and let me know if you need help :-)&lt;/p&gt;
&lt;p&gt;&lt;a href=&quot;http://scalablesolutions.se/&quot;&gt;http://scalablesolutions.se&lt;/a&gt;&lt;/p&gt;</content>
 </entry>
 
 <entry>
   <title>Blogging Like a Hacker using Git and Jekyll</title>
   <link href="http://jboner.github.com/2009/01/07/blogging-like-a-hacker-using-git-and-jekyll"/>
   <updated>2009-01-07T00:00:00+00:00</updated>
   <id>hhttp://jboner.github.com/2009/01/07/blogging-like-a-hacker-using-git-and-jekyll</id>
   <content type="html">&lt;h1&gt;Blogging Like a Hacker using Git and Jekyll&lt;/h1&gt;
&lt;p class=&quot;meta&quot;&gt;7 Jan 2009&lt;/p&gt;
&lt;p&gt;For the last year and have been falling in love with &lt;a href=&quot;http://git-scm.com&quot;&gt;Git&lt;/a&gt;. Git is truly an amazing&amp;#8230;..tool. I was going to say Source Code Management System (&lt;span class=&quot;caps&quot;&gt;SCM&lt;/span&gt;), but that ain&amp;#8217;t fair, Git is so much more. Git deserves a post on its own, however in this one I&amp;#8217;m going to talk about one of the things it can help you with; to blog like a hacker.&lt;/p&gt;
&lt;p&gt;For the last year I have not only used Git but have hosted my whole professional life on &lt;a href=&quot;http://github.com&quot;&gt;GitHub&lt;/a&gt;. GitHub started as a server with Git repositories but have since then grown into a thriving community with a lot of interesting and cool features and side projects up its sleeve. One of these &amp;#8220;features&amp;#8221; is &lt;a href=&quot;http://github.com/mojombo/jekyll/tree/master&quot;&gt;Jekyll&lt;/a&gt;. Jekyll is a minimalistic blog engine based upon Git. It allows you to write all your blog posts in either Textile, Markdown or &lt;span class=&quot;caps&quot;&gt;HTML&lt;/span&gt; while having Git (and GitHub) take care of storage, versioning etc.&lt;/p&gt;
&lt;p&gt;It is dead simple. Generates everything into static &lt;span class=&quot;caps&quot;&gt;HTML&lt;/span&gt;, which I think is great. And as with all viral good open source, there are already a bunch of plug-ins such as for example converting your MT, WordPress, Typo etc blogs into Textile/Markdown for Jekyll to consume. The thing I like most is that everything is plain text, residing in your Git repository (e.g. on your file system). This means that I can write my blog post in Emacs, commit the changes using the Emacs (emacs-git), push it up to GitHub using Emacs, generate my site and &lt;span class=&quot;caps&quot;&gt;FTP&lt;/span&gt; it up to my server from Emacs (replace Emacs with your favorite editor). Never been easier (or more fun) to blog. Hopefully this will make me do it more often.&lt;/p&gt;
&lt;p&gt;For a sample of a Jekyll-powered blog, take a look at mine: &lt;a href=&quot;http://jonasboner.com&quot;&gt;http://jonasboner.com&lt;/a&gt;&lt;/p&gt;</content>
 </entry>
 
 <entry>
   <title>Real-World Scala: Fault-tolerant Concurrent Asynchronous Components</title>
   <link href="http://jboner.github.com/2008/12/11/real-world-scala-fault-tolerant-concurrent-asynchronous-components"/>
   <updated>2008-12-11T00:00:00+00:00</updated>
   <id>hhttp://jboner.github.com/2008/12/11/real-world-scala-fault-tolerant-concurrent-asynchronous-components</id>
   <content type="html">&lt;h1&gt;
&lt;p&gt;Real-World Scala: Fault-tolerant Concurrent Asynchronous Components&lt;/p&gt;
&lt;/h1&gt;
&lt;p&gt;&lt;strong&gt;Introduction&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;In a &lt;a href=&quot;http://jonasboner.com/2008/06/16/erlang-style-supervisor-module-for-scala-actors.html&quot;&gt;previous post&lt;/a&gt; I wrote about &lt;a href=&quot;http://github.com/jboner/scala-otp/tree/master&quot;&gt;Scala &lt;span class=&quot;caps&quot;&gt;OTP&lt;/span&gt;&lt;/a&gt;, an initial attempt to bring the power or &lt;a href=&quot;http://www.erlang.se/doc/index.shtml&quot;&gt;Erlang &lt;span class=&quot;caps&quot;&gt;OTP&lt;/span&gt;&lt;/a&gt;, in particular its supervisor hierarchies and generic server to the &lt;a href=&quot;
http://www.scala-lang.org/node/242&quot;&gt;Scala Actors library&lt;/a&gt;. &lt;span class=&quot;caps&quot;&gt;OTP&lt;/span&gt; is one of the key parts in the Erlang success story and is in my opinion a requirement for Scala Actors to succeed in the real world.&lt;/p&gt;
&lt;p&gt;Actors can simplify concurrent programming and reasoning immensely and I believe that Scala Actors is a key piece in the future Java concurrency puzzle. However, programming with actors and with explicit message passing and message dispatch loops can feel a bit unnatural and unnecessary verbose for Java developers that are used to regular OO method invocations and synchronous control flow.&lt;/p&gt;
&lt;p&gt;For example, if we want to be able to pass a single message from one actor to the next we have to define two things.&lt;/p&gt;
&lt;p&gt;A message with optional payload.&lt;/p&gt;
&lt;pre name=&quot;code&quot; class=&quot;scala&quot;&gt;
case class MyMessage(payload: AnyRef)
&lt;/pre&gt;
&lt;p&gt;A message dispatch matching loop (partial function) in the receiving actor.&lt;/p&gt;

&lt;pre name=&quot;code&quot; class=&quot;scala&quot;&gt;
def act = {
  loop {
    react {
      case MyMessage(payload) =&amp;amp;gt; 
        ...  // do something with payload
      case _ =&amp;gt; 
        ... // default case
    }
  }
}
&lt;/pre&gt;
&lt;p&gt;It is also complicated to do things like stateful control flow, e.g. send message to A, wait for reply, then send message to B, wait for reply, then do C. (this can in some ways be addressed using &lt;a href=&quot;http://lampsvn.epfl.ch/trac/scala/attachment/ticket/781/monadActor.scala&quot;&gt;monad continuations&lt;/a&gt;, but is still not simple and intuitive to use).&lt;/p&gt;
&lt;p&gt;Don&amp;#8217;t get me wrong. Even though it has its &amp;#8220;flaws&amp;#8221;, using actors (message-passing concurrency) for concurrent programming is still &lt;strong&gt;so much&lt;/strong&gt; simpler than using threads and locks (shared-state concurrency).&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Active Objects (Concurrent Asynchronous Components)&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;In this post I&amp;#8217;ll outline a little library that we have been using that tries to unify Scala Actors, Scala &lt;span class=&quot;caps&quot;&gt;OTP&lt;/span&gt; (supervisor hierarchies for fault tolerance) and regular OO method dispatch into an asynchronous component framework that I think can be best described as &lt;a href=&quot;http://en.wikipedia.org/wiki/Active_Object&quot;&gt;&lt;em&gt;Active Objects&lt;/em&gt;&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;Each so-called &lt;em&gt;Active Object&lt;/em&gt; (concurrent asynchronous component) is a &lt;code&gt;GenericServer&lt;/code&gt; that is managed by a &lt;code&gt;Supervisor&lt;/code&gt;, either in isolation or as part of a supervisor hierarchy/tree. E.g. each component is fully fault-tolerant. For more information about how supervisor hierarchies work, read this post.  Each component consists of an interface (trait) and a regular Scala class as implementation for the interface. The catch is that each component has to be instantiate through a factory. Let&amp;#8217;s first take a look at the &lt;span class=&quot;caps&quot;&gt;API&lt;/span&gt; and how to use this thing before we dig into the actual implementation.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Usage&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Here is a simple example of a component that is using default supervisor management (which among other things mean that it is not part of a supervisor tree/hierarchy, but is still managed and will be restarted upon failure). We start with the component interface and implementation.&lt;/p&gt;
&lt;pre name=&quot;code&quot; class=&quot;scala&quot;&gt;
  trait Foo {
    def foo(msg: String): String    
    @oneway def bar(msg: String)
  }

  class FooImpl extends Foo {
    def foo(msg: String): String = { println(&quot;foo: &quot; + msg); msg } 
    def bar(msg: String) = println(&quot;bar: &quot; + msg)
  }
&lt;/pre&gt;

&lt;p&gt;Now let&amp;#8217;s instantiate this component. The integer 1000 specifies the time interval an (asynchronous) invocation should have before timing out.&lt;/p&gt;
&lt;pre name=&quot;code&quot; class=&quot;scala&quot;&gt;
  val foo = ActiveObject.newInstance[Foo](classOf[Foo], new FooImpl, 1000)
&lt;/pre&gt;

&lt;p&gt;Now we can use this component as any regular instance of &lt;code&gt;Foo&lt;/code&gt;. The difference is that invocations are now asynchronously dispatched in an event-based actor with its return value is wrapped in a &lt;code&gt;Future&lt;/code&gt;, this emulates synchronous behavior but is non-blocking. The  exception to this behavior is if a method is annotated with the &lt;code&gt;@scala.actors.annotation.oneway&lt;/code&gt; annotation, then it returns immediately. All components created through the factory are fault-tolerant &lt;code&gt;GenericServers&lt;/code&gt; managed by a &lt;code&gt;Supervisor&lt;/code&gt;, which means that they will be restarted upon failure using the restart scheme the supervisor has defined it under.&lt;/p&gt;
&lt;pre name=&quot;code&quot; class=&quot;scala&quot;&gt;
   // use as usual
   foo.foo(&quot;foo&quot;)
   foo.bar(&quot;bar&quot;) // returns immediately since annotated with @oneway
&lt;/pre&gt;

&lt;p&gt;Now, if I would like to have more control over the &lt;code&gt;Supervisor&lt;/code&gt; configuration and/or want to compose different components into supervisor hierarchies, then we can use another factory method along with a method called &lt;code&gt;start&lt;/code&gt; that allows us to pass in a &lt;code&gt;Supervisor&lt;/code&gt; configuration (as defined by the Scala &lt;span class=&quot;caps&quot;&gt;OTP&lt;/span&gt; Supervisor).&lt;/p&gt;
&lt;p&gt;Let&amp;#8217;s take a look at a full example:&lt;/p&gt;
&lt;pre name=&quot;code&quot; class=&quot;scala&quot;&gt;
  trait Foo {
    def foo(msg: String): String    
    @oneway def bar(msg: String)
  }

  class FooImpl extends Foo {
    val bar: Bar = new BarImpl 
    def foo(msg: String): String = { println(&quot;foo: &quot; + msg); msg } 
    def bar(msg: String) = bar.bar(msg)
  }

  trait Bar {
    def bar(msg: String)
  }

  class BarImpl extends Bar {
    def bar(msg: String) = println(&quot;bar: &quot; + msg)
  }
&lt;/pre&gt;

&lt;p&gt;First create proxies (&lt;code&gt;GenericServer&lt;/code&gt;s) for our services.&lt;/p&gt;
&lt;pre name=&quot;code&quot; class=&quot;scala&quot;&gt;
  val fooProxy = new ActiveObjectProxy(new FooImpl, 1000)
  val barProxy = new ActiveObjectProxy(new BarImpl, 1000)
&lt;/pre&gt;
&lt;p&gt;Then let&amp;#8217;s configure the &lt;code&gt;GenericServer&lt;/code&gt;(s) by creating a list of &lt;code&gt;GenericServer&lt;/code&gt; configurations (defining lifecycles, restart strategies etc.). This configuration is passed into the &lt;code&gt;ActiveObject.supervise&lt;/code&gt; method which starts up the &lt;code&gt;Supervisor&lt;/code&gt; which starts up all &lt;code&gt;GenericServer&lt;/code&gt;&amp;#8217;s according to their configurations before returning the &lt;code&gt;Supervisor&lt;/code&gt; instance (which can be used for management of the components).&lt;/p&gt;
&lt;pre name=&quot;code&quot; class=&quot;scala&quot;&gt;
val supervisor = 
  ActiveObject.supervise(
    RestartStrategy(AllForOne, 3, 100),
    Component(
      fooProxy,
      LifeCycle(Permanent, 100)) ::
    Component(
      barProxy,
      LifeCycle(Permanent, 100))
    :: Nil)
&lt;/pre&gt;

&lt;p&gt;Create and use the components as in the previous example..&lt;/p&gt;
&lt;pre name=&quot;code&quot; class=&quot;scala&quot;&gt;
  val foo = ActiveObject.newInstance[Foo](classOf[Foo], fooProxy)
  val bar = ActiveObject.newInstance[Bar](classOf[Bar], barProxy)

  foo.foo(&quot;foo &quot;) 
  bar.bar(&quot;bar &quot;)
&lt;/pre&gt;

&lt;p&gt;That pretty much sums it up. So how is this implemented?&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Implementation&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;The first thing we need to do is to define an invocation context, holding arguments, method to be invoked as well as the target instance.&lt;/p&gt;
&lt;pre name=&quot;code&quot; class=&quot;scala&quot;&gt;
  case class Invocation(val method: Method, val args: Array[AnyRef], val target: AnyRef) {
    def invoke: AnyRef = method.invoke(target, args:_*)
    override def toString: String = &quot;Invocation [method: &quot; + method.getName + &quot;, args: &quot; + args + &quot;, target: &quot; + target + &quot;]&quot;
    override def hashCode(): Int = { ... }
    override def equals(that: Any): Boolean = { ... }
  }
&lt;/pre&gt;
&lt;p&gt;The second thing we need to do is to create a dynamic proxy wrapping the asynchronous dispatch. This proxy holds an instance of an actor dressed up in a &lt;code&gt;GenericServer&lt;/code&gt;.  Now comes the little trick; we will now use the &lt;code&gt;Invocation&lt;/code&gt; context as the message to our &lt;code&gt;GenericServer&lt;/code&gt; actor.&lt;/p&gt;
&lt;p&gt;As you can see in the code for the &lt;code&gt;dispatcher&lt;/code&gt; we are defining a partial function that is defined for two different messages; &lt;code&gt;Invocation&lt;/code&gt; and &lt;code&gt;&#39;exit&lt;/code&gt;. If the &lt;code&gt;Invocation&lt;/code&gt; message is received, then we invoke the invocation context, e.g. the method on the implementation instance and are returning the result to the caller using &lt;code&gt;reply(..)&lt;/code&gt;. If we receive an &lt;code&gt;&#39;exit&lt;/code&gt; message and we terminate the &lt;code&gt;GenericServer&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;The call flow for this proxy is as follows. When a regular synchronous method invocation is made on the service interface or component is redirected to the &lt;code&gt;invoke(..)&lt;/code&gt; method.  In this method we simply create an invocation context for this specific method invocation and sends it as a message to our &lt;code&gt;GenericServer&lt;/code&gt; (server). Here we have two options.  If the target method is annotated with the &lt;code&gt;@scala.actors.annotation.oneway&lt;/code&gt; annotation then we fire the message and forget by invoking &lt;code&gt;server ! invocation&lt;/code&gt;, else we are sending the message using the &lt;code&gt;server !!! invocation&lt;/code&gt; operator which returns a &lt;code&gt;Future&lt;/code&gt; which we then wait on (emulating a synchronous method call).&lt;/p&gt;
&lt;pre name=&quot;code&quot; class=&quot;scala&quot;&gt;
class ActiveObjectProxy(val target: AnyRef, val timeout: Int) extends InvocationHandler {
  private val oneway = classOf[scala.actors.annotation.oneway]
 
  private[ActiveObjectProxy] object dispatcher extends GenericServer {
    override def body: PartialFunction[Any, Unit] = {
      case invocation: Invocation =&amp;gt;
        try {
          reply(ErrRef(invocation.invoke))
        } catch {
          case e: InvocationTargetException =&amp;amp;gt; reply(ErrRef({ throw e.getTargetException }))
          case e =&amp;amp;gt; reply(ErrRef({ throw e }))
        }
      case &#39;exit =&amp;amp;gt; exit; reply()
      case unexpected =&amp;gt; throw new ActiveObjectException(&quot;Unexpected message to actor proxy: &quot; + unexpected)
    }
  }
 
  private[component] val server = new GenericServerContainer(target.getClass.getName, () =&amp;gt; dispatcher)
  server.setTimeout(timeout)
  
  def invoke(proxy: AnyRef, m: Method, args: Array[AnyRef]): AnyRef = invoke(Invocation(m, args, target))
 
  def invoke(invocation: Invocation): AnyRef = {
    if (invocation.method.isAnnotationPresent(oneway)) server ! invocation // fire and forget
    else {
      val result: ErrRef[AnyRef] = server !!! (invocation, ErrRef({ throw new ActiveObjectInvocationTimeoutException(&quot;proxy invocation timed out after &quot; + timeout + &quot; milliseconds&quot;) }))
      result() // wait on future for result
    }
  }
}
&lt;/pre&gt;
&lt;p&gt;Finally we create a factory which will do the &lt;code&gt;Supervisor&lt;/code&gt; configuration, wiring and startup.&lt;/p&gt;
&lt;pre name=&quot;code&quot; class=&quot;scala&quot;&gt;
object ActiveObject {
 
  def newInstance[T](intf: Class[T] forSome {type T}, target: AnyRef, timeout: Int): T = {
    val proxy = new ActiveObjectProxy(target, timeout)
    supervise(proxy)
    newInstance(intf, proxy)
  }
 
  def newInstance[T](intf: Class[T] forSome {type T}, proxy: ActiveObjectProxy): T = {
    Proxy.newProxyInstance(
      proxy.target.getClass.getClassLoader,
      Array(intf),
      proxy).asInstanceOf[T]
  }
 
  def supervise(restartStrategy: RestartStrategy, components: List[Component]): Supervisor = {
    object factory extends SupervisorFactory {
      override def getSupervisorConfig: SupervisorConfig = {
        SupervisorConfig(restartStrategy, components.map(c =&amp;gt; Worker(c.component.server, c.lifeCycle)))
      }
    }
    val supervisor = factory.newSupervisor
    supervisor ! scala.actors.behavior.Start
    supervisor
  }
 
  private def supervise(proxy: ActiveObjectProxy): Supervisor =
    supervise(
      RestartStrategy(OneForOne, 5, 1000),
      Component(
        proxy,
        LifeCycle(Permanent, 100))
      :: Nil)
}
 &lt;/pre&gt;
&lt;p&gt;That&amp;#8217;s pretty much all there is to it. The code is available as part of the Scala &lt;span class=&quot;caps&quot;&gt;OTP&lt;/span&gt; library:&lt;/p&gt;
&lt;p&gt;&lt;a href=&quot;http://github.com/jboner/scala-otp/tree/master/component&quot;&gt;http://github.com/jboner/scala-otp/tree/master/component&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Check it out by invoking: &lt;code&gt;git clone git://github.com/jboner/scala-otp.git&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;All ideas, improvements, patches etc. are most welcome.&lt;/p&gt;</content>
 </entry>
 
 <entry>
   <title>Real-World Scala: Managing Cross-Cutting Concerns using Mixin Composition and AOP</title>
   <link href="http://jboner.github.com/2008/12/09/real-world-scala-managing-cross-cutting-concerns-using-mixin-composition-and-aop"/>
   <updated>2008-12-09T00:00:00+00:00</updated>
   <id>hhttp://jboner.github.com/2008/12/09/real-world-scala-managing-cross-cutting-concerns-using-mixin-composition-and-aop</id>
   <content type="html">&lt;h1&gt;
&lt;p&gt;Real-World Scala: Managing Cross-Cutting Concerns using Mixin Composition and &lt;span class=&quot;caps&quot;&gt;AOP&lt;/span&gt;&lt;/p&gt;
&lt;/h1&gt;
&lt;p&gt;In a &lt;a href=&quot;http://jonasboner.com/2008/10/06/real-world-scala-dependency-injection-di.html&quot;&gt;previous post&lt;/a&gt; I showed you how you could use mixin composition and self type annotations to enable Dependency Injection (DI). Mixin composition is an extremely powerful tool that you can utilize in many different ways to enable modular and reusable code.  In this post I&amp;#8217;ll try to show you how you can use it to solve the problem of crosscutting concerns using &lt;span class=&quot;caps&quot;&gt;AOP&lt;/span&gt;/interceptor-style composition.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Crosscutting concerns and &lt;span class=&quot;caps&quot;&gt;AOP&lt;/span&gt;&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;&lt;span class=&quot;caps&quot;&gt;OOP&lt;/span&gt; has given us tools to reduce software complexity by introducing concepts like inheritance, abstraction, and polymorphism. However, developers face daily problems in software design that can&amp;#8217;t be solved easily using &lt;span class=&quot;caps&quot;&gt;OOP&lt;/span&gt;. One of these problems is how to handle cross-cutting concerns in the application.&lt;/p&gt;
&lt;p&gt;So what is a cross-cutting concern? A concern is a particular concept or area of interest. For example, in an ordering system the core concerns could be order processing and manufacturing, while the system concerns could be transaction handling and security management. A cross-cutting concern is a concern that affects several classes or modules, a concern that is not well localized and modularized.&lt;/p&gt;
&lt;p&gt;Symptoms of a cross-cutting concern are:&lt;/p&gt;
&lt;ul&gt;
	&lt;li&gt;Code tangling &amp;#8211; when a module or code section is managing several concerns simultaneously&lt;/li&gt;
	&lt;li&gt;Code scattering &amp;#8211; when a concern is spread over many modules and is not well localized and modularized&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;These symptoms affect software in various ways; for example, they make it harder to maintain and reuse software as well as harder to write and understand.&lt;/p&gt;
&lt;p&gt;Aspect-Oriented Programming (&lt;span class=&quot;caps&quot;&gt;AOP&lt;/span&gt;) tries to solve these problems by introducing the concept of separation of concerns, in which concerns can be implemented in a modular and well-localized way. &lt;span class=&quot;caps&quot;&gt;AOP&lt;/span&gt; solves this by adding an extra dimension to the design space, and introduces constructs that allow us to define the cross-cutting concerns, to lift them out into a new dimension and package them in a modular way.&lt;/p&gt;
&lt;p&gt;We are currently using two different types of interceptors (aspects if you like):&lt;/p&gt;
&lt;ul&gt;
	&lt;li&gt;Mixin composition stacks &amp;#8212; a limited but sometimes very useful approach&lt;/li&gt;
	&lt;li&gt;Generic interceptors/aspects using a pointcut pattern language&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;strong&gt;Mixin composition stacks&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Mixin composition stacks is a core language feature of Scala and is similar to Rickard Oberg&amp;#8217;s idea on using the so-called &lt;a href=&quot;http://www.jroller.com/rickard/date/20031028&quot;&gt;Abstract Schema&lt;/a&gt; pattern for type-safe &lt;span class=&quot;caps&quot;&gt;AOP&lt;/span&gt; in plain Java. (This is a very contrived example that probably shows that don&amp;#8217;t know a zip about dogs, but please bare with me.)&lt;/p&gt;
&lt;p&gt;First let&amp;#8217;s define a couple of interfaces; &lt;code&gt;Dog&lt;/code&gt; and &lt;code&gt;DogMood&lt;/code&gt; modeled as a mixins (in this case without an implementation so similar to Java&amp;#8217;s interface):&lt;/p&gt;
&lt;pre name=&quot;code&quot; class=&quot;scala&quot;&gt;
trait Dog {
  def greet(me: Human)
}

trait DogMood extends Dog {
  def greet(me: Human) {
    println(me.sayHello)
  }
}
&lt;/pre&gt;
&lt;p&gt;Now let&amp;#8217;s define two different mixin &amp;#8220;interceptors&amp;#8221; that implement these interfaces. The first one defining an angry dog and the other one a hungry dog:&lt;/p&gt;
&lt;pre name=&quot;code&quot; class=&quot;scala&quot;&gt;
trait AngryDog extends DogMood {
  abstract override def greet(me: Human) {
    println(&quot;Dog: Barks @ &quot; + me)
    super.greet(me)
  }
}
&lt;/pre&gt;

&lt;pre name=&quot;code&quot; class=&quot;scala&quot;&gt;
trait HungryDog extends DogMood {
  abstract override def greet(me: Human) {
    super.greet(me)
    println(&quot;Dog: Bites &quot; + me)
  }
}
&lt;/pre&gt;
&lt;p&gt;As we can see in this example they both override the &lt;code&gt;Mood.greet&lt;/code&gt; method. If we look more closely we can see that they follow the same pattern:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Enter method (&lt;tt&gt;greet&lt;/tt&gt;)&lt;/li&gt;
&lt;li&gt;Do something&lt;/li&gt;
&lt;li&gt;Invoke the same method on &lt;tt&gt;super&lt;/tt&gt; (&lt;tt&gt;super.greet&lt;/tt&gt;)&lt;/li&gt;
&lt;li&gt;Do something else&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;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 &amp;#8220;next&amp;#8221; mixin that have been mixed in. Exactly what f.e. &lt;a href=&quot;http://aspectj.org&quot;&gt;AspectJ&lt;/a&gt; does in its &lt;tt&gt;proceed(..)&lt;/tt&gt; method and what Spring does in its interceptors.&lt;/p&gt;
&lt;p&gt;Now let&amp;#8217;s fire up the Scala &lt;span class=&quot;caps&quot;&gt;REPL&lt;/span&gt; and create a component based on the &lt;code&gt;Dog&lt;/code&gt; interface. Scala&amp;#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.&lt;/p&gt;
&lt;pre name=&quot;code&quot; class=&quot;scala&quot;&gt;
scala&amp;gt; val dog = new Dog with AngryDog with HungryDog 
stuff2: Dog with AngryDog with HungryDog = $anon$1@1082d45

scala&amp;gt; dog.greet(new Human(&quot;Me&quot;))
Dog: Barks @ Me
Me: Hello doggiedoggie
Dog: Bites Me
&lt;/pre&gt;
&lt;p&gt;As you can see the call to &lt;code&gt;Dog.greet&lt;/code&gt; is intercepted by the different moods that are added to the dog at instantiation time.&lt;/p&gt;
&lt;p&gt;Interceptors like this are as you can see not generically reusable since they are tied to a specific interface, however if well designed can be a pretty powerful technique.  It has the advantage that everything is statically compiled and type-checked by the Scala compiler&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Generic pointcut-based aspects&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;The main usage of generic aspects is for implementing infrastructure concerns such as logging, transaction demarcation, security, clustering, persistence etc.&lt;/p&gt;
&lt;p&gt;In order to create a framework for implementing generic aspects, the first thing we need to do is to define an invocation context, holding arguments, method to be invoked as well as the target instance.&lt;/p&gt;
&lt;pre name=&quot;code&quot; class=&quot;scala&quot;&gt;
case class Invocation(val method: Method, val args: Array[AnyRef], val target: AnyRef) {
  def invoke: AnyRef = method.invoke(target, args:_*)
  override def toString: String = &quot;Invocation [method: &quot; + method.getName + &quot;, args: &quot; + args + &quot;, target: &quot; + target + &quot;]&quot;
  override def hashCode(): Int = { ... }
  override def equals(that: Any): Boolean = { ... }
}
&lt;/pre&gt;
&lt;p&gt;The second thing that we need to do is to create a base Interceptor trait. This interface defines two different pointcut matching methods.  The first one matches a precompiled &lt;a href=&quot;http://aspectj.org&quot;&gt;AspectJ &lt;/a&gt;pointcut expression using the PointcutParser in AspectJ. This allows defining interceptors matches AspectJ compatible (method) pointcut expressions. The second matcher matches methods or classes that is annotated with a specific annotation.&lt;/p&gt;
&lt;pre name=&quot;code&quot; class=&quot;scala&quot;&gt;
trait Interceptor {
  protected val parser = PointcutParser.getPointcutParserSupportingAllPrimitivesAndUsingContextClassloaderForResolution

  protected def matches(pointcut: PointcutExpression, invocation: Invocation): Boolean = {
    pointcut.matchesMethodExecution(invocation.method).alwaysMatches ||
    invocation.target.getClass.getDeclaredMethods.exists(pointcut.matchesMethodExecution(_).alwaysMatches) ||
    false
  }

  protected def matches(annotationClass: Class[T] forSome {type T &amp;lt;: Annotation}, invocation: Invocation): Boolean = {
    invocation.method.isAnnotationPresent(annotationClass) ||
    invocation.target.getClass.isAnnotationPresent(annotationClass) ||
    false
  }

  def invoke(invocation: Invocation): AnyRef
}
&lt;/pre&gt;
&lt;p&gt;The last thing we need to do is to create a factory method allows us to wire in our interceptors, declarative, in a seamless fashion. This factory is using the plain old Java Dynamic Proxy &lt;span class=&quot;caps&quot;&gt;API&lt;/span&gt; to create a proxy for our base components.&lt;/p&gt;
&lt;pre name=&quot;code&quot; class=&quot;scala&quot;&gt;
object ManagedComponentFactory {
  def createComponent[T](intf: Class[T] forSome {type T}, proxy: ManagedComponentProxy): T =
    Proxy.newProxyInstance(
      proxy.target.getClass.getClassLoader,
      Array(intf),
      proxy).asInstanceOf[T]
}

class ManagedComponentProxy(val target: AnyRef) extends InvocationHandler {
  def invoke(proxy: AnyRef, m: Method, args: Array[AnyRef]): AnyRef = invoke(Invocation(m, args, target))
  def invoke(invocation: Invocation): AnyRef = invocation.invoke
}
&lt;/pre&gt;
&lt;p&gt;Just using this factory pass is won&amp;#8217;t do any wiring for us, which is actually good since if we would use the dynamic proxy the old-fashioned way and we would have it to invoke each interceptor explicitly using reflection. But we can do better than that. Instead we will let the Scala compiler statically compiled in an interceptor stack with all our interceptors. This is best explained with an example.&lt;/p&gt;
&lt;p&gt;In this example we will define a couple of simple services called &lt;code&gt;Foo&lt;/code&gt; and &lt;code&gt;Bar&lt;/code&gt; along with their implementations. We will then implement two different infrastructure in interceptors; logging and transaction demarcation.&lt;/p&gt;
&lt;p&gt;Let&amp;#8217;s first define the service.&lt;/p&gt;
&lt;pre name=&quot;code&quot; class=&quot;scala&quot;&gt;
  import javax.ejb.{TransactionAttribute, TransactionAttributeType}

  trait Foo {
    @TransactionAttribute(TransactionAttributeType.REQUIRED)
    def foo(msg: String)
    def bar(msg: String)
  }

  class FooImpl extends Foo {
    val bar: Bar = new BarImpl
    def foo(msg: String) = println(&quot;msg: &quot; + msg)
    def bar(msg: String) = bar.bar(msg)
  }

  trait Bar {
    def bar(msg: String)
  }

  class BarImpl extends Bar {
    def bar(msg: String) = println(&quot;msg: &quot; + msg)
  }
&lt;/pre&gt;
&lt;p&gt;Now let&amp;#8217;s define a logging interceptor.  Both of these interceptors are just mockups, since the actual implementation is not really of interest. The logging interceptor is defined using a standard AspectJ pointcut while the transaction interceptor is wired to a specific annotation.&lt;/p&gt;
&lt;pre name=&quot;code&quot; class=&quot;scala&quot;&gt;
  trait LoggingInterceptor extends Interceptor {
    val loggingPointcut = parser.parsePointcutExpression(&quot;execution(* *.bar(..))&quot;)

    abstract override def invoke(invocation: Invocation): AnyRef = 
      if (matches(loggingPointcut , invocation)) {
        println(&quot;=====&amp;gt; Enter: &quot; + invocation.method.getName + &quot; @ &quot; + invocation.target.getClass.getName)
        val result = super.invoke(invocation)
        println(&quot;=====&amp;gt; Exit: &quot; + invocation.method.getName + &quot; @ &quot; + invocation.target.getClass.getName)
        result
      } else super.invoke(invocation)
  }

  trait TransactionInterceptor extends Interceptor {
    val matchingJtaAnnotation = classOf[javax.ejb.TransactionAttribute]

    abstract override def invoke(invocation: Invocation): AnyRef = 
      if (matches(matchingJtaAnnotation, invocation)) {
        println(&quot;=====&amp;gt; TX begin&quot;)
        try {
          val result = super.doStuff
          println(&quot;=====&amp;gt; TX commit&quot;)
          result     
        } catch {
          case e: Exception =&amp;gt; 
            println(&quot;=====&amp;gt; TX rollback &quot;)
        } 
      } else super.invoke(invocation)
  }
&lt;/pre&gt;
&lt;p&gt;Now let&amp;#8217;s do the wiring.  Here we are using dynamic proxy-based factory that we implemented because you can see, the actual wiring on the interceptor stack is done using Scala mixing composition and therefore has all its benefits, like compiler type checking and enforcement, the speed of statically compiled code, refactoring safety etc.&lt;/p&gt;
&lt;pre name=&quot;code&quot; class=&quot;scala&quot;&gt;
  var foo = ManagedComponentFactory.createComponent[Foo](
    classOf[Foo],
    new ManagedComponentProxy(new FooImpl)
      with LoggingInterceptor
      with TransactionInterceptor)

  foo.foo(&quot;foo&quot;)
  foo.bar(&quot;bar&quot;)
 }
&lt;/pre&gt;

&lt;p&gt;This will produce the following output:&lt;/p&gt;
&lt;pre name=&quot;code&quot; class=&quot;scala&quot;&gt;
=====&amp;gt; TX begin
msg: foo
=====&amp;gt; TX commit
=====&amp;gt; Enter: bar @ FooImpl
msg: bar
=====&amp;gt; Exit: bar @ FooImpl
&lt;/pre&gt;
&lt;p&gt;So this wraps it up.  I hope that you have learned a little bit about how powerful mixin composition in Scala is and how it can be used to write modular and reusable components with little effort.&lt;/p&gt;
&lt;p&gt;This is working fine for us, but there is definitely room for improvement. For example, runtime matcher in the interceptor is fast enough for the annotation matching (only a boolean check) but the AspectJ pointcut matcher is a bit slower since it has to do some more work. This might turn out be a problem or not, most infrastructure services (like persistence and security) performs quite a lot to work and in these cases the overhead over the interceptor of matching will not affect the overall performance much, but in other cases (such as logging or auditing) it might. We are so far only use the annotation matching, so it has not turned out to be a problem so far.  However, if it turns out to be a performance bottleneck then we will most likely switch to using my old AspectWerkz &lt;a href=&quot;http://jonasboner.com/2004/12/08/awproxy-proxy-on-steroids.html&quot;&gt;AWProxy&lt;/a&gt; to get rid of all the Java reflection code and runtime matching.&lt;/p&gt;
&lt;p&gt;For those that are interested, here is the actual &lt;span class=&quot;caps&quot;&gt;JTA&lt;/span&gt; transaction demarcation interceptor that we are using in production (implementing all the &lt;span class=&quot;caps&quot;&gt;EJB&lt;/span&gt; transaction semantics).&lt;/p&gt;
&lt;pre name=&quot;code&quot; class=&quot;scala&quot;&gt;
trait EjbTransactionInterceptor extends Interceptor with TransactionProtocol {
  val matchingJtaAnnotation = classOf[javax.ejb.TransactionAttribute]

  abstract override def invoke(invocation: Invocation): AnyRef = if (matches(matchingJtaAnnotation, invocation)) {
    val txType = getTransactionAttributeTypeFor(invocation.target.getClass, invocation.method)
    if (txType == TransactionAttributeType.REQUIRED)           withTxRequired { super.invoke(invocation) }
    else if (txType == TransactionAttributeType.REQUIRES_NEW)  withTxRequiresNew { super.invoke(invocation) }
    else if (txType == TransactionAttributeType.MANDATORY)     withTxMandatory { super.invoke(invocation) }
    else if (txType == TransactionAttributeType.NEVER)         withTxNever { super.invoke(invocation) }
    else if (txType == TransactionAttributeType.SUPPORTS)      withTxSupports { super.invoke(invocation) }
    else if (txType == TransactionAttributeType.NOT_SUPPORTED) withTxNotSupported { super.invoke(invocation) }
    else super.invoke(invocation)
  } else super.invoke(invocation)
}
&lt;/pre&gt;</content>
 </entry>
 
 <entry>
   <title>Reward Failure</title>
   <link href="http://jboner.github.com/2008/10/14/reward-failure"/>
   <updated>2008-10-14T00:00:00+00:00</updated>
   <id>hhttp://jboner.github.com/2008/10/14/reward-failure</id>
   <content type="html">&lt;h1&gt;
&lt;p&gt;Reward Failure&lt;/p&gt;
&lt;/h1&gt;
&lt;p&gt;Alex Miller has written a very important post on the &lt;a href=&quot;http://tech.puredanger.com/2008/10/09/fail/&quot;&gt;importance of failure&lt;/a&gt;, putting, what has been one of my guiding principles in leadership and working environment building, in words.&lt;/p&gt;
&lt;p&gt;It can&amp;#8217;t be emphasized enough that for a company to be a leader and not a follower one of the most important things is to foster an environment that encourages and rewards failure &amp;#8211; as a potential outcome of taking risks, trying crazy ideas out and out-of-the-box-thinking. It is only in a working environment where the fear of failing has been eliminated that the employees are set free to create amazing things.&lt;/p&gt;
&lt;p&gt;This was one of the things that I loved most while working for Terracotta, an amazing place to be working at, and one of the reason why it has managed to hire and keep so many brilliant developers and visionaries.&lt;/p&gt;</content>
 </entry>
 
 <entry>
   <title>Real-World Scala: Dependency Injection (DI) </title>
   <link href="http://jboner.github.com/2008/10/06/real-world-scala-dependency-injection-di"/>
   <updated>2008-10-06T00:00:00+00:00</updated>
   <id>hhttp://jboner.github.com/2008/10/06/real-world-scala-dependency-injection-di</id>
   <content type="html">&lt;p&gt;&lt;b&gt;Posted: 2008-10-06&lt;/b&gt;&lt;/p&gt;
&lt;p&gt;In this second post in the &lt;a href=&quot;http://jonasboner.com/2008/10/01/real-world-scala-introduction/&quot;&gt;Real-World Scala series&lt;/a&gt; I am going to discuss how to implement/achieve &lt;a href=&quot;http://www.martinfowler.com/articles/injection.html&quot;&gt;Depenency Injection&lt;/a&gt; (DI) in Scala. Scala is a very rich and deep language that gives you several ways of doing DI solely based on language constructs, but nothing prevents you from using existing Java DI frameworks, if that is preferred.&lt;/p&gt;
&lt;h3&gt;Using the Cake Pattern&lt;/h3&gt;
&lt;p&gt;The current strategy we are using is based on the so-called Cake Pattern. This pattern is first explained in Martin Oderskys&amp;#8217; paper &lt;a href=&quot;http://lamp.epfl.ch/~odersky/papers/ScalableComponent.pdf&quot;&gt;Scalable Component Abstractions&lt;/a&gt; (which is an excellent paper that is highly recommended) as the way he and his team structured the Scala compiler. But rather than trying to explain the pattern and how it can be used to implement DI in plain English let&amp;#8217;s take a look at some (naive) sample code (loosely based on our production code).&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;Note: &lt;br /&gt;
I will try to explain things in steps which I refactor towards the final version (this is only to help with the understanding), so please wait with yelling: &lt;em&gt;&amp;#8220;This sucks!&amp;#8221;&lt;/em&gt;, until you have read and understood the final version (after which you are of course allowed come with any criticism/praise/suggestions/ideas you feel is necessary). Also, the sample code will, as in all these kind of examples, look like an insanely complicated way of doing almost nothing, but bare with me and try to envision real services in a large production system and how it applies there.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;First, let&amp;#8217;s create a &lt;code&gt;UserRepository&lt;/code&gt; (&lt;span class=&quot;caps&quot;&gt;DAO&lt;/span&gt;) implementation.&lt;/p&gt;
&lt;pre name=&quot;code&quot; class=&quot;scala&quot;&gt;
// a dummy service that is not persisting anything
// solely prints out info
class UserRepository {
  def authenticate(user: User): User = { 
    println(&quot;authenticating user: &quot; + user)
    user
   }
  def create(user: User) = println(&quot;creating user: &quot; + user)
  def delete(user: User) = println(&quot;deleting user: &quot; + user)
}
&lt;/pre&gt;
&lt;p&gt;Here we could have split up the implementation in a trait interface and its implementation, but in order to keep things simple I didn&amp;#8217;t see the need.&lt;/p&gt;
&lt;p&gt;Now let&amp;#8217;s create a user service (also a dummy one, merely redirecting to our repository).&lt;/p&gt;
&lt;pre name=&quot;code&quot; class=&quot;scala&quot;&gt;
class UserService {
  def authenticate(username: String, password: String): User = 
    userRepository.authenticate(username, password)  

  def create(username: String, password: String) = 
    userRepository.create(new User(username, password))

  def delete(user: User) = All is statically typed.  
    userRepository.delete(user)
}
&lt;/pre&gt;
&lt;p&gt;Here you can see that we are referencing an instance of the &lt;code&gt;UserRepository&lt;/code&gt;. This is the dependency that we would like to have injected for us.&lt;/p&gt;
&lt;p&gt;Ok. Now the interesting stuff starts.  Let&amp;#8217;s first wrap the &lt;code&gt;UserRepository&lt;/code&gt; in an enclosing trait and instantiate the user repository there.&lt;/p&gt;
&lt;pre name=&quot;code&quot; class=&quot;scala&quot;&gt;
trait UserRepositoryComponent {
  val userRepository = new UserRepository
  class UserRepository {
    def authenticate(user: User): User = { 
      println(&quot;authenticating user: &quot; + user)
      user
    }
    def create(user: User) = println(&quot;creating user: &quot; + user)
    def delete(user: User) = println(&quot;deleting user: &quot; + user)
  }
} 
&lt;/pre&gt;
&lt;p&gt;This simply creates a component namespace for our repository. Why? Stay with me and I&amp;#8217;ll show you how to make use of this namespace in a second.&lt;/p&gt;
&lt;p&gt;Now let&amp;#8217;s look at the &lt;code&gt;UserService&lt;/code&gt;, the user of the repository. In order to declare that we would like to have the &lt;code&gt;userRepository&lt;/code&gt; instance injected in the &lt;code&gt;UserService&lt;/code&gt; we will first do what we did with the repository above; wrap the it in an enclosing (namespace) trait and use a so-called &lt;a href=&quot;http://www.scala-lang.org/node/124&quot;&gt;self-type annotation&lt;/a&gt; to declare our need for the &lt;code&gt;UserRepository&lt;/code&gt; service. Sounds more complicated than it is. Let&amp;#8217;s look at the code.&lt;/p&gt;
&lt;pre name=&quot;code&quot; class=&quot;scala&quot;&gt;
// using self-type annotation declaring the dependencies this 
// component requires, in our case the UserRepositoryComponent
trait UserServiceComponent { this: UserRepositoryComponent =&amp;gt;
  val userService = new UserService  
  class UserService {
    def authenticate(username: String, password: String): User = 
      userRepository.authenticate(username, password)  
    def create(username: String, password: String) = 
      userRepository.create(new User(username, password))
    def delete(user: User) = userRepository.delete(user)
  }
}
&lt;/pre&gt;
&lt;p&gt;The self-type annotation we are talking about is this code snippet:&lt;/p&gt;
&lt;pre name=&quot;code&quot; class=&quot;scala&quot;&gt;
this: UserRepositoryComponent =&amp;gt;
&lt;/pre&gt;
&lt;p&gt;If you need to declare more than one dependency then you can compose the annotations like this:&lt;/p&gt;
&lt;pre name=&quot;code&quot; class=&quot;scala&quot;&gt;
this: Foo with Bar with Baz =&amp;gt;
&lt;/pre&gt;
&lt;p&gt;Ok. Now we have declared the &lt;code&gt;UserRepository&lt;/code&gt; dependency. What is left is the actual wiring.&lt;/p&gt;
&lt;p&gt;In order to do that the only thing we need to do is to merge/join the different namespaces into one single application (or module) namespace. This is done by creating a &amp;#8220;module&amp;#8221; object composed of all our components. When we do that all wiring is happening automatically.&lt;/p&gt;
&lt;pre name=&quot;code&quot; class=&quot;scala&quot;&gt;
object ComponentRegistry extends
  UserServiceComponent with
  UserRepositoryComponent 
&lt;/pre&gt;
&lt;p&gt;One of the beauties here is that all wiring is statically typed. For example, if we have a dependency declaration missing, if it is misspelled or something else is screwed up then we get a compilation error. This also makes it very fast.&lt;/p&gt;
&lt;p&gt;Another beauty is that everything is immutable (all dependencies are declared as &lt;code&gt;val&lt;/code&gt;).&lt;/p&gt;
&lt;p&gt;In order to use the application we only need to get the &amp;#8220;top-level&amp;#8221; component from the registry, and all other dependencies are wired for us (similar to how Guice/Spring works).&lt;/p&gt;
&lt;pre name=&quot;code&quot; class=&quot;scala&quot;&gt;
val userService = ComponentRegistry.userService
...
val user = userService.authenticate(..) 
&lt;/pre&gt;
&lt;p&gt;So far so good?&lt;/p&gt;
&lt;p&gt;Well, no. This sucks.&lt;/p&gt;
&lt;p&gt;We have strong coupling between the service implementation and its creation, the wiring configuration is scattered all over our code base; utterly inflexible.&lt;/p&gt;
&lt;p&gt;Let&amp;#8217;s fix it.&lt;/p&gt;
&lt;p&gt;Instead of instantiating the services in their enclosing component trait, let&amp;#8217;s change it to an abstract member field.&lt;/p&gt;
&lt;pre name=&quot;code&quot; class=&quot;scala&quot;&gt;
trait UserRepositoryComponent {
  val userRepository: UserRepository

  class UserRepository {
    ...
  }
} 
&lt;/pre&gt;

&lt;pre name=&quot;code&quot; class=&quot;scala&quot;&gt;
trait UserServiceComponent { 
  this: UserRepositoryComponent =&amp;gt; 

  val userService: UserService  

  class UserService {
    ... 
  }
}
&lt;/pre&gt;
&lt;p&gt;Now, we can move the instantiation (and configuration) of the services to the &lt;code&gt;ComponentRegistry&lt;/code&gt; module.&lt;/p&gt;
&lt;pre name=&quot;code&quot; class=&quot;scala&quot;&gt;
object ComponentRegistry extends 
  UserServiceComponent with 
  UserRepositoryComponent 
{
  val userRepository = new UserRepository
  val userService = new UserService
}
&lt;/pre&gt;

&lt;p&gt;By doing this switch we have now abstracted away the actual component instantiation as well as the wiring into a single &amp;#8220;configuration&amp;#8221; object.&lt;/p&gt;
&lt;p&gt;The neat thing is that we can here switch between different implementations of the services (if we had defined an interface trait and multiple implementations). But even more interestingly, we can create multiple &amp;#8220;worlds&amp;#8221; or &amp;#8220;environments&amp;#8221; by simply composing the traits in different combinations.&lt;/p&gt;
&lt;p&gt;To show you what I mean, we&amp;#8217;ll now create a &amp;#8220;testing environment&amp;#8221; to be used during unit testing.&lt;/p&gt;
&lt;p&gt;Now, instead of instantiating the actual services we instead create mocks to each one of them. We also change the &amp;#8220;world&amp;#8221; to a trait (why, I will show you in a second).&lt;/p&gt;
&lt;pre name=&quot;code&quot; class=&quot;scala&quot;&gt;
trait TestEnvironment extends
  UserServiceComponent with
  UserRepositoryComponent with 
  org.specs.mock.JMocker
{
  val userRepository = mock(classOf[UserRepository])
  val userService = mock(classOf[UserService])
}
&lt;/pre&gt;
&lt;p&gt;Here we are not merely creating mocks but the mocks we create are wired in as the declared dependencies wherever defined.&lt;/p&gt;
&lt;p&gt;Ok, now comes the fun part. Let&amp;#8217;s create a unit test in which we are mixing in the &lt;code&gt;TestEnvironment&lt;/code&gt; mixin, which is holding all our mocks.&lt;/p&gt;
&lt;pre name=&quot;code&quot; class=&quot;scala&quot;&gt;
class UserServiceSuite extends TestNGSuite with TestEnvironment {

  @Test { val groups=Array(&quot;unit&quot;) }
  def authenticateUser = {

    // create a fresh and clean (non-mock) UserService 
    // (who&#39;s userRepository is still a mock)
    val userService = new UserService

    // record the mock invocation
    expect {
      val user = new User(&quot;test&quot;, &quot;test&quot;)
      one(userRepository).authenticate(user) willReturn user
    }
    
    ... // test the authentication method
  }
  
  ...
}
&lt;/pre&gt;
&lt;p&gt;This pretty much sums it up and is just one example on how you can compose your components in the way you want.&lt;/p&gt;
&lt;h3&gt;Other alternatives&lt;/h3&gt;
&lt;p&gt;Let&amp;#8217;s now take a look at some other ways of doing DI in Scala. This post is already pretty long and therefore I will only walk through the techniques very briefly, but it will hopefully be enough for you to understand how it is done. I have based all these remaining examples on the same little dummy program to make it easier to digest and to compare (taken from some discussion found on the Scala User mailing list). In all these examples you can just copy the code and run it in the Scala interpreter, in case you want to play with it.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Using structural typing&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;This technique using &lt;a href=&quot;http://scala.sygneca.com/patterns/duck-typing-done-right&quot;&gt;structural typing&lt;/a&gt; was posted by Jamie Webb on the Scala User mailing list some time ago. I like this approach; elegant, immutable, type-safe.&lt;/p&gt;
&lt;pre name=&quot;code&quot; class=&quot;scala&quot;&gt;
// =======================
// service interfaces
trait OnOffDevice {
  def on: Unit
  def off: Unit
}
trait SensorDevice {
  def isCoffeePresent: Boolean
}

// =======================
// service implementations
class Heater extends OnOffDevice {
  def on = println(&quot;heater.on&quot;)
  def off = println(&quot;heater.off&quot;)
}
class PotSensor extends SensorDevice {
  def isCoffeePresent = true
}

// =======================
// service declaring two dependencies that it wants injected,
// is using structural typing to declare its dependencies
class Warmer(env: {
  val potSensor: SensorDevice
  val heater: OnOffDevice
}) {
  def trigger = {
    if (env.potSensor.isCoffeePresent) env.heater.on
    else env.heater.off
  }
}

class Client(env : { val warmer: Warmer }) {
  env.warmer.trigger
}

// =======================
// instantiate the services in a configuration module 
object Config {
  lazy val potSensor = new PotSensor
  lazy val heater = new Heater
  lazy val warmer = new Warmer(this) // this is where injection happens
}

new Client(Config)
&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Using implicit declarations&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;This approach is simple and straight-forward. But I don&amp;#8217;t really like that the actual wiring (importing the implicit declarations) is scattered and tangled with the application code.&lt;/p&gt;
&lt;pre name=&quot;code&quot; class=&quot;scala&quot;&gt;
// =======================
// service interfaces
trait OnOffDevice {
  def on: Unit
  def off: Unit
}
trait SensorDevice {
  def isCoffeePresent: Boolean
}

// =======================
// service implementations
class Heater extends OnOffDevice {
  def on = println(&quot;heater.on&quot;)
  def off = println(&quot;heater.off&quot;)
}
class PotSensor extends SensorDevice {
  def isCoffeePresent = true
}

// =======================
// service declaring two dependencies that it wants injected
class Warmer(
  implicit val sensor: SensorDevice, 
  implicit val onOff: OnOffDevice) {

  def trigger = {
    if (sensor.isCoffeePresent) onOff.on
    else onOff.off
  }
}

// =======================
// instantiate the services in a module 
object Services {
  implicit val potSensor = new PotSensor
  implicit val heater = new Heater
}

// =======================
// import the services into the current scope and the wiring 
// is done automatically using the implicits
import Services._

val warmer = new Warmer
warmer.trigger
&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Using Google Guice&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Scala works nicely with separate DI frameworks and early on we were using &lt;a href=&quot;http://code.google.com/p/google-guice/&quot;&gt;Google Guice&lt;/a&gt;. You can use Guice in many different ways, but here we will discuss a slick technique based on a &lt;code&gt;ServiceInjector&lt;/code&gt; mixin that my Jan Kriesten showed me.&lt;/p&gt;
&lt;pre name=&quot;code&quot; class=&quot;scala&quot;&gt;
// =======================
// service interfaces
trait OnOffDevice {
  def on: Unit
  def off: Unit
}
trait SensorDevice {
  def isCoffeePresent: Boolean
}
trait IWarmer {
  def trigger
}
trait Client

// =======================
// service implementations
class Heater extends OnOffDevice {
  def on = println(&quot;heater.on&quot;)
  def off = println(&quot;heater.off&quot;)
}
class PotSensor extends SensorDevice {
  def isCoffeePresent = true
}
class @Inject Warmer(
  val potSensor: SensorDevice, 
  val heater: OnOffDevice) 
  extends IWarmer {

  def trigger = {
    if (potSensor.isCoffeePresent) heater.on
    else heater.off
  }
}

// =======================
// client
class @Inject Client(val warmer: Warmer) extends Client {
  warmer.trigger
}

// =======================
// Guice&#39;s configuration class that is defining the 
// interface-implementation bindings 
class DependencyModule extends Module {
  def configure(binder: Binder) = {
    binder.bind(classOf[OnOffDevice]).to(classOf[Heater])
    binder.bind(classOf[SensorDevice]).to(classOf[PotSensor])
    binder.bind(classOf[IWarmer]).to(classOf[Warmer])
    binder.bind(classOf[Client]).to(classOf[MyClient])
  }
}

// =======================
// Usage: val bean = new Bean with ServiceInjector
trait ServiceInjector {
  ServiceInjector.inject(this)
}

// helper companion object 
object ServiceInjector {
  private val injector = Guice.createInjector(
    Array[Module](new DependencyModule))
  def inject(obj: AnyRef) = injector.injectMembers(obj)
}

// =======================
// mix-in the ServiceInjector trait to perform injection
// upon instantiation
val client = new MyClient with ServiceInjector

println(client)
&lt;/pre&gt;
&lt;p&gt;That sums up what I had planned to go through in this article. I hope that you have gained some insight in how one can do DI in Scala, either using language abstractions or a separate DI framework. What works best for you is up to your use-case, requirements and taste.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Update: &lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Below I have added a Cake Pattern version of the last example for easier comparison between the different DI strategies. Just a note, if you compare the different strategies using this naive example then the Cake Pattern might look a bit overly complicated with its nested (namespace) traits, but it really starts to shine when you have a less then trivial example with many components with more or less complex dependencies to manage.&lt;/p&gt;
&lt;pre name=&quot;code&quot; class=&quot;scala&quot;&gt;
// =======================
// service interfaces
trait OnOffDeviceComponent {
  val onOff: OnOffDevice
  trait OnOffDevice {
    def on: Unit
    def off: Unit
  }
}
trait SensorDeviceComponent {
  val sensor: SensorDevice
  trait SensorDevice {
    def isCoffeePresent: Boolean
  }
}

// =======================
// service implementations
trait OnOffDeviceComponentImpl extends OnOffDeviceComponent {
  class Heater extends OnOffDevice {
    def on = println(&quot;heater.on&quot;)
    def off = println(&quot;heater.off&quot;)
  }
}
trait SensorDeviceComponentImpl extends SensorDeviceComponent {
  class PotSensor extends SensorDevice {
    def isCoffeePresent = true
  }
}
// =======================
// service declaring two dependencies that it wants injected
trait WarmerComponentImpl {
  this: SensorDeviceComponent with OnOffDeviceComponent =&amp;gt;
  class Warmer {
    def trigger = {
      if (sensor.isCoffeePresent) onOff.on
      else onOff.off
    }
  }
}

// =======================
// instantiate the services in a module
object ComponentRegistry extends
  OnOffDeviceComponentImpl with
  SensorDeviceComponentImpl with
  WarmerComponentImpl {

  val onOff = new Heater
  val sensor = new PotSensor
  val warmer = new Warmer
}

// =======================
val warmer = ComponentRegistry.warmer
warmer.trigger
&lt;/pre&gt;</content>
 </entry>
 
 <entry>
   <title>Real-World Scala: Introduction</title>
   <link href="http://jboner.github.com/2008/10/01/real-world-scala-introduction"/>
   <updated>2008-10-01T00:00:00+00:00</updated>
   <id>hhttp://jboner.github.com/2008/10/01/real-world-scala-introduction</id>
   <content type="html">&lt;h1&gt;
&lt;p&gt;Real-World Scala: Introduction&lt;/p&gt;
&lt;/h1&gt;
&lt;p&gt;The last nine months I have been running my own business together with some friends (&lt;a href=&quot;http://www.triental.com/&quot;&gt;Triental AB&lt;/a&gt;). We are building a product suite for private banking and wealth management with a focus on portfolio management, analysis and simulation.&lt;/p&gt;
&lt;p&gt;One of the great things of being your own is that you get to choose whatever technology you like and think is best suitable for the job. The last years I have been studying and playing with Functional Programming (FP) in general and &lt;a href=&quot;http://www.scala-lang.org/&quot;&gt;Scala&lt;/a&gt; in particular (among with Erlang, Clojure and Haskell). FP has been used successfully in the financial domain before (for example &lt;a href=&quot;http://ocaml.janestcapital.com/&quot;&gt;Jane Street Capital (OCaml)&lt;/a&gt; and &lt;a href=&quot;http://labs.businessobjects.com/cal/&quot;&gt;Business Objects (&lt;span class=&quot;caps&quot;&gt;CAL&lt;/span&gt;)&lt;/a&gt;), and the work we do is heavily based on mathematics which maps excellent to the FP paradigm.&lt;/p&gt;
&lt;p&gt;Apart from the FP properties (such as immutability, high-order functions, closures etc.) Scala also have some other interesting properties such as:&lt;/p&gt;
&lt;ul&gt;
	&lt;li&gt;Mixin composition (enables creation of components build up of reusable fragments, even runtime composition)&lt;/li&gt;
	&lt;li&gt;Actors library (message-passing concurrency, an easy way of parallelizing long-running computations and simulations out on multi-core or &lt;span class=&quot;caps&quot;&gt;SMP&lt;/span&gt; machines)&lt;/li&gt;
	&lt;li&gt;Flexible syntax with decent type inference (high productivity, great for DSLs etc.)&lt;/li&gt;
	&lt;li&gt;Statically typed (fast, in most cases as fast as Java)&lt;/li&gt;
	&lt;li&gt;Pattern matching&lt;/li&gt;
	&lt;li&gt;Seamless interoperability with Java&lt;/li&gt;
	&lt;li&gt;and much much more&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;So I decided to try use FP and Scala for real.&lt;/p&gt;
&lt;p&gt;By choosing to base the development on FP and a new and fairly unproven (at least in the industry) language like Scala, we were exposing ourselves to two main risks:&lt;/p&gt;
&lt;ul&gt;
	&lt;li&gt;Technology: will the technology deliver, can we fix or work around the problems that will emerge down the road etc.?&lt;/li&gt;
	&lt;li&gt;Education: will the process and time needed to be invested in learning a new language and paradigm slow us down too much?&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;And will the benefit of using it in terms of:&lt;/p&gt;
&lt;ul&gt;
	&lt;li&gt;higher productivity,&lt;/li&gt;
	&lt;li&gt;cleaner, more stable and more reusable code, and&lt;/li&gt;
	&lt;li&gt;more fun&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;outweigh the potential risks and upfront time (and money) investment?&lt;/p&gt;
&lt;p&gt;To be honest, the trip has been a bit bumpy sometimes, but now after nine months of development I can say that Scala pulled it off. Both of these risks were manageable. We are very happy with the strategic decision to use Scala.&lt;/p&gt;
&lt;p&gt;Technology-wise, one of the biggest problems of using Scala in a &lt;span class=&quot;caps&quot;&gt;JEE&lt;/span&gt; application stack is that there are (currently) no frameworks, patterns or &amp;#8220;best-practices&amp;#8221; that will help you address fundamental issues like:&lt;/p&gt;
&lt;ul&gt;
	&lt;li&gt;Dependency Injection (DI)&lt;/li&gt;
	&lt;li&gt;Code tangling and scattering e.g. &lt;span class=&quot;caps&quot;&gt;AOP&lt;/span&gt;&lt;/li&gt;
	&lt;li&gt;Transaction demarcation and context propagation&lt;/li&gt;
	&lt;li&gt;Component life-cycle&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Basically, we didn&amp;#8217;t have a &amp;#8220;container&amp;#8221; that could handle this for us so we had to write it ourselves. The good thing is that most Java frameworks works very nicely with Scala. For example, we have had using no problems using &lt;span class=&quot;caps&quot;&gt;JPA&lt;/span&gt;, Wicket etc. and deploy it in a standard &lt;span class=&quot;caps&quot;&gt;JEE&lt;/span&gt; appserver.&lt;/p&gt;
&lt;p&gt;Regarding the second risk; education, it turned out to not be much of an issue. Our developers who had never written a line in Scala and had very little experience with FP in general were fully up to speed in a couple of months. The first weeks there were some complaints but now they are loving it. Scala gives a smooth learning curve to FP since it, being a unique blend of the OO and FP paradigms, allows one to start with a very Java-esque imperative style of programming and gradually move towards a functional style. Now we have rewritten most of the imperative chunks of code that were written during the early education stages.&lt;/p&gt;
&lt;p&gt;This is the first post in a series of articles in which I will try to explain how we are bridging the &amp;#8220;Scala &amp;lt;&amp;#8212;&amp;gt; Real-World&amp;#8221; gap. I have to stress that these are by no means the only way to do things and perhaps not the best way of doing things, but simply the way we have solved our problems and what work for us. Hopefully it will also work for you.&lt;/p&gt;
&lt;p&gt;For those that are interested here is a list of the Scala frameworks that we are currently using:&lt;/p&gt;
&lt;ul&gt;
	&lt;li&gt;Scala (for the domain model, persistence layer, service layer, facade layer and container code)&lt;/li&gt;
	&lt;li&gt;&lt;a href=&quot;http://www.lag.net/configgy/&quot;&gt;Configgy&lt;/a&gt; (logging and configuration)&lt;/li&gt;
	&lt;li&gt;&lt;a href=&quot;http://github.com/jboner/scala-otp/tree/master&quot;&gt;Scala &lt;span class=&quot;caps&quot;&gt;OTP&lt;/span&gt;&lt;/a&gt; (actor management)&lt;/li&gt;
	&lt;li&gt;&lt;a href=&quot;http://liftweb.net&quot;&gt;Lift&lt;/a&gt; (misc util)&lt;/li&gt;
	&lt;li&gt;&lt;a href=&quot;http://scalax.scalaforge.org/&quot;&gt;scalax&lt;/a&gt; (misc util)&lt;/li&gt;
	&lt;li&gt;&lt;a href=&quot;http://www.artima.com/scalatest/&quot;&gt;ScalaTest&lt;/a&gt; (testing)&lt;/li&gt;
	&lt;li&gt;&lt;a href=&quot;http://code.google.com/p/scalacheck/&quot;&gt;ScalaCheck&lt;/a&gt; (testing)&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The rest is a fairly standard Java stack:&lt;/p&gt;
&lt;ul&gt;
	&lt;li&gt;Wicket&lt;/li&gt;
	&lt;li&gt;Hibernate (&lt;span class=&quot;caps&quot;&gt;JPA&lt;/span&gt;)&lt;/li&gt;
	&lt;li&gt;Atomikos (&lt;span class=&quot;caps&quot;&gt;JTA&lt;/span&gt;)&lt;/li&gt;
	&lt;li&gt;Terracotta&lt;/li&gt;
	&lt;li&gt;Wicket-Push (Cometd)&lt;/li&gt;
	&lt;li&gt;Dojo&lt;/li&gt;
	&lt;li&gt;AspectJ&lt;/li&gt;
	&lt;li&gt;XStream&lt;/li&gt;
	&lt;li&gt;TestNG&lt;/li&gt;
	&lt;li&gt;DBUnit&lt;/li&gt;
	&lt;li&gt;EasyMock&lt;/li&gt;
	&lt;li&gt;MySQL&lt;/li&gt;
	&lt;li&gt;Jetty&lt;/li&gt;
	&lt;li&gt;Maven&lt;/li&gt;
	&lt;li&gt;Hudson&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The topics I am planning on covering are:&lt;/p&gt;
&lt;ul&gt;
	&lt;li&gt;Dependency Injection&lt;/li&gt;
	&lt;li&gt;&lt;span class=&quot;caps&quot;&gt;AOP&lt;/span&gt;&lt;/li&gt;
	&lt;li&gt;Transaction demarcation (&lt;span class=&quot;caps&quot;&gt;JTA&lt;/span&gt;) and context propagation&lt;/li&gt;
	&lt;li&gt;Asynchronous event-driven components&lt;/li&gt;
	&lt;li&gt;&lt;span class=&quot;caps&quot;&gt;ORM&lt;/span&gt; (&lt;span class=&quot;caps&quot;&gt;JPA&lt;/span&gt;)&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Stay tuned for the next article.&lt;/p&gt;</content>
 </entry>
 
 <entry>
   <title>The Book Is Out</title>
   <link href="http://jboner.github.com/2008/06/30/the-book-is-out"/>
   <updated>2008-06-30T00:00:00+00:00</updated>
   <id>hhttp://jboner.github.com/2008/06/30/the-book-is-out</id>
   <content type="html">&lt;h1&gt;
&lt;p&gt;The Book Is Out&lt;/p&gt;
&lt;/h1&gt;
&lt;p&gt;&lt;a href=&quot;http://www.amazon.com/Definitive-Guide-Terracotta-Hibernate-Scalability/dp/1590599861&quot;&gt;The Definite Guide to Terracotta&lt;/a&gt; by me and my colleagues; &lt;a href=&quot;http://blog.terracottatech.com&quot;&gt;Ari&lt;/a&gt;, &lt;a href=&quot;http://rifers.org/blogs/gbevin&quot;&gt;Geert&lt;/a&gt;, &lt;a href=&quot;http://tech.puredanger.com/&quot;&gt;Alex&lt;/a&gt;, &lt;a href=&quot;http://orionl.blogspot.com/&quot;&gt;Orion&lt;/a&gt; and &lt;a href=&quot;http://javathink.blogspot.com/&quot;&gt;Taylor&lt;/a&gt;, is now available in the stores. &lt;br /&gt;
It is a pragmatic and practical book, with tons of real-world examples and stories, so if you want to learn how to use Terracotta for real then go and &lt;a href=&quot;http://www.amazon.com/Definitive-Guide-Terracotta-Hibernate-Scalability/dp/1590599861&quot;&gt;order a copy&lt;/a&gt;.&lt;/p&gt;</content>
 </entry>
 
 <entry>
   <title>Erlang-style Supervisor Module for Scala Actors</title>
   <link href="http://jboner.github.com/2008/06/16/erlang-style-supervisor-module-for-scala-actors"/>
   <updated>2008-06-16T00:00:00+00:00</updated>
   <id>hhttp://jboner.github.com/2008/06/16/erlang-style-supervisor-module-for-scala-actors</id>
   <content type="html">&lt;h1&gt;
&lt;p&gt;Erlang-style Supervisor Module for Scala Actors&lt;/p&gt;
&lt;/h1&gt;
&lt;p&gt;In this post I will explain how you can build fault-tolerant systems using Scala Actors by arranging them in Supervisor hierarchies using a library for Scala Supervisors that I just released.&lt;/p&gt;
&lt;p&gt;But first, let&amp;#8217;s recap what Actors are and what makes them useful.&lt;/p&gt;
&lt;p&gt;An actor is an abstraction that implements &lt;a href=&quot;http://c2.com/cgi/wiki?MessagePassingConcurrency&quot;&gt;Message-Passing Concurrency&lt;/a&gt;. 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 &lt;a href=&quot;http://c2.com/cgi/wiki?SharedStateConcurrency&quot;&gt;Shared-State Concurrency&lt;/a&gt; (the scheme adopted by C, Java, C# etc.) and making it easier to avoid problems like deadlocks, live locks, thread starvation etc. This makes it possible to write code that is deterministic and &lt;a href=&quot;http://en.wikipedia.org/wiki/Side_effect_%28computer_science%29&quot;&gt;side-effect-free&lt;/a&gt;, 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 &lt;span class=&quot;caps&quot;&gt;OTP&lt;/span&gt; platform) where it has been used to implement extremely fault tolerant (&lt;a href=&quot;http://ll2.ai.mit.edu/talks/armstrong.pdf &quot;&gt;99.9999999% reliability &amp;#8211; 9 nines&lt;/a&gt;) and massively concurrent systems (with hundreds of thousand simultaneous actors).&lt;/p&gt;
&lt;p&gt;So what are Supervisor hierarchies? Let&amp;#8217;s go to the source; &lt;a href=&quot;http://www.erlang.org/doc/design_principles/sup_princ.html#5&quot;&gt;http://www.erlang.org/doc/design_principles/sup_princ.html#5&lt;/a&gt;.&lt;/p&gt;
&lt;blockquote&gt;A supervisor is responsible for starting, stopping and monitoring its child processes. The basic idea of a supervisor is that it should keep its child processes alive by restarting them when necessary.&lt;/blockquote&gt;
&lt;p&gt;It has two different restart strategies; &lt;em&gt;All-For-One&lt;/em&gt; and &lt;em&gt;One-For-One&lt;/em&gt;. Best explained using some pictures (referenced from erlang.org):&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;OneForOne&lt;/strong&gt;&lt;br /&gt;
&lt;img src=&quot;http://www.erlang.org/doc/design_principles/sup4.gif&quot; alt=&quot;OneForOne&quot; /&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;AllForOne&lt;/strong&gt;&lt;br /&gt;
&lt;img src=&quot;http://www.erlang.org/doc/design_principles/sup5.gif&quot; alt=&quot;AllForOne&quot; /&gt;&lt;/p&gt;
&lt;p&gt;Naturally, the library I have written for Scala is by no means as complete and hardened as Erlang&amp;#8217;s, but it seems to do a decent job in providing the core functionality.&lt;/p&gt;
&lt;p&gt;The implementation consists of two main abstractions; &lt;code&gt;Supervisor&lt;/code&gt; and &lt;code&gt;GenericServer&lt;/code&gt;.&lt;/p&gt;
&lt;ul&gt;
	&lt;li&gt;The Supervisor manages hierarchies of Scala actors and provides fault-tolerance in terms of different restart semantics. The configuration and semantics is almost a 1-1 port of the Erlang Supervisor implementation, explained in the &lt;a href=&quot;http://erlang.org&quot;&gt;erlang.org&lt;/a&gt; doc referenced above. Read this document in order to understand how to configure the Supervisor properly.&lt;/li&gt;
&lt;/ul&gt;
&lt;ul&gt;
	&lt;li&gt;The GenericServer (which subclasses the Actor class) is a trait that forms the base for a server to be managed by a Supervisor.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The GenericServer is wrapped by a GenericServerContainer instance providing a necessary indirection needed to be able to fully manage the life-cycle of the GenericServer in an easy way.&lt;/p&gt;
&lt;p&gt;So, let&amp;#8217;s try it out by writing a small example in which we create a couple of servers, configure them, use them in various ways, kill one of them, see it recover, hotswap its implementation etc.&lt;/p&gt;
&lt;p&gt;(Sidenote: I have &lt;a href=&quot;http://jonasboner.com/2007/12/19/hotswap-code-using-scala-and-actors.html&quot;&gt;written about hotswapping actors&lt;/a&gt; before, however this library has taken this approach a but further and provides a more flexible and powerful way of achieving this. Thanks &lt;a href=&quot;http://blog.lostlake.org/&quot;&gt;&lt;span class=&quot;caps&quot;&gt;DPP&lt;/span&gt;&lt;/a&gt;.)&lt;/p&gt;
&lt;p&gt;This walk-through will only cover some of the &lt;span class=&quot;caps&quot;&gt;API&lt;/span&gt;, for more details look at the code or the tests.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;1. Create our server messages&lt;/strong&gt;&lt;/p&gt;
&lt;pre name=&quot;code&quot; class=&quot;scala&quot;&gt;
import scala.actors._
import scala.actors.Actor._

import com.jonasboner.supervisor._
import com.jonasboner.supervisor.Helpers._

sealed abstract class SampleMessage
case object Ping extends SampleMessage
case object Pong extends SampleMessage
case object OneWay extends SampleMessage
case object Die extends SampleMessage
&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;2. Create a &lt;code&gt;GenericServer&lt;/code&gt;&lt;/strong&gt;&lt;br /&gt;
We do that by extending the &lt;code&gt;GenericServer&lt;/code&gt; trait and override the &lt;code&gt;body&lt;/code&gt; method.&lt;/p&gt;
&lt;pre name=&quot;code&quot; class=&quot;scala&quot;&gt;
class SampleServer extends GenericServer {

  // This method implements the core server logic and naturally has to be overridden
  override def body: PartialFunction[Any, Unit] = {
    case Ping =&amp;gt; 
      println(&quot;Received Ping&quot;); reply(Pong)

    case OneWay =&amp;gt; 
      println(&quot;Received OneWay&quot;)

    case Die =&amp;gt; 
      println(&quot;Received Die..dying...&quot;) 
      throw new RuntimeException(&quot;Received Die message&quot;)
  }

}
&lt;/pre&gt;
&lt;p&gt;&lt;code&gt;GenericServer&lt;/code&gt; also has some callback life-cycle methods, such as &lt;code&gt;init(..)&lt;/code&gt; and &lt;code&gt;shutdown(..)&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;3. Wrap our &lt;code&gt;SampleServer&lt;/code&gt; in a &lt;code&gt;GenericServerContainer&lt;/code&gt;&lt;/strong&gt;&lt;br /&gt;
Here we also give it a name to be able to refer to it later. We are creating two instances of the same server impl in order to try out multiple server restart in case of failure.&lt;/p&gt;
&lt;pre name=&quot;code&quot; class=&quot;scala&quot;&gt;
object sampleServer1 extends GenericServerContainer(&quot;sample1&quot;, () =&amp;gt; new SampleServer)
object sampleServer2 extends GenericServerContainer(&quot;sample2&quot;, () =&amp;gt; new SampleServer)
&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;4. Create a &lt;code&gt;Supervisor&lt;/code&gt; configuration&lt;/strong&gt;&lt;br /&gt;
Here we create a &lt;code&gt;SupervisorFactory&lt;/code&gt; that is configuring our servers. The configuration mimics the Erlang configuration and defines a general restart strategy for our &lt;code&gt;Supervisor&lt;/code&gt; as well as a list of workers (servers) which for each we define a specific life-cycle.&lt;/p&gt;
&lt;pre name=&quot;code&quot; class=&quot;scala&quot;&gt;
object factory extends SupervisorFactory {
  override protected def getSupervisorConfig: SupervisorConfig = {
    SupervisorConfig(
      RestartStrategy(AllForOne, 3, 10000),
       Worker(
        sampleServer1,
        LifeCycle(Permanent, 1000)) ::
       Worker(
        sampleServer2,
        LifeCycle(Permanent, 1000)) ::
      Nil)
  }
}
&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;5. Create a new &lt;code&gt;Supervisor&lt;/code&gt;&lt;/strong&gt;&lt;/p&gt;
&lt;pre name=&quot;code&quot; class=&quot;scala&quot;&gt;
val supervisor = factory.newSupervisor
&lt;/pre&gt;
&lt;p&gt;Output: &lt;br /&gt;
&lt;pre name=&quot;code&quot; class=&quot;scala&quot;&gt;&lt;br /&gt;
12:25:30.031 [Thread-2] &lt;span class=&quot;caps&quot;&gt;DEBUG&lt;/span&gt; com.jonasboner.supervisor.Supervisor &amp;#8211; Configuring supervisor:com.jonasboner.supervisor.Supervisor@860d49&lt;br /&gt;
12:25:30.046 [Thread-2] &lt;span class=&quot;caps&quot;&gt;DEBUG&lt;/span&gt; com.jonasboner.supervisor.Supervisor &amp;#8211; Linking actor [Main$SampleServer$1@1b9240e] to supervisor [com.jonasboner.supervisor.Supervisor@860d49]&lt;br /&gt;
12:25:30.062 [Thread-2] &lt;span class=&quot;caps&quot;&gt;DEBUG&lt;/span&gt; com.jonasboner.supervisor.Supervisor &amp;#8211; Linking actor [Main$SampleServer$1@1808199] to supervisor [com.jonasboner.supervisor.Supervisor@860d49]&lt;br /&gt;
12:25:30.062 [main] &lt;span class=&quot;caps&quot;&gt;DEBUG&lt;/span&gt; Main$factory$2$ &amp;#8211; Supervisor successfully configured&lt;br /&gt;
&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;6. Start the &lt;code&gt;Supervisor&lt;/code&gt;&lt;/strong&gt;&lt;br /&gt;
This also starts the servers.&lt;/p&gt;
&lt;pre name=&quot;code&quot; class=&quot;scala&quot;&gt;
supervisor ! Start
&lt;/pre&gt;
&lt;p&gt;Output: &lt;br /&gt;
&lt;pre name=&quot;code&quot; class=&quot;scala&quot;&gt;&lt;br /&gt;
12:25:30.078 [Thread-8] &lt;span class=&quot;caps&quot;&gt;INFO&lt;/span&gt;  com.jonasboner.supervisor.Supervisor &amp;#8211; Starting server: Main$sampleServer2$2$&lt;code&gt;1479feb
12:25:30.078 [Thread-8] INFO  com.jonasboner.supervisor.Supervisor - Starting server: Main$sampleServer1$2$&lt;/code&gt;97a560&lt;br /&gt;
&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;7. Try to communicate with the servers.&lt;/strong&gt;&lt;br /&gt;
Here we try to send a couple one way asynchronous messages to our servers. &lt;br /&gt;
&lt;pre name=&quot;code&quot; class=&quot;scala&quot;&gt;&lt;br /&gt;
sampleServer1 ! OneWay&lt;br /&gt;
&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;Try to get a reference to our sampleServer2 (by name) from the Supervisor before sending a message.&lt;/p&gt;
&lt;pre name=&quot;code&quot; class=&quot;scala&quot;&gt;
supervisor.getServer(&quot;sample2&quot;) match {
  case Some(server2) =&amp;gt; server2 ! OneWay
  case None =&amp;gt; println(&quot;server [sample2] could not be found&quot;)
}
&lt;/pre&gt;
&lt;p&gt;Output: &lt;br /&gt;
&lt;pre name=&quot;code&quot; class=&quot;scala&quot;&gt;&lt;br /&gt;
Received OneWay&lt;br /&gt;
Received OneWay&lt;br /&gt;
&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;8. Send a message using a future&lt;/strong&gt;&lt;br /&gt;
Try to send an asynchronous message &amp;#8211; receive a future &amp;#8211; and wait 100 ms (time-out) for the reply.&lt;/p&gt;
&lt;pre name=&quot;code&quot; class=&quot;scala&quot;&gt;
val future = sampleServer1 !! Ping
val reply1 = future.receiveWithin(100) match {
  case Some(reply) =&amp;gt; 
    println(&quot;Received reply: &quot; + reply)
  case None =&amp;gt; 
    println(&quot;Did not get a reply witin 100 ms&quot;)
}
&lt;/pre&gt;
&lt;p&gt;Output: &lt;br /&gt;
&lt;pre name=&quot;code&quot; class=&quot;scala&quot;&gt;&lt;br /&gt;
Received Ping&lt;br /&gt;
Received reply: Pong&lt;br /&gt;
&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;9. Kill one of the servers&lt;/strong&gt;&lt;br /&gt;
Try to send a message (Die) telling the server to kill itself (by throwing an exception).&lt;/p&gt;
&lt;pre name=&quot;code&quot; class=&quot;scala&quot;&gt;
sampleServer1 ! Die
&lt;/pre&gt;
&lt;p&gt;Output: &lt;br /&gt;
&lt;pre name=&quot;code&quot; class=&quot;scala&quot;&gt;&lt;br /&gt;
Received Die..dying&amp;#8230;&lt;br /&gt;
12:25:30.093 [Thread-8] &lt;span class=&quot;caps&quot;&gt;ERROR&lt;/span&gt; c.j.supervisor.AllForOneStrategy &amp;#8211; Server [Main$SampleServer$1@1b9240e] has failed due to [java.lang.RuntimeException: Received Die message] &amp;#8211; scheduling restart &amp;#8211; scheme: ALL_FOR_ONE.&lt;br /&gt;
12:25:30.093 [Thread-8] &lt;span class=&quot;caps&quot;&gt;DEBUG&lt;/span&gt; Main$sampleServer2$2$ &amp;#8211; Waiting 1000 milliseconds for the server to shut down before killing it.&lt;br /&gt;
12:25:30.093 [Thread-8] &lt;span class=&quot;caps&quot;&gt;DEBUG&lt;/span&gt; Main$sampleServer2$2$ &amp;#8211; Server [sample2] has been shut down cleanly.&lt;br /&gt;
12:25:30.093 [Thread-8] &lt;span class=&quot;caps&quot;&gt;DEBUG&lt;/span&gt; c.j.supervisor.AllForOneStrategy &amp;#8211; Restarting server [sample2] configured as &lt;span class=&quot;caps&quot;&gt;PERMANENT&lt;/span&gt;.&lt;br /&gt;
12:25:30.093 [Thread-8] &lt;span class=&quot;caps&quot;&gt;DEBUG&lt;/span&gt; com.jonasboner.supervisor.Supervisor &amp;#8211; Linking actor [Main$SampleServer$1@166aa18] to supervisor [com.jonasboner.supervisor.Supervisor@860d49]&lt;br /&gt;
12:25:30.093 [Thread-8] &lt;span class=&quot;caps&quot;&gt;DEBUG&lt;/span&gt; Main$sampleServer1$2$ &amp;#8211; Waiting 1000 milliseconds for the server to shut down before killing it.&lt;br /&gt;
12:25:30.093 [main] &lt;span class=&quot;caps&quot;&gt;DEBUG&lt;/span&gt; com.jonasboner.supervisor.Helpers$ &amp;#8211; Future timed out while waiting for actor: Main$SampleServer$1@1b9240e&lt;br /&gt;
Expected exception: java.lang.RuntimeException: Time-out&lt;br /&gt;
12:25:31.093 [Thread-8] &lt;span class=&quot;caps&quot;&gt;DEBUG&lt;/span&gt; c.j.supervisor.AllForOneStrategy &amp;#8211; Restarting server [sample1] configured as &lt;span class=&quot;caps&quot;&gt;PERMANENT&lt;/span&gt;.&lt;br /&gt;
12:25:31.093 [Thread-8] &lt;span class=&quot;caps&quot;&gt;DEBUG&lt;/span&gt; com.jonasboner.supervisor.Supervisor &amp;#8211; Linking actor [Main$SampleServer$1@1968e23] to supervisor [com.jonasboner.supervisor.Supervisor@860d49]&lt;br /&gt;
&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;10. Send an asyncronous message and wait on a future.&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;If this call times out, the error handler we define will be invoked &amp;#8211; in this case throw an exception. It is likely that this call will time out since the server is in the middle of recovering from failure and we are on purpose defining a very short time-out to trigger this behavior.&lt;/p&gt;
&lt;pre name=&quot;code&quot; class=&quot;scala&quot;&gt;
val reply2 = try {
  sampleServer1 !!! (Ping, throw new RuntimeException(&quot;Time-out&quot;), 10) 
} catch { case e =&amp;gt; println(&quot;Expected exception: &quot; + e.toString); Pong } 
&lt;/pre&gt;
&lt;p&gt;The output of this call (due to the async nature of actors) is interleaved with the logging for the restart of the servers. As you can see the log below can be found in the middle of the restart output.  &lt;br /&gt;
&lt;pre name=&quot;code&quot; class=&quot;scala&quot;&gt;&lt;br /&gt;
12:25:30.093 [main] &lt;span class=&quot;caps&quot;&gt;DEBUG&lt;/span&gt; com.jonasboner.supervisor.Helpers$ &amp;#8211; Future timed out while waiting for actor: Main$SampleServer$1@1b9240e&lt;br /&gt;
Expected exception: java.lang.RuntimeException: Time-out&lt;br /&gt;
&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;Server should be up again. Try the same call again&lt;/p&gt;
&lt;pre name=&quot;code&quot; class=&quot;scala&quot;&gt;
val reply3 = try {
  sampleServer1 !!! (Ping, throw new RuntimeException(&quot;Time-out&quot;), 1000) 
} catch { case e =&amp;gt; println(&quot;Expected exception: &quot; + e.toString); Pong } 
&lt;/pre&gt;
&lt;p&gt;Output: &lt;br /&gt;
&lt;pre name=&quot;code&quot; class=&quot;scala&quot;&gt;&lt;br /&gt;
Received Ping&lt;br /&gt;
&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;Also check that server number 2 is up and healthy.&lt;/p&gt;
&lt;pre name=&quot;code&quot; class=&quot;scala&quot;&gt;
sampleServer2 ! Ping 
&lt;/pre&gt;
&lt;p&gt;Output: &lt;br /&gt;
&lt;pre name=&quot;code&quot; class=&quot;scala&quot;&gt;&lt;br /&gt;
Received Ping&lt;br /&gt;
&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;11. Try to hotswap the server implementation&lt;/strong&gt;&lt;br /&gt;
Here we are passing in a completely new implementation of the server logic (doesn&amp;#8217;t look that different tough, but it can be any piece of scala pattern matching code) to the server&amp;#8217;s hotswap method.&lt;/p&gt;
&lt;pre name=&quot;code&quot; class=&quot;scala&quot;&gt;
sampleServer1.hotswap(Some({
  case Ping =&amp;gt; 
    println(&quot;Hotswapped Ping&quot;)
}))
&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;12. Try the hotswapped server out&lt;/strong&gt;&lt;/p&gt;
&lt;pre name=&quot;code&quot; class=&quot;scala&quot;&gt;
sampleServer1 ! Ping
&lt;/pre&gt;
&lt;p&gt;Output: &lt;br /&gt;
&lt;pre name=&quot;code&quot; class=&quot;scala&quot;&gt;&lt;br /&gt;
Hotswapped Ping&lt;br /&gt;
&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;13. Hotswap again&lt;br /&gt;
&lt;/strong&gt;&lt;/p&gt;
&lt;pre name=&quot;code&quot; class=&quot;scala&quot;&gt;
sampleServer1.hotswap(Some({
  case Pong =&amp;gt; 
    println(&quot;Hotswapped again, now doing Pong&quot;)
    reply(Ping)
}))
&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;14. Send an asyncronous message that will wait on a future (using a different syntax/method).&lt;/strong&gt;&lt;br /&gt;
Method returns an &lt;code&gt;Option[T]&lt;/code&gt; which can be of two different types; &lt;code&gt;Some(result)&lt;/code&gt; or &lt;code&gt;None&lt;/code&gt;. If we receive &lt;code&gt;Some(result)&lt;/code&gt; then we return the result, but if &lt;code&gt;None&lt;/code&gt; is received then we invoke the error handler that we define in the &lt;code&gt;getOrElse&lt;/code&gt; method. In this case print out an info message (but you could throw an exception or do whatever you like&amp;#8230;) and return a default value (&lt;code&gt;Ping&lt;/code&gt;).&lt;/p&gt;
&lt;pre name=&quot;code&quot; class=&quot;scala&quot;&gt;
val reply4 = (sampleServer1 !!! Pong).getOrElse({
  println(&quot;Time out when sending Pong&quot;)
  Ping
})
&lt;/pre&gt;
&lt;p&gt;Output: &lt;br /&gt;
&lt;pre name=&quot;code&quot; class=&quot;scala&quot;&gt;&lt;br /&gt;
Hotswapped again, now doing Pong&lt;br /&gt;
&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;Same invocation with pattern matching syntax.&lt;/p&gt;
&lt;pre name=&quot;code&quot; class=&quot;scala&quot;&gt;
val reply5 = sampleServer1 !!! Pong match {
  case Some(result) =&amp;gt; result
  case None =&amp;gt; println(&quot;Time out when sending Pong&quot;); Ping  
}
&lt;/pre&gt;
&lt;p&gt;Output: &lt;br /&gt;
&lt;pre name=&quot;code&quot; class=&quot;scala&quot;&gt;&lt;br /&gt;
Hotswapped again, now doing Pong&lt;br /&gt;
&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;15. Hotswap back to original implementation.&lt;/strong&gt;&lt;br /&gt;
This is done by passing in &lt;code&gt;None&lt;/code&gt; to the hotswap method.&lt;/p&gt;
&lt;pre name=&quot;code&quot; class=&quot;scala&quot;&gt;
sampleServer1.hotswap(None)
&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;16. Test the final hotswap&lt;/strong&gt;&lt;/p&gt;
&lt;pre name=&quot;code&quot; class=&quot;scala&quot;&gt;
sampleServer1 !  Ping
&lt;/pre&gt;
&lt;p&gt;Output: &lt;br /&gt;
&lt;pre name=&quot;code&quot; class=&quot;scala&quot;&gt;&lt;br /&gt;
Received Ping &lt;br /&gt;
&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;17. Shut down the supervisor and its server(s)&lt;/strong&gt;&lt;/p&gt;
&lt;pre name=&quot;code&quot; class=&quot;scala&quot;&gt;
supervisor ! Stop
&lt;/pre&gt;
&lt;p&gt;Output: &lt;br /&gt;
&lt;pre name=&quot;code&quot; class=&quot;scala&quot;&gt;&lt;br /&gt;
12:25:31.093 [Thread-6] &lt;span class=&quot;caps&quot;&gt;INFO&lt;/span&gt;  com.jonasboner.supervisor.Supervisor &amp;#8211; Stopping server: Main$sampleServer2$2$&lt;code&gt;1479feb
12:25:31.093 [Thread-6] INFO  com.jonasboner.supervisor.Supervisor - Stopping server: Main$sampleServer1$2$&lt;/code&gt;97a560&lt;br /&gt;
12:25:31.093 [Thread-6] &lt;span class=&quot;caps&quot;&gt;INFO&lt;/span&gt;  com.jonasboner.supervisor.Supervisor &amp;#8211; Stopping supervisor: com.jonasboner.supervisor.Supervisor@860d49&lt;br /&gt;
&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;You can find this code in the &lt;code&gt;sample.scala&lt;/code&gt; file in the root directory of the distribution. Run it by invoking:&lt;br /&gt;
&lt;pre name=&quot;code&quot; class=&quot;scala&quot;&gt;&lt;br /&gt;
scala -cp target/supervisor-0.3.jar:[dependency jars: slf4j and logback] sample.scala&lt;br /&gt;
&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Check out&lt;/strong&gt;&lt;br /&gt;
The &lt;span class=&quot;caps&quot;&gt;SCM&lt;/span&gt; system used is Git.&lt;/p&gt;
&lt;p&gt;1. Download and install &lt;a href=&quot;http://www.google.com/search?q=git&quot;&gt;Git&lt;/a&gt;&lt;br /&gt;
2. Invoke &lt;code&gt;git clone git@github.com:jboner/scala-supervisor.git&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Build it&lt;/strong&gt;&lt;br /&gt;
The build system used is Maven 2.&lt;/p&gt;
&lt;p&gt;1. Download and install &lt;a href=&quot;http://maven.apache.org/&quot;&gt;Maven 2&lt;/a&gt;. &lt;br /&gt;
2. Step into the root dir &lt;code&gt;scala-supervisor&lt;/code&gt;.&lt;br /&gt;
3. Invoke &lt;code&gt;mvn install&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;This will build the project, run all tests, create a jar and upload it to your local Maven repository ready for use.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Runtime dependencies&lt;/strong&gt;&lt;br /&gt;
Automatically downloaded my Maven.&lt;/p&gt;
&lt;p&gt;1. Scala 2.7.1-final&lt;br /&gt;
2. SLF4J 1.5.2&lt;br /&gt;
3. LogBack Classic 0.9.9&lt;/p&gt;
&lt;p&gt;That&amp;#8217;s all to it.&lt;/p&gt;
&lt;p&gt;Have fun.&lt;/p&gt;</content>
 </entry>
 
 <entry>
   <title>AOP-style Mixin Composition Stacks in Scala</title>
   <link href="http://jboner.github.com/2008/02/06/aop-style-mixin-composition-stacks-in-scala"/>
   <updated>2008-02-06T00:00:00+00:00</updated>
   <id>hhttp://jboner.github.com/2008/02/06/aop-style-mixin-composition-stacks-in-scala</id>
   <content type="html">&lt;h1&gt;
&lt;p&gt;&lt;span class=&quot;caps&quot;&gt;AOP&lt;/span&gt;-style Mixin Composition Stacks in Scala&lt;/p&gt;
&lt;/h1&gt;
&lt;p&gt;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.&lt;/p&gt;
&lt;p&gt;In this blog post I will try to highlight the power of &lt;a href=&quot;http://www.scala-lang.org/intro/mixin.html&quot;&gt;Scala&amp;#8217;s mixins&lt;/a&gt; and how you can use mixin composition to get &lt;span class=&quot;caps&quot;&gt;AOP&lt;/span&gt;/interceptor-like style of programming.&lt;/p&gt;
&lt;p&gt;First let&amp;#8217;s define our service interface, modeled as a mixin (in this case without an implementation so similar to Java&amp;#8217;s interface):&lt;/p&gt;
&lt;pre name=&quot;code&quot; class=&quot;scala&quot;&gt;
trait Stuff {
  def doStuff
}
&lt;/pre&gt;
&lt;p&gt;Now let&amp;#8217;s define two different mixin &amp;#8220;interceptors&amp;#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):&lt;/p&gt;
&lt;pre name=&quot;code&quot; class=&quot;scala&quot;&gt;
trait LoggableStuff extends Stuff {
  abstract override def doStuff {
    println(&quot;logging enter&quot;)
    super.doStuff
    println(&quot;logging exit&quot;)
  }
}

trait TransactionalStuff extends Stuff {
  abstract override def doStuff {
    println(&quot;start TX&quot;)
    try {
      super.doStuff
      println(&quot;commit TX&quot;)      
    } catch {
      case e: Exception =&amp;gt; 
        println(&quot;rollback TX&quot;)   
    } 
  }
}
&lt;/pre&gt;
&lt;p&gt;As we can see in this example they both override the &lt;code&gt;Stuff.doStuff&lt;/code&gt; method. If we look more closely we can see that they follow the same pattern:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Enter method (&lt;tt&gt;doStuff&lt;/tt&gt;)&lt;/li&gt;
&lt;li&gt;Do something (log, start tx etc.)&lt;/li&gt;
&lt;li&gt;Invoke the same method on &lt;tt&gt;super&lt;/tt&gt; (&lt;tt&gt;super.doStuff&lt;/tt&gt;)&lt;/li&gt;
&lt;li&gt;Do something (log, commit tx etc.)&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;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 &amp;#8220;next&amp;#8221; mixin that have been mixed in. Exactly what f.e. &lt;a href=&quot;http://aspectj.org&quot;&gt;AspectJ&lt;/a&gt; does in its &lt;tt&gt;proceed(..)&lt;/tt&gt; method and what Spring does in its interceptors.&lt;/p&gt;
&lt;p&gt;But before we try this out, let&amp;#8217;s create a concrete implementation of our &lt;code&gt;Stuff&lt;/code&gt; service, called &lt;code&gt;RealStuff&lt;/code&gt;:&lt;/p&gt;
&lt;pre name=&quot;code&quot; class=&quot;scala&quot;&gt;
class RealStuff {
  def doStuff = println(&quot;doing real stuff&quot;)
}
&lt;/pre&gt;
&lt;p&gt;Now we have everything we need, so let&amp;#8217;s fire up the Scala &lt;span class=&quot;caps&quot;&gt;REPL&lt;/span&gt; and create a component based on the &lt;code&gt;RealStuff&lt;/code&gt; class and a mixin stack with support for logging and transactionality. Scala&amp;#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.&lt;/p&gt;
&lt;p&gt;First let&amp;#8217;s create a plain &lt;code&gt;RealStuff&lt;/code&gt; instance and run it:&lt;br /&gt;
&lt;pre name=&quot;code&quot; class=&quot;scala&quot;&gt;&lt;br /&gt;
scala&amp;gt; import stacks._&lt;br /&gt;
import stacks._&lt;/p&gt;
&lt;p&gt;scala&amp;gt; val stuff = new RealStuff &lt;br /&gt;
stuff: stacks.RealStuff = $anon$1@6732d42&lt;/p&gt;
&lt;p&gt;scala&amp;gt; stuff.doStuff&lt;br /&gt;
doing real stuff&lt;br /&gt;
&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;Not too exciting, but let&amp;#8217;s do it again and this time mix in the &lt;code&gt;LoggableStuff&lt;/code&gt; mixin:&lt;/p&gt;
&lt;pre name=&quot;code&quot; class=&quot;scala&quot;&gt;
scala&amp;gt; val stuff2 = new RealStuff with LoggableStuff 
stuff2: stacks.RealStuff with stacks.LoggableStuff = $anon$1@1082d45

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

scala&amp;gt; stuff3.doStuff
start TX
logging enter
doing real stuff
logging exit
commit TX
&lt;/pre&gt;
&lt;p&gt;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.&lt;/p&gt;
&lt;p&gt;This approach is similar to Rickard Oberg&amp;#8217;s idea on using the so-called &lt;a href=&quot;http://www.jroller.com/rickard/date/20031028&quot;&gt;Abstract Schema&lt;/a&gt; pattern for type-safe &lt;span class=&quot;caps&quot;&gt;AOP&lt;/span&gt; in plain Java.&lt;/p&gt;
&lt;p&gt;It is both simple and intuitive to change the order of the mixin &amp;#8220;interceptors&amp;#8221;, simply change the order in which they are applied to the target instance:&lt;/p&gt;
&lt;pre name=&quot;code&quot; class=&quot;scala&quot;&gt;
scala&amp;gt; val stuff4 = new RealStuff with TransactionalStuff with LoggableStuff
stuff4: stacks.RealStuff with stacks.TransactionalStuff with stacks.LoggableStuff = $anon$1@a20232

scala&amp;gt; stuff4.doStuff
logging enter
start TX
doing real stuff
commit TX
logging exit
&lt;/pre&gt;
&lt;p&gt;Finally, just for fun, let&amp;#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:&lt;/p&gt;
&lt;pre name=&quot;code&quot; class=&quot;scala&quot;&gt;
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 =&amp;gt; 
          if (times &amp;lt; 3) { // retry 3 times
            times += 1
            println(&quot;operation failed - retrying: &quot; + times)
          } else {
            retry = false
            throw e 
          }
      }
    }
  }
}&lt;/pre&gt;
&lt;p&gt;To test this behavior (as well as the &lt;em&gt;rollback&lt;/em&gt; feature in the &lt;code&gt;TransactionalStuff&lt;/code&gt;) we can change the &lt;code&gt;RealStuff.getStuff&lt;/code&gt; method to throw an exception:&lt;/p&gt;
&lt;p&gt;&lt;/pre&gt;&lt;pre name=&quot;code&quot; class=&quot;scala&quot;&gt;&lt;br /&gt;
class RealStuff {&lt;br /&gt;
  def doStuff {&lt;br /&gt;
    println(&amp;#8220;doing real stuff&amp;#8221;)&lt;br /&gt;
    throw new RuntimeException(&amp;#8220;expected&amp;#8221;)&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;Now we can try to add this mixin to the beginning of our our stack and run the service:&lt;/p&gt;
&lt;pre name=&quot;code&quot; class=&quot;scala&quot;&gt;
scala&amp;gt; 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&amp;gt; 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
&lt;/pre&gt;
&lt;p&gt;Pretty neat, right?&lt;/p&gt;
&lt;p&gt;That&amp;#8217;s all for now. In the next post I will cover a bunch of ways to use Scala&amp;#8217;s language primitives to do Dependency Injection (DI).&lt;/p&gt;</content>
 </entry>
 
 <entry>
   <title>Clustering Scala Actors with Terracotta</title>
   <link href="http://jboner.github.com/2008/01/25/clustering-scala-actors-with-terracotta"/>
   <updated>2008-01-25T00:00:00+00:00</updated>
   <id>hhttp://jboner.github.com/2008/01/25/clustering-scala-actors-with-terracotta</id>
   <content type="html">&lt;h1&gt;
&lt;p&gt;Clustering Scala Actors with Terracotta&lt;/p&gt;
&lt;/h1&gt;
&lt;h2&gt;Introduction&lt;/h2&gt;
&lt;p&gt;A month ago I wrote an introductory post about &lt;a href=&quot;http://lamp.epfl.ch/~phaller/actors.html&quot;&gt;Scala Actors&lt;/a&gt;: &lt;a href=&quot;http://jonasboner.com/2007/12/19/hotswap-code-using-scala-and-actors.html&quot;&gt;HotSwap Code using Scala and Actors&lt;/a&gt;. For you who don&amp;#8217;t know what it is I don&amp;#8217;t want to start by reading the previous post let&amp;#8217;s briefly recap what Actors are and what you can use them for.&lt;/p&gt;
&lt;p&gt;An Actor is an abstraction that implements &lt;a href=&quot;http://c2.com/cgi/wiki?MessagePassingConcurrency&quot;&gt;Message-Passing Concurrency&lt;/a&gt;. 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 &lt;a href=&quot;http://c2.com/cgi/wiki?SharedStateConcurrency&quot;&gt;Shared-State Concurrency&lt;/a&gt; (the scheme adopted by C, Java, C# etc.) and is avoiding most of the latter one&amp;#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.&lt;/p&gt;
&lt;p&gt;Scala&amp;#8217;s Actors are based on Doug Lea&amp;#8217;s &lt;a href=&quot;http://gee.cs.oswego.edu/dl/papers/fj.pdf&quot;&gt;Fork/Join library&lt;/a&gt; and have for example been used very effectively in the excellent &lt;a href=&quot;http://liftweb.net&quot;&gt;&lt;em&gt;lift&lt;/em&gt;&lt;/a&gt; web framework to among other things to enable &lt;a href=&quot;http://en.wikipedia.org/wiki/Comet_(programming)&quot;&gt;Comet&lt;/a&gt; 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/&amp;#8230; core or &lt;span class=&quot;caps&quot;&gt;SMP&lt;/span&gt; machines that we are starting to get now days. But this also poses challenges; how can we make applications build on this &amp;#8220;new&amp;#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?&lt;/p&gt;
&lt;p&gt;Note: &lt;a href=&quot;http://erlang.org&quot;&gt;Erlang&lt;/a&gt;, 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.&lt;/p&gt;
&lt;p&gt;I have spent some time last weeks looking into if would make sense to utilize &lt;a href=&quot;http://terracotta.org&quot;&gt;Terracotta&lt;/a&gt; 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&amp;#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.&lt;/p&gt;
&lt;h2&gt;Check out the code from &lt;span class=&quot;caps&quot;&gt;SVN&lt;/span&gt;&lt;/h2&gt;
&lt;p&gt;But before we do anything, let my point you to the &lt;a href=&quot;http://svn.terracotta.org/svn/forge/projects/labs/tim-scala-actors-2.6.1/&quot;&gt;&lt;span class=&quot;caps&quot;&gt;SVN&lt;/span&gt; repository&lt;/a&gt; where you can fetch the Terracotta Integration Module (&lt;span class=&quot;caps&quot;&gt;TIM&lt;/span&gt;) that I have implemented for Scala Actors. You can check it out anonymously by invoking:&lt;/p&gt;
&lt;pre name=&quot;code&quot; class=&quot;scala&quot;&gt;
svn co http://svn.terracotta.org/svn/forge/projects/labs/tim-scala-actors-2.6.1/
&lt;/pre&gt;
&lt;p&gt;When that is done, step into &lt;code&gt;tim-scala-actors-2.6.1/trunk&lt;/code&gt; and invoke &lt;code&gt;mvn install&lt;/code&gt; (the last command requires that you have &lt;a href=&quot;http://maven.apache.org/&quot;&gt;Maven&lt;/a&gt; installed). it&amp;#8217;ll take a while for Maven to download all its dependencies but after a while you will have a shiny new &lt;span class=&quot;caps&quot;&gt;TIM&lt;/span&gt; for Scala Actors installed in your local Maven repository (usually in &lt;code&gt;~/.m2&lt;/code&gt;). The sample that we will discuss in the next sections is available in the &lt;code&gt;src/samples/scala&lt;/code&gt; directory with the sample configuration in the &lt;code&gt;src/samples/resources&lt;/code&gt; directory.&lt;/p&gt;
&lt;h2&gt;Write an Actor&lt;/h2&gt;
&lt;p&gt;Now, let&amp;#8217;s write a little &lt;code&gt;Cart&lt;/code&gt; actor. This actor response to two different messages &lt;code&gt;AddItem(item)&lt;/code&gt; and &lt;code&gt;Tick&lt;/code&gt;. The former one adds an item to the &lt;code&gt;Cart&lt;/code&gt; while the latter one triggers the &lt;code&gt;Cart&lt;/code&gt; to print out its content (I&amp;#8217;ll let you know why it&amp;#8217;s called &lt;code&gt;Tick&lt;/code&gt; in a second):&lt;/p&gt;
&lt;pre name=&quot;code&quot; class=&quot;scala&quot;&gt;
// 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) =&amp;gt;
          items = item :: items
        case Tick =&amp;gt;
          println(&quot;Items: &quot; + items)
      }
    }
  }
  
  def ping = ActorPing.scheduleAtFixedRate(this, Tick, 0L, 5000L)
}
&lt;/pre&gt;
&lt;p&gt;As you see the state is held by a Scala &lt;code&gt;var&lt;/code&gt;, which holds onto a &lt;code&gt;List&lt;/code&gt; (immutable). In &lt;code&gt;react&lt;/code&gt; we wait for the next incoming message and if it is of type &lt;code&gt;AddItem&lt;/code&gt; then we grab the item and append it to the list with all our items, but if the message is of type &lt;code&gt;Tick&lt;/code&gt; we simply print the list of items out. Simple enough. But what is this method &lt;code&gt;ping&lt;/code&gt; doing? It uses an object called &lt;code&gt;ActorPing&lt;/code&gt; to schedule that a &lt;code&gt;Tick&lt;/code&gt; should be sent to the &lt;code&gt;Cart&lt;/code&gt; every 5 seconds (&lt;code&gt;ActorPing&lt;/code&gt; is shamelessly stolen from &lt;a href=&quot;http://blog.lostlake.org/&quot;&gt;Dave Pollak&amp;#8217;s&lt;/a&gt; &lt;em&gt;lift&lt;/em&gt;).&lt;/p&gt;
&lt;h2&gt;Configuration&lt;/h2&gt;
&lt;p&gt;In order to cluster the &lt;code&gt;Cart&lt;/code&gt; 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 &lt;code&gt;tc-config.xml&lt;/code&gt; file, but for now we have to live with it. So let&amp;#8217;s create a file with one single line, stating the fully qualified name of the &lt;code&gt;Cart&lt;/code&gt; actor; &lt;pre name=&quot;code&quot; class=&quot;scala&quot;&gt; samples.Cart &lt;/pre&gt; We can either name this file &lt;code&gt;clustered-scala-actors.conf&lt;/code&gt; and put it in root directory of the application or name it whatever we want and feed it to Terracotta using the &lt;code&gt;-Dclustered.scala.actors.config=[path to the file]&lt;/code&gt; &lt;span class=&quot;caps&quot;&gt;JVM&lt;/span&gt; property. Second, we have to write the regular Terracotta configuration file (tc-config.xml). Here we essentially have to define three things; the &lt;span class=&quot;caps&quot;&gt;TIM&lt;/span&gt; for Scala Actors, locks to guard our mutable state and finally which classes should be included for bytecode instrumentation.&lt;/p&gt;
&lt;p&gt;Starting with the &lt;span class=&quot;caps&quot;&gt;TIM&lt;/span&gt; for Scala Actors. Here we define the version on the module as well as the &lt;span class=&quot;caps&quot;&gt;URL&lt;/span&gt; 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).&lt;/p&gt;
&lt;pre name=&quot;code&quot; class=&quot;scala&quot;&gt;
&amp;lt;modules&amp;gt;
 &amp;lt;repository&amp;gt;file:///Users/jonas/.m2/repository&amp;lt;/repository&amp;gt;
 &amp;lt;module name=&quot;clustered-scala-actors-2.6.1&quot; version=&quot;2.6.0.SNAPSHOT&quot;/&amp;gt;
&amp;lt;/modules&amp;gt;
&lt;/pre&gt;
&lt;p&gt;Now we have to define the locks, which in Terracotta, also marks the transaction boundaries. The &lt;code&gt;Cart&lt;/code&gt; has one mutable field (the &lt;code&gt;var&lt;/code&gt; named &lt;code&gt;items&lt;/code&gt;) 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 &lt;code&gt;_$eq&lt;/code&gt;. That gives us the following lock definition:&lt;/p&gt;
&lt;pre name=&quot;code&quot; class=&quot;scala&quot;&gt;
&amp;lt;locks&amp;gt;
  &amp;lt;named-lock&amp;gt;
    &amp;lt;lock-name&amp;gt;cart_items_write&amp;lt;/lock-name&amp;gt;
    &amp;lt;lock-level&amp;gt;write&amp;lt;/lock-level&amp;gt;
    &amp;lt;method-expression&amp;gt;* samples.Cart.items_$eq(..)&amp;lt;/method-expression&amp;gt;
  &amp;lt;/named-lock&amp;gt;
  &amp;lt;named-lock&amp;gt;
    &amp;lt;lock-name&amp;gt;cart_items_read&amp;lt;/lock-name&amp;gt;
    &amp;lt;lock-level&amp;gt;read&amp;lt;/lock-level&amp;gt;
    &amp;lt;method-expression&amp;gt;* samples.Cart.items()&amp;lt;/method-expression&amp;gt;
  &amp;lt;/named-lock&amp;gt;
&amp;lt;/locks&amp;gt;
&lt;/pre&gt;
&lt;p&gt;We have to define a pair like this for each mutable &lt;strong&gt;user defined&lt;/strong&gt; field in a clustered actor (not the standard one&amp;#8217;s that are common for all Scala Actors, those are automatically defined).&lt;/p&gt;
&lt;p&gt;It important to understand the &lt;span class=&quot;caps&quot;&gt;TIM&lt;/span&gt; automatically clusters the Actor&amp;#8217;s mailbox, which means that no messages will ever be lost &amp;#8211; providing full fault-tolerance.&lt;/p&gt;
&lt;p&gt;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 &lt;code&gt;Cart&lt;/code&gt; actor in one way or the other. Those are picked out by the pattern like: &lt;code&gt;&#39;samples.*&#39;&lt;/code&gt;. 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 &lt;code&gt;List&lt;/code&gt; abstraction in Scala. Here is the full listing:&lt;/p&gt;
&lt;pre name=&quot;code&quot; class=&quot;scala&quot;&gt;
&amp;lt;instrumented-classes&amp;gt;
 &amp;lt;include&amp;gt;
   &amp;lt;class-expression&amp;gt;samples.*&amp;lt;/class-expression&amp;gt;
 &amp;lt;/include&amp;gt;
 &amp;lt;include&amp;gt;
   &amp;lt;class-expression&amp;gt;scala.List&amp;lt;/class-expression&amp;gt;
 &amp;lt;/include&amp;gt;
 &amp;lt;include&amp;gt;
   &amp;lt;class-expression&amp;gt;scala.$colon$colon&amp;lt;/class-expression&amp;gt;
 &amp;lt;/include&amp;gt;
 &amp;lt;include&amp;gt;
   &amp;lt;class-expression&amp;gt;scala.Nil$&amp;lt;/class-expression&amp;gt;
 &amp;lt;/include&amp;gt;
&amp;lt;/instrumented-classes&amp;gt;
&lt;/pre&gt;
&lt;p&gt;I could have included these (and many more) classes in the &lt;span class=&quot;caps&quot;&gt;TIM&lt;/span&gt;, 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 &lt;span class=&quot;caps&quot;&gt;XML&lt;/span&gt; snippet that you have to put in the tc-config.xml file. So don&amp;#8217;t panic if things blow up.&lt;/p&gt;
&lt;h2&gt;Enable Terracotta&lt;/h2&gt;
&lt;p&gt;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 &amp;#8211; however I think it might still be useful to be able to try the application out in the Scala &lt;span class=&quot;caps&quot;&gt;REPL&lt;/span&gt;). The simplest way of doing that is to do some minor changes to the &lt;code&gt;scala&lt;/code&gt; command. First, let&amp;#8217;s step down into the &lt;code&gt;scala/bin&lt;/code&gt; directory and make a copy of the &lt;code&gt;scala&lt;/code&gt; command called &lt;code&gt;tc-scala&lt;/code&gt;, then scroll down all the way to the bottom. As you can see it is just a wrapper around the regular &lt;code&gt;java&lt;/code&gt; command, which makes things pretty easy for us. We start by defining some environmental variables (here showing my local settings):&lt;/p&gt;
&lt;pre name=&quot;code&quot; class=&quot;scala&quot;&gt;
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=&quot;$TC_INSTALL_DIR&quot;/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
&lt;/pre&gt;
&lt;p&gt;When these variables have been defined we can replace the existing invocation of &lt;code&gt;java&lt;/code&gt; with the following:&lt;/p&gt;
&lt;pre name=&quot;code&quot; class=&quot;scala&quot;&gt;
${JAVACMD:=java} ${JAVA_OPTS:=-Xmx256M -Xms256M} \
 -Xbootclasspath/p:&quot;$TC_BOOT_JAR&quot; \
 -Dtc.install-root=&quot;$TC_INSTALL_DIR&quot; \
 -Dtc.config=&quot;$TC_CONFIG_FILE&quot; \
 -Dclustered.scala.actors.config=&quot;$TC_SCALA_ACTORS_CONFIG_FILE&quot; \
 -Dscala.home=&quot;$SCALA_HOME&quot; \
 -Denv.classpath=&quot;$CLASSPATH&quot; \
 -Denv.emacs=&quot;$EMACS&quot; \
 -cp &quot;$BOOT_CLASSPATH&quot;:&quot;$EXTENSION_CLASSPATH&quot;:&quot;$TC_TIM_SCALA_ACTORS_JAR&quot; \
 scala.tools.nsc.MainGenericRunner  &quot;$@&quot;
&lt;/pre&gt;
&lt;h2&gt;Let&amp;#8217;s run it&lt;/h2&gt;
&lt;p&gt;Enough hacking. Now let&amp;#8217;s try it out. I think that the best way of learning new things in Scala is to use its &lt;span class=&quot;caps&quot;&gt;REPL&lt;/span&gt;, so let&amp;#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:&lt;/p&gt;
&lt;pre name=&quot;code&quot; class=&quot;scala&quot;&gt;
$ ./start-tc-server.sh
&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; you need to grab Terracotta from &lt;span class=&quot;caps&quot;&gt;SVN&lt;/span&gt; trunk to get the bits that work with the Scala &lt;span class=&quot;caps&quot;&gt;TIM&lt;/span&gt;. See instructions on how to &lt;a href=&quot;http://www.terracotta.org/confluence/display/wiki/Source+Repository&quot;&gt;check out the sources&lt;/a&gt; and &lt;a href=&quot;http://www.terracotta.org/confluence/display/devdocs/Building+Terracotta&quot;&gt;how to build it&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;Now, we can start up the Terracotta enabled Scala &lt;span class=&quot;caps&quot;&gt;REPL&lt;/span&gt;:&lt;/p&gt;
&lt;pre name=&quot;code&quot; class=&quot;scala&quot;&gt;
$ 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 &#39;/Users/jonas/src/java/tc-forge/projects/tim-scala-actors-2.6.1/trunk/src/samples/resources/tc-config.xml&#39;.
2008-01-25 07:42:12,325 INFO - Log file: &#39;/Users/jonas/terracotta/client-logs/scala/actors/20080125074212303/terracotta-client.log&#39;.
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&amp;gt;
&lt;/pre&gt;
&lt;p&gt;Here we can see that it has found and connected to the Terracotta server, found our &lt;code&gt;clustered-scala-actors.conf&lt;/code&gt; config file and configured clustering for one Scala Actor; &lt;code&gt;samples.Cart&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;Let&amp;#8217;s have some fun and start up another &lt;span class=&quot;caps&quot;&gt;REPL&lt;/span&gt; in another terminal window. In each of these we do the following; import our classes, create a new &lt;code&gt;Cart&lt;/code&gt; (Actor) and start up the Actor.&lt;/p&gt;
&lt;pre name=&quot;code&quot; class=&quot;scala&quot;&gt;
scala&amp;gt; import samples._              
import samples._

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

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

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

scala&amp;gt; cart ! AddItem(&quot;apples&quot;)

scala&amp;gt; cart ! Tick

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

scala&amp;gt; 
&lt;/pre&gt;
&lt;p&gt;Ok, so far no news. But comes the moment of truth, let&amp;#8217;s take the other &lt;span class=&quot;caps&quot;&gt;REPL&lt;/span&gt; and fire of a &lt;code&gt;Tick&lt;/code&gt;:&lt;/p&gt;
&lt;pre name=&quot;code&quot; class=&quot;scala&quot;&gt;
scala&amp;gt; cart ! Tick

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

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

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

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

scala&amp;gt; cart ! AddItem(&quot;football&quot;)

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

...
&lt;/pre&gt;

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

class Bar {
  val actor = ActorFactory.create
}
&lt;/pre&gt;
&lt;p&gt;Then &lt;code&gt;Foo&lt;/code&gt; and &lt;code&gt;Bar&lt;/code&gt; will have two distinct clustered Actors each with a unique but cluster-wide identity.&lt;/p&gt;
&lt;p&gt;The &lt;code&gt;class&lt;/code&gt; 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.&lt;/p&gt;
&lt;p&gt;Finally we have the &lt;code&gt;custom&lt;/code&gt; scope. Which, as it sounds, allows custom user defined scoping.&lt;/p&gt;
&lt;h2&gt;How to define custom scoped Actors?&lt;/h2&gt;
&lt;p&gt;If you want more control over scope and life-cycle of a specific Actor then you can define it to have &lt;code&gt;custom&lt;/code&gt; scope in the &lt;code&gt;clustered-scala-actors.conf&lt;/code&gt; 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 &lt;code&gt;tc-config.xml&lt;/code&gt; file. The factory might look something like this:&lt;/p&gt;
&lt;pre name=&quot;code&quot; class=&quot;scala&quot;&gt;
// 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) =&amp;gt; cart
      case None =&amp;gt; 
        val cart = new Cart
        instances += (id -&amp;gt; cart)
        cart
    }
  }
}
&lt;/pre&gt;
&lt;p&gt;This means that we have to add some more configuration elements to our Terracotta configuration. First we need to add the root &lt;code&gt;samples.Cart$.instances&lt;/code&gt; (&lt;code&gt;Cart$&lt;/code&gt; is the name of Scala&amp;#8217;s compiled &lt;code&gt;Cart&lt;/code&gt; companion object, all companion objects compiles to a class with the name of the original class suffixed with &lt;code&gt;$&lt;/code&gt;):&lt;/p&gt;
&lt;pre name=&quot;code&quot; class=&quot;scala&quot;&gt;
&amp;lt;roots&amp;gt;
  &amp;lt;root&amp;gt;
    &amp;lt;field-name&amp;gt;samples.Cart$.instances&amp;lt;/field-name&amp;gt;
  &amp;lt;/root&amp;gt;
&amp;lt;/roots&amp;gt;
&lt;/pre&gt;
&lt;p&gt;Then we have to add locking for the &lt;code&gt;Cart.newInstance(..)&lt;/code&gt; method and finally a whole bunch of new include statements for all the Scala types that are referenced by the &lt;code&gt;scala.collection.mutable.HashMap&lt;/code&gt; that we used as root:&lt;/p&gt;
&lt;pre name=&quot;code&quot; class=&quot;scala&quot;&gt;
...  
&amp;lt;named-lock&amp;gt;
  &amp;lt;lock-name&amp;gt;Cart_newInstance&amp;lt;/lock-name&amp;gt;
  &amp;lt;lock-level&amp;gt;write&amp;lt;/lock-level&amp;gt;
  &amp;lt;method-expression&amp;gt;* samples.Cart$.newInstance(..)&amp;lt;/method-expression&amp;gt;
&amp;lt;/named-lock&amp;gt;

...
          
&amp;lt;include&amp;gt;
  &amp;lt;class-expression&amp;gt;scala.collection.mutable.HashMap&amp;lt;/class-expression&amp;gt;
&amp;lt;/include&amp;gt;
&amp;lt;include&amp;gt;
  &amp;lt;class-expression&amp;gt;scala.runtime.BoxedAnyArray&amp;lt;/class-expression&amp;gt;
&amp;lt;/include&amp;gt;
&amp;lt;include&amp;gt;
  &amp;lt;class-expression&amp;gt;scala.runtime.BoxedArray&amp;lt;/class-expression&amp;gt;
&amp;lt;/include&amp;gt;
&amp;lt;include&amp;gt;
  &amp;lt;class-expression&amp;gt;scala.collection.mutable.DefaultEntry&amp;lt;/class-expression&amp;gt;
&amp;lt;/include&amp;gt;
  
...
&lt;/pre&gt;
&lt;p&gt;That&amp;#8217;s pretty much all there&amp;#8217;s to it. Check out the code, play with it and come back with feedback, bug reports, patches etc.&lt;/p&gt;
&lt;p&gt;Enjoy. &lt;/p&gt;</content>
 </entry>
 
 <entry>
   <title>HotSwap Code using Scala and Actors</title>
   <link href="http://jboner.github.com/2007/12/19/hotswap-code-using-scala-and-actors"/>
   <updated>2007-12-19T00:00:00+00:00</updated>
   <id>hhttp://jboner.github.com/2007/12/19/hotswap-code-using-scala-and-actors</id>
   <content type="html">&lt;h1&gt;
&lt;p&gt;HotSwap Code using Scala and Actors&lt;/p&gt;
&lt;/h1&gt;
&lt;p&gt;&lt;strong&gt;Update&lt;/strong&gt;: In this article I am showing an even more powerful way of doing hotswap in Scala: &lt;br /&gt;
&lt;a href=&quot;http://jonasboner.com/2008/06/16/erlang-style-supervisor-module-for-scala-actors.html&quot;&gt;http://jonasboner.com/2008/06/16/erlang-style-supervisor-module-for-scala-actors.html&lt;/a&gt;.&lt;br /&gt;
Se the bottom of this article for an example of how this is used to enable hotswap of arbitrary pieces of pattern matching code.&lt;/p&gt;
&lt;hr /&gt;

&lt;p&gt;In this post I will show you how you can do code hotswap in the same fashion as in &lt;a href=&quot;http://erlang.org&quot;&gt;Erlang&lt;/a&gt; using &lt;a href=&quot;http://www.scala-lang.org&quot;&gt;Scala&lt;/a&gt; and its &lt;a href=&quot;http://lamp.epfl.ch/~phaller/actors.html&quot;&gt;Actors&lt;/a&gt; package.&lt;/p&gt;
&lt;p&gt;An actor is an abstraction that implements &lt;a href=&quot;http://c2.com/cgi/wiki?MessagePassingConcurrency&quot;&gt;Message-Passing Concurrency&lt;/a&gt;. 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 &lt;a href=&quot;http://c2.com/cgi/wiki?SharedStateConcurrency&quot;&gt;Shared-State Concurrency&lt;/a&gt; (the scheme adopted by C, Java, C# etc.) and is avoiding all of the latter one&amp;#8217;s problems with deadlocks, live locks, thread starvation etc. This makes it possible to write code that is deterministic and &lt;a href=&quot;http://en.wikipedia.org/wiki/Side_effect_%28computer_science%29&quot;&gt;side-effect-free&lt;/a&gt;, 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 &lt;span class=&quot;caps&quot;&gt;OTP&lt;/span&gt; platform) where it has been used to implement extremely fault tolerant (&lt;a href=&quot;http://ll2.ai.mit.edu/talks/armstrong.pdf &quot;&gt;99.9999999% reliability &amp;#8211; 9 nines&lt;/a&gt;) and massively concurrent systems (with hundreds of thousand simultaneous actors).&lt;/p&gt;
&lt;p&gt;Let&amp;#8217;s start by writing a little server. We implement this in the form of a &lt;code&gt;trait&lt;/code&gt;, which is Scala&amp;#8217;s &lt;a href=&quot;http://en.wikipedia.org/wiki/Mixin&quot;&gt;mixin&lt;/a&gt; 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 &lt;code&gt;trait&lt;/code&gt; only defines a single method named &lt;code&gt;status&lt;/code&gt; 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 &lt;code&gt;ServerOne&lt;/code&gt; concrete server class (with the &lt;code&gt;status&lt;/code&gt; method mixed in).&lt;/p&gt;
&lt;pre name=&quot;code&quot; class=&quot;scala&quot;&gt;
// servers
trait Server {
  def status = println(&quot;current server: &quot; + this)
}
class ServerOne extends Server 
&lt;/pre&gt;
&lt;p&gt;Let&amp;#8217;s instantiate the &lt;code&gt;ServerOne&lt;/code&gt; class and see what the &lt;code&gt;status&lt;/code&gt; method it will print out. Here we are doing it interactively through Scala&amp;#8217;s &lt;span class=&quot;caps&quot;&gt;REPL&lt;/span&gt; (read-eval-print-loop).&lt;/p&gt;
&lt;pre name=&quot;code&quot; class=&quot;scala&quot;&gt;
$ scala -cp .

scala&amp;gt; val server = new ServerOne
server: ServerOne = ServerOne@7be75d

scala&amp;gt; server status
current server: ServerOne@7be75d
&lt;/pre&gt;
&lt;p&gt;Now, before we write the actor we have to define the messages it responds to. Here Scala is using something called &lt;em&gt;case classes&lt;/em&gt; 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 &lt;code&gt;new&lt;/code&gt;, 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 &lt;a href=&quot;http://c2.com/cgi/wiki?ValueObject&quot;&gt;value objects&lt;/a&gt;, but that is another story). We define two different messages; Status and HotSwap.&lt;/p&gt;
&lt;pre name=&quot;code&quot; class=&quot;scala&quot;&gt;
// triggers the status method
case object Status

// triggers hotswap - carries the new server to be hotswapped to
case class HotSwap(s: Server)
&lt;/pre&gt;
&lt;p&gt;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 &lt;code&gt;actor {...}&lt;/code&gt; construct. When we subclass the actor we have to implement the method called &lt;code&gt;act&lt;/code&gt; which is the callback method that is invoked when the actor is started up.&lt;/p&gt;
&lt;p&gt;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 &lt;code&gt;react&lt;/code&gt; method instead of the &lt;code&gt;receive&lt;/code&gt; method for receiving messages).&lt;/p&gt;
&lt;p&gt;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 &lt;code&gt;ServerOne&lt;/code&gt;. The pattern matching is happening in &lt;code&gt;react&lt;/code&gt; statement in which we have three different cases (pattern matchers).&lt;/p&gt;
&lt;p&gt;The first one matches our &lt;code&gt;Status&lt;/code&gt;, when we receive this message we simply invoke the &lt;code&gt;status&lt;/code&gt; method on our server and then taking another round in the &lt;code&gt;loop&lt;/code&gt; passing along the server.&lt;/p&gt;
&lt;p&gt;The second one matches our &lt;code&gt;HotSwap&lt;/code&gt; message. It is here things are starting to get interesting. Now we can take the new replacement server (here called &lt;code&gt;newServer&lt;/code&gt;), which is passed to us as an argument to the &lt;code&gt;HotSwap&lt;/code&gt; message, and pass it in to the call to the &lt;code&gt;loop&lt;/code&gt; method. Voila, we have updated our server at runtime. Now all subsequent messages will act on our new server instance.&lt;/p&gt;
&lt;p&gt;This will work since the &lt;code&gt;react&lt;/code&gt; 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 &lt;a href=&quot;http://en.wikipedia.org/wiki/Tail_recursion&quot;&gt;tail-recursive&lt;/a&gt; algorithms and turns them into regular loops.&lt;/p&gt;
&lt;p&gt;At the end we have also added a match-all pattern that does nothing, this is defined by the &lt;code&gt;case _ =&amp;gt; ...&lt;/code&gt; clause. Let&amp;#8217;s take a look at the code.&lt;/p&gt;
&lt;pre name=&quot;code&quot; class=&quot;scala&quot;&gt;
class ServerActor extends Actor {
  def act = {
    println(&quot;starting server actor...&quot;)
    loop(new ServerOne)
  }
  
  def loop(server: Server) {
    react {
      case Status =&amp;gt; 
        server.status
        loop(server)

      case HotSwap(newServer) =&amp;gt; 
        println(&quot;hot swapping code...&quot;)
        loop(newServer)

      case _ =&amp;gt; loop(server)
    }
  }
}
&lt;/pre&gt;
&lt;p&gt;Finally we will follow one of Scala&amp;#8217;s idioms and create a companion object for our &lt;code&gt;ServerActor&lt;/code&gt; 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.&lt;/p&gt;
&lt;p&gt;Worth to note is that the &lt;code&gt;val&lt;/code&gt; 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 &lt;code&gt;val&lt;/code&gt;, the actor will not be started until it is used for the first time.&lt;/p&gt;
&lt;pre name=&quot;code&quot; class=&quot;scala&quot;&gt;
// actor companion object
object ServerActor {
  val actor = {
    val a = new ServerActor
    a.start; a
  }
}
&lt;/pre&gt;
&lt;p&gt;Let&amp;#8217;s try to run it in the Scala &lt;span class=&quot;caps&quot;&gt;REPL&lt;/span&gt;. The Scala function &lt;em&gt;!&lt;/em&gt; (pronounced bang) means &amp;#8220;send a message&amp;#8221;. So &lt;em&gt;act ! msg&lt;/em&gt; means send message &lt;em&gt;msg&lt;/em&gt; to actor &lt;em&gt;act&lt;/em&gt;.&lt;/p&gt;
&lt;pre name=&quot;code&quot; class=&quot;scala&quot;&gt;
$ scala -cp .  

scala&amp;gt; import hotswap._
import hotswap._

scala&amp;gt; val actor = ServerActor.actor
starting actor...
actor: hotswap.ServerActor = hotswap.ServerActor@528ed7

scala&amp;gt; actor ! Status
current server: hotswap.ServerOne@226445

scala&amp;gt; class ServerTwo extends Server {
     | override def status = println(&quot;hotswapped server: &quot; + this)
     | }
defined class ServerTwo

scala&amp;gt; actor ! HotSwap(new ServerTwo)
hot swapping code...

scala&amp;gt; actor ! Status
hotswapped server: line5$object$$iw$$iw$$iw$ServerTwo@b556
&lt;/pre&gt;
&lt;p&gt;Pretty cool, right?&lt;/p&gt;
&lt;p&gt;This would be even more cool if Scala came with an &lt;span class=&quot;caps&quot;&gt;SSH&lt;/span&gt; server that could provide this &lt;span class=&quot;caps&quot;&gt;REPL&lt;/span&gt; remotely (like we have in Erlang &lt;span class=&quot;caps&quot;&gt;OTP&lt;/span&gt;). 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 &lt;a href=&quot;http://www.scala-lang.org/docu/files/api/scala/actors/remote$content.html&quot;&gt;remote actors&lt;/a&gt; in the Scala distribution, but that is something for another post.&lt;/p&gt;
&lt;hr /&gt;
&lt;p&gt;&lt;strong&gt;Update&lt;/strong&gt;:&lt;/p&gt;
&lt;p&gt;This is a slightly simplified version of the GenericServer code (as discussed &lt;a href=&quot;http://jonasboner.com/2008/06/16/erlang-style-supervisor-module-for-scala-actors.html&quot;&gt;here&lt;/a&gt;), allowing hotswap of arbitrary pieces of pattern matching code:&lt;/p&gt;
&lt;pre name=&quot;code&quot; class=&quot;scala&quot;&gt;
// the actor&#39;s main loop
def act = loop { react { genericBase orElse actorBase } }

// should we go with the hotswapped impl or the regular server impl (body)
private def actorBase: PartialFunction[Any, Unit] = hotswap getOrElse body

// the hotswapped impl
private var hotswap: Option[PartialFunction[Any, Unit]] = None

// generic functionality
private val genericBase: PartialFunction[Any, Unit] = {
  case HotSwap(code) =&amp;gt; hotswap = code
}

// the regular server implementation
def body: PartialFunction[Any, Unit] = {
  ...
}

&lt;/pre&gt;
&lt;p&gt;This code can be used like this:&lt;/p&gt;
&lt;pre name=&quot;code&quot; class=&quot;scala&quot;&gt;
server ! HotSwap(Some({
  case Ping =&amp;gt; 
    println(&quot;Ping&quot;)
}))
&lt;/pre&gt;</content>
 </entry>
 
 <entry>
   <title>The Terracotta Book</title>
   <link href="http://jboner.github.com/2007/12/18/the-terracotta-book"/>
   <updated>2007-12-18T00:00:00+00:00</updated>
   <id>hhttp://jboner.github.com/2007/12/18/the-terracotta-book</id>
   <content type="html">&lt;h1&gt;
&lt;p&gt;The Terracotta Book&lt;/p&gt;
&lt;/h1&gt;
&lt;p&gt;The last month I have been very busy writing on &lt;a href=&quot;http://www.apress.com/book/view/1590599861&quot;&gt;The Definitive Guide to Terracotta&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;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.&lt;/p&gt;
&lt;p&gt;It will be available sometime next year and if you&amp;#8217;re interested in getting updates on the book can subscribe to: bookupdate AT terracottatech &lt;span class=&quot;caps&quot;&gt;DOT&lt;/span&gt; com.&lt;/p&gt;</content>
 </entry>
 
 <entry>
   <title>Oredev 2007: BDD, LINQ, Scalability trends etc.</title>
   <link href="http://jboner.github.com/2007/11/17/oredev-2007-bdd-linq-terracotta-etc"/>
   <updated>2007-11-17T00:00:00+00:00</updated>
   <id>hhttp://jboner.github.com/2007/11/17/oredev-2007-bdd-linq-terracotta-etc</id>
   <content type="html">&lt;h1&gt;
&lt;p&gt;Oredev 2007: &lt;span class=&quot;caps&quot;&gt;BDD&lt;/span&gt;, &lt;span class=&quot;caps&quot;&gt;LINQ&lt;/span&gt;, Scalability trends etc.&lt;/p&gt;
&lt;/h1&gt;
&lt;p&gt;I had the pleasure of attending &lt;a href=&quot;http://oredev.org&quot;&gt;Oredev&lt;/a&gt; last month. This was a great conference. I have attended this conference three years in a row and it&amp;#8217;s getting better and better every year.&lt;/p&gt;
&lt;p&gt;This year they had a great speaker lineup and I was able to catch some very interesting talks. Among the most memorable ones were &lt;a href=&quot;http://dannorth.net/&quot;&gt;Dan North&lt;/a&gt;&amp;#8216;s keynote about why &amp;#8220;Best Practices&amp;#8221; in software development are neither &amp;#8220;Best&amp;#8221; no &amp;#8220;Practices&amp;#8221;. I also attended Dan&amp;#8217;s talk on &lt;span class=&quot;caps&quot;&gt;BDD&lt;/span&gt; (&lt;a href=&quot;http://behaviour-driven.org/&quot;&gt;Behavior-Driven&lt;br /&gt;
Development&lt;/a&gt;). Great talk. I find &lt;span class=&quot;caps&quot;&gt;BDD&lt;/span&gt; very interesting, it feels like the natural extension of, or complement to &lt;span class=&quot;caps&quot;&gt;TDD&lt;/span&gt; (Test-Drived Development) in that it focuses on getting complete concept coverage in the tests instead of code coverage (as in &lt;span class=&quot;caps&quot;&gt;TDD&lt;/span&gt;).&lt;/p&gt;
&lt;p&gt;Other great talks were &lt;a href=&quot;http://blog.toolshed.com/&quot;&gt;Andy Hunt&lt;/a&gt;&amp;#8216;s (Pragmatic Programmers) keynote and &lt;a href=&quot;http://research.microsoft.com/~emeijer/&quot;&gt;Erik Meijer&lt;/a&gt;&amp;#8217;s talk on &lt;a href=&quot;http://msdn2.microsoft.com/en-us/netframework/aa904594.aspx&quot;&gt;&lt;span class=&quot;caps&quot;&gt;LINQ&lt;/span&gt;&lt;/a&gt;. The latter one was fun to watch, Erik (undeliberately it felt like) turned it more or less into a praise of &lt;a href=&quot;http://haskell.org&quot;&gt;Haskell&lt;/a&gt;; how they have stolen all the good stuff in &lt;span class=&quot;caps&quot;&gt;LINQ&lt;/span&gt; from Haskell and that the world would be a better place if everyone just used Haskell.&lt;/p&gt;
&lt;p&gt;My talk on &lt;a href=&quot;http://terracotta.org&quot;&gt;Terracotta&lt;/a&gt; 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 &lt;span class=&quot;caps&quot;&gt;RAM&lt;/span&gt; and similar services has increased; that people have started to consider writing stateful applications with rich domain models &amp;#8211; which implies another solution to HA and scalability than Oracle &lt;span class=&quot;caps&quot;&gt;RAC&lt;/span&gt; and similar.&lt;/p&gt;
&lt;p&gt;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.&lt;/p&gt;</content>
 </entry>
 
 <entry>
   <title>Interview with Joe Armstrong on Erlang and more...</title>
   <link href="http://jboner.github.com/2007/10/30/interview-with-joe-armstrong-on-erlang-and-more"/>
   <updated>2007-10-30T00:00:00+00:00</updated>
   <id>hhttp://jboner.github.com/2007/10/30/interview-with-joe-armstrong-on-erlang-and-more</id>
   <content type="html">&lt;h1&gt;
&lt;p&gt;Interview with Joe Armstrong on Erlang and more&amp;#8230;&lt;/p&gt;
&lt;/h1&gt;
&lt;p&gt;I had the pleasure of attending Joe Armstrong&amp;#8217;s fantastic presentation at &lt;a href=&quot;http://jaoo.dk/&quot;&gt;&lt;span class=&quot;caps&quot;&gt;JAOO&lt;/span&gt;&lt;/a&gt; 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 &amp;#8211; Joe was incredibly funny, I sometimes felt like had attended a stand-up comedian show.&lt;/p&gt;
&lt;p&gt;Anyway, for all of you that missed Joe&amp;#8217;s talk, &lt;a href=&quot;http://channel9.msdn.com/ShowPost.aspx?PostID=351659#351659&quot;&gt;here is a great interview&lt;/a&gt; with him discussing Erlang&amp;#8217;s (and a functional programmer&amp;#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.&lt;/p&gt;</content>
 </entry>
 
 <entry>
   <title>Clojure: a Lisp-dialect for the JVM - with focus on Functional and Concurrent Programming</title>
   <link href="http://jboner.github.com/2007/10/18/clojure-a-lisp-dialect-for-the-jvm-with-focus-on-functional-and-concurrent-programming"/>
   <updated>2007-10-18T00:00:00+00:00</updated>
   <id>hhttp://jboner.github.com/2007/10/18/clojure-a-lisp-dialect-for-the-jvm-with-focus-on-functional-and-concurrent-programming</id>
   <content type="html">&lt;h1&gt;
&lt;p&gt;Clojure: a Lisp-dialect for the &lt;span class=&quot;caps&quot;&gt;JVM&lt;/span&gt; &amp;#8211; with focus on Functional and Concurrent Programming&lt;/p&gt;
&lt;/h1&gt;
&lt;p&gt;Yesterday, Rich Hickey announced the birth of &lt;a href=&quot;http://clojure.sourceforge.net/&quot;&gt;Clojure&lt;/a&gt; &amp;#8211; a lisp dialect for the &lt;span class=&quot;caps&quot;&gt;JVM&lt;/span&gt;.&lt;/p&gt;
&lt;p&gt;After just a brief look, Clojure is perhaps the most interesting language in the (now fairly large) family of &amp;#8216;dynamic languages for the &lt;span class=&quot;caps&quot;&gt;JVM&lt;/span&gt;&amp;#8217;. It brings the power of Lisp to the &lt;span class=&quot;caps&quot;&gt;JVM&lt;/span&gt; but have made some design decisions that in some ways makes it more interesting than &lt;a href=&quot;http://en.wikipedia.org/wiki/Common_lisp&quot;&gt;&lt;span class=&quot;caps&quot;&gt;ANSI&lt;/span&gt; Common Lisp&lt;/a&gt;. Here are some of the things that I find particularly interesting:&lt;/p&gt;
&lt;ul&gt;
	&lt;li&gt;Allows pure functional programming with immutable state (with immutable data-structures etc.) for side-effect-free code (possible in CL but hard to enforce).&lt;/li&gt;
	&lt;li&gt;&lt;ul&gt;&lt;br /&gt;
Great support for concurrent programming: &lt;br /&gt;
	&lt;li&gt;Immutable state can be freely shared across threads.&lt;/li&gt;
	&lt;li&gt;&lt;a href=&quot;http://en.wikipedia.org/wiki/Software_transactional_memory&quot;&gt;Software Transactional Memory&lt;/a&gt; (&lt;span class=&quot;caps&quot;&gt;STM&lt;/span&gt;) that allows atomic and isolated updates to mutable state (through &amp;#8220;Refs&amp;#8221;) with rollback and retry upon collision.&lt;/li&gt;
	&lt;li&gt;Safe usage of mutable state through thread isolation (using &amp;#8220;Vars&amp;#8221;).&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
	&lt;li&gt;Full Lisp-style macro support and eval.&lt;/li&gt;
	&lt;li&gt;Compiles to &lt;span class=&quot;caps&quot;&gt;JVM&lt;/span&gt; bytecode but still fully dynamic (sounds promising but I don&amp;#8217;t know its actual performance).&lt;/li&gt;
	&lt;li&gt;Excellent integration with Java APIs, with type inference for static compilation of Java &lt;span class=&quot;caps&quot;&gt;API&lt;/span&gt; calls.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Here are some tasters (from the &lt;a href=&quot;http://groups.google.com/group/clojure&quot;&gt;forum&lt;/a&gt;).&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Java integration:&lt;/strong&gt;&lt;br /&gt;
&lt;pre&gt;&lt;br /&gt;
(new java.util.Date)&lt;br /&gt;
=&amp;gt; Wed Oct 17 20:01:38 &lt;span class=&quot;caps&quot;&gt;CEST&lt;/span&gt; 2007&lt;/p&gt;
&lt;p&gt;(. (new java.util.Date) (getTime))&lt;br /&gt;
=&amp;gt; 1192644138751&lt;/p&gt;
&lt;p&gt;(.. System out (println &amp;#8220;This is cool!&amp;#8221;))&lt;br /&gt;
This is cool!&lt;br /&gt;
&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Macros:&lt;/strong&gt;&lt;br /&gt;
&lt;pre&gt;  &lt;br /&gt;
(defmacro time [form]&lt;br /&gt;
  `(let [t0# (. System (currentTimeMillis))&lt;br /&gt;
         res# ~form&lt;br /&gt;
         t1# (. System (currentTimeMillis))]&lt;br /&gt;
    (.. System out (println (strcat &amp;quot;Execution took &amp;quot;&lt;br /&gt;
                                    (/ (- t1# t0#) 1000.0) &amp;quot; s&amp;quot;)))&lt;br /&gt;
    res#))&lt;/p&gt;
&lt;p&gt;Usage:&lt;br /&gt;
(defn factorial [n]&lt;br /&gt;
   (if (&amp;lt; n 2)&lt;br /&gt;
       1&lt;/p&gt;
&lt;ul&gt;
	&lt;li&gt;n (factorial (- n 1)))))&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;(time (factorial 1000))&lt;br /&gt;
=&amp;gt; Execution took 0.012 s&lt;br /&gt;
     40&amp;#8230; &lt;br /&gt;
&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;It is still in beta but if you want to start playing around with it yourself, dive into the &lt;a href=&quot;http://clojure.sourceforge.net/reference/getting_started.html&quot;&gt;docs&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Dreaming on:&lt;/strong&gt;&lt;br /&gt;
Stuff that I would like to see (in order to make it the ultimate playground) are among other things: message-passing concurrency (I don&amp;#8217;t fully believe in &lt;span class=&quot;caps&quot;&gt;STM&lt;/span&gt;&amp;#8230;yet) and declarative pattern matching (from &lt;a href=&quot;http://erlang.org&quot;&gt;Erlang&lt;/a&gt;), implicit currying and laziness (as in &lt;a href=&quot;http://haskell.org&quot;&gt;Haskell&lt;/a&gt;), transparent distribution (as in &lt;a href=&quot;http://www.mozart-oz.org/&quot;&gt;Mozart/Oz&lt;/a&gt; and Erlang) and optional static typing. Some of these can be found in &lt;a href=&quot;http://www.lambdassociates.org/&quot;&gt;Qi&lt;/a&gt;, I just would love to see them on the &lt;span class=&quot;caps&quot;&gt;JVM&lt;/span&gt;.&lt;/p&gt;</content>
 </entry>
 
 <entry>
   <title>Interviewed by Xebia: Part 2</title>
   <link href="http://jboner.github.com/2007/10/10/terracotta-podcast-part-2"/>
   <updated>2007-10-10T00:00:00+00:00</updated>
   <id>hhttp://jboner.github.com/2007/10/10/terracotta-podcast-part-2</id>
   <content type="html">&lt;h1&gt;
&lt;p&gt;Interviewed by Xebia: Part 2&lt;/p&gt;
&lt;/h1&gt;
&lt;p&gt;The second half of an interview that Xebia did with me some months ago have been published.&lt;/p&gt;
&lt;p&gt;In this half which we discuss &lt;a href=&quot;http://terracotta.org&quot;&gt;Terracotta&lt;/a&gt; in more detail, covering Network-Attached Memory and &lt;span class=&quot;caps&quot;&gt;JVM&lt;/span&gt;-level clustering in depth (some stuff that we cover is slightly outdated, but most of it still holds).&lt;/p&gt;
&lt;p&gt;You can find it &lt;a href=&quot;http://podcast.xebia.com/Xebia_Audio_and_Video_Podcast/Entries/2007/10/10_Episode_13_-_Terracotta_Part_2.html&quot;&gt;here&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;If you missed the first part you can read more it &lt;a href=&quot;http://jonasboner.com/2007/09/18/terracotta-podcast-part-1/&quot;&gt;here&lt;/a&gt; (which gives more of an overview).&lt;/p&gt;</content>
 </entry>
 
 <entry>
   <title>Interviewed by Xebia: Part 1</title>
   <link href="http://jboner.github.com/2007/09/18/terracotta-podcast-part-1"/>
   <updated>2007-09-18T00:00:00+00:00</updated>
   <id>hhttp://jboner.github.com/2007/09/18/terracotta-podcast-part-1</id>
   <content type="html">&lt;h1&gt;
&lt;p&gt;Interviewed by Xebia: Part 1&lt;/p&gt;
&lt;/h1&gt;
&lt;p&gt;&lt;a href=&quot;http://xebia.com/&quot;&gt;Xebia&lt;/a&gt; did an interview with me some months ago in which we discuss things like &lt;a href=&quot;http://terracotta.org/&quot;&gt;Terracotta&lt;/a&gt;, scale-out and high-availability for Java, &lt;span class=&quot;caps&quot;&gt;JVM&lt;/span&gt;-level clustering, network-attached memory, distributed and concurrent programming, &lt;span class=&quot;caps&quot;&gt;JEE&lt;/span&gt; best practices etc.&lt;/p&gt;
&lt;p&gt;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.&lt;/p&gt;
&lt;p&gt;You can find the first part &lt;a href=&quot;http://podcast.xebia.com/Xebia_Audio_and_Video_Podcast/Entries/2007/9/19_Episode_11_-_Terracotta_Part_1.html&quot;&gt;here&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;Second part will be published in 2 weeks.&lt;/p&gt;</content>
 </entry>
 
 <entry>
   <title>Article: Distributed Web Continuations with RIFE and Terracotta</title>
   <link href="http://jboner.github.com/2007/08/08/article-distributed-web-continuations-with-rife-and-terracotta"/>
   <updated>2007-08-08T00:00:00+00:00</updated>
   <id>hhttp://jboner.github.com/2007/08/08/article-distributed-web-continuations-with-rife-and-terracotta</id>
   <content type="html">&lt;h1&gt;
&lt;p&gt;Article: Distributed Web Continuations with &lt;span class=&quot;caps&quot;&gt;RIFE&lt;/span&gt; and Terracotta&lt;/p&gt;
&lt;/h1&gt;
&lt;p&gt;My and &lt;a href=&quot;http://rifers.org/blogs/gbevin&quot;&gt;Geert Bevin&amp;#8217;s&lt;/a&gt; article about how to cluster &lt;a href=&quot;http://rifers.org&quot;&gt;RIFE&amp;#8217;s&lt;/a&gt; Web Continuations with &lt;a href=&quot;http://terracotta.org&quot;&gt;Terracotta&lt;/a&gt; has just been &lt;a href=&quot;http://www.artima.com/lejava/articles/distributed_continuations.html&quot;&gt;published on Artima&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;Here is the abstract:&lt;/p&gt;
&lt;blockquote&gt;In this article, we discuss how the &lt;span class=&quot;caps&quot;&gt;RIFE&lt;/span&gt; Web framework helps you become productive and efficient in building conversational Web applications. Productivity with &lt;span class=&quot;caps&quot;&gt;RIFE&lt;/span&gt; is in large part due to RIFE&amp;#8217;s unique approach to Web developmentâ€”its use of continuations for conversational logic, and complete integration of meta-programming to minimize boilerplate code.&lt;br /&gt;
&lt;br /&gt;
We also introduce you to Terracotta and it&amp;#8217;s &lt;span class=&quot;caps&quot;&gt;JVM&lt;/span&gt;-level clustering technology, and show you how Terracotta and &lt;span class=&quot;caps&quot;&gt;RIFE&lt;/span&gt; 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.&lt;/blockquote&gt;
&lt;p&gt;It tries to not only explain but to show you in a pragmatic way how RIFE&amp;#8217;s continuations and Terracotta is a perfect match with their common goal of power with simplicity. But don&amp;#8217;t take my word for it, go on and &lt;a href=&quot;http://www.artima.com/lejava/articles/distributed_continuations.html&quot;&gt;read it yourself&lt;/a&gt;.&lt;/p&gt;</content>
 </entry>
 
 <entry>
   <title>The Power of Sockets and Dynamic Proxies</title>
   <link href="http://jboner.github.com/2007/07/27/the-power-of-sockets-and-dynamic-proxies"/>
   <updated>2007-07-27T00:00:00+00:00</updated>
   <id>hhttp://jboner.github.com/2007/07/27/the-power-of-sockets-and-dynamic-proxies</id>
   <content type="html">&lt;h1&gt;
&lt;p&gt;The Power of Sockets and Dynamic Proxies&lt;/p&gt;
&lt;/h1&gt;
&lt;p&gt;I was going through some of the older parts of my &lt;a href=&quot;http://darcs.net/&quot;&gt;darcs&lt;/a&gt; repository the other day when I stumbled upon a pretty neat and powerful little remoting library that I wrote back in 2001. It is based on &lt;a href=&quot;http://java.sun.com/j2se/1.3/docs/guide/reflection/proxy.html&quot;&gt;dynamic proxies&lt;/a&gt; and plain sockets, almost legacy programming techniques nowadays in a world of &lt;span class=&quot;caps&quot;&gt;BCI&lt;/span&gt; (bytecode instrumentation), &lt;a href=&quot;http://www.eclipse.org/aspectj/&quot;&gt;&lt;span class=&quot;caps&quot;&gt;AOP&lt;/span&gt;&lt;/a&gt;, &lt;a href=&quot;http://java.sun.com/products/ejb/&quot;&gt;EJBs&lt;/a&gt;, &lt;a href=&quot;http://terracotta.org&quot;&gt;Terracotta&lt;/a&gt; etc.&lt;/p&gt;
&lt;p&gt;Anyway, I thought that the implementation is fairly interesting (and useful) and would make good blog post.&lt;/p&gt;
&lt;p&gt;It basically implements a remote proxy, that can either be instantiated by the client or instantiated by the server and sent to the client. In either case, when methods are invoked upon the proxy then they are executed on the server. The communication is socket-based and the server is holding a resizable thread pool that can grow and shrink based on usage. The beauty of using dynamic proxies is that once the proxy has been instantiated, the &lt;a href=&quot;http://en.wikipedia.org/wiki/Remote_procedure_call&quot;&gt;&lt;span class=&quot;caps&quot;&gt;RPC&lt;/span&gt;&lt;/a&gt; is transparent, e.g. pretty much the same as with &lt;a href=&quot;http://java.sun.com/javase/technologies/core/basic/rmi/index.jsp&quot;&gt;&lt;span class=&quot;caps&quot;&gt;RMI&lt;/span&gt;&lt;/a&gt; but without all the stub and skeleton mess. Is a very simple library, not meant as a full &lt;span class=&quot;caps&quot;&gt;RMI&lt;/span&gt; replacement, but does its job pretty well. I have used it among other things for:&lt;/p&gt;
&lt;ul&gt;
	&lt;li&gt;getting a remote &lt;a href=&quot;http://www.hibernate.org/hib_docs/v3/api/org/hibernate/context/package-summary.html&quot;&gt;Hibernate Context&lt;/a&gt; in 5 lines of code&lt;/li&gt;
	&lt;li&gt;the remoting layer in a &lt;a href=&quot;http://java.sun.com/products/jndi/&quot;&gt;&lt;span class=&quot;caps&quot;&gt;JNDI&lt;/span&gt;&lt;/a&gt; implementation&lt;/li&gt;
	&lt;li&gt;remote access to &lt;a href=&quot;http://aspectwerkz.codehaus.org/&quot;&gt;AspectWerkz&lt;/a&gt; aspects and mixins&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;So without further ado, let&amp;#8217;s dive into some code. First, and most importantly, would be to take a look at how the remote proxy can be used from a client&amp;#8217;s perspective.&lt;/p&gt;
&lt;p&gt;This simple unit test is testing (and highlights) two basic features.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Create and use a client side remote proxy (that should create and use a matching instance on the server).&lt;/li&gt;
&lt;li&gt;Let the server create a proxy and send it to the client which uses it.&lt;/li&gt;
&lt;/ul&gt;
&lt;pre name=&quot;code&quot; class=&quot;java&quot;&gt;
public void testSimpleProxy() {

    // 1)
    // creates a new remote proxy for the POJOImpl1 class 
    // which maps to an instance of this class on the server
    RemoteProxy proxy1 = RemoteProxy.createClientProxy(
            new String[]{&quot;test.POJO1&quot;}, // interface(s)
            &quot;test.POJOImpl1&quot;,  // implementation
            &quot;localhost&quot;,       // server IP or hostname
            6663               // server port
    );
    // retrieves the POJOImpl1 instance
    POJO1 pojo1 = (POJO1) proxy1.getInstance();

    // 2)
    // invoke a method on the proxy (executed on the server)
    assertEquals(&quot;POJO1 here&quot;, pojo1.test());

    // 3)
    // retrieve the proxy that is created on the server
    RemoteProxy proxy2 = pojo1.getPOJO2();
    
    // retrieves the POJOImpl2 instance
    POJO2 pojo2 = (POJO2) proxy2.getInstance();

    // 4)
    // invoke a method on the proxy (executed on the server)
    assertEquals(&quot;POJO2 here&quot;, pojo2.test());

    // 5)
    // close the proxies (close() must always be called)
    proxy1.close();
    proxy2.close();
}
&lt;/pre&gt;
&lt;p&gt;That was easy. So much for the client side.&lt;/p&gt;
&lt;p&gt;How can we now create the server that serves these two proxies? The only thing we have to do is to create a &lt;code&gt;RemoteProxyServer&lt;/code&gt; by passing in the class loader that we want to use to instantiate our proxied objects as well as an implementation of the &lt;code&gt;Invoker&lt;/code&gt; interface (which has one single method called &lt;code&gt;invoke&lt;/code&gt;), an interface that gives you the possibility to invoke methods on your proxied objects any way you want. This example simply shows the most basic way of doing it:&lt;/p&gt;
&lt;pre name=&quot;code&quot; class=&quot;java&quot;&gt;
// create a remote proxy server with a simple Invoker impl
RemoteProxyServer remoteProxyServer = new RemoteProxyServer(
        classLoader,
        return new Invoker() {
            public Object invoke(String handle,
                                 String methodName,
                                 Class[] paramTypes,
                                 Object[] args,
                                 Object context) {
                Object result;
                try {
                    Object instance = RemoteProxy.getWrappedInstance(handle);
                    Method method = instance.getClass().getMethod(methodName, paramTypes);
                    result = method.invoke(instance, args);
                } catch (Exception e) {
                    throw new WrappedRuntimeException(e);
                }
                return result;
            }
        };);

// start all server threads
remoteProxyServer.start();
&lt;/pre&gt;
&lt;p&gt;Let&amp;#8217;s now dive into the implementation of the server a little bit. When we invoke &lt;code&gt;remoteProxyServer.start()&lt;/code&gt; then the server starts up X worker threads (managed by a thread pool). The work done by of one of these thread is roughly &amp;#8211; in pseudo code:&lt;/p&gt;
&lt;ul&gt;
	&lt;li&gt;Get the object output and input streams&lt;/li&gt;
	&lt;li&gt;Loop:: read from input stream&lt;ul&gt;&lt;br /&gt;
	&lt;li&gt;if command == &lt;span class=&quot;caps&quot;&gt;CREATE&lt;/span&gt;: create an instance on the server and send a handle to the output stream&lt;/li&gt;
	&lt;li&gt;else if command == &lt;span class=&quot;caps&quot;&gt;INVOKE&lt;/span&gt;: grab the parameters, invoke the method and send the result to the output stream&lt;/li&gt;
	&lt;li&gt;else if command == &lt;span class=&quot;caps&quot;&gt;CLOSE&lt;/span&gt;: exit the thread&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Here are some code excerpts, highlighting the algorithm:&lt;/p&gt;
&lt;pre name=&quot;code&quot; class=&quot;java&quot;&gt;
public void run() {
    try {
        m_socket.setTcpNoDelay(true);
        m_socket.setSoTimeout(m_timeout);
        m_in = new ObjectInputStream(m_socket.getInputStream());
        m_out = new ObjectOutputStream(m_socket.getOutputStream());
    } catch (IOException e) {
        throw new WrappedRuntimeException(e);
    }
    while (m_running) {
        try {
            switch (m_in.read()) {
                case Command.CREATE:
                    handleCreateCommand();
                    break;
                case Command.INVOKE:
                    handleInvocationCommand();
                    break;
                case Command.CLOSE:
                    m_running = false;
                    break;
                default:
                    break;
            }
        } catch (Exception e) {
            close();
            throw new WrappedRuntimeException(e);
        }
    }
    close();
}

private void handleCreateCommand() 
        throws IOException, ClassNotFoundException,
        InstantiationException, IllegalAccessException {
    String className = (String) m_in.readObject();
    Class klass = Class.forName(className, false, m_loader);
    Object instance = klass.newInstance();
    // get a handle to the proxied instance
    String handle = RemoteProxy.wrapInstance(instance);
    m_out.writeObject(handle);
    m_out.flush();
}

private void handleInvocationCommand() 
        throws IOException, ClassNotFoundException {
    Object context = m_in.readObject();
    String handle = (String) m_in.readObject();
    String methodName = (String) m_in.readObject();
    Class[] paramTypes = (Class[]) m_in.readObject();
    Object[] args = (Object[]) m_in.readObject();
    Object result = null;
    try {
        result = m_invoker.invoke(handle, methodName, paramTypes, args, context);
    } catch (Exception e) {
        result = e; // pass the exception to the client
    }
    m_out.writeObject(result);
    m_out.flush();
}
&lt;/pre&gt;
&lt;p&gt;If the client asks for a server created proxy, then the server would create it like this: &lt;code&gt;RemoteProxy proxy = RemoteProxy.createServerProxy(myInstance, &quot;localhost&quot;, 6663);&lt;/code&gt;, and write it to the object output stream &amp;#8211; or even more simple have one of the already proxied objects create it (on the server) and return it (to the client).&lt;/p&gt;
&lt;p&gt;This is pretty much the whole server. But if you&amp;#8217;re still with me, I&amp;#8217;m sure you&amp;#8217;re eager to know what the &lt;code&gt;RemoteProxy&lt;/code&gt; looks like. Well, here are some of the more interesting parts of its internals:&lt;/p&gt;
&lt;pre name=&quot;code&quot; class=&quot;java&quot;&gt;
public class RemoteProxy implements InvocationHandler, Serializable {

    ... // constructors and field declarations are omitted

    /**
     * Creates a new proxy to a class. To be used on the client side to 
     * create a new proxy to an object.
     *
     * @param interfaces the name of the interfaces for the object to create the proxy for
     * @param impl       the name of the the impl class to create the proxy for
     * @param address    the address to connect to
     * @param port       the port to connect to
     * @param ctx        the context carrying the users principals and credentials
     * @param loader     the class loader to use for instantiating the proxy
     * @return the new remote proxy instance
     */
     public static RemoteProxy createClientProxy(String[] interfaces,
                                                String impl,
                                                String address,
                                                int port,
                                                Object context,
                                                ClassLoader loader) {
        return new RemoteProxy(interfaces, impl, address, port, context, loader);
    }

    ... // some factory methods are omitted

    /**
     * Look up and retrives a proxy to an object from the server.
     *
     * @return the proxy instance
     */
    public Object getInstance() {
        if (m_proxy != null) {
            return m_proxy;
        }
        if (m_loader == null) {
            m_loader = Thread.currentThread().getContextClassLoader();
        }
        try {
            m_socket = new Socket(InetAddress.getByName(m_address), m_port);
            m_socket.setTcpNoDelay(true);
            m_out = new ObjectOutputStream(m_socket.getOutputStream());
            m_in = new ObjectInputStream(m_socket.getInputStream());
        } catch (Exception e) {
            throw new WrappedRuntimeException(e);
        }
        if (m_handle == null) {
            // is a client side proxy
            if (m_targetInterfaceNames == null) {
                throw new IllegalStateException(&quot;interface class name can not be null&quot;);
            }
            if (m_targetImplName == null) {
                throw new IllegalStateException(&quot;implementation class name can not be null&quot;);
            }
            try {
                // create a new instance on the server and get the handle to it in return
                m_out.write(Command.CREATE);
                m_out.writeObject(m_targetImplName);
                m_out.flush();
                m_handle = (String) m_in.readObject();
                m_targetInterfaces = new Class[m_targetInterfaceNames.length];
                for (int i = 0; i &amp;lt; m_targetInterfaceNames.length; i++) {
                    try {
                        m_targetInterfaces[i] = Class.forName(
                            m_targetInterfaceNames[i], false, m_loader
                        );
                    } catch (ClassNotFoundException e) {
                        throw new WrappedRuntimeException(e);
                    }
                }
            } catch (Exception e) {
                throw new WrappedRuntimeException(e);
            }
        }

        // create and return a regular Java Dynamic Proxy
        m_proxy = Proxy.newProxyInstance(m_loader, m_targetInterfaces, this);
        return m_proxy;
    }

    /**
     * This method is invoked automatically by the proxy. Should not be called directly.
     *
     * @param proxy  the proxy instance that the method was invoked on
     * @param method the Method instance corresponding to the interface method invoked 
     *               on the proxy instance.
     * @param args   an array of objects containing the values of the arguments passed 
     *               in the method invocation on the proxy instance.
     * @return the value to return from the method invocation on the proxy instance.
     */
    public Object invoke(Object proxy, Method method, Object[] args) {
        try {
            m_out.write(Command.INVOKE);
            m_out.writeObject(m_context);
            m_out.writeObject(m_handle);
            m_out.writeObject(method.getName());
            m_out.writeObject(method.getParameterTypes());
            m_out.writeObject(args);
            m_out.flush();
            final Object response = m_in.readObject();
            if (response instanceof Exception) {
                throw (Exception) response;
            }
            return response;
        } catch (Exception e) {
            throw new WrappedRuntimeException(e);
        }
    }

    /**
     * Closes the proxy and the connection to the server.
     */
    public void close() {
        try {
            m_out.write(Command.CLOSE);
            m_out.flush();
            m_out.close();
            m_in.close();
            m_socket.close();
        } catch (IOException e) {
            throw new WrappedRuntimeException(e);
        }
    }
}
&lt;/pre&gt;
&lt;p&gt;That&amp;#8217;s all there is to it.&lt;/p&gt;
&lt;p&gt;Ok, perhaps not as powerful and flexible as &lt;a href=&quot;http://www.springframework.org/docs/reference/remoting.html&quot;&gt;Spring Remoting&lt;/a&gt;, &lt;a href=&quot;http://java.sun.com/products/ejb/&quot;&gt;EJBs&lt;/a&gt;, &lt;a href=&quot;http://java.sun.com/javase/technologies/core/basic/rmi/index.jsp&quot;&gt;&lt;span class=&quot;caps&quot;&gt;RMI&lt;/span&gt;&lt;/a&gt; or &lt;a href=&quot;http://terracotta.org&quot;&gt;Terracotta&amp;#8217;s Network-Attached Memory&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;But plain sockets and DPs are not so bad either. Or is it just me that&amp;#8217;s being a bit sentimental? At least it was pretty cool stuff back in 2001 :-)&lt;/p&gt;
&lt;p&gt;If you want to take a look at the code or are thinking of using it then you can check it out  using this command (if you don&amp;#8217;t have darcs installed you can get it &lt;a href=&quot;http://darcs.net/&quot;&gt;here&lt;/a&gt;):&lt;/p&gt;
&lt;p&gt;&lt;code&gt;darcs get http://jonasboner.com/darcs/remoteproxy&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;Happy hacking. &lt;br /&gt;
&lt;/pre&gt;&lt;/p&gt;</content>
 </entry>
 
 <entry>
   <title>Love What You Do</title>
   <link href="http://jboner.github.com/2007/07/04/love-what-you-do"/>
   <updated>2007-07-04T00:00:00+00:00</updated>
   <id>hhttp://jboner.github.com/2007/07/04/love-what-you-do</id>
   <content type="html">&lt;h1&gt;
&lt;p&gt;Love What You Do&lt;/p&gt;
&lt;/h1&gt;
&lt;p&gt;I was in Rome, Italy some weeks ago. After strolling around in the old town for a couple hours I felt the need to grab a bite. So I stopped at a cafÃ©, sat down and decided to order a pasta fettuccine and a glass of white.&lt;/p&gt;
&lt;p&gt;The waiter, perhaps in his 50s, comes up to me and not only takes my order but treats me like a celebrity, like I was the only thing that mattered. It is hard to put the finger on it, but his charisma, the way he sets the table, folds the napkin, lays out the silverware, uses subtle gestures and facial expressions is close to perfection &amp;#8211; truly a master of his craft. It a matter of minimalism and style, the way he pays attention to the tiniest detail. It really shines through that he takes pride in, and loves, his job.&lt;/p&gt;
&lt;p&gt;We all have a lesson to learn here. If you feel stuck and trapped at work, not doing things you are passionate and care about, then I think you have two options:&lt;/p&gt;
&lt;ul&gt;
	&lt;li&gt;Try to change your mind set. Care about your work. Set high bars, strive for perfection, pay attention to the details. Never settle &amp;#8212; constantly try improve and challenge the way you work and think. Take pride in what you do and you will (hopefully) not only learn to love it, but people will notice and you will make a difference.&lt;/li&gt;
	&lt;li&gt;Or&amp;#8230;quit and find something else to do.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Life is too short to spend 9-5 doing stuff you don&amp;#8217;t love and are passionate about. Steve Jobs puts it very well in his Stanford University Commencement speech (&lt;a href=&quot;http://youtube.com/watch?v=D1R-jKKp3NA&quot;&gt;video&lt;/a&gt; &amp;#8211; &lt;a href=&quot;http://news-service.stanford.edu/news/2005/june15/jobs-061505.html&quot;&gt;transcription&lt;/a&gt;):&lt;/p&gt;
&lt;blockquote&gt;You&amp;#8217;ve got to find what you love&amp;#8230;.for the past 33 years, I have looked in the mirror every morning and asked myself: &amp;#8220;If today were the last day of my life, would I want to do what I am about to do today?&amp;#8221; And whenever the answer has been &amp;#8220;No&amp;#8221; for too many days in a row, I know I need to change something&amp;#8230;almost everything &amp;#8211; all external expectations, all pride, all fear of embarrassment or failure &amp;#8211; these things just fall away in the face of death, leaving only what is truly important. Remembering that you are going to die is the best way I know to avoid the trap of thinking you have something to lose. You are already naked. There is no reason not to follow your heart.&lt;/blockquote&gt;
&lt;p&gt;Love what you do.&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>How To Keep Your Creative Flow</title>
   <link href="http://jboner.github.com/2007/06/28/how-to-keep-your-creative-flow"/>
   <updated>2007-06-28T00:00:00+00:00</updated>
   <id>hhttp://jboner.github.com/2007/06/28/how-to-keep-your-creative-flow</id>
   <content type="html">&lt;h1&gt;
&lt;p&gt;How To Keep Your Creative Flow&lt;/p&gt;
&lt;/h1&gt;
&lt;p&gt;I recently saw an interview with &lt;a href=&quot;http://en.wikipedia.org/wiki/Jan_Guillou&quot;&gt;Jan Guillou&lt;/a&gt;, one of the most successful and creative authors in Sweden. He had some interesting thoughts about creativity that I think applies to all pretty much an creative craft.&lt;/p&gt;
&lt;p&gt;He is extremely structured and always does his writing in a predefined time box, when the time is up (triggered by an alarm clock or similar) he just stops typing, right in the middle of the sentence he was just typing.&lt;/p&gt;
&lt;p&gt;I think there are two lessons learned here. I am myself a big fan of using time boxing to boost creativity and to force myself to eliminate the not so important stuff and focus on the essence, the things that will lead to actual progress. This holds both for writing, coding and meetings.&lt;/p&gt;
&lt;p&gt;The second lesson is to stop right in the middle of an unfinished task. Doing that will help you to: &lt;br /&gt;
&lt;ul&gt;&lt;br /&gt;
	&lt;li&gt;know exactly what you should do next (leaves no room for writer&amp;#8217;s block or procrastination)&lt;/p&gt;
&lt;/li&gt;
	&lt;li&gt;keep the context of what you just worked on and more easily &amp;#8220;boot the system&amp;#8221; and get into the creative flow that you had when you stopped&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;It reminds me of an advice &lt;a href=&quot;http://en.wikipedia.org/wiki/Kent_Beck&quot;&gt;Kent Beck&lt;/a&gt; gave in his book &lt;a href=&quot;http://www.amazon.com/Test-Driven-Development-Addison-Wesley-Signature/dp/0321146530&quot;&gt;Test-Drived Development&lt;/a&gt;: In which he states that you should always stop coding with a failing test. This is the exact same idea; knowing what to start with when you come in to work the next morning, more easily loading in the &amp;#8220;context into &lt;span class=&quot;caps&quot;&gt;RAM&lt;/span&gt;&amp;#8221; and forcing your self to become productive immediately.&lt;/p&gt;
&lt;p&gt;Another, fairly controversial comment that I remember from the interview with Jan Guillou was:&lt;/p&gt;
&lt;blockquote&gt;Inspiration is for amateurs.&lt;/blockquote&gt;

&lt;p&gt;Although a bit arrogant, I think it is true and something that holds for most creative crafts. Inspiration is overrated, what matters is perseverance and creative talent (in that order). If you just start with whatever you are about to do, then inspiration will usually follow.&lt;/p&gt;</content>
 </entry>
 
 <entry>
   <title>Invert Your Database Caching Dependency for Extreme Scalability</title>
   <link href="http://jboner.github.com/2007/06/27/invert-your-database-caching-dependency-for-extreme-scalability"/>
   <updated>2007-06-27T00:00:00+00:00</updated>
   <id>hhttp://jboner.github.com/2007/06/27/invert-your-database-caching-dependency-for-extreme-scalability</id>
   <content type="html">&lt;h1&gt;
&lt;p&gt;Invert Your Database Caching Dependency for Extreme Scalability&lt;/p&gt;
&lt;/h1&gt;
&lt;p&gt;Today most applications rely on a relational database (&lt;span class=&quot;caps&quot;&gt;RDBMS&lt;/span&gt;) as its primary Service of Record (SoR) &amp;#8212; the master copy of the data. But in order to minimize latencies in response time (from the database transaction roundtrips), most applications uses some sort of database cache in order to try to keep the data close to its processing context.&lt;/p&gt;
&lt;p&gt;Database caches are most often seen deployed as the second-level cache in O/R mapping tools such as Hibernate or TopLink, but custom-built or general-purpose caches, like Ehcache and OScache, are also common. This architecture and design is proven, sound and has turned into the de facto standard way of building enterprise applications.&lt;/p&gt;
&lt;p&gt;But does it really solve the problem? First, the cache can only help us with read-mostly data and second, we still have to struggle with the object/relational impedance mismatch.&lt;/p&gt;
&lt;p&gt;The first step is to distinguish between temporary/intermediate/transient data and business-critical data. Where the former is the data that is needed/used in the process of doing a computation or keeping a conversation with the client etc., while the latter is the result of competition or the outcome of the client conversation (f.e. a completed order form). The latter naturally belongs in a &lt;span class=&quot;caps&quot;&gt;RDBMS&lt;/span&gt; (for reporting, billing etc.) while the former is best kept persisted in memory (needed to for H/A) in some sort of cache (preferably &lt;a href=&quot;http://terracotta.org/confluence/display/orgsite/WebSearchCacheLanding&quot;&gt;backed up by Terracotta&lt;/a&gt;) &amp;#8212; keeping the data close to its processing context. Unfortunately, many developers fail to do this distinction and end up shoveling everything down in the &lt;span class=&quot;caps&quot;&gt;RDBMS&lt;/span&gt;, with a high latencies, bad throughput and more complicated and hard to maintain software as the result. So far no news.&lt;/p&gt;
&lt;p&gt;But let me now propose an, in some ways, new and radical solution: &lt;strong&gt;Invert the &lt;span class=&quot;caps&quot;&gt;RDBMS&lt;/span&gt;-Cache dependency&lt;/strong&gt;.&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;Let the in-memory cache become the master SoR &amp;#8212; which is persisted in memory using an appliances-like infrastructure service &amp;#8212; like &lt;a href=&quot;http://terracotta.org&quot;&gt;Terracotta&amp;#8217;s&lt;/a&gt; &lt;a href=&quot;http://www.ddj.com/dept/java/199703478&quot;&gt;Network-Attached Memory (&lt;span class=&quot;caps&quot;&gt;NAM&lt;/span&gt;)&lt;/a&gt;.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Keep a transaction log, which logs every modification to the in-memory data.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Let a low priority thread asynchronously process the transaction log every X minutes/seconds and serially execute the database transactions.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Treat the &lt;span class=&quot;caps&quot;&gt;RDBMS&lt;/span&gt; as an &amp;#8220;offline&amp;#8221; data snapshot on which you can run the usual reporting and data mining tools â€“ needed for billing, weekly reports etc.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Since your SoR is now effectively persisted &amp;#8220;on the network&amp;#8221;, instead of in the &lt;span class=&quot;caps&quot;&gt;RDBMS&lt;/span&gt;, you can without any further effort add as many nodes to process the data as you want, e.g. scale-out your application and to ensure high-availability.&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;I am not arguing that this is a general-purpose solution, and there are certainly cases where it will be very hard or impossible to implement due to either political or practical constraints (like need for real-time access to data in the &lt;span class=&quot;caps&quot;&gt;RDBMS&lt;/span&gt; etc.). But it fits many more use-cases than you might think. Mainly requires a bit of courage and out-of-the-box thinking.&lt;/p&gt;
&lt;p&gt;For those that have doubt; this is not vaporware but actually how some of Terracotta&amp;#8217;s customers have used Terracotta&amp;#8217;s &lt;span class=&quot;caps&quot;&gt;NAM&lt;/span&gt; to solve their performance and scalability problems, and have been able to reach extreme scalability while keeping the simplicity of their &lt;span class=&quot;caps&quot;&gt;POJO&lt;/span&gt; model intact.&lt;/p&gt;</content>
 </entry>
 
 <entry>
   <title>Terracotta War Story:How We Clustered RIFE  </title>
   <link href="http://jboner.github.com/2007/05/29/terracotta-war-storyhow-we-clustered-rife"/>
   <updated>2007-05-29T00:00:00+00:00</updated>
   <id>hhttp://jboner.github.com/2007/05/29/terracotta-war-storyhow-we-clustered-rife</id>
   <content type="html">&lt;h1&gt;
&lt;p&gt;Terracotta War Story:How We Clustered &lt;span class=&quot;caps&quot;&gt;RIFE&lt;/span&gt;&lt;/p&gt;
&lt;/h1&gt;
&lt;p&gt;This is the story how &lt;a href=&quot;http://rifers.org/blogs/gbevin&quot;&gt;Geert Bevin&lt;/a&gt; and I clustered &lt;a href=&quot;http://rifers.org&quot;&gt;&lt;span class=&quot;caps&quot;&gt;RIFE&lt;/span&gt;&lt;/a&gt; with &lt;a href=&quot;http://terracotta.org&quot;&gt;Terracotta&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;The purpose of this blog post is to give an example of how &lt;span class=&quot;caps&quot;&gt;JVM&lt;/span&gt;-level clustering of a nontrivial application can be done. Actually, clustering &lt;span class=&quot;caps&quot;&gt;RIFE&lt;/span&gt; turned out to be much more challenging than we initially thought, mainly due to its extreme dynamicity, and I hope that our insights and solutions can be of value to someone trying to do a similar thing.&lt;/p&gt;
&lt;p&gt;The first thing that we had to do in order to cluster &lt;span class=&quot;caps&quot;&gt;RIFE&lt;/span&gt; with Terracotta was to cluster its &lt;a href=&quot;http://en.wikipedia.org/wiki/Continuation&quot;&gt;continuations&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;RIFEâ€™s continuations aims to make support for continuations in pure Java available as a general-purpose library, a library that can and is being used by other frameworks and tools. It is implementing continuations by allowing it to use regular Java methods to mark the execution flow to be suspended or resumed. It is using bytecode instrumentation (based on the &lt;a href=&quot;http://asm.objectweb.org/&quot;&gt;&lt;span class=&quot;caps&quot;&gt;ASM&lt;/span&gt; bytecode library&lt;/a&gt;) to generate code and redefine classes in order to implement this in the most effective way possible. It is not relying on Java serialization but is instead breaking down the object graph into primitives and stores the actual data on the execution stack (similar to the approach taken by Terracotta). This makes it very nonintrusive and simple to use, since you can work with POJOs with minimal interference from the continuations framework itself. The continuations are linked together in a tree structure which allows you to traverse to the different execution steps the way you want. This means that you can actually step back (and forward) in time, which is a very neat way of solving the browser back button problem &amp;#8211; if used in the context of web applications.&lt;/p&gt;
&lt;p&gt;Internally &lt;span class=&quot;caps&quot;&gt;RIFE&lt;/span&gt; is storing its continuations in a regular &lt;code&gt;java.util.HashMap&lt;/code&gt;:&lt;/p&gt;
&lt;pre name=&quot;code&quot; class=&quot;java&quot;&gt;
private final Map mContexts = new HashMap();
&lt;/pre&gt;

&lt;p&gt;This means that this map is a data structure that holds on to all the state that we want to cluster, something that makes it a perfect fit for becoming a Terracotta &amp;#8216;root&amp;#8217;, e.g. the top-level object in a shared/clustered object graph. So, we simply had to define the fully qualified name of the field that held on to this map as the &amp;#8216;root&amp;#8217; in our Terracotta configuration:&lt;/p&gt;
&lt;pre name=&quot;code&quot; class=&quot;java&quot;&gt;
&amp;lt;root&amp;gt;
  &amp;lt;field-name&amp;gt;
    com.uwyn.rife.continuations.ContinuationManager.mContexts
  &amp;lt;/field-name&amp;gt;
  &amp;lt;root-name&amp;gt;mContexts&amp;lt;/root-name&amp;gt;
&amp;lt;/root&amp;gt;
&lt;/pre&gt;
&lt;p&gt;That was quite simple. However, initially &lt;span class=&quot;caps&quot;&gt;RIFE&lt;/span&gt; was designed to only access this map using a single thread, which allowed it (for performance reasons) to choose to not make it thread safe. Unfortunately, this is an assumption that does not hold when we are clustering the map with Terracotta since then you will have, not multi-threaded, but concurrent access from multiple JVMs. The promise of Terracotta is that it maintains the semantics of Java across the cluster which means that we need to guard our data correctly for concurrent access using Javaâ€™s concurrency primitives or abstractions, primitives and abstractions that Terracotta will maintain the semantics of across the cluster. In short, Terracotta requires you to write thread-safe code. What this boiled down to in practice was that we had to make the access and modifications to the map itself and all the continuations in the map guarded (using synchronized blocks). The simplest option would have been to swap the regular &lt;code&gt;HashMap&lt;/code&gt; to a &lt;code&gt;java.util.concurrent.ConcurrentHashMap&lt;/code&gt;, but since &lt;span class=&quot;caps&quot;&gt;RIFE&lt;/span&gt; needs backward compatibility with Java 1.4, that was not an option. So after making the code thread-safe we could see the continuations effectively clustered by Terracotta. (Some readers might see that what we have actually done here is a simple way of cross-&lt;span class=&quot;caps&quot;&gt;JVM&lt;/span&gt; thread migration.)&lt;/p&gt;
&lt;p&gt;This works fine if all the clustered classes are loaded through Java&amp;#8217;s system class loader (or the boot or ext class loaders) since that is a class loader that Terracotta knows about and can identify uniquely. But unfortunately, this is not the case if we are running in an application server since they are normally using a more or less complex hierarchy of class loaders that are specific to the application server itself. The reason why Terracotta needs to be able to uniquely define a class loader is that it needs a way of, at any point in time and on an arbitrary cluster-node, retrieve the class loader instance that has loaded a specific class in order to maintain object identity across the cluster. In the case of &lt;span class=&quot;caps&quot;&gt;RIFE&lt;/span&gt; we had two class loaders &amp;#8211; &lt;code&gt;EngineClassLoader&lt;/code&gt; and &lt;code&gt;TemplateClassLoader&lt;/code&gt; &amp;#8211; that we had to enhance and make implement the &lt;code&gt;NamedClassLoader&lt;/code&gt; interface in Terracotta:&lt;/p&gt;
&lt;pre name=&quot;code&quot; class=&quot;java&quot;&gt;
public interface NamedClassLoader {
  public static final String CLASS = 
      &quot;com/tc/object/loaders/NamedClassLoader&quot;;
  public static final String TYPE  = &quot;L&quot; + CLASS + &quot;;&quot;;
  public String __tc_getClassLoaderName();
  public void __tc_setClassLoaderName(String name);
}
&lt;/pre&gt;
&lt;p&gt;We also had to add code for the &lt;span class=&quot;caps&quot;&gt;RIFE&lt;/span&gt; class loaders to register themselves in the Terracotta runtime:&lt;/p&gt;
&lt;pre name=&quot;code&quot; class=&quot;java&quot;&gt;
public EngineClassLoader(ClassLoader parent) {
  com.tc.object.bytecode.hook.impl.ClassProcessorHelper.
      registerGlobalLoader(this);
}
&lt;/pre&gt;
&lt;p&gt;Now for the biggest challenge; cluster the &lt;span class=&quot;caps&quot;&gt;RIFE&lt;/span&gt; template engine. &lt;span class=&quot;caps&quot;&gt;RIFE&lt;/span&gt; is relying heavily on on-the-fly class generation using bytecode instrumentation. This is extremely powerful, since it allows the user to to find what he wants to do in metadata (with heavy reliance on defaults) and then using that metadata together with  templates to generate the actual classes dynamically on demand.&lt;/p&gt;
&lt;p&gt;This extreme dynamicity imposed a lot of interesting challenges in terms of clustering. For example, if a user request comes in to a node (think application server or &lt;span class=&quot;caps&quot;&gt;JVM&lt;/span&gt;), let&amp;#8217;s call it Node1, and asks for for example the Order form. Then, if form does not yet exist, the template engine will do its job of introspecting the metadata and generate this class on-the-fly for us before it continues serving the user. Now imagine a scenario where Node1 crashes and the load balancer redirects the user to another node, let&amp;#8217;s call it Node2. Then Terracotta will do its job of loading the Order class and instantiate a new Order instance before it pages in the state from that same instance on Node1. The problem is that this class is not available on Node2, it was generated at runtime on Node1. So we end up getting a &lt;code&gt;ClassNotFoundException&lt;/code&gt; on Node2. Hmm, too bad. What to do about it?&lt;/p&gt;
&lt;p&gt;The solution turned out to be quite simple. First we created an interface called &lt;code&gt;BytecodeProvider&lt;/code&gt;:&lt;/p&gt;
&lt;pre name=&quot;code&quot; class=&quot;java&quot;&gt;
public interface BytecodeProvider {
  /**
   * Returns the bytecode for a class with the name specified.
   *
   * @param className the name of the class who&#39;s bytecode 
   *             is missing
   * @return the bytecode for the class or NULL if bytecode is 
   *             not in the repository
   */
  public byte[] __tc_getBytecodeForClass(String className);
}
&lt;/pre&gt;
&lt;p&gt;Then we modified RIFE&amp;#8217;s &lt;code&gt;TemplateClassLoader&lt;/code&gt;, which is the class that generates the templates in &lt;span class=&quot;caps&quot;&gt;RIFE&lt;/span&gt;, to implement this interface:&lt;/p&gt;
&lt;pre name=&quot;code&quot; class=&quot;java&quot;&gt;â€¦
public byte[] __tc_getBytecodeForClass(final String className) {
  if (null == mBytecodeRepository) {
    return null;
  }		
  synchronized (mBytecodeRepository) {
    return mBytecodeRepository.get(getKeyFor(className));  
  }			    
}
â€¦
&lt;/pre&gt;
&lt;p&gt;We also added a code snippet in the the Terracotta runtime to &amp;#8211; in the case of a &lt;code&gt;ClassNotFoundException&lt;/code&gt; &amp;#8211; check if the class loader it tried to use to load a specific class implements this interface, and if it does use the &lt;code&gt;__tc_getBytecodeForClass(String className)&lt;/code&gt; method to retrieve the bytes for the class, load it and go on from there:&lt;/p&gt;
&lt;pre name=&quot;code&quot; class=&quot;java&quot;&gt;â€¦
try {
  return Class.forName(className, false, loader);
} catch (ClassNotFoundException e) {
  if (loader instanceof BytecodeProvider) {
    BytecodeProvider provider = (BytecodeProvider) loader;
    byte[] bytes = provider.__tc_getBytecodeForClass(className);
    if (bytes != null &amp;amp;&amp;amp; bytes.length != 0) { 
      return AsmHelper.defineClass(loader, bytes, className); 
    }
  }
  throw e;
}
â€¦
&lt;/pre&gt;
&lt;p&gt;So far so good. But what about transferring the bytecode for one node to another, e.g. make it available to any node that needs it? Actually, this was fairly easily solved by using Terracotta itself. We added a &lt;code&gt;HashMap&lt;/code&gt; in the implementor of the &lt;code&gt;BytecodeProvider&lt;/code&gt; interface, e.g. the &lt;code&gt;TemplateClassLoader&lt;/code&gt;, and made sure that as soon as a new class is generated on-the-fly, it is added to the map. After that, the only thing we had to do was to define this map as a shared Terracotta &amp;#8216;root&amp;#8217; and we had a cluster wide bytecode repository ready to be used:&lt;/p&gt;
&lt;pre name=&quot;code&quot; class=&quot;java&quot;&gt;
&amp;lt;root&amp;gt;
  &amp;lt;field-name&amp;gt;
    com.uwyn.rife.template.TemplateClassLoader.mBytecodeRepository
  &amp;lt;/field-name&amp;gt;
  &amp;lt;root-name&amp;gt;mBytecodeRepository&amp;lt;/root-name&amp;gt;
&amp;lt;/root&amp;gt;
&lt;/pre&gt;
&lt;p&gt;Ok, weâ€™re not really done yet, but close. There are some nitty-gritty details that we had to add to the configuration file in order to let Terracotta do its work. First a hint to use auto-locking (for details on lock configuration, see the &lt;a href=&quot;http://terracotta.org&quot;&gt;official documention&lt;/a&gt;):&lt;/p&gt;
&lt;pre name=&quot;code&quot; class=&quot;java&quot;&gt; 
 &amp;lt;locks&amp;gt;
   &amp;lt;autolock&amp;gt;
     &amp;lt;method-expression&amp;gt;* *..*.*(..)&amp;lt;/method-expression&amp;gt;
     &amp;lt;lock-level&amp;gt;write&amp;lt;/lock-level&amp;gt;
   &amp;lt;/autolock&amp;gt;
 &amp;lt;/locks&amp;gt;
&lt;/pre&gt;
&lt;p&gt;Then we also had to make shure that didn&amp;#8217;t cluster more than we wanted to, e.g. that we cut the object graph at the appropriate places. This was done by declaring specific fields as &amp;#8216;transient&amp;#8217;:&lt;/p&gt;
&lt;pre name=&quot;code&quot; class=&quot;java&quot;&gt;
&amp;lt;transient-fields&amp;gt;
  &amp;lt;field-name&amp;gt;com.uwyn.rife.continuations.ContinuationManager.mRandom&amp;lt;/field-name&amp;gt;
  &amp;lt;field-name&amp;gt;com.uwyn.rife.engine.Gate.mInitException&amp;lt;/field-name&amp;gt;
  &amp;lt;field-name&amp;gt;com.uwyn.rife.engine.RequestState.mResponse&amp;lt;/field-name&amp;gt;
  &amp;lt;field-name&amp;gt;com.uwyn.rife.engine.RequestState.mInitConfig&amp;lt;/field-name&amp;gt;
  &amp;lt;field-name&amp;gt;com.uwyn.rife.engine.ElementContext.mResponse&amp;lt;/field-name&amp;gt;
  &amp;lt;field-name&amp;gt;com.uwyn.rife.engine.Site$SiteData.mResourceModificationTimes&amp;lt;/field-name&amp;gt;
  &amp;lt;field-name&amp;gt;com.uwyn.rife.engine.Site$SiteData.mUrls&amp;lt;/field-name&amp;gt;
  &amp;lt;field-name&amp;gt;com.uwyn.rife.servlet.RifeFilter.mClassloader&amp;lt;/field-name&amp;gt;
  &amp;lt;field-name&amp;gt;com.uwyn.rife.servlet.HttpResponse.mResponse&amp;lt;/field-name&amp;gt;
  &amp;lt;field-name&amp;gt;com.uwyn.rife.servlet.HttpRequest.mHttpServletRequest&amp;lt;/field-name&amp;gt;
  &amp;lt;field-name&amp;gt;com.uwyn.rife.template.InternalString.mBytesValue_US_ASCII&amp;lt;/field-name&amp;gt;
  &amp;lt;field-name&amp;gt;com.uwyn.rife.template.InternalString.mBytesValue_ISO_8859_1&amp;lt;/field-name&amp;gt;
  &amp;lt;field-name&amp;gt;com.uwyn.rife.template.InternalString.mBytesValue_UTF_8&amp;lt;/field-name&amp;gt;
  &amp;lt;field-name&amp;gt;com.uwyn.rife.template.InternalString.mBytesValue_UTF_16&amp;lt;/field-name&amp;gt;
  &amp;lt;field-name&amp;gt;com.uwyn.rife.template.InternalString.mBytesValue_UTF_16BE&amp;lt;/field-name&amp;gt;
  &amp;lt;field-name&amp;gt;com.uwyn.rife.template.InternalString.mBytesValue_UTF_16LE&amp;lt;/field-name&amp;gt;
&amp;lt;/transient-fields&amp;gt;
&lt;/pre&gt;
&lt;p&gt;A bit ugly, but that is about it.&lt;/p&gt;
&lt;p&gt;Finally, we wrapped everything up in a Terracotta Configuration Module which means that the only thing you need to do in order to cluster &lt;span class=&quot;caps&quot;&gt;RIFE&lt;/span&gt; is to write a Terracotta configuration file that looks like this:&lt;/p&gt;
&lt;pre name=&quot;code&quot; class=&quot;java&quot;&gt;
&amp;lt;tc:tc-config xmlns:tc=&quot;http://www.terracotta.org/config&quot;&amp;gt;
  &amp;lt;!-- ...system and server stuff... --&amp;gt;
  &amp;lt;clients&amp;gt;
    &amp;lt;modules&amp;gt;
      &amp;lt;module name=&quot;clustered-rife-1.6.0&quot; version=&quot;1.0.0&quot;/&amp;gt;
    &amp;lt;/modules&amp;gt;
  &amp;lt;/client&amp;gt;
&amp;lt;/tc:tc-config&amp;gt;
&lt;/pre&gt;
&lt;p&gt;That was a long story. I hoped that you learned something along the way. If not, at least try it out and enjoy the outcome of our efforts &amp;#8211; it is actually quite a breeze to use.&lt;/p&gt;</content>
 </entry>
 
 <entry>
   <title>Distributed Continuations for Java: RIFE and Open Terracotta</title>
   <link href="http://jboner.github.com/2007/04/16/distributed-continuations-for-java-rife-and-open-terracotta"/>
   <updated>2007-04-16T00:00:00+00:00</updated>
   <id>hhttp://jboner.github.com/2007/04/16/distributed-continuations-for-java-rife-and-open-terracotta</id>
   <content type="html">&lt;h1&gt;
&lt;p&gt;Distributed Continuations for Java: &lt;span class=&quot;caps&quot;&gt;RIFE&lt;/span&gt; and Open Terracotta&lt;/p&gt;
&lt;/h1&gt;
&lt;p&gt;The last couple of years we have seen a new generation of Web frameworks with the ability to keep and maintain user specific conversational (stateful) state emerge in the enterprise. Among all these, I think that &lt;a href=&quot;http://rifers.org/&quot;&gt;&lt;span class=&quot;caps&quot;&gt;RIFE&lt;/span&gt;&lt;/a&gt; is one of the most interesting ones. It has a rich and coherent model for rapid development of Web applications. It also has its unique concept of continuations which provides a both rich, simplistic and intuitive programming model for implementing conversational workflows &amp;#8211; a concept I think is applicable to a wide range of use-cases and technology areas, even outside Web development.&lt;/p&gt;
&lt;p&gt;But the question is, how can we scale-out and ensure high-availability of these continuation based applications &amp;#8211; and how can we do that while preserving its simplicity and semantics? It is here the recent collaboration between &lt;span class=&quot;caps&quot;&gt;RIFE&lt;/span&gt; and &lt;a href=&quot;http://terracotta.org/&quot;&gt;Open Terracotta&lt;/a&gt; really shines. Open Terracotta does not force you to choose between simplicity and scale-out, but its &lt;span class=&quot;caps&quot;&gt;JVM&lt;/span&gt;-level clustering allows &lt;span class=&quot;caps&quot;&gt;RIFE&lt;/span&gt; applications to remain simple and pure &lt;span class=&quot;caps&quot;&gt;POJO&lt;/span&gt; based while still getting all the advantages of unlimited scale-out and high-availability.&lt;/p&gt;
&lt;p&gt;It is important to understand that &lt;a href=&quot;http://rifers.org/wiki/display/RIFECNT/Home&quot;&gt;&lt;span class=&quot;caps&quot;&gt;RIFE&lt;/span&gt; Continuations&lt;/a&gt; is a completely stand-alone library (which can be used outside &lt;span class=&quot;caps&quot;&gt;RIFE&lt;/span&gt; to implement all kinds of interesting stuff, from the declarative workflow used in &lt;span class=&quot;caps&quot;&gt;RIFE&lt;/span&gt; itself to thread migration across JVMs (if used with Open Terracotta which can be used with solely the continuation library if needed). These ideas are currently being formalized into a &lt;a href=&quot;http://rifers.org/wiki/display/RIFECNT/JSR+Continuations+Provider+API&quot;&gt;&lt;span class=&quot;caps&quot;&gt;JSR&lt;/span&gt; draft&lt;/a&gt; that have been submitted to the &lt;span class=&quot;caps&quot;&gt;JCP&lt;/span&gt;, which &amp;#8211; if accepted &amp;#8211; would bring this very powerful concept into the &lt;span class=&quot;caps&quot;&gt;JVM&lt;/span&gt; (or at least the &lt;span class=&quot;caps&quot;&gt;JDK&lt;/span&gt; &amp;#8211; depending on how it is implemented).&lt;/p&gt;
&lt;p&gt;I am very excited about the work that I have had the opportunity to do with &lt;a href=&quot;http://rifers.org/blogs/gbevin&quot;&gt;Geert Bevin&lt;/a&gt; in order to make this happen and think (and hope) that:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;span class=&quot;caps&quot;&gt;RIFE&lt;/span&gt; and Open Terracotta will be the number one application stack of choice for future enterprise Java web deployments.&lt;/li&gt;
&lt;li&gt;Continuations in general will be better understood and get more traction in the Java space and help to continue to push the limits of Java.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The upcoming release of Open Terracotta will include a sample application with &lt;span class=&quot;caps&quot;&gt;RIFE&lt;/span&gt; continuations clustered. But if you don&amp;#8217;t want to wait you should be able to grab the latest sources from the &lt;span class=&quot;caps&quot;&gt;RIFE&lt;/span&gt; and Open Terracotta &lt;span class=&quot;caps&quot;&gt;SVN&lt;/span&gt; repositories build and run it yourself.&lt;/p&gt;
&lt;p&gt;Enjoy.&lt;/p&gt;</content>
 </entry>
 
 <entry>
   <title>Building Stateful, Scalable and Highly-Available Web Applications with Spring Web Flow and Open Terracotta</title>
   <link href="http://jboner.github.com/2007/04/03/building-stateful-scalable-and-highly-available-web-applications-with-spring-web-flow-and-open-terracotta"/>
   <updated>2007-04-03T00:00:00+00:00</updated>
   <id>hhttp://jboner.github.com/2007/04/03/building-stateful-scalable-and-highly-available-web-applications-with-spring-web-flow-and-open-terracotta</id>
   <content type="html">&lt;h1&gt;
&lt;p&gt;Building Stateful, Scalable and Highly-Available Web Applications with Spring Web Flow and Open Terracotta&lt;/p&gt;
&lt;/h1&gt;
&lt;p&gt;My and &lt;a href=&quot;http://jroller.com/page/eu&quot;&gt;Eugene&amp;#8217;s&lt;/a&gt; article on how to use &lt;a href=&quot;http://www.springframework.org/webflow&quot;&gt;Spring Web Flow&lt;/a&gt; together with &lt;a href=&quot;http://terracotta.org/&quot;&gt;Open Terracotta&lt;/a&gt; to build conversational, scalable and highly-available web applications has been &lt;a href=&quot;http://www.infoq.com/articles/spring-web-flow-terracotta&quot;&gt;published on InfoQ&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;Now go ahead and read it :-)&lt;/p&gt;</content>
 </entry>
 
 <entry>
   <title>Clustering the JVM using AOP: the Inner Workings of Terracotta DSO</title>
   <link href="http://jboner.github.com/2007/03/03/clustering-the-jvm-using-aop-the-inner-workings-of-terracotta-dso"/>
   <updated>2007-03-03T00:00:00+00:00</updated>
   <id>hhttp://jboner.github.com/2007/03/03/clustering-the-jvm-using-aop-the-inner-workings-of-terracotta-dso</id>
   <content type="html">&lt;h1&gt;
&lt;p&gt;Clustering the &lt;span class=&quot;caps&quot;&gt;JVM&lt;/span&gt; using &lt;span class=&quot;caps&quot;&gt;AOP&lt;/span&gt;: the Inner Workings of Terracotta &lt;span class=&quot;caps&quot;&gt;DSO&lt;/span&gt;&lt;/p&gt;
&lt;/h1&gt;
&lt;p&gt;Our paper for &lt;a href=&quot;http://www.aosd.net/2007/&quot;&gt;&lt;span class=&quot;caps&quot;&gt;AOSD&lt;/span&gt; 2007&lt;/a&gt; has been published. It is titled &amp;#8216;Clustering the Java Virtual Machine using Aspect-Oriented Programming&amp;#8217; and is a thorough and pragmatic discussion on how &lt;a href=&quot;http://terracotta.org/&quot;&gt;Terracotta&amp;#8217;s &lt;span class=&quot;caps&quot;&gt;JVM&lt;/span&gt;-level clustering technology&lt;/a&gt; is implemented from an &lt;span class=&quot;caps&quot;&gt;AOP&lt;/span&gt; and bytecode weaving perspective. The discussion is driven by examples in AspectJ.&lt;/p&gt;
&lt;p&gt;The paper is available for download &lt;a href=&quot;http://www.aosd.net/2007/program/industry/I1-ClusteringJVMUsingAOP.pdf&quot;&gt;here&lt;/a&gt;.&lt;/p&gt;</content>
 </entry>
 
 <entry>
   <title>Clustering JRuby with Terracotta</title>
   <link href="http://jboner.github.com/2007/02/05/clustering-jruby-with-open-terracotta"/>
   <updated>2007-02-05T00:00:00+00:00</updated>
   <id>hhttp://jboner.github.com/2007/02/05/clustering-jruby-with-open-terracotta</id>
   <content type="html">&lt;h1&gt;
&lt;p&gt;Clustering JRuby with Terracotta&lt;/p&gt;
&lt;/h1&gt;
&lt;p&gt;Yesterday I was spending some time thinking about the possibilities to cluster applications written in &lt;a href=&quot;http://jruby.codehaus.org/&quot;&gt;JRuby&lt;/a&gt; with &lt;a href=&quot;http://www.terracotta.org/&quot;&gt;Terracotta&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;Sounds like a crazy idea? Well, I don&amp;#8217;t know. Perhaps it is. But thinking ahead a bit, with perhaps future deployments of &lt;a href=&quot;http://www.rubyonrails.org/&quot;&gt;Ruby on Rails&lt;/a&gt; applications etc. on JRuby makes it a bit more interesting.&lt;/p&gt;
&lt;p&gt;Anyway, let&amp;#8217;s give it a try.&lt;/p&gt;
&lt;h1&gt;Chatter sample application&lt;/h1&gt;
&lt;p&gt;First let&amp;#8217;s start with writing a &lt;b&gt;very&lt;/b&gt; simple chat application in JRuby.&lt;/p&gt;
&lt;pre name=&quot;code&quot; class=&quot;ruby&quot;&gt;
require &#39;java&#39;

class Chatter

  # set the name and create a List to hold our messages
  def initialize
    @name = ARGV[0]
    @messages = java.util.ArrayList.new
    puts &quot;--- Hi #{@name}. Welcome to Chatter. Press Enter to refresh ---&quot;
  end

  # takes text that the user enters
  # 1) if enter is pressed -&amp;gt; show the latest messages (refresh)
  # 2) if text is entered -&amp;gt; prepend the user name and add it to
  # the list of messages
  # 3) display the messages
  def run
    while true do
      print &quot;Enter Text&amp;gt;&amp;gt;&quot;
      text = STDIN.gets.chomp
      if text.length &amp;gt; 0 then
        @messages.add &quot;[#{Time.now} -- #{@name}] #{text}&quot;
        puts @messages
      else
        puts @messages
     end
    end
  end
end

Chatter.new.run
&lt;/pre&gt;
&lt;h1&gt;Run it&lt;/h1&gt;
&lt;p&gt;Now let&amp;#8217;s run it.&lt;/p&gt;
&lt;pre name=&quot;code&quot; class=&quot;terminal&quot;&gt;
--&amp;gt; jruby ./chat.rb Jonas 
                                                                                                  
--- Hi Jonas. Welcome to Chatter. Press Enter to refresh ---
Enter Text&amp;gt;&amp;gt;Hello. I am Jonas.
[[Fri Feb 02 11:57:56 CET 2007 -- Jonas] Hello. I am Jonas.
]
Enter Text&amp;gt;&amp;gt;Anybody there?
[[Fri Feb 02 11:57:56 CET 2007 -- Jonas] Hello. I am Jonas.
, [Fri Feb 02 11:58:01 CET 2007 -- Jonas] Anybody there?
]
Enter Text&amp;gt;&amp;gt;Ping...
[[Fri Feb 02 11:57:56 CET 2007 -- Jonas] Hello. I am Jonas.
, [Fri Feb 02 11:58:01 CET 2007 -- Jonas] Anybody there?
, [Fri Feb 02 11:58:25 CET 2007 -- Jonas] Ping...
]
Enter Text&amp;gt;&amp;gt;hmmm, no, seems to be just me
[[Fri Feb 02 11:57:56 CET 2007 -- Jonas] Hello. I am Jonas.
, [Fri Feb 02 11:58:01 CET 2007 -- Jonas] Anybody there?
, [Fri Feb 02 11:58:25 CET 2007 -- Jonas] Ping...
, [Fri Feb 02 11:58:59 CET 2007 -- Jonas] hmmm, no, seems to be just me
]
Enter Text&amp;gt;&amp;gt;
&lt;/pre&gt;
&lt;p&gt;Of course, this application is completely useless. It is in process, single threaded and only recieves input from &lt;span class=&quot;caps&quot;&gt;STDIN&lt;/span&gt;, which means that it can only be used by one single user at a time.&lt;/p&gt;
&lt;p&gt;But, what if we could make a single instance of the &lt;code&gt;@messages&lt;/code&gt; list available on the network and then run multiple instances of the application (each on its own &lt;span class=&quot;caps&quot;&gt;JVM&lt;/span&gt; instance, even on multiple machines) and have each one of them use this shared list?&lt;/p&gt;
&lt;p&gt;This would solve our problem and this in actually exactly what Terracotta could [conceptually] do for us. But Terracotta is a Java infrastructure service without any bindings or support for JRuby. So how can we proceed?&lt;/p&gt;
&lt;h1&gt;Terracotta for JRuby&lt;/h1&gt;
&lt;p&gt;I can see two different ways we can bridge JRuby and Terracotta:&lt;/p&gt;
&lt;h2&gt;Create a pattern language&lt;/h2&gt;
&lt;p&gt;We could hook into and extend the Terracotta pattern matching language (the language that is used to pick out the shared state and the methods modifying it) to support the Ruby syntax. This would mean allowing the user to define the patterns based on the Ruby language but then, under the hood, actually map the Ruby syntax to the real Java classes that are generated by JRuby (this assumes using the compiler and not the interpreter). The benefit here is that it would be &amp;#8220;transparent&amp;#8221;, in the same way as it is for regular Java applications. This is perhaps the best solution long term, but requires quite a lot of work and requires a fully working JRuby compiler. &lt;ref&gt;The development of the JRuby compiler has just started. When I tried it today it did not even compile the samples shipped with the distribution, so the Terracotta support for the compiler naturally has to wait until the implementation gets more complete and stable.&lt;/ref&gt;&lt;/p&gt;
&lt;h2&gt;Create a JRuby &lt;span class=&quot;caps&quot;&gt;API&lt;/span&gt;&lt;/h2&gt;
&lt;p&gt;The minimal set of abstractions we need in this &lt;span class=&quot;caps&quot;&gt;API&lt;/span&gt; is:&lt;/p&gt;
&lt;p&gt;1. State sharing: Be able to pick out the state that should be shared &amp;#8211; e.g. the top level &amp;#8220;root&amp;#8221; object of a shared object graph&lt;br /&gt;
2. State guarding: Be able to safely update this graph within the scope of a transaction&lt;/p&gt;
&lt;p&gt;Ok, let&amp;#8217;s try to design this &lt;span class=&quot;caps&quot;&gt;API&lt;/span&gt;.&lt;/p&gt;
&lt;h1&gt;Designing the JRuby &lt;span class=&quot;caps&quot;&gt;API&lt;/span&gt;&lt;/h1&gt;
&lt;p&gt;Most people are probably not aware of that Terracotta actually has an &lt;span class=&quot;caps&quot;&gt;API&lt;/span&gt; that can do all this (and much more) for us. It is well hidden in the &lt;a href=&quot;http://svn.terracotta.org/fisheye/browse/Terracotta&quot;&gt;&lt;span class=&quot;caps&quot;&gt;SVN&lt;/span&gt; repository&lt;/a&gt; and is used internally as the &lt;span class=&quot;caps&quot;&gt;API&lt;/span&gt; for the hooks, added to the target application during the bytecode instrumentation process, to call.&lt;/p&gt;
&lt;p&gt;The class in question is the: &lt;a href=&quot;http://svn.terracotta.org/fisheye/browse/Terracotta/branches/2.2.0/community/open/code/base/dso-l1/src/com/tc/object/bytecode/ManagerUtil.java&quot;&gt;ManagerUtil&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;So, everything we need is in the &lt;code&gt;ManagerUtil&lt;/code&gt; class. But before we roll up our sleeves and start hacking on the glue code, let&amp;#8217;s take a step back and think through what we want out of the &lt;span class=&quot;caps&quot;&gt;API&lt;/span&gt; from a user&amp;#8217;s perspective.&lt;/p&gt;
&lt;p&gt;First we need to be able to create a &amp;#8220;shared root&amp;#8221;, e.g. create a top-level instance of a shared object graph. One way of doing it would be to create some sort of factory method that can do the heavy lifting for us, similar to this:&lt;/p&gt;
&lt;pre name=&quot;code&quot; class=&quot;ruby&quot;&gt;
@messages = createRoot java.util.ArrayList.new
&lt;/pre&gt;
&lt;p&gt;Here we told some factory method &lt;code&gt;createRoot&lt;/code&gt; to create a root instance of the type &lt;code&gt;java.util.ArrayList&lt;/code&gt;. Seems reasonable I think.&lt;/p&gt;
&lt;p&gt;The other thing we need to do is to create some sort of transactions. We want to be able to start a transaction, lock the [shared] instance being updated, update it, unlock the instance and finally commit the transaction. Here is some basic pseudo code:&lt;/p&gt;
&lt;pre name=&quot;code&quot; class=&quot;terminal&quot;&gt;
lock target
    transaction begin
        modify target
    transaction commit
unlock target
&lt;/pre&gt;
&lt;p&gt;All steps except &amp;#8216;modify target&amp;#8217; is done by infrastructure code. Code that has nothing to do with your business logic, code that we want to eliminate and &amp;#8220;untangle&amp;#8221; as much as possible.&lt;/p&gt;
&lt;p&gt;In Ruby, a common design pattern to accomplish this is to use a method that takes a &lt;a href=&quot;http://en.wikipedia.org/wiki/Closure_(computer_science)&quot;&gt;block/closure&lt;/a&gt;. Using this pattern we could design the &lt;span class=&quot;caps&quot;&gt;API&lt;/span&gt; to look something like this:&lt;/p&gt;
&lt;pre name=&quot;code&quot; class=&quot;ruby&quot;&gt;
guard @messages do
  @messages.add msg
end
&lt;/pre&gt;
&lt;p&gt;Here the semantics are:&lt;/p&gt;
&lt;ul&gt;
	&lt;li&gt;the &lt;code&gt;@messages&lt;/code&gt; list is guarded for concurrent access (both in a single &lt;span class=&quot;caps&quot;&gt;JVM&lt;/span&gt; or in the cluster)&lt;/li&gt;
	&lt;li&gt;the &amp;#8216;do&amp;#8217; keyword takes the lock (on &lt;code&gt;@messages&lt;/code&gt;) and initiates the transaction (unit of work)&lt;/li&gt;
	&lt;li&gt;all updates to &lt;code&gt;@messages&lt;/code&gt; that are done within the &amp;#8216;do-end&amp;#8217; block are recorded in a change set (and are guaranteed to be done in isolation)&lt;/li&gt;
	&lt;li&gt;when the &amp;#8216;end&amp;#8217; of the block is reached then the transaction is committed, the change set is replicated and the lock (on &lt;code&gt;@messages&lt;/code&gt;) is released&lt;/li&gt;
&lt;/ul&gt;
&lt;h1&gt;Implementing the JRuby &lt;span class=&quot;caps&quot;&gt;API&lt;/span&gt;&lt;/h1&gt;
&lt;p&gt;Now, let&amp;#8217;s take a look at the &lt;code&gt;ManagerUtil&lt;/code&gt; class. It has a lot of useful stuff but the methods that we are currently interested in has the following signatures:&lt;/p&gt;
&lt;pre name=&quot;code&quot; class=&quot;java&quot;&gt;
Object lookupOrCreateRoot(String rootName, Object object);
void monitorEnter(Object obj, int lockType);
void monitorExit(Object obj);
&lt;/pre&gt;
&lt;p&gt;Based on these methods we can create the following JRuby module implementing the requirements we outlined above (and let&amp;#8217;s put it in a flle called &amp;#8216;terracotta.rb&amp;#8217; to be able to easily include it into the applications we want) :&lt;/p&gt;
&lt;pre name=&quot;code&quot; class=&quot;ruby&quot;&gt;
# Usage:
#
# # create an instance of &#39;foo&#39; as a DSO root this
# # means that it will be shared across the cluster
# foo = DSO.createRoot &quot;foo&quot;, Foo.new
#
# # update &#39;foo&#39; in a guarded block, get result back
# result = DSO.guard foo, DSO::WRITE_LOCK do
#   foo.add bar
#   foo.getSum
# end

TC = com.tc.object.bytecode.ManagerUtil

module DSO

  # The different lock types
  WRITE_LOCK = com.tc.object.bytecode.Manager::LOCK_TYPE_WRITE
  READ_LOCK = com.tc.object.bytecode.Manager::LOCK_TYPE_READ
  CONCURRENT_LOCK = com.tc.object.bytecode.Manager::LOCK_TYPE_CONCURRENT

  # Creates a Terracotta shared root, &#39;name&#39; is the name of the root
  # (can be anything that uniquily defines the root), &#39;object&#39; is an
  # instance of the object to be shared. If the root the given name
  # already exists it simply returns it.
  def DSO.createRoot(name, object)
    guardWithNamedLock name, WRITE_LOCK do
      TC.lookupOrCreateRoot name, object
    end
  end

  # Creates a transaction and guards the object (passed in as the
  # &#39;object&#39; argument) during the execution of the block passed into
  # the method. Similar to Java&#39;s synchronized(object) {...} blocks.
  # Garantuees that the critical section is maintained correctly across
  # the cluster. The type of the lock can be one of: DSO:WRITE_LOCK,
  # DSO::READ_LOCK or DSO::CONCURRENT_LOCK (default is DSO:WRITE_LOCK).
  def DSO.guard(object, type = WRITE_LOCK)
    TC.monitorEnter object, type
    begin
      yield
    ensure
      TC.monitorExit object
    end
  end

  # Creates a transaction and guards the critical section using a virtual
  # so called &#39;named lock. It is held during the execution of the block
  # passed into the method. Garantuees that the critical section is
  # maintained correctly across the cluster. The type of the lock can
  # be one of: DSO:WRITE_LOCK, DSO::READ_LOCK or DSO::CONCURRENT_LOCK
  # (default is DSO:WRITE_LOCK)8.
  def DSO.guardWithNamedLock(name, type = WRITE_LOCK)
    TC.beginLock name, type
    begin
      yield
    ensure
      TC.commitLock name
    end
  end

  # Dispatches a Distributed Method Call (DMI). Ensures that the
  # particular method will be invoked on all nodes in the cluster.
  def DSO.dmi(object, methodName, arguments)
    TC.distributedMethodCall object, methodName, arguments
  end
end
&lt;/pre&gt;
&lt;p&gt;The &lt;code&gt;ManagerUtil&lt;/code&gt; has a whole bunch of other useful and cool methods, such as for example &lt;code&gt;optimisticBegin&lt;/code&gt;, &lt;code&gt;optimiticCommit&lt;/code&gt; and &lt;code&gt;deepClone&lt;/code&gt; for creating optimistic concurrency transactions, etc. But I&amp;#8217;ll leave these for a later blog post.&lt;/p&gt;
&lt;h1&gt;Creating a distributed version of the Chatter application&lt;/h1&gt;
&lt;p&gt;Great. Now we have a JRuby &lt;span class=&quot;caps&quot;&gt;API&lt;/span&gt; with all the abstractions needed to create a distributed version of our little chat application. What we have to do is simply to create the &lt;code&gt;@messsages&lt;/code&gt; variable using the factory method for creating roots in the &lt;span class=&quot;caps&quot;&gt;API&lt;/span&gt;. Then we also have to make sure that we guard the updating of the shared &lt;code&gt;java.util.ArrayList&lt;/code&gt; using a guarded block.&lt;/p&gt;
&lt;p&gt;Let&amp;#8217;s take a look at the final version of the application.&lt;/p&gt;
&lt;pre name=&quot;code&quot; class=&quot;ruby&quot;&gt;
require &#39;java&#39;
load &#39;terracotta.rb&#39;

class Chatter
  def initialize
    @name = ARGV[0]
    @messages = DSO.createRoot &quot;chatter&quot;, java.util.ArrayList.new
    puts &quot;--- Hi #{@name}. Welcome to Chatter. Press Enter to refresh ---&quot;
  end

  def run
    while true do
      print &quot;Enter Text&amp;gt;&amp;gt;&quot;
      text = STDIN.gets.chomp
      if text.length &amp;gt; 0 then
        DSO.guard @messages do
          @messages.add &quot;[#{Time.now} -- #{@name}] #{text}&quot;
          puts @messages
        end
      else
        puts @messages
     end
    end
  end
end

Chatter.new.run
&lt;/pre&gt;
&lt;p&gt;Pretty simple and fairly intuitive, right?&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Note 1&lt;/b&gt;: Actually, we could have made it even more simple if we had taken a &lt;code&gt;java.util.concurrent.LinkedBlockingQueue&lt;/code&gt; instead of a regular &lt;code&gt;java.util.ArrayList&lt;/code&gt; as the list to hold our messages. If we would have done that then we could have skipped the &lt;code&gt;DSO.guard&lt;/code&gt; block altogether since the Java concurrency abstractions are natively supported by Terracotta. But then I would have missed the opportunity to show you how to handle non-thread-safe data access.&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Note 2&lt;/b&gt;: It currently only works for sharing native Java classes. In other words, you can currently &lt;b&gt;not&lt;/b&gt; cluster native JRuby constructs since it would mean cluster the internals of the JRuby interpreter (which is a lot of work and most likely not possible without rewriting parts of the interpreter). However, one feasible approach would be to not use the JRuby interpreter but the JRuby compiler and cluster its generated Java classes &amp;#8211; but unfortunately the compiler is not ready for general use yet (see the footnote at the bottom).&lt;/p&gt;
&lt;h1&gt;Enable Terracotta for JRuby&lt;/h1&gt;
&lt;p&gt;In order to enable Terracotta for JRuby we have to add a couple of &lt;span class=&quot;caps&quot;&gt;JVM&lt;/span&gt; options to the startup of our application, or add them directly in the &lt;tt&gt;jruby.(sh|bat)&lt;/tt&gt; script.&lt;/p&gt;
&lt;p&gt;You have to change:&lt;/p&gt;
&lt;pre name=&quot;code&quot; class=&quot;terminal&quot;&gt;
java -jar jruby.jar chat.rb
&lt;/pre&gt;
&lt;p&gt;to:&lt;/p&gt;
&lt;pre name=&quot;code&quot; class=&quot;terminal&quot;&gt;
java -Xbootclasspath/p:[path to terracotta boot jar] \
     -Dtc.config=path/to/your/tc-config.xml \
     -Dtc.install-root=[path to terracotta install dir] \
     -jar jruby.jar chat.rb    
&lt;/pre&gt;
&lt;p&gt;I know what you are thinking:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;&amp;#8220;Hey! What&amp;#8217;s up with that &lt;code&gt;tc-config.xml&lt;/code&gt; file? Do I have to write that? I hate &lt;span class=&quot;caps&quot;&gt;XML&lt;/span&gt;!&amp;#8221;&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;Yes, unfortunately you have to feed Terracotta with a tiny bit of &lt;span class=&quot;caps&quot;&gt;XML&lt;/span&gt;. This can perhaps be eliminated in the future (and replaced with a couple of command line options more or a JRuby config). But for now you have to write an &lt;span class=&quot;caps&quot;&gt;XML&lt;/span&gt; file that  looks like this:&lt;/p&gt;
&lt;pre name=&quot;code&quot; class=&quot;xml&quot;&gt;
&amp;lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&amp;gt;
&amp;lt;tc:tc-config xmlns:tc=&quot;http://www.terracotta.org/config&quot;&amp;gt;
  &amp;lt;servers&amp;gt;
    &amp;lt;server name=&quot;localhost&quot;/&amp;gt;
  &amp;lt;/servers&amp;gt;
  &amp;lt;clients&amp;gt;
    &amp;lt;logs&amp;gt;%(user.home)/terracotta/jtable/client-logs&amp;lt;/logs&amp;gt;
  &amp;lt;/clients&amp;gt;
  &amp;lt;application&amp;gt;
    &amp;lt;dso&amp;gt;
      &amp;lt;instrumented-classes&amp;gt;
        &amp;lt;include&amp;gt;
          &amp;lt;class-expression&amp;gt;*..*&amp;lt;/class-expression&amp;gt;
        &amp;lt;/include&amp;gt;
      &amp;lt;/instrumented-classes&amp;gt;
    &amp;lt;/dso&amp;gt;
  &amp;lt;/application&amp;gt;
&amp;lt;/tc:tc-config&amp;gt;
&lt;/pre&gt;
&lt;p&gt;As you can see it contains the name of the server where the Terracotta server resides, the path to the logs and a statement that says; include all classes for instrumentation. That&amp;#8217;s it.&lt;/p&gt;
&lt;p&gt;Now let&amp;#8217;s run it.&lt;/p&gt;
&lt;h1&gt;Run the distributed Chatter&lt;/h1&gt;
&lt;p&gt;Here is an sample of me trying it out with my wife Sara. It shows my session window:&lt;/p&gt;
&lt;pre name=&quot;code&quot; class=&quot;terminal&quot;&gt;
--&amp;gt; jruby ./chat.rb Jonas         

--- Hi Jonas. Welcome to Chatter. Press Enter to refresh ---
Enter Text&amp;gt;&amp;gt;Hi there. Is it working?
[[Fri Feb 02 13:07:19 CET 2007 -- Jonas] Hi there. Is it working?
]
Enter Text&amp;gt;&amp;gt;
[[Fri Feb 02 13:07:19 CET 2007 -- Jonas] Hi there. Is it working?
, [Fri Feb 02 13:08:09 CET 2007 -- Sara] I think so, I could see your message when I clicked Enter
]
Enter Text&amp;gt;&amp;gt;Awesome!! Isn&#39;t this amazingly cool? Terracotta rocks!
[[Fri Feb 02 13:07:19 CET 2007 -- Jonas] Hi there. Is it working?
, [Fri Feb 02 13:08:09 CET 2007 -- Sara]  I think so, I could see your message when I clicked Enter
, [Fri Feb 02 13:08:59 CET 2007 -- Jonas] Awesome!! Isn&#39;t this amazingly cool? Terracotta rocks!
]
Enter Text&amp;gt;&amp;gt;
[[Fri Feb 02 13:07:19 CET 2007 -- Jonas] Hi there. Is it working?
, [Fri Feb 02 13:08:09 CET 2007 -- Sara]  I think so, I could see your message when I clicked Enter
, [Fri Feb 02 13:08:59 CET 2007 -- Jonas] Awesome!! Isn&#39;t this amazingly cool? Terracotta rocks!
, [Fri Feb 02 13:10:17 CET 2007 -- Sara] What do you mean? This is it? 
]
Enter Text&amp;gt;&amp;gt;Well, yeah...
[[Fri Feb 02 13:07:19 CET 2007 -- Jonas] Hi there. Is it working?
, [Fri Feb 02 13:08:09 CET 2007 -- Sara]  I think so, I could see your message when I clicked Enter
, [Fri Feb 02 13:08:59 CET 2007 -- Jonas] Awesome!! Isn&#39;t this amazingly cool? Terracotta rocks!
, [Fri Feb 02 13:10:17 CET 2007 -- Sara] What do you mean? This is it? 
, [Fri Feb 02 13:10:36 CET 2007 -- Jonas] Well, yeah...
]
Enter Text&amp;gt;&amp;gt;
&lt;/pre&gt;
&lt;p&gt;Well, I hope that you find it a bit more exciting&amp;#8230;&lt;/p&gt;
&lt;p&gt;Feel like helping out? Drop me a line.&lt;/p&gt;
&lt;hr /&gt;
&lt;h1 style=&quot;vertical-align:middle;&quot;&gt;And one more thing&amp;#8230;&lt;/h1&gt;
&lt;p&gt;I also had some fun porting the JTable sample in the Terracotta distribution. It worked out nicely. I only had one problem and that was that I encountered a JRuby bug when trying to create a multi-dimensional &lt;code&gt;java.lang.Object&lt;/code&gt; array. I was able to work around the bug by creating the array using reflection, but this unfortunately had the effect of making the final code much longer. Anyway, here is the code in case you want to try it out:&lt;/p&gt;
&lt;pre name=&quot;code&quot; class=&quot;ruby&quot;&gt;
require &#39;java&#39;
load &#39;terracotta.rb&#39;

# need to create the object arrays using reflection
# due to a bug in JRuby
tableHeader = java.lang.Object[].new(4)
tableHeader[0] = &quot;Time&quot;
tableHeader[1] = &quot;Room A&quot;
tableHeader[2] = &quot;Room B&quot;
tableHeader[3] = &quot;Room C&quot;
dim = java.lang.reflect.Array.newInstance(java.lang.Integer::TYPE, 2)
java.lang.reflect.Array.setInt(dim, 0, 9)
java.lang.reflect.Array.setInt(dim, 1, 3)
tableData = java.lang.reflect.Array.newInstance(java.lang.Object, dim)
tableData[0][0] = &quot;9:00&quot;
tableData[1][0] = &quot;10:00&quot;
tableData[2][0] = &quot;11:00&quot;
tableData[3][0] = &quot;12:00&quot;
tableData[4][0] = &quot;1:00&quot;
tableData[5][0] = &quot;2:00&quot;
tableData[6][0] = &quot;3:00&quot;
tableData[7][0] = &quot;4:00&quot;
tableData[8][0] = &quot;5:00&quot;

# create the model as a DSO root
model = DSO.createRoot &quot;jtable.model&quot;, javax.swing.table.DefaultTableModel.new(tableData, tableHeader)

table = javax.swing.JTable.new(model)
frame = javax.swing.JFrame.new &quot;Table Demo&quot;
frame.getContentPane().add(javax.swing.JScrollPane.new(table))
frame.setDefaultCloseOperation javax.swing.JFrame::EXIT_ON_CLOSE
frame.setSize 500, 200
frame.pack
frame.setVisible true
&lt;/pre&gt;</content>
 </entry>
 
 <entry>
   <title>How to Build a POJO-based Data Grid using Terracotta</title>
   <link href="http://jboner.github.com/2007/01/29/how-to-build-a-pojo-based-data-grid-using-open-terracotta"/>
   <updated>2007-01-29T00:00:00+00:00</updated>
   <id>hhttp://jboner.github.com/2007/01/29/how-to-build-a-pojo-based-data-grid-using-open-terracotta</id>
   <content type="html">&lt;h1&gt;
&lt;p&gt;How to Build a &lt;span class=&quot;caps&quot;&gt;POJO&lt;/span&gt;-based Data Grid using Terracotta&lt;/p&gt;
&lt;/h1&gt;
&lt;h1&gt;Introduction&lt;/h1&gt;
&lt;p&gt;In this article, I will show you step by step how you can build a reusable &lt;span class=&quot;caps&quot;&gt;POJO&lt;/span&gt;-based &lt;em&gt;Data Grid&lt;/em&gt; with nothing but standard &lt;span class=&quot;caps&quot;&gt;JDK&lt;/span&gt; *5 and then make it distributed through the use of the declarative &lt;span class=&quot;caps&quot;&gt;JVM&lt;/span&gt;-level clustering technology provided by [Terracotta](http://terracotta.org).&lt;/p&gt;
&lt;p&gt;The article is divided into three sections:&lt;/p&gt;
&lt;ul&gt;
	&lt;li&gt;In the first section I will talk about the concepts of &lt;em&gt;Data Grids&lt;/em&gt;, what they are and what they do. I will discuss some of the underlying mechanisms that are used in &lt;em&gt;Data Grids&lt;/em&gt; and finally how one can scale out applications on a &lt;em&gt;Data Grid&lt;/em&gt;.&lt;/li&gt;
	&lt;li&gt;The second section will walk you through how to build a naive but fully usable implementation of a &lt;span class=&quot;caps&quot;&gt;POJO&lt;/span&gt;-based &lt;em&gt;Data Grid&lt;/em&gt; by implementing a multi-threaded &lt;em&gt;Master/Worker&lt;/em&gt; container and cluster it using &lt;a href=&quot;http://terracotta.org&quot;&gt;Terracotta&amp;#8217;s &lt;span class=&quot;caps&quot;&gt;JVM&lt;/span&gt;-level clustering technologies&lt;/a&gt;.&lt;/li&gt;
	&lt;li&gt;Last, in the third section I will show you how to extend the initial implementation to handle real-world requirements such as dealing with; high volumes of data, work failure, different routing algorithms, ordering of work and worker failure.&lt;/li&gt;
&lt;/ul&gt;
&lt;h1&gt;Part 1: Data Grids &amp;#8211; What&amp;#8217;s behind the buzzwords?&lt;/h1&gt;
&lt;h2&gt;What are Data Grids?&lt;/h2&gt;
&lt;p&gt;A &lt;em&gt;Data Grid&lt;/em&gt; is a set of servers that together creates a mainframe-class processing service where data and operations can move seamlessly across the grid in order to optimize the performance and scalability of the computing tasks submitted to the grid. A &lt;em&gt;Data Grid&lt;/em&gt; scales through use of &lt;a href=&quot;http://en.wikipedia.org/wiki/Locality_of_reference&quot;&gt;Locality of Reference&lt;/a&gt; (see the section &amp;#8220;How do Data Grids scale?&amp;#8221;) and is highly-available through effective use of data duplication. It combines data management with data processing.&lt;/p&gt;
&lt;h2&gt;What are Data Grids used for?&lt;/h2&gt;
&lt;p&gt;Applications that could benefit from being run on a &lt;em&gt;Data Grid&lt;/em&gt; are usually applications that needs to work on large data sets and/or have a need to parallelize processing of the data in order to get better throughput and performance. Examples are financial risk analysis and other simulations, searching and aggregation on large datasets as well as sales order pipeline processing.&lt;/p&gt;
&lt;h2&gt;How do Data Grids scale?&lt;/h2&gt;
&lt;p&gt;One of the main reasons why &lt;em&gt;Data Grids&lt;/em&gt; can scale so well is that they can make intelligent choices if it should move data to its processing context or moved the processing context (the operations) to the data. Effective use of the latter means that it can make use of &lt;em&gt;Locality of Reference&lt;/em&gt;; this means that data that is being processed on a specific node stays local to that node and will in the ideal case never have to leave that node. Instead, all operations that are working on this specific data set will always be routed to this specific node.&lt;/p&gt;
&lt;p&gt;The immediate benefits are that it has the advantage of minimizing the latency and can in the ideal case give unlimited and linear scalability. How well it scales is of course use-case specific, and it can take both effort and skills in partitioning the data and work sets, as well as potential routing algorithms. The ultimate (and simplest) situation is when all work are what we call &lt;a href=&quot;http://en.wikipedia.org/wiki/Embarrassingly_parallel&quot;&gt;Embarrassingly Parallel&lt;/a&gt; &amp;#8211; which means that it has no shared state and can to its working complete isolation. But it is in most cases acceptable to partition the work into &lt;em&gt;work sets&lt;/em&gt;, in such a way that the &lt;em&gt;work sets&lt;/em&gt; have no shared state. However, the latter solution might need some intelligent routing algorithms, something that we will take a look at later.&lt;/p&gt;
&lt;h2&gt;How do Data Grids deal with failure?&lt;/h2&gt;
&lt;p&gt;&lt;em&gt;Data Grids&lt;/em&gt; are resilient to failure and down time by effective use of data duplication. &lt;em&gt;Data Grids&lt;/em&gt; can be seen as an organic system of cells (nodes) that is designed to not only handle, but expect failure of individual cells (nodes). This is very different from traditional design of distributed systems in which each node is seen as an isolated unit which must always expect the worst and protect itself accordingly. See &lt;a href=&quot;http://www.artima.com/forums/flat.jsp?forum=106&amp;amp;thread=172063&quot;&gt;this page&lt;/a&gt;) for a more thorough discussion on the subject.&lt;/p&gt;
&lt;h1&gt;Part 2: Build a multi-threaded Master/Worker container and cluster it with Terracotta&lt;/h1&gt;
&lt;p&gt;&lt;img src=&quot;http://www.jonasboner.com/images/master_worker.jpg&quot; alt=&quot;Master/Worker algorithm&quot; align=&quot;right&quot;/&gt;&lt;/p&gt;
&lt;h2&gt;What is the Master/Worker pattern?&lt;/h2&gt;
&lt;p&gt;The &lt;em&gt;Master/Worker&lt;/em&gt; pattern is a pattern that is heavily used in &lt;em&gt;Data Grids&lt;/em&gt; and is one of the most well-known and common patterns for parallelizing work. We will now explain the characteristics of the pattern and then go through the possible approaches for a Java implementation.&lt;/p&gt;
&lt;p&gt;So, how does it work?&lt;/p&gt;
&lt;p&gt;The &lt;em&gt;Master/Worker&lt;/em&gt; pattern consists of three logical entities: a &lt;em&gt;Master&lt;/em&gt;, a &lt;em&gt;Shared Space&lt;/em&gt; and one or more instances of a &lt;em&gt;Worker&lt;/em&gt;. The &lt;em&gt;Master&lt;/em&gt; initiates the computation by creating a set of tasks, puts them in some shared space and then waits for the tasks to be picked up and completed by the &lt;em&gt;Workers&lt;/em&gt;. The shared space is usually some sort of &lt;em&gt;Shared Queue&lt;/em&gt;, but it can also be implemented as a &lt;em&gt;Tuple Space&lt;/em&gt; (for example in Linda programming environments, such as JavaSpaces, where the pattern is used extensively). One of the advantages of using this pattern is that the algorithm automatically balances the load. This is possible due to the simple fact that, the work set is shared, and the workers continue to pull work from the set until there is no more work to be done. The algorithm usually has good scalability as long as the number of tasks, by far exceeds the number of workers and if the tasks take a fairly similar amount of time to complete.&lt;/p&gt;
&lt;h2&gt;Possible approaches in Java&lt;/h2&gt;
&lt;p&gt;Let&amp;#8217;s now look at the different alternatives we have for implementing this in Java. There might be more ways of doing it, but we will focus the discussion on three different alternatives, each one with a higher abstraction level.&lt;/p&gt;
&lt;h3&gt;Using Java&amp;#8217;s threading primitives&lt;/h3&gt;
&lt;p&gt;The most hard-core approach is to use the concurrency and threading primitives that we have in the &lt;em&gt;Java Language Specification&lt;/em&gt; (&lt;span class=&quot;caps&quot;&gt;JLS&lt;/span&gt;), e.g. &lt;em&gt;wait/notify&lt;/em&gt; and the &lt;em&gt;synchronized&lt;/em&gt; and &lt;em&gt;volatile&lt;/em&gt; keywords. The benefits are that everything is really &amp;#8220;under your fingers&amp;#8221;, meaning that you could customize the solution without limitations.  However, this is also its main problem, since it is both a very hard and tedious to implement this yourself, and will most likely be even worse to maintain.  These low-level abstractions is not something that you want to work with on a day-to-day basis.  We need to raise the abstractions level above the core primitives in the &lt;em&gt;Java Memory Model&lt;/em&gt; and that is exactly what the data structure abstractions in the &lt;em&gt;java.util.concurrent&lt;/em&gt; library in &lt;span class=&quot;caps&quot;&gt;JDK&lt;/span&gt; *5 does for us.&lt;/p&gt;
&lt;h3&gt;Using the java.util.concurrent abstractions&lt;/h3&gt;
&lt;p&gt;Given that using the low-level abstractions in &lt;span class=&quot;caps&quot;&gt;JLS&lt;/span&gt; is both tedious and hard to use, the concurrency abstractions in &lt;span class=&quot;caps&quot;&gt;JDK&lt;/span&gt; *5 was both a very welcome and natural addition to the Java libraries. It is a very rich &lt;span class=&quot;caps&quot;&gt;API&lt;/span&gt; that provides everything from semaphores and barriers to implementations of the Java Collections data structure interfaces highly tuned for concurrent access. It also provides an &lt;em&gt;ExecutorService&lt;/em&gt;, which is mainly a thread pool that provides direct support for the &lt;em&gt;Master/Worker&lt;/em&gt; pattern. This is very powerful, since you&amp;#8217;re basically getting support from the &lt;em&gt;Master/Worker&lt;/em&gt; pattern in a single abstraction.&lt;/p&gt;
&lt;p&gt;It is possible to cluster the &lt;em&gt;ExecutorService&lt;/em&gt; using &lt;em&gt;Terracotta&lt;/em&gt;, you can read about that exercise in &lt;a href=&quot;http://www.theserverside.com/tt/articles/article.tss?l=DistCompute&quot;&gt;this article&lt;/a&gt;. Even though this approach would be sufficient in many situations and use-cases it has some problems:&lt;/p&gt;
&lt;ul&gt;
	&lt;li&gt;First, it does not separate the master from the worker (since they are both part of the same abstraction). What this means in practice is that you cannot scale out that the master independently of the worker but each node will contain both a master and a worker.&lt;/li&gt;
&lt;/ul&gt;
&lt;ul&gt;
	&lt;li&gt;Second, and perhaps even more important, is that it is lacking a layer of reliability and control.  Meaning that it is no way of knowing if a specific work task has been started, completed or if it has been rejected due to some error.  This means that there is no way we can detect work failure and can retry the work task on the same node or on another node. So we need a way to deal with these things in a simple and if possible standardized way.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;Using the CommonJ WorkManager specification&lt;/h3&gt;
&lt;p&gt;Spawning and coordinating threads is something that have been prohibited by the &lt;span class=&quot;caps&quot;&gt;EJB&lt;/span&gt; specification.  However, there has naturally been (and still is) a common need for coordinating work in &lt;span class=&quot;caps&quot;&gt;JEE&lt;/span&gt; (something that was partly, but not completely solved in a very verbose way with &lt;em&gt;Message-Driven Beans&lt;/em&gt; (&lt;span class=&quot;caps&quot;&gt;MDB&lt;/span&gt;)).  This need was the main motivation why &lt;span class=&quot;caps&quot;&gt;IBM&lt;/span&gt; and &lt;span class=&quot;caps&quot;&gt;BEA&lt;/span&gt; decided to do a joint specification that solves this problem by providing a standardized way of executing concurrent tasks in a &lt;span class=&quot;caps&quot;&gt;JEE&lt;/span&gt; environment. The specification is called &lt;em&gt;CommonJ&lt;/em&gt; and has support for the &lt;em&gt;Master/Worker&lt;/em&gt; pattern in its &lt;em&gt;WorkManager&lt;/em&gt; &lt;span class=&quot;caps&quot;&gt;API&lt;/span&gt;.&lt;/p&gt;
&lt;p&gt;From BEA&amp;#8217;s documentation about the specification:&lt;/p&gt;
&lt;p&gt;&lt;cite&gt;&amp;#8220;The Work Manager provides a simple &lt;span class=&quot;caps&quot;&gt;API&lt;/span&gt; for application-server-supported concurrent execution of work items. This enables J2EE-based applications (including Servlets and EJBs) to schedule work items for concurrent execution, which will provide greater throughput and increased response time. After an application submits work items to a Work Manager for concurrent execution, the application can gather the results. The Work Manager provides common &amp;#8220;join&amp;#8221; operations, such as waiting for any or all work items to complete. The Work Manager for Application Servers specification provides an application-server-supported alternative to using lower-level threading APIs, which are inappropriate for use in managed environments such as Servlets and EJBs, as well as being too difficult to use for most applications.&amp;quot;&lt;/cite&gt;&lt;/p&gt;
&lt;p&gt;What is interesting is that specification not only defines an &lt;span class=&quot;caps&quot;&gt;API&lt;/span&gt; for submitting work and getting the result back, but it also provides functionality for tracking work status in that it allows you to register listener that will receive callback events whenever the state of the work has been changed. This makes it possible to for example not only detect work failure but also the reason why it failed, which gives us a possibility of restarting the work.&lt;/p&gt;
&lt;p&gt;Each of the three approaches we have looked at so far have been built upon the previous one and has gradually raised the abstraction level. But even more importantly minimized and simplified the code that we as users have to write and maintain.&lt;/p&gt;
&lt;p&gt;The most natural choice is to base our implementation on the &lt;em&gt;CommonJ WorkManager&lt;/em&gt; specification. It is a simple and minimalistic specification and seems to provide everything that we need in order to build a reliable &lt;em&gt;Master/Worker&lt;/em&gt; container.&lt;/p&gt;
&lt;p&gt;Here are the interfaces in the specification:&lt;/p&gt;
&lt;pre name=&quot;code&quot; class=&quot;java&quot;&gt;
public interface Work extends Runnable { }

public interface WorkManager {
  WorkItem schedule(Work work);
  WorkItem schedule(Work work, WorkListener listener);
  boolean waitForAll(Collection workItems, long timeout);
  Collection waitForAny(Collection workItems, long timeout);
}

public interface WorkItem {
  Work getResult();
  int getStatus();
}

public interface WorkListener {
  void workAccepted(WorkEvent we);
  void workRejected(WorkEvent we);
  void workStarted(WorkEvent we);
  void workCompleted(WorkEvent we);
}

public interface WorkEvent {
  int WORKACCEPTED  = 1;
  int WORKREJECTED  = 2;
  int WORKSTARTED   = 3;
  int WORKCOMPLETED = 4;
  public int getType();
  public WorkItem getWorkItem();
  public WorkException getException();
}
&lt;/pre&gt;
&lt;h2&gt;The Plan&lt;/h2&gt;
&lt;p&gt;The plan is that we will first implement the specification as a regular multi-threaded application using the &lt;em&gt;java.util.concurrent&lt;/em&gt; abstractions (in particular the &lt;em&gt;ExecutorService&lt;/em&gt; and the &lt;em&gt;LinkedBlockingQueue&lt;/em&gt; classes) and then use &lt;em&gt;Terracotta&lt;/em&gt; to declaratively (and transparently), turn it into a multi-&lt;span class=&quot;caps&quot;&gt;JVM&lt;/span&gt;, distributed implementation.&lt;/p&gt;
&lt;h2&gt;Creating the Master (WorkManager)&lt;/h2&gt;
&lt;p&gt;We start with creating the &lt;em&gt;SingleQueueWorkManager&lt;/em&gt;, which serves as our &lt;em&gt;Master.&lt;/em&gt; It implements the &lt;em&gt;CommonJ WorkManager&lt;/em&gt; interface which provides the &lt;span class=&quot;caps&quot;&gt;API&lt;/span&gt; that the user uses to schedule the &lt;em&gt;Work&lt;/em&gt; and wait for the &lt;em&gt;Work&lt;/em&gt; to be completed. It has a reference to the work queue that is shared between the &lt;em&gt;Master&lt;/em&gt; and the &lt;em&gt;Workers&lt;/em&gt;, in this case represented by the &lt;em&gt;SingleWorkQueue&lt;/em&gt; abstraction.&lt;/p&gt;
&lt;p&gt;Here is how we could implement the work manager:&lt;/p&gt;
&lt;pre name=&quot;code&quot; class=&quot;java&quot;&gt;
public class SingleQueueWorkManager implements WorkManager {

private final SingleWorkQueue m_queue;

  public SingleQueueWorkManager(final SingleWorkQueue queue) {
    m_queue = queue;
  }

  public WorkItem schedule(final Work work) throws WorkException {
    DefaultWorkItem workItem = new DefaultWorkItem(work, null);
    m_queue.put(workItem);
    return workItem;
  }

  public WorkItem schedule(final Work work, final WorkListener listener)
      throws WorkException {
    DefaultWorkItem workItem = new DefaultWorkItem(work, listener);
    m_queue.put(workItem);
    return workItem;
  }

  public boolean waitForAll(Collection workItems, long timeout) {
    long start = System.currentTimeMillis();
    do {
      boolean isAllCompleted = true;
      for (Iterator it = workItems.iterator();
           it.hasNext() &amp;amp;&amp;amp; isAllCompleted;) {
        int status = ((WorkItem) it.next()).getStatus();
        isAllCompleted =
            status == WorkEvent.WORKCOMPLETED ](](
            status == WorkEvent.WORKREJECTED;
      }
      if (isAllCompleted) { return true; }
      if (timeout == IMMEDIATE) { return false; }
      if (timeout == INDEFINITE) { continue; }
    } while ((System.currentTimeMillis() - start) &amp;lt; timeout);
    return false;
  }

  public Collection waitForAny(Collection workItems, long timeout) {
    long start = System.currentTimeMillis();
    do {
      synchronized (this) {
        Collection completed = new ArrayList();
        for (Iterator it = workItems.iterator(); it.hasNext();) {
          WorkItem workItem = (WorkItem) it.next();
          if (workItem.getStatus() == WorkEvent.WORKCOMPLETED ](](
              workItem.getStatus() == WorkEvent.WORKREJECTED) {
            completed.add(workItem);
          }
        }
        if (!completed.isEmpty()) { return completed; }
      }
      if (timeout == IMMEDIATE) { return Collections.EMPTYLIST; }
      if (timeout == INDEFINITE) { continue; }
    } while ((System.currentTimeMillis() - start) &amp;lt; timeout);
    return Collections.EMPTYLIST;
  }
}
&lt;/pre&gt;

&lt;h2&gt;Creating the Shared Queue&lt;/h2&gt;
&lt;p&gt;The &lt;em&gt;SingleQueueWorkManager&lt;/em&gt; schedules work by adding work to the &lt;em&gt;SingleWorkQueue&lt;/em&gt;. The &lt;em&gt;SingleWorkQueue&lt;/em&gt; is the artifact that has state that needs to be shared between the &lt;em&gt;Master&lt;/em&gt; and the &lt;em&gt;Workers&lt;/em&gt;, since it holds the queue with all the pending &lt;em&gt;Work&lt;/em&gt;. We need to have a single instance of this queue that can be available to both the &lt;em&gt;Master&lt;/em&gt; and all its &lt;em&gt;Workers&lt;/em&gt;.&lt;/p&gt;
&lt;p&gt;The work queue can be implemented like this:&lt;/p&gt;
&lt;pre name=&quot;code&quot; class=&quot;java&quot;&gt;
public class SingleWorkQueue {

  // Terracotta Shared Root
  private final BlockingQueue m_workQueue =
      new LinkedBlockingQueue();

  public void put(final DefaultWorkItem workItem) throws WorkException {
    try {
      mworkQueue.put(workItem); // blocks if queue is full
    } catch (InterruptedException e) {
      e.printStackTrace();
      WorkRejectedException we = new WorkRejectedException(e.getMessage());
      workItem.setStatus(WorkEvent.WORKREJECTED, we);
      Thread.currentThread().interrupt();
      return we;            }
  }

  public DefaultWorkItem peek() throws WorkException {
    return m_workQueue.peek(); // returns null if queue is empty
  }

  public DefaultWorkItem take() throws WorkException {
    try {
      return m_workQueue.take(); // blocks if queue is empty
    } catch (InterruptedException e) {
      Thread.currentThread().interrupt();
      throw new WorkException(e);
    }
  }
}
&lt;/pre&gt;
&lt;p&gt;As you can see, it is a simple wrapper around a &lt;em&gt;java.util.concurrent.BlockingQueue&lt;/em&gt; queue and has three methods; &lt;em&gt;take(), put()&lt;/em&gt; and &lt;em&gt;peek()&lt;/em&gt; which simply delegates to the &lt;em&gt;BlockingQueue&lt;/em&gt; but also adds error handling in that it sets the status for the &lt;em&gt;Work&lt;/em&gt; in case of failure.&lt;/p&gt;
&lt;h2&gt;Creating the Worker&lt;/h2&gt;
&lt;p&gt;Finally, we have the &lt;em&gt;Worker&lt;/em&gt;, in our case represented by the &lt;em&gt;SingleQueueWorker&lt;/em&gt; abstraction. This class uses a thread pool to spawn up N number of worker threads that continuously grabs and executes &lt;em&gt;Work&lt;/em&gt; from the &lt;em&gt;SingleWorkQueue&lt;/em&gt;. During the processing of the &lt;em&gt;Work&lt;/em&gt;, the status flag in the wrapping &lt;em&gt;WorkItem&lt;/em&gt; is maintained (can be one of either &lt;em&gt;&lt;span class=&quot;caps&quot;&gt;ACCEPTED&lt;/span&gt;, &lt;span class=&quot;caps&quot;&gt;STARTED&lt;/span&gt;, &lt;span class=&quot;caps&quot;&gt;COMPLETED&lt;/span&gt;&lt;/em&gt; or &lt;em&gt;&lt;span class=&quot;caps&quot;&gt;REJECTED&lt;/span&gt;&lt;/em&gt;). This is needed in order for the &lt;em&gt;SingleQueueWorkManager&lt;/em&gt; to be able to continuously monitor the status of the &lt;em&gt;Work&lt;/em&gt; it has scheduled.&amp;nbsp;&lt;/p&gt;
&lt;p&gt;This is what a &lt;em&gt;Worker&lt;/em&gt; implementation can look like. As you can see we choose to make use of the &lt;em&gt;ExecutorService&lt;/em&gt; thread pool implementation in the &lt;em&gt;java.util.concurrent&lt;/em&gt; package:&lt;/p&gt;
&lt;pre name=&quot;code&quot; class=&quot;java&quot;&gt;
public class SingleQueueWorker implements Worker {

  protected final ExecutorService mthreadPool = Executors.newCachedThreadPool();
  protected final SingleWorkQueue mqueue;
  protected volatile boolean m_isRunning = true;

  public SingleQueueWorker(final SingleWorkQueue queue) {
    m_queue = queue;
  }

  public void start() throws WorkException {
    while (misRunning) {
      final DefaultWorkItem workItem = mqueue.take();
      final Work work = workItem.getResult();
      mthreadPool.execute(new Runnable() {
        public void run() {
          try {
            workItem.setStatus(WorkEvent.WORKSTARTED, null);
            work.run();
            workItem.setStatus(WorkEvent.WORKCOMPLETED, null);
          } catch (Throwable e) {
            workItem.setStatus(
                WorkEvent.WORKREJECTED, new WorkRejectedException(e));
          }
        }
      });
    }
  }

  public void stop() {
    misRunning = false;
    mthreadPool.shutdown();
  }
}
&lt;/pre&gt;
&lt;h2&gt;What about my Work?&lt;/h2&gt;
&lt;p&gt;Now we have the three main artifacts in place; the &lt;em&gt;Master&lt;/em&gt;, the &lt;em&gt;Worker&lt;/em&gt; and the &lt;em&gt;Shared Queue&lt;/em&gt;. But what about this &lt;em&gt;Work&lt;/em&gt; abstraction and which role does the &lt;em&gt;DefaultWorkItem&lt;/em&gt; play?&lt;/p&gt;
&lt;p&gt;Starting with the &lt;em&gt;Work&lt;/em&gt; abstraction. It is an interface in the &lt;em&gt;CommonJ WorkManager&lt;/em&gt; specification but is an interface that we cannot implement generically in the &lt;em&gt;WorkManager&lt;/em&gt; &amp;#8220;container&amp;#8221; we are building now, but should be implemented by the user of this container since the implementation of the actual work that is supposed to be done is of course use-case specific.&lt;/p&gt;
&lt;p&gt;The &lt;em&gt;DefaultWorkItem&lt;/em&gt; is an implementation of the &lt;em&gt;WorkItem&lt;/em&gt; interface in the specification. It&amp;#8217;s purpose is simply to wrap a &lt;em&gt;Work&lt;/em&gt; instance and provide additional information, such as status and an optional &lt;em&gt;WorkListener&lt;/em&gt; and can look something like this:&lt;/p&gt;
&lt;pre name=&quot;code&quot; class=&quot;java&quot;&gt;
public class DefaultWorkItem implements WorkItem {

private volatile int mstatus;
  private final Work mwork;
  private final WorkListener m_workListener;

  public DefaultWorkItem(final Work work, final WorkListener workListener) {
    mwork = work;
    mstatus = WorkEvent.WORKACCEPTED;
    mworkListener = workListener;
  }

  public Work getResult() {
    return m_work;
  }

  public synchronized void setStatus(
      final int status, final WorkException exception) {
    mstatus = status;
    if (mworkListener != null) {
      switch (status) {
        case WorkEvent.WORKACCEPTED:
          mworkListener.workAccepted(
              new DefaultWorkEvent(WorkEvent.WORKACCEPTED, this, exception));
          break;
        case WorkEvent.WORKREJECTED:
          mworkListener.workRejected(
              new DefaultWorkEvent(WorkEvent.WORKREJECTED, this, exception));
          break;
        case WorkEvent.WORKSTARTED:
          mworkListener.workStarted(
              new DefaultWorkEvent(WorkEvent.WORKSTARTED, this, exception));
          break;
        case WorkEvent.WORKCOMPLETED:
          mworkListener.workCompleted(
              new DefaultWorkEvent(WorkEvent.WORKCOMPLETED, this, exception));
          break;
      }
    }
  }

  public synchronized int getStatus() {
    return m_status;
  }
  ... // remaining methods omitted (toString() and compareTo())
}
&lt;/pre&gt;
&lt;p&gt;A &lt;em&gt;WorkListener&lt;/em&gt; is a listener that the user can register in order to track the status of the work and can upon reception of a state-changed-event take proper action (like retry upon failure etc.)&lt;/p&gt;
&lt;p&gt;That&amp;#8217;s it, now we now have a fully functional single-&lt;span class=&quot;caps&quot;&gt;JVM&lt;/span&gt;, multi-threaded, implementation of the &lt;em&gt;CommonJ WorkManager&lt;/em&gt; specification. Even thought this could be useful as is, there is no way we can scale it out with the needs of the application, since we are bound by the computing power in the single box we are using. Plus, it is not topic for the article. So, let&amp;#8217;s make it a bit more interesting.&lt;/p&gt;
&lt;h2&gt;Introducing Terracotta&lt;/h2&gt;
&lt;p&gt;&lt;a href=&quot;http://terracotta.org&quot;&gt;Terracotta&lt;/a&gt; is a product that delivers &lt;span class=&quot;caps&quot;&gt;JVM&lt;/span&gt;-level clustering as a runtime infrastructure service. It is Open Source and available under a Mozilla-based license.&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Terracotta&lt;/em&gt; uses bytecode instrumentation to adapt the target application at class load time. In this phase it extends the application in order to ensure that the semantics of the &lt;em&gt;Java Language Specification&lt;/em&gt; (&lt;span class=&quot;caps&quot;&gt;JLS&lt;/span&gt;) are correctly maintained across the cluster, including object references, thread coordination, garbage collection etc.&lt;/p&gt;
&lt;p&gt;Another important thing to mention is that &lt;em&gt;Terracotta&lt;/em&gt; does not use &lt;em&gt;Java Serialization&lt;/em&gt;, which means that any &lt;span class=&quot;caps&quot;&gt;POJO&lt;/span&gt; can be shared across the cluster. What this also means is that &lt;em&gt;Terracotta&lt;/em&gt; is not sending the whole object graph for the &lt;span class=&quot;caps&quot;&gt;POJO&lt;/span&gt; state to all nodes but breaks down the graph into pure data and is only sending the actual &amp;#8220;delta&amp;#8221; over the wire, meaning the actual changes &amp;#8211; the data that is &amp;#8220;stale&amp;#8221; on the other node(s).&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Terracotta&lt;/em&gt; is using an architecture known as &lt;em&gt;hub-and-spoke&lt;/em&gt;, which means that it has one central L2 server and N number of L1 clients. (The L1 client is the Terracotta client JARs running inside the target &lt;span class=&quot;caps&quot;&gt;JVM&lt;/span&gt;, while the L2 server is the central Terracotta server. L1 and L2 refers to first and second level caches.)&lt;/p&gt;
&lt;p&gt;This might seem strange, since most clustering solutions on the market today are using &lt;em&gt;peer-to-peer&lt;/em&gt;, but as we will see, &lt;em&gt;hub-and-spoke&lt;/em&gt; has some advantages and makes it possible to do some very interesting optimizations. The server plays two different roles:&lt;/p&gt;
&lt;ul&gt;
	&lt;li&gt;First it serves as the coordinator (&amp;#8220;the traffic cop&amp;#8221;) in the cluster. It keeps track of things like; which thread in which node is holding which lock, which nodes are referencing which part of the shared state, which objects have not been used for a specific time period and can be paged out, etc. Keeping all this knowledge in one single place is very valuable and allows for very interesting optimizations.&lt;/li&gt;
&lt;/ul&gt;
&lt;ul&gt;
	&lt;li&gt;Second, it serves as a dedicated state &lt;em&gt;Service of Record&lt;/em&gt; (SoR), meaning that it stores all the shared state in the cluster. The state server does not know anything about Java, but only stores the bytes of the data that has changed plus a minimal set of meta info. The L2 server itself is clusterable through a &lt;span class=&quot;caps&quot;&gt;SAN&lt;/span&gt;-based failover mechanism. This means that it is possible to scale-out the L2 server in the same fashion as most peer-to-peer solutions but with the advantage of keeping the L2 separate from the L* This separation allows us to scale out the L1 clients and the L2 servers independently of each other, which is the way that the &lt;em&gt;Internet&lt;/em&gt; scales.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;One way of looking at &lt;em&gt;Terracotta&lt;/em&gt; is to see it as &lt;a href=&quot;http://www.devx.com/Java/Article/32603/1763?supportItem&quot;&gt;&lt;em&gt;Network Attached Memory&lt;/em&gt;&lt;/a&gt;=* &lt;em&gt;Network Attached Memory&lt;/em&gt; (&lt;span class=&quot;caps&quot;&gt;NAM&lt;/span&gt;) is similar to &lt;em&gt;Network Attached Storage&lt;/em&gt; (&lt;span class=&quot;caps&quot;&gt;NAS&lt;/span&gt;) in the sense that &lt;span class=&quot;caps&quot;&gt;JVM&lt;/span&gt;-level (heap-level) replication is making NAM&amp;#8217;s presence transparent just like &lt;span class=&quot;caps&quot;&gt;NAS&lt;/span&gt; can be transparent behind a file I/O &lt;span class=&quot;caps&quot;&gt;API&lt;/span&gt;. Getting &lt;span class=&quot;caps&quot;&gt;NAM&lt;/span&gt; to perform and scale is similar to any I/O platform; read-ahead buffering, read/write locking optimizations etc.&lt;/p&gt;
&lt;p&gt;Even though it is in clustering, meaning scalability and high-availability, of Web and enterprise applications, that &lt;em&gt;Terracotta&lt;/em&gt; can bring its most immediate value, it is really a platform for solving generic distributed computing and shared memory problems in plain Java code. This is something that makes it applicable to a wide range problem domains, for example building a &lt;span class=&quot;caps&quot;&gt;POJO&lt;/span&gt;-based &lt;em&gt;Data Grid&lt;/em&gt;.&lt;/p&gt;
&lt;h2&gt;Let&amp;#8217;s cluster it using Terracotta!&lt;/h2&gt;
&lt;p&gt;I know what you are thinking:&lt;br /&gt;
&amp;gt; &amp;#8220;Clustering, hmmm&amp;#8230; Now comes the hard part right?&amp;#8221;&lt;/p&gt;
&lt;p&gt;Well&amp;#8230;no.&lt;/p&gt;
&lt;p&gt;It turns out that in order to cluster our current &lt;em&gt;WorkManager&lt;/em&gt; implementation, all we have to do is to create a &lt;em&gt;Terracotta&lt;/em&gt; configuration file in which we define three things:&lt;/p&gt;
&lt;ul&gt;
	&lt;li&gt;The fully qualified name of the top level object in the object graph that we want to share. In our case we want to share the work queue. This means that the most natural root would be the &lt;em&gt;LinkedBlockingQueue&lt;/em&gt; in the &lt;em&gt;SingleWorkQueue&lt;/em&gt; class, e.g. the m_workQueue field.&lt;/li&gt;
	&lt;li&gt;The classes that we want to include for instrumentation. We can include all classes for instrumentation, e.g. use a &amp;#8220;match-all&amp;#8221; pattern, but it is usually better to narrow down the scope of the classes that &lt;em&gt;Terracotta&lt;/em&gt; needs to introspect.&lt;/li&gt;
	&lt;li&gt;The potential lock boundaries that we want Terracotta to introspect. These are called &lt;em&gt;auto-locks&lt;/em&gt; and is simply a hint that Terracotta should treat the synchronized blocks in these places as transaction boundaries. (You can also define explicit locks called &lt;em&gt;named-locks&lt;/em&gt;.) In our case we will define a &amp;#8220;match-all&amp;#8221; pattern for the locking, something that works fine in most cases and should be treated as the default.&lt;/li&gt;
&lt;/ul&gt;
&lt;pre name=&quot;code&quot; class=&quot;java&quot;&gt;
...
&amp;lt;roots&amp;gt;
  &amp;lt;root&amp;gt;
    &amp;lt;field-name&amp;gt;
      org.terracotta.datagrid.workmanager.singlequeue.SingleWorkQueue.m_workQueue
    &amp;lt;/field-name&amp;gt;
  &amp;lt;/root&amp;gt;
&amp;lt;/roots&amp;gt;
&amp;lt;instrumented-classes&amp;gt;
  &amp;lt;include&amp;gt;
    &amp;lt;class-expression&amp;gt;
      org.terracotta.datagrid.workmanager..*
    &amp;lt;/class-expression&amp;gt;
    &amp;lt;honor-transient&amp;gt;true&amp;lt;/honor-transient&amp;gt;
  &amp;lt;/include&amp;gt;
&amp;lt;/instrumented-classes&amp;gt;

&amp;lt;locks&amp;gt;
  &amp;lt;autolock&amp;gt;
    &amp;lt;method-expression&amp;gt;* ...*(..)&amp;lt;/method-expression&amp;gt;
    &amp;lt;lock-level&amp;gt;write&amp;lt;/lock-level&amp;gt;
  &amp;lt;/autolock&amp;gt;
&amp;lt;/locks&amp;gt;
...
&lt;/pre&gt;
&lt;p&gt;Done!&lt;/p&gt;
&lt;p&gt;Now we have a distributed/multi-&lt;span class=&quot;caps&quot;&gt;JVM&lt;/span&gt; &lt;em&gt;CommonJ WorkManager&lt;/em&gt; ready for use. But in order to call it a &lt;span class=&quot;caps&quot;&gt;POJO&lt;/span&gt;-based &lt;em&gt;Data Grid&lt;/em&gt;, we need extend it a bit and address some challenges that are likely to arise if we were to deploy this implementation into production.&lt;/p&gt;
&lt;h1&gt;Part 3: Getting serious &amp;#8211; Handling real-world challenges&lt;/h1&gt;
&lt;p&gt;Now we have learned the basics of &lt;em&gt;Data Grids&lt;/em&gt;, the &lt;em&gt;Master/Worker&lt;/em&gt; pattern and what the &lt;em&gt;CommonJ WorkManager&lt;/em&gt; specification is all about. We also walked you through how to implement a distributed &lt;em&gt;CommonJ WorkManager&lt;/em&gt; by first creating a single-&lt;span class=&quot;caps&quot;&gt;JVM&lt;/span&gt; implementation that we then cluster into a distributed multi-&lt;span class=&quot;caps&quot;&gt;JVM&lt;/span&gt; implementation with &lt;em&gt;Terracotta&lt;/em&gt;.&lt;/p&gt;
&lt;p&gt;However, it was a fairly simple and in some sense naÃ¯ve implementation. This was a good exercise in terms of education and understanding, but in order to use the implementation in the real world we need to know how to address some of the challenges is that might come up. Now we will discuss some of these challenges and how we can extend the initial implementation to address them.&lt;/p&gt;
&lt;p&gt;The challenges that we will look at, one by one, are how to handle:&lt;/p&gt;
&lt;ul&gt;
	&lt;li&gt;Very high volumes of data?&lt;/li&gt;
	&lt;li&gt;Work failure?&lt;/li&gt;
	&lt;li&gt;Routing?&lt;/li&gt;
	&lt;li&gt;Ordering?&lt;/li&gt;
	&lt;li&gt;Worker failure?&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;Dealing with very high volumes of data&lt;/h2&gt;
&lt;p&gt;Our current implementation has one single queue that is shared by the master(s) and the all workers. This usually gives acceptable performance and scalability when used with a moderate work load. However, if we need to deal with very high volumes of data then it is likely that we will bottleneck on the single queue. So how can we address this in the most simple fashion?&lt;/p&gt;
&lt;p&gt;The perhaps simplest solution is to create one queue per worker, and have the master do some more or less intelligent load-balancing. If we are able to do a good partition of the work and data, that we discussed in the previous section (&amp;#8220;Scaling Data Grids&amp;#8221;), then this solution is one that will:&lt;/p&gt;
&lt;ul&gt;
	&lt;li&gt;&lt;b&gt;Maximize the use of &lt;em&gt;Locality of Reference&lt;/em&gt;&lt;/b&gt; &amp;#8211; since all work that are operating on the same data set will be routed to the same queue with the same worker working on them&lt;/li&gt;
	&lt;li&gt;&lt;b&gt;Minimize contention&lt;/b&gt; &amp;#8211; since since there will only be one reader and one writer per queue.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;If we take a look at the current code, what needs to be changed is to first change the &lt;em&gt;SingleWorkQueue&lt;/em&gt; class to a &lt;em&gt;WorkQueueManager&lt;/em&gt; class and swap the single &lt;em&gt;LinkedBlockingQueue&lt;/em&gt; to a &lt;em&gt;ConcurrentHashMap&lt;/em&gt; with entries containing a &lt;em&gt;routing ID&lt;/em&gt; mapped to a &lt;em&gt;LinkedBlockingQueue&lt;/em&gt;:&lt;/p&gt;
&lt;p&gt;Swap the wrapped &lt;em&gt;LinkedBlockingQueue&lt;/em&gt;:&lt;/p&gt;
&lt;pre name=&quot;code&quot; class=&quot;java&quot;&gt;
public class SingleWorkQueue {
  BlockingQueue&amp;lt;WorkItem&amp;gt; m_workQueue = new LinkedBlockingQueue&amp;lt;WorkItem&amp;gt;();
}
&lt;/pre&gt;
&lt;p&gt;To a &lt;em&gt;ConcurrentHashMap&lt;/em&gt; of _LinkedBlockingQueue_&amp;#8217;s :&lt;/p&gt;
&lt;pre name=&quot;code&quot; class=&quot;java&quot;&gt;
public class DefaultWorkQueueManager&amp;lt;ID&amp;gt; implements WorkQueueManager {
  Map&amp;lt;ID , BlockingQueue&amp;lt;WorkItem&amp;gt;&amp;gt; m_workQueues =
      new ConcurrentHashMap&amp;lt;ID, BlockingQueue&amp;lt;WorkItem&amp;gt;&amp;gt;();
}
&lt;/pre&gt;
&lt;p&gt;We also need to change the &lt;em&gt;WorkManager&lt;/em&gt; implementation and rewrite the &lt;em&gt;schedule(..)&lt;/em&gt; methods to use a &lt;em&gt;Router&lt;/em&gt; abstraction and not put the work directly into the queue:&lt;/p&gt;
&lt;p&gt;Change the &lt;em&gt;schedule(..)&lt;/em&gt; method from:&lt;/p&gt;
&lt;pre name=&quot;code&quot; class=&quot;java&quot;&gt;
public class DefaultWorkManager implements WorkManager {

  public WorkItem schedule(final Work work) {
    WorkItem workItem = new DefaultWorkItem(work, null);
    m_queue.put(workItem);
    return workItem;
  }
  ...
}
&lt;/pre&gt;
&lt;p&gt;To redirect to the &lt;em&gt;Router&lt;/em&gt; abstraction (more on the &lt;em&gt;Router&lt;/em&gt; below):&lt;/p&gt;
&lt;pre name=&quot;code&quot; class=&quot;java&quot;&gt;
public class RoutingAwareWorkManager&amp;lt;ID&amp;gt; implements WorkManager {

  public WorkItem schedule(final Work work) {
    return m_router.route(work);
  }
  ...
}
&lt;/pre&gt;
&lt;p&gt;In the last code block we could see that we have introduced a new abstraction; the &lt;em&gt;Router&lt;/em&gt;, which leads us to the topic of how to deal with different routing schemes.&lt;/p&gt;
&lt;h2&gt;Dealing with different routing schemes&lt;/h2&gt;
&lt;p&gt;By splitting up the single working queue into multiple queues each with a unique routing ID we are opening up for the possibility of providing different routing schemes that can be customized to address specific use cases.&lt;/p&gt;
&lt;p&gt;As in the previous section, we have to make some changes to the initial single queue implementation. First we need to add a routing id to the &lt;em&gt;WorkItem&lt;/em&gt; abstraction, we do that by adding the generic type ID and let the &lt;em&gt;WorkItem&lt;/em&gt; implement the &lt;em&gt;Routable&lt;/em&gt; interface:&lt;/p&gt;
&lt;pre name=&quot;code&quot; class=&quot;java&quot;&gt;
public class RoutableWorkItem&amp;lt;ID&amp;gt; extends DefaultWorkItem implements Routable&amp;lt;ID&amp;gt; {

  protected ID m_routingID;

  public ID getRoutingID() {
    return m_routingID;
  }
  ...
}
&lt;/pre&gt;
&lt;p&gt;As we saw in the previous section, we also introduce a new abstraction called &lt;em&gt;Router&lt;/em&gt;:&lt;br /&gt;
&lt;pre name=&quot;code&quot; class=&quot;java&quot;&gt;&lt;br /&gt;
public interface Router&lt;ID&gt; {&lt;br /&gt;
  RoutableWorkItem&lt;ID&gt; route(Work work);&lt;br /&gt;
  RoutableWorkItem&lt;ID&gt; route(Work work, WorkListener listener);&lt;br /&gt;
  RoutableWorkItem&lt;ID&gt; route(RoutableWorkItem&lt;ID&gt; WorkItem);&lt;br /&gt;
}&lt;br /&gt;
&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;This abstraction can be used to implement various load-balancing algorithms, such as for example:&lt;/p&gt;
&lt;ul&gt;
	&lt;li&gt;&lt;b&gt;Round-robin&lt;/b&gt; &amp;#8211; &lt;em&gt;Router&lt;/em&gt; circles around all queues one by one&lt;/li&gt;
	&lt;li&gt;&lt;b&gt;Work load sensitive balancing&lt;/b&gt; &amp;#8211; &lt;em&gt;Router&lt;/em&gt; looks at queue depth and always sends the next pending work to the shortest queue&lt;/li&gt;
	&lt;li&gt;&lt;b&gt;Data affinity&lt;/b&gt; &amp;#8211; &amp;#8220;Sticky routing&amp;#8221;, meaning that the &lt;em&gt;Router&lt;/em&gt; sends all pending work of a specific type to a specific queue&lt;/li&gt;
	&lt;li&gt;&lt;b&gt;Roll your own&lt;/b&gt; &amp;#8211; to maximize &lt;em&gt;Locality of Reference&lt;/em&gt; for your specific requirements&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;In our &lt;em&gt;Router&lt;/em&gt; implementation we have three default algorithms; &lt;em&gt;round-robin&lt;/em&gt;, &lt;em&gt;load-balancing&lt;/em&gt; and &lt;em&gt;single queue&lt;/em&gt;. You can find them as static inner classes in the &lt;em&gt;Router&lt;/em&gt; interface.&lt;/p&gt;
&lt;p&gt;Here is an example of the load-balancing router implementation that takes an array of the routing IDs that are registered and always sends the next pending work to the shortest queue:&lt;/p&gt;
&lt;pre name=&quot;code&quot; class=&quot;java&quot;&gt;
public class LoadBalancingRouter&amp;lt;ID&amp;gt; implements Router&amp;lt;ID&amp;gt; {
    
  private final WorkQueueManager&amp;lt;ID&amp;gt; m_queueManager;

  private static class WorkQueueInfo&amp;lt;ID&amp;gt; {
    public ID routingID;
    public BlockingQueue&amp;lt;RoutableWorkItem&amp;lt;ID&amp;gt;&amp;gt; workQueue;
    public int queueLength = Integer.MAX_VALUE;
  }

  public LoadBalancingRouter(final WorkQueueManager&amp;lt;ID&amp;gt; queueManager, final ID[] routingIDs) {
    m_queueManager = queueManager;
    // create all queues upfront
    for (int i = 0; i &amp;lt; routingIDs.length; i++) {
      m_queueManager.getOrCreateQueueFor(routingIDs[i]);        
    }
  }
    
  public RoutableWorkItem&amp;lt;ID&amp;gt; route(final Work work) {
    return route(work, null);
  }

  public RoutableWorkItem&amp;lt;ID&amp;gt; route(final Work work, final WorkListener listener) {
    WorkQueueLength&amp;lt;ID&amp;gt; shortestQueue = getShortestWorkQueue();
    RoutableWorkItem&amp;lt;ID&amp;gt; workItem = new RoutableWorkItem&amp;lt;ID&amp;gt;(work, listener, shortestQueue.routingID);
    try {
      shortestQueue.workQueue.put(workItem);
    } catch (InterruptedException e) {
      workItem.setStatus(WorkEvent.WORK_REJECTED, new WorkException(e));
      Thread.currentThread().interrupt();
    }
    return workItem;
  }

  public RoutableWorkItem&amp;lt;ID&amp;gt; route(final RoutableWorkItem&amp;lt;ID&amp;gt; workItem) {
    WorkQueueLength&amp;lt;ID&amp;gt; shortestQueue = getShortestWorkQueue();
    synchronized (workItem) {
      workItem.setRoutingID(shortestQueue.routingID);        
    }
    try {
      shortestQueue.workQueue.put(workItem);
    } catch (InterruptedException e) {
      workItem.setStatus(WorkEvent.WORK_REJECTED, new WorkException(e));
      Thread.currentThread().interrupt();
    }
    return workItem;
  }

  private WorkQueueLength&amp;lt;ID&amp;gt; getShortestWorkQueue() {
    WorkQueueLength&amp;lt;ID&amp;gt; shortestQueue = new WorkQueueLength&amp;lt;ID&amp;gt;();
    
    int queueLengthForShortestQueue = Integer.MAX_VALUE;
    ID routingIDForShortestQueue = null;
    Map&amp;lt;ID, BlockingQueue&amp;lt;RoutableWorkItem&amp;lt;ID&amp;gt;&amp;gt;&amp;gt; queues = m_queueManager.getQueues();
    for (Map.Entry&amp;lt;ID, BlockingQueue&amp;lt;RoutableWorkItem&amp;lt;ID&amp;gt;&amp;gt;&amp;gt; entry: queues.entrySet()) {
      ID routingID = entry.getKey();
      BlockingQueue&amp;lt;RoutableWorkItem&amp;lt;ID&amp;gt;&amp;gt; queue = entry.getValue();
      int queueSize = queue.size();
      if (queueSize &amp;lt;= queueLengthForShortestQueue) {
        queueLengthForShortestQueue = queueSize;
        routingIDForShortestQueue = routingID;
      } 
    }
    shortestQueue.workQueue = m_queueManager.getOrCreateQueueFor(routingIDForShortestQueue);         
    shortestQueue.routingID = routingIDForShortestQueue;
    return shortestQueue;
  }
}
&lt;/pre&gt;
&lt;h2&gt;Dealing with work failure&lt;/h2&gt;
&lt;p&gt;As we discussed earlier in this article the &lt;em&gt;CommonJ WorkManager&lt;/em&gt; specification provides APIs for event-based failure reporting and tracking of work status.  Each &lt;em&gt;Work&lt;/em&gt; instance is wrapped in a &lt;em&gt;WorkItem&lt;/em&gt; instance which provides status information. It also gives us the possibility of defining an optional &lt;em&gt;WorkListener&lt;/em&gt; through which we will get callback events whenever the status of the work has been changed.&lt;/p&gt;
&lt;p&gt;The  &lt;em&gt;WorkListener&lt;/em&gt; interface looks like this:&lt;/p&gt;
&lt;pre name=&quot;code&quot; class=&quot;java&quot;&gt;
public interface WorkListener {
  void workAccepted(WorkEvent we);
  void workRejected(WorkEvent we);
  void workStarted(WorkEvent we);
  void workCompleted(WorkEvent we);
}
&lt;/pre&gt;
&lt;p&gt;As you can see, we can implement callbacks methods that subscribes to events triggered by work being &lt;em&gt;accepted&lt;/em&gt;, &lt;em&gt;rejected&lt;/em&gt;, &lt;em&gt;started&lt;/em&gt; and &lt;em&gt;completed&lt;/em&gt;. In this particular case we are mainly interested in doing something when receiving the &lt;em&gt;work rejected&lt;/em&gt; event. In order to do that to we simply need to create a implementation of the &lt;em&gt;WorkListener&lt;/em&gt; interface and add some code in the &lt;em&gt;workRejected&lt;/em&gt; method:&lt;/p&gt;
&lt;pre name=&quot;code&quot; class=&quot;java&quot;&gt;
public class RetryingWorkListener implemets WorkListener {

  public void workRejected(WorkEvent we) {
    Expection cause = we.getException();
    WorkItem wi = we.getWorkItem();
    Work work = wi.getResult(); // the rejected work

    ... // reroute the work onto queue X
  }

  public void workAccepted(WorkEvent event) {}
  public void workCompleted(WorkEvent event) {}
  public void workStarted(WorkEvent event) {}
}
&lt;/pre&gt;
&lt;h2&gt;Dealing with ordering of work&lt;/h2&gt;
&lt;p&gt;In some situations it might be important to define and maintain ordering of the work.  This doesn&amp;#8217;t seem to be a problem if we have a single instance of the master, but imagine scaling out the master; then it immediately gets worse.&lt;/p&gt;
&lt;p&gt;So, how can we maintain ordering of the work? Since the internal implementation is based on POJOs and everything is open and customizable, it is actually very easy to implement support for ordering.  All we need to do is to swap our map of &lt;em&gt;LinkedBlockingQueue&lt;/em&gt;(s) to a map of &lt;em&gt;PriorityBlockingQueue&lt;/em&gt; (s).&lt;/p&gt;
&lt;p&gt;Then you can let your &lt;em&gt;Work&lt;/em&gt; implement &lt;em&gt;Comparable&lt;/em&gt; and create a custom &lt;em&gt;Comparator&lt;/em&gt; that you pass it into the constructor of the &lt;em&gt;PriorityBlockingQueue&lt;/em&gt;:&lt;/p&gt;
&lt;pre name=&quot;code&quot; class=&quot;java&quot;&gt;
Comparator c = new Comparator&amp;lt;RoutableWorkItem&amp;lt;ID&amp;gt;&amp;gt;() {
    public int compare(
        RoutableWorkItem&amp;lt;ID&amp;gt; workItem1,
        RoutableWorkItem&amp;lt;ID&amp;gt; workItem2)
      Comparable work1 =
         (Comparable)workItem*getResult();
      Comparable work2 =
         (Comparable)workItem2.getResult();
      return work*compareTo(work2);
    }};
&lt;/pre&gt;
&lt;p&gt;If you need to maintain ordering of the result then you have to do something similar with the references to the &lt;em&gt;WorkItem&lt;/em&gt;(s) that you get when invoking &lt;em&gt;schedule(..)&lt;/em&gt; on the &lt;em&gt;WorkManager&lt;/em&gt; instance.&lt;/p&gt;
&lt;h2&gt;Dealing with worker failure&lt;/h2&gt;
&lt;p&gt;In a reliable distributed system it is important to be able to detect if a worker goes down.  (In order to simplify the discussion, let&amp;#8217;s define a worker as being a &lt;span class=&quot;caps&quot;&gt;JVM&lt;/span&gt;.) There are some different ways this could be implemented and we will now discuss some of the different strategies that we can take.&lt;/p&gt;
&lt;ul&gt;
	&lt;li&gt;&lt;b&gt;Heartbeat mechanism&lt;/b&gt; &amp;#8211; In this strategy each worker has an open connection to the master through which it continuously (and periodically) sends a heartbeat (some sort of &amp;#8220;I&amp;#8217;m-alive&amp;#8221; event) to the master. The master can then detect if the heartbeat for a specific worker stops and can after a certain time period consider the node to be dead. This is for example the way that &lt;a href=&quot;http://www.cs.wisc.edu/~dusseau/Classes/CS739/Writeups/mapreduce.pdf&quot;&gt;Google&amp;#8217;s MapReduce implementation&lt;/a&gt; detects worker failure.&lt;/li&gt;
	&lt;li&gt;&lt;b&gt;Work timestamp&lt;/b&gt; &amp;#8211; Here we are adding a timestamp to each pending work item. The master can then periodically peek into the head of each work queue, read the timestamp and and match it to a predefined timeout interval. If it detects that a work item has timed out then it can consider the worker(s) that is polling from the specific queue to be dead.&lt;/li&gt;
	&lt;li&gt;&lt;b&gt;Worker &amp;#8220;is-alive-lock&amp;#8221;&lt;/b&gt; &amp;#8211; This strategy utilizes the cross-&lt;span class=&quot;caps&quot;&gt;JVM&lt;/span&gt; thread coordination that &lt;em&gt;Terracotta&lt;/em&gt; enables.  The first thing that each worker does when it starts up is spawn a thread that takes a worker specific lock:&lt;/li&gt;
&lt;/ul&gt;
&lt;pre name=&quot;code&quot; class=&quot;java&quot;&gt;
synchronized(workerLock) { 
  while (isRunning) {}; // hold the lock until worker is shut down or dies 
}
&lt;/pre&gt;
&lt;p&gt;Then the the worker connects to the master which spawns up a thread that tries to take the exact same lock. Here comes the trick &amp;#8211; the master thread will block until the lock is released, something that will only happen if the worker dies:&lt;br /&gt;
&lt;pre name=&quot;code&quot; class=&quot;java&quot;&gt;&lt;br /&gt;
synchronized(workerLock) { // will block here until worker dies&lt;br /&gt;
  &amp;#8230; // worker dead &amp;#8211; take proper action&lt;br /&gt;
}&lt;br /&gt;
&lt;/pre&gt;&lt;/p&gt;
&lt;ul&gt;
	&lt;li&gt;&lt;b&gt;Terracotta Server notifications&lt;/b&gt; &amp;#8211; Since detection of a node failure is such a common problem, an implementation in which the &lt;em&gt;Terracotta&lt;/em&gt; clients can subscribe on notifications of node death from the &lt;em&gt;Terracotta Server&lt;/em&gt; is underway and will be released in an upcoming version of &lt;em&gt;Terracotta&lt;/em&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;In all of the strategies we need to take proper action when worker failure has been detected. In our case it would (among other things) mean to reroute all non-completed work to another queue.&lt;/p&gt;
&lt;h2&gt;Rewrite the Terracotta config&lt;/h2&gt;
&lt;p&gt;As you perhaps remember from section &amp;#8220;Very high volumes of data?&amp;#8221;, in order to make the implementation more scalable we had to change the &lt;em&gt;SingleWorkQueue&lt;/em&gt; class to a &lt;em&gt;WorkQueueManager&lt;/em&gt; class and swap the single &lt;em&gt;LinkedBlockingQueue&lt;/em&gt; to a &lt;em&gt;ConcurrentHashMap&lt;/em&gt; with entries containing a &lt;em&gt;routing ID&lt;/em&gt; mapped to a &lt;em&gt;LinkedBlockingQueue&lt;/em&gt;. When we did that we also changed the name of the field and the name of class holding this field, this is something that needs to be reflected in the &lt;em&gt;Terracotta&lt;/em&gt; configuration file:&lt;/p&gt;
&lt;pre name=&quot;code&quot; class=&quot;java&quot;&gt;
...
&amp;lt;roots&amp;gt;
  &amp;lt;root&amp;gt;
    &amp;lt;field-name&amp;gt;org.terracotta.datagrid.workmanager.routing.DefaultWorkQueueManager.m_workQueues&amp;lt;/field-name&amp;gt;
  &amp;lt;/root&amp;gt;
&amp;lt;/roots&amp;gt;
...
&lt;/pre&gt;
&lt;h2&gt;How to use the &lt;span class=&quot;caps&quot;&gt;POJO&lt;/span&gt; Grid?&lt;/h2&gt;
&lt;p&gt;Here is an example of the usage of the &lt;em&gt;WorkManager&lt;/em&gt;, &lt;em&gt;WorkQueueManager&lt;/em&gt;, &lt;em&gt;Router&lt;/em&gt; and &lt;em&gt;WorkListener&lt;/em&gt; abstractions:&lt;/p&gt;
&lt;pre name=&quot;code&quot; class=&quot;java&quot;&gt;
// Create the work queue manager with &#39;String&#39; as routing ID type. The DefaultWorkQueueManager 
// is usually sufficient, but you can easily provide your own implementation of the 
// WorkQueueManager interface
WorkQueueManager workQueueManager = new DefaultWorkQueueManager();

// create the router - there are a bunch of default routers in the Router interface, 
// such as SingleQueueRouter, RoundRobinRouter and LoadBalancingRouter, but
// it is probably here that you need to plug in your custom implementaion 
Router router = new Router.LoadBalancingRouter(workQueueManager);   
    
// create the work manager - this implementation is very generic and most likely enough
// for you needs
WorkManager workManager = new RoutingAwareWorkManager(router);

// optionally (null is ok) create a work listener that will get a call back each time 
// the status of a WorkEvent (Work) has changed - allows you to for example take proper 
// action upon failure and retry etc. 
WorkListener workListener = null;

Set workSet = ... // create a set with the work to be done

// loop over the work set
for (Work work: workSet) {
  RoutableWorkItem workItem; // the work item, wrapping the work (result) and holds the status 
  try {
    // schedule the work, e.g. add it to the work queue and get the work item in return that can
    // be used to keep track of the pending work
    workItem = workManager.schedule(work, workListener); 
  } catch (WorkException e) {
    // handle failure
  }
}
&lt;/pre&gt;
&lt;p&gt;To start up a &lt;em&gt;Worker&lt;/em&gt; you simply have to create an instance of the &lt;em&gt;Worker&lt;/em&gt;, pass in a reference to the &lt;em&gt;DefaultWorkQueueManager&lt;/em&gt; and the routing id to use, and finally invoke &lt;em&gt;start():&lt;/em&gt;&lt;/p&gt;
&lt;pre name=&quot;code&quot; class=&quot;java&quot;&gt;
try {
  // create a work queue manager with &#39;String&#39; as routing ID type
  WorkQueueManager workQueueManager = new DefaultWorkQueueManager();
     
  // create and start up a Worker implementation - the provided RoutingAwareWorker 
  // implementation is usually enough
  // we pass in the work queue manager and the routing ID for this worker
  Worker worker = new RoutingAwareWorker(workQueueManager, &quot;ROUTING_ID_1&quot;);
  worker.start();
  
} catch (WorkException e) {
  // handle failure
}
&lt;/pre&gt;
&lt;h2&gt;Don&amp;#8217;t we need a result queue?&lt;/h2&gt;
&lt;p&gt;There is no need for a result queue, since the &lt;em&gt;Master&lt;/em&gt; is holding on to the &lt;em&gt;WorkItem&lt;/em&gt; e.g. the pending work (the work that is in progress) including its result. &lt;em&gt;Terracotta&lt;/em&gt; maintains Java&amp;#8217;s pass-by-reference semantics, the regular (local) reference to the &lt;em&gt;WorkItem&lt;/em&gt; that the &lt;em&gt;Master&lt;/em&gt; holds, will work transparently across the cluster. This means that a &lt;em&gt;Worker&lt;/em&gt; can update the exact same &lt;em&gt;WorkItem&lt;/em&gt; and the &lt;em&gt;Master&lt;/em&gt; would know about it immediately.&lt;/p&gt;
&lt;h2&gt;Enabling Terracotta&lt;/h2&gt;
&lt;p&gt;The client usage would roughly be to start up one &lt;em&gt;WorkManager&lt;/em&gt; and N number of &lt;em&gt;Workers&lt;/em&gt;, each one on a different &lt;span class=&quot;caps&quot;&gt;JVM&lt;/span&gt;. Before you start up the &lt;em&gt;WorkManager&lt;/em&gt; and &lt;em&gt;Workers&lt;/em&gt; you have to enable the &lt;em&gt;Terracotta&lt;/em&gt; runtime.&lt;/p&gt;
&lt;p&gt;There are two ways you can do this:&lt;/p&gt;
&lt;h3&gt;Use Terracotta wrapper script&lt;/h3&gt;
&lt;em&gt;Terracotta&lt;/em&gt; ships with a script that should be used as a drop-in replacement to the regular &lt;em&gt;java&lt;/em&gt; command. The name of the script is &lt;em&gt;dso-java&lt;/em&gt; and can be found in the &lt;em&gt;bin&lt;/em&gt; directory in the distribution. To use the recommended &lt;em&gt;Terracotta&lt;/em&gt; startup script:
&lt;ul&gt;
	&lt;li&gt;Find the &lt;em&gt;dso-java&lt;/em&gt; script in the &lt;em&gt;dso/bin&lt;/em&gt; directory.&lt;/li&gt;
	&lt;li&gt;Replace the regular invocation to &lt;em&gt;java&lt;/em&gt; with the invocation to &lt;em&gt;dso-java&lt;/em&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;Use &lt;span class=&quot;caps&quot;&gt;JVM&lt;/span&gt; options&lt;/h3&gt;
&lt;p&gt;You can also use additional &lt;span class=&quot;caps&quot;&gt;JVM&lt;/span&gt; options for applications inside the web container, perform the following:&lt;/p&gt;
&lt;ul&gt;
	&lt;li&gt;Prepend the &lt;em&gt;Terracotta Boot Jar&lt;/em&gt; to the &lt;em&gt;Java bootclasspath&lt;/em&gt;.&lt;/li&gt;
	&lt;li&gt;Define the path to the &lt;em&gt;Terracotta&lt;/em&gt; configuration file.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Here is an example (assuming you are running &lt;em&gt;Windows&lt;/em&gt;):&lt;/p&gt;
&lt;pre name=&quot;code&quot; class=&quot;java&quot;&gt;
java -Xbootclasspath/p:&amp;lt;path to terracotta boot jar&amp;gt; \
     -Dtc.config=path/to/your/tc-config.xml \
     -Dtc.install-root=&amp;lt;path to terracotta install dir&amp;gt; \
      ... &amp;lt;your regular options&amp;gt; ...
&lt;/pre&gt;
&lt;h2&gt;Run it&lt;/h2&gt;
&lt;p&gt;Now we are almost done, but before you spawn up the &lt;em&gt;Master&lt;/em&gt; and the &lt;em&gt;Workers&lt;/em&gt; we must first start the &lt;em&gt;Terracotta Server&lt;/em&gt;. This is done by invoking the &lt;em&gt;start-tc-server&lt;/em&gt; script in the &lt;em&gt;dso/bin&lt;/em&gt; directory. After you have done that then you can just start up the &lt;em&gt;Master&lt;/em&gt; and the &lt;em&gt;Workers&lt;/em&gt; (in any order you like).&lt;/p&gt;
&lt;p&gt;That is all there is to it.&lt;/p&gt;
&lt;h2&gt;Benefits of implementing a &lt;span class=&quot;caps&quot;&gt;POJO&lt;/span&gt;-based Data Grid&lt;/h2&gt;
&lt;p&gt;So let&amp;#8217;s wrap up with a brief discussion of the most immediate benefits of building and using a &lt;span class=&quot;caps&quot;&gt;POJO&lt;/span&gt;-based &lt;em&gt;Data Grid&lt;/em&gt;. Here are some of the benefits that we value:&lt;/p&gt;
&lt;ul&gt;
	&lt;li&gt;&lt;b&gt;Work with POJOs&lt;/b&gt; &amp;#8211; You get to work with POJOs, meaning simple and plain Java code. Any &lt;span class=&quot;caps&quot;&gt;POJO&lt;/span&gt; can be shared across the cluster and migrated between worker nodes. There is no need for implementing &lt;em&gt;Serializable&lt;/em&gt; or any other interface. It is as simple as Java can be.&lt;/li&gt;
	&lt;li&gt;&lt;b&gt;Event-driven development&lt;/b&gt; &amp;#8211; The &lt;em&gt;Master/Worker&lt;/em&gt; (and &lt;em&gt;CommonJ WorkManager&lt;/em&gt;) pattern leans itself towards event-driven development with no need for explicit threading and guarding. This can simplify the user code immensely compared to the use of explicit thread coordination.&lt;/li&gt;
	&lt;li&gt;&lt;b&gt;Simpler development and testing&lt;/b&gt; &amp;#8211; We have talked about how &lt;em&gt;Terracotta&lt;/em&gt; allows you to develop an application for a single &lt;span class=&quot;caps&quot;&gt;JVM&lt;/span&gt; and then simply cluster it in order to run it on multiple JVMs. This means that you can simplify the development and testing of your Data Grid application by doing the development and testing on your workstation, utilizing multiple threads instead of JVMs and then deploy the application onto multiple nodes at a later stage.&lt;/li&gt;
	&lt;li&gt;&lt;b&gt;White Box container implementation&lt;/b&gt; &amp;#8211; The whole grid implementation is &amp;#8220;under your fingers&amp;#8221;, nothing is really abstracted away from you as a developer. This gives you the freedom to design &lt;em&gt;Master&lt;/em&gt;, &lt;em&gt;Worker&lt;/em&gt;, routing algorithms, fail-over schemes etc. the way you need, you can customize everything as much as you want.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;Resources&lt;/h2&gt;
&lt;ul&gt;
	&lt;li&gt;Checkout the distribution for the &lt;span class=&quot;caps&quot;&gt;POJO&lt;/span&gt;-based Data Grid. It is a Maven 2 based project that includes a sample implementation of a distributed web spider. Read the readme.html for instructions on how to build and run the sample: &lt;br /&gt;
&lt;a href=&quot;http://svn.terracotta.org/svn/forge&quot;&gt;http://svn.terracotta.org/svn/forge&lt;/a&gt; &amp;#8211; (module projects/labs/opendatagrid)&lt;/li&gt;
	&lt;li&gt;Download Terracotta&amp;#8217;s &lt;span class=&quot;caps&quot;&gt;JVM&lt;/span&gt;-clustering technology &amp;#8211; Open Source: &lt;br /&gt;
&lt;a href=&quot;http://terracotta.org&quot;&gt;http://terracotta.org&lt;/a&gt;&lt;/li&gt;
	&lt;li&gt;Tutorial that is using the implementation outlined in this article to build a parallel web spider (that is part of the pojo-grid distribution): &lt;br /&gt;
&lt;a href=&quot;http://terracotta.org/confluence/display/orgsite/TutorialTerracottaDsoSpider&quot;&gt;http://terracotta.org/confluence/display/orgsite/TutorialTerracottaDsoSpider&lt;/a&gt;&lt;/li&gt;
	&lt;li&gt;Article &amp;#8211; Distributed Computing Made Easy: &lt;br /&gt;
&lt;a href=&quot;http://www.theserverside.com/tt/articles/article.tss?l=DistCompute&quot;&gt;http://www.theserverside.com/tt/articles/article.tss?l=DistCompute&lt;/a&gt;&lt;/li&gt;
	&lt;li&gt;Article &amp;#8211; Stateful Session Clustering: Have Your Availability and Scale It Too: &lt;br /&gt;
&lt;a href=&quot;http://www.devx.com/Java/Article/32603/&quot;&gt;http://www.devx.com/Java/Article/32603/&lt;/a&gt;&lt;/li&gt;
	&lt;li&gt;Documentation for Terracotta&amp;#8217;s &lt;span class=&quot;caps&quot;&gt;JVM&lt;/span&gt;-clustering technology: &lt;br /&gt;
&lt;a href=&quot;http://terracotta.org/confluence/display/orgsite/Documentation&quot;&gt;http://terracotta.org/confluence/display/orgsite/Documentation&lt;/a&gt;&lt;/li&gt;
	&lt;li&gt;Author&amp;#8217;s weblog:&lt;br /&gt;
&lt;a href=&quot;http://jonasboner.com&quot;&gt;http://jonasboner.com&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;</content>
 </entry>
 
 <entry>
   <title>...and the Award for Best Programming Font Goes To...</title>
   <link href="http://jboner.github.com/2007/01/11/and-the-award-for-best-programming-font-goes-to"/>
   <updated>2007-01-11T00:00:00+00:00</updated>
   <id>hhttp://jboner.github.com/2007/01/11/and-the-award-for-best-programming-font-goes-to</id>
   <content type="html">&lt;h1&gt;
&lt;p&gt;&amp;#8230;and the Award for Best Programming Font Goes To&amp;#8230;&lt;/p&gt;
&lt;/h1&gt;
&lt;p&gt;&lt;a href=&quot;http://www.tobias-jung.de/seekingprofont/&quot;&gt;ProFont&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;It is the best programming font I have seen and I use it all the time (for most stuff &amp;#8211; not just programming), both on Windows and Mac &amp;#8211; even though it looks slightly better on Mac. Do &lt;b&gt;not&lt;/b&gt; antialias it and use 9 pt (else it looks awful). Before I switched to ProFont I had to use at least 12 pt (Monaco for Mac and Consolas on Win) &amp;#8211; I am getting old I guess &amp;#8211; but ProFont is very readable at only 9 pt.&lt;/p&gt;
&lt;p&gt;Here is a screenshot so you can get the feel of it. For example, see how it clearly distinguishes between &amp;#8220;0&amp;#8221; (number zero) and &amp;#8220;O&amp;#8221; (capital letter O) and between &amp;#8220;1&amp;#8221; (number one), &amp;#8220;l&amp;#8221; (lowercase letter L) and &amp;#8220;I&amp;#8221; (capital letter I) &amp;#8211; something many fonts (including Monaco) have a problem with. But you should really try it out yourself.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;http://jonasboner.com/images/profont-sample.png&quot; alt=&quot;ProFont sample screenshot&quot; /&gt;&lt;/p&gt;</content>
 </entry>
 
 <entry>
   <title>In Search For the Ultimate Cross-Platform Backup Solution</title>
   <link href="http://jboner.github.com/2007/01/10/in-search-for-the-ultimate-cross-platform-backup-solution"/>
   <updated>2007-01-10T00:00:00+00:00</updated>
   <id>hhttp://jboner.github.com/2007/01/10/in-search-for-the-ultimate-cross-platform-backup-solution</id>
   <content type="html">&lt;h1&gt;
&lt;p&gt;In Search For the Ultimate Cross-Platform Backup Solution&lt;/p&gt;
&lt;/h1&gt;
&lt;p&gt;The last year I have been spending time finding the &amp;#8220;ultimate&amp;#8221; backup solution for Mac. I also have the requirement that it had to work equally good on Windows, since I am running both Windows and Mac. For example, I would like to be able to commit to my local [&lt;span class=&quot;caps&quot;&gt;SVN&lt;/span&gt;](http://subversion.tigris.org/) repository on my [MacBook](http://www.apple.com/macbookpro/), type one single command to upload the changes (only the deltas) to my backup hosting, then invoke one single command on my Windows box to retrieve the latest changes so I can build and test it on Windows &amp;#8211; and vice versa.&lt;/p&gt;
&lt;p&gt;The different approaches that I have been playing with the last year are:&lt;/p&gt;
&lt;ul&gt;
	&lt;li&gt;CD-RW&lt;/li&gt;
	&lt;li&gt;External HD with &lt;span class=&quot;caps&quot;&gt;USB&lt;/span&gt;/FireWire connection (small portable to carry around).&lt;/li&gt;
	&lt;li&gt;Running a Linux server on a spare box  (&lt;strong&gt;openssh&lt;/strong&gt;, &lt;strong&gt;rsync&lt;/strong&gt; etc.).&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Unfortunately, none of these alternatives really worked for me, each one had its problems.&lt;/p&gt;
&lt;p&gt;I wanted the backup solution to have:&lt;/p&gt;
&lt;p&gt;1. Zero administration time, it should just work &amp;#8211; on Windows and Mac.2. 24/7 uptime.&lt;br /&gt;
2. Fast connection.&lt;br /&gt;
3. Secure connection.&lt;br /&gt;
4. No cables or other hardware to carry around.&lt;/p&gt;
&lt;p&gt;Then I found [Amazon S3](http://aws.amazon.com/s3).&lt;/p&gt;
&lt;p&gt;It is inexpensive:&lt;/p&gt;
&lt;ul&gt;
	&lt;li&gt;Pay only for what you use. There is no minimum fee, and no start-up cost.&lt;/li&gt;
	&lt;li&gt;$0.15 per GB-Month of storage used.&lt;/li&gt;
	&lt;li&gt;$0.20 per GB of data transferred.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;It has a fairly rich &lt;span class=&quot;caps&quot;&gt;API&lt;/span&gt;:&lt;/p&gt;
&lt;ul&gt;
	&lt;li&gt;Write, read, and delete objects containing from 1 byte to 5 gigabytes of data each. The number of objects you can store is unlimited.&lt;/li&gt;
	&lt;li&gt;Each object is stored and retrieved via a unique, developer-assigned key.&lt;/li&gt;
	&lt;li&gt;Authentication mechanisms are provided to ensure that data is kept secure from unauthorized access. Objects can be made private or public, and rights can be granted to specific users.&lt;/li&gt;
	&lt;li&gt;Uses standards-based &lt;span class=&quot;caps&quot;&gt;REST&lt;/span&gt; and &lt;span class=&quot;caps&quot;&gt;SOAP&lt;/span&gt; interfaces designed to work with any Internet-development toolkit.&lt;/li&gt;
	&lt;li&gt;Built to be flexible so that protocol or functional layers can easily be added.  Default download protocol is &lt;span class=&quot;caps&quot;&gt;HTTP&lt;/span&gt;.  A BitTorrent&amp;#8482; protocol interface is provided to lower costs for high-scale distribution.  Additional interfaces will be added in the future.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Great stuff, seemed to do what I needed. So far so good. But then the next problem &amp;#8211; how to find a usable S3 client that:&lt;/p&gt;
&lt;ul&gt;
	&lt;li&gt;Worked equally good on Mac an on Windows.&lt;/li&gt;
	&lt;li&gt;Could be run in command mode (so I could schedule it with &lt;strong&gt;cron&lt;/strong&gt;).&lt;/li&gt;
	&lt;li&gt;Detected updated and new files &amp;#8211; e.g. did not just shovel everything up to the server.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;After X hours trying out different (bad and worse) clients I found [jetS3t](https://jets3t.dev.java.net/) &amp;#8211; which actually seemed very usable. It is a Java based application, that has two apps in one:&lt;/p&gt;
&lt;ul&gt;
	&lt;li&gt;[Cockpit](https://jets3t.dev.java.net/cockpit.html): a Swing &lt;span class=&quot;caps&quot;&gt;GUI&lt;/span&gt; program that is fairly easy to configure. It supports drag and drop, detects new and updated files, keeps the original file name &lt;b&gt;and&lt;/b&gt; path (more important than you think), compresses (gzip) files, encrypts them etc. Read the documentation for more details. It also has a Java applet that you can use if you want to download some files to a computer that does not have Cockpit installed: [http://jets3t.s3.amazonaws.com/index.html](http://jets3t.s3.amazonaws.com/index.html).&lt;/li&gt;
&lt;/ul&gt;
&lt;ul&gt;
	&lt;li&gt;[Synchronize](https://jets3t.dev.java.net/synchronize.html): a command line tool that has the same functionality as Cockpit but runs in headless mode. This is great if you want to schedule backups (using &lt;strong&gt;cron&lt;/strong&gt; or similar) or just want a simple &amp;#8211; single command &amp;#8211; way of synching up the data.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Good stuff. Perhaps not ultimate, but I am happy for now.&lt;/p&gt;
&lt;p&gt;Another thing that I have found very useful in S3 is the possibility of creating public URLs or Torrents that can be made available for a specific amount of time. This is very useful if you want to share files with friends. We even use this feature at [Terracotta](http://terracotta.org/) to host the distribution of our downloads.&lt;/p&gt;
&lt;p&gt;Thoughts? Can it be improved?&lt;/p&gt;
&lt;hr /&gt;
&lt;p&gt;&lt;b&gt;Update:&lt;/b&gt; [Geert Bevin](http://rifers.org/blogs) pointed me to the [js3tream](http://js3tream.sourceforge.net/) project. It plays nicely with standard shell tools such as &lt;strong&gt;tar&lt;/strong&gt;, &lt;strong&gt;gzip&lt;/strong&gt;, piping etc.: &lt;br /&gt;
&lt;pre&gt;&lt;br /&gt;
tar -C / -czpO /etc | java -jar js3tream.jar -K mykey.txt \&lt;br /&gt;
-b mybucket:etc.tgz -i -t &amp;#8220;An archive of my /etc directory&amp;#8221;&lt;br /&gt;
&lt;/pre&gt;&lt;br /&gt;
According to Geert it is working fine. I have to try it out.&lt;/p&gt;</content>
 </entry>
 
 <entry>
   <title>You're Smarter Than They Think</title>
   <link href="http://jboner.github.com/2007/01/09/youre-smarter-than-they-think"/>
   <updated>2007-01-09T00:00:00+00:00</updated>
   <id>hhttp://jboner.github.com/2007/01/09/youre-smarter-than-they-think</id>
   <content type="html">&lt;h1&gt;
&lt;p&gt;You&amp;#8217;re Smarter Than They Think&lt;/p&gt;
&lt;/h1&gt;
&lt;p&gt;[Seth Godin](http://sethgodin.typepad.com/) has written yet an amazing and inspirational piece on [how to be remarkable](http://sethgodin.typepad.com/seths_blog/2007/01/how_to_be_remar.html). It is a short write-up of some of the ideas he has been talking and writing about for years.&lt;/p&gt;
&lt;p&gt;Read it, be inspired and then&amp;#8230;&lt;b&gt;make something happen&lt;/b&gt;.&lt;/p&gt;
&lt;p&gt;I am currently reading his new book [Small Is the New Big](http://www.amazon.com/exec/obidos/&lt;span class=&quot;caps&quot;&gt;ASIN&lt;/span&gt;/1591841267/permissionmarket), which is a collection of the best columns, blog posts and articles he has written over the years. It&amp;#8217;s a gem.&lt;/p&gt;
&lt;p&gt;If you have the urge to achieve something more in life, feel stuck, don&amp;#8217;t dare taking the leap of faith to do what you are made to do and have a creativity nerve that keeps on nagging, then read it. Please read it. It might actually change your life.&lt;/p&gt;
&lt;p&gt;&amp;#8230;as its back cover boldly proclaims: &amp;#8220;you&amp;#8217;re smarter than they think&amp;#8221;.&lt;/p&gt;</content>
 </entry>
 
 <entry>
   <title>Why Did We Open Source Terracotta?</title>
   <link href="http://jboner.github.com/2007/01/05/why-did-we-open-source-terracotta"/>
   <updated>2007-01-05T00:00:00+00:00</updated>
   <id>hhttp://jboner.github.com/2007/01/05/why-did-we-open-source-terracotta</id>
   <content type="html">&lt;h1&gt;
&lt;p&gt;Why Did We Open Source Terracotta?&lt;/p&gt;
&lt;/h1&gt;
&lt;p&gt;The past month I have been asked the same question over and over again: &amp;#8220;Why did you open source Terracotta?&amp;#8221;. It is odd, but it seems that for some people the question &amp;#8220;why did they do it?&amp;#8221; is much more interesting then &amp;#8220;what does it mean &amp;#8211; for me &amp;#8211; for the industry?&amp;#8221;&lt;/p&gt;
&lt;p&gt;I will continue to spend most of my time trying to answer the latter question. But since the former one keeps coming up and is for many people still an unanswered question, I will point you to a good write up on the subject written by our &lt;span class=&quot;caps&quot;&gt;CEO&lt;/span&gt; Amit Pandey:&lt;/p&gt;
&lt;blockquote&gt;&lt;a href=&quot;http://opensource.sys-con.com/read/318781.htm&quot;&gt;Building a Sustainable Open Source Business&lt;/a&gt;&lt;br /&gt;
â€” Terracotta took a major step this month by open sourcing all of our Java clustering technology. We believe that this step will make a powerful clustering option readily available to the Java community and indeed could help make the notion of clustering itself simpler and more ubiquitous. This is an initiative that is many months in the making, and I wanted to share our thinking and motivation for it with you.&lt;/blockquote&gt;</content>
 </entry>
 
 <entry>
   <title>Terracotta is Open Source</title>
   <link href="http://jboner.github.com/2006/12/04/terracotta-is-open-source"/>
   <updated>2006-12-04T00:00:00+00:00</updated>
   <id>hhttp://jboner.github.com/2006/12/04/terracotta-is-open-source</id>
   <content type="html">&lt;h1&gt;
&lt;p&gt;Terracotta is Open Source&lt;/p&gt;
&lt;/h1&gt;
&lt;p&gt;Today we open sourced Terracotta and launched a new site for the Open Terracotta Project; &lt;a href=&quot;http://terracotta.org/&quot;&gt;http://terracotta.org&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;http://terracotta.org/confluence/download/userResources/logo&quot; alt=&quot;Open Terracotta&quot; /&gt;&lt;/p&gt;
&lt;p&gt;Personally, I am very excited and think that Open Terracotta&amp;#8217;s potential impact on the Java community cannot be underestimated. Even though it is in clustering, meaning scalability and high-availability, that it can bring its most immediate value to the community, it is really a platform for solving generic distributed computing and shared memory problems in plain Java code &amp;#8211; something that makes it applicable to a wide range problem domains. I am very curious to see what the Java community &amp;#8211; thinking out-of-the-box &amp;#8211; can/will do with it.&lt;/p&gt;
&lt;p&gt;Get involved. Download it, play with it, use it and then contribute.&lt;/p&gt;</content>
 </entry>
 
 <entry>
   <title>Clustering Your Struts 2 Application With Terracotta</title>
   <link href="http://jboner.github.com/2006/11/27/clustering-your-struts-2-application-with-terracotta"/>
   <updated>2006-11-27T00:00:00+00:00</updated>
   <id>hhttp://jboner.github.com/2006/11/27/clustering-your-struts-2-application-with-terracotta</id>
   <content type="html">&lt;h1&gt;
&lt;p&gt;Clustering Your Struts 2 Application With Terracotta&lt;/p&gt;
&lt;/h1&gt;
&lt;p&gt;Don Brown (Struts 2 lead) and I are doing a live webinar on clustering Apache Struts 2 with Terracotta. We will discuss how to build stateful Struts 2 applications that scales like stateless ones and varying solutions to clustering Struts when running enterprise applications.&lt;/p&gt;
&lt;p&gt;Wednesday, November 29, 2006 8:00 am Pacific Daylight Time.&lt;/p&gt;
&lt;p&gt;&lt;a href=&quot;https://terracottat.webex.com/ec0507l/eventcenter/event/eventAction.do?siteurl=terracottat&amp;theAction=detail&amp;confViewID=277584840&amp;path=edit_sucessful&amp;eventBackUrl=scheduler&amp;elq=FC611C350E3F40A9AAB5625F70DF0120&quot;&gt;Enroll here to attend.&lt;/a&gt;&lt;/p&gt;</content>
 </entry>
 
 <entry>
   <title>Doom and the $3000 suite</title>
   <link href="http://jboner.github.com/2006/11/17/doom-and-the-3000-suite"/>
   <updated>2006-11-17T00:00:00+00:00</updated>
   <id>hhttp://jboner.github.com/2006/11/17/doom-and-the-3000-suite</id>
   <content type="html">&lt;h1&gt;
&lt;p&gt;Doom and the $3000 suite&lt;/p&gt;
&lt;/h1&gt;
&lt;p&gt;Doom and a $3000 suite. Two things that have nothing in common more than being the things that I remember most from the &lt;a href=&quot;http://www.oredev.org/&quot;&gt;Oredev conference&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;First Doom. &lt;a href=&quot;http://rifers.org/blogs&quot;&gt;Geert Bevin&lt;/a&gt; (founder of &lt;span class=&quot;caps&quot;&gt;RIFE&lt;/span&gt;) did one of the most fun and hard-to-forget presentation openings I&amp;#8217;ve seen.  When I among the rest of the attendees, walked into the session room, Geert was right in the middle of a pretty violent first-person-shooter game (I think it was Doom) with monsters and blood all over the big movie screen behind him. He seemed very much into the game and continued to play while the audience filled the room.  Eventually things got tougher and tougher, and just before he was about to die; he did &amp;#8216;&lt;em&gt;Save Game&lt;/em&gt;&amp;#8217;, switched to his slides, introduced himself and the topic for the day: &lt;em&gt;Continuations&lt;/em&gt;. :-)&lt;/p&gt;
&lt;p&gt;One of the best illustrations of continuations I have seen, and one that noone will ever forget.&lt;/p&gt;
&lt;p&gt;Second, the $3000 suite. When I arrived to my hotel to check in before going to the conference, it turned out that the Oredev staff had forgotten to reserve me a room. The hotel was fully booked apart from the top suite for $3000. So I sighed, took my stuff and went to the conference. I told the staff about my problem and asked them to solve it and after a little while they came up to  me and told me they could get the suite. So I ended up sleeping in hotel room almost as big as our house. Any good? Well, I had to say that it only felt like a complete waste of money (luckily not my money), but now I at least know what people are paying for.&lt;/p&gt;
&lt;p&gt;Other highlights.&lt;/p&gt;
&lt;p&gt;&lt;a href=&quot;http://jroller.com/page/rickard&quot;&gt;Rickard Oberg&lt;/a&gt; did a very interesting talk about &lt;a href=&quot;http://www.w3.org/RDF/&quot;&gt;&lt;span class=&quot;caps&quot;&gt;RDF&lt;/span&gt;&lt;/a&gt;, &lt;a href=&quot;http://www.w3.org/TR/rdf-sparql-query/&quot;&gt;&lt;span class=&quot;caps&quot;&gt;SPARQL&lt;/span&gt;&lt;/a&gt; and the Semantic Web. In which &lt;span class=&quot;caps&quot;&gt;RDF&lt;/span&gt; can be used as a data model to index data from all kinds of data storages (&lt;span class=&quot;caps&quot;&gt;RDBMS&lt;/span&gt;, &lt;span class=&quot;caps&quot;&gt;OOBMDS&lt;/span&gt;, &lt;span class=&quot;caps&quot;&gt;LDAP&lt;/span&gt;, flat file etc.) which can then be queried using &lt;span class=&quot;caps&quot;&gt;SPARQL&lt;/span&gt;. It all felt both natural and very much like Tuple Spaces.&lt;/p&gt;
&lt;p&gt;I did a talk about how &lt;a href=&quot;http://terracottatech.com/&quot;&gt;Terracotta&lt;/a&gt; can be used as a generic distributed computing platform, that turns plain Java into a language with native and transparent distribution (something that you normally would need to use some narrow, academic and/or obscure languages to do &amp;#8212; Erlang, Oz etc.). The talk was driven by the exercise of building a &lt;span class=&quot;caps&quot;&gt;POJO&lt;/span&gt;-based Data Grid and how to scale it out using Locality of Reference and moving operations to the data (and not the contrary).&lt;/p&gt;</content>
 </entry>
 
 <entry>
   <title>Back From Serbia</title>
   <link href="http://jboner.github.com/2006/11/15/back-from-serbia"/>
   <updated>2006-11-15T00:00:00+00:00</updated>
   <id>hhttp://jboner.github.com/2006/11/15/back-from-serbia</id>
   <content type="html">&lt;h1&gt;
&lt;p&gt;Back From Serbia&lt;/p&gt;
&lt;/h1&gt;
&lt;p&gt;I just got back from a small &lt;span class=&quot;caps&quot;&gt;JUG&lt;/span&gt; roadshow in Serbia. I did two 2 1/2 hours talk in two days, the first one in Novi Sad and the second one in Belgrade. Both talks were well attended, ~100 attendees per session. Serbia seems to be a thriving Java community and there was a lot of interest in both &lt;a href=&quot;http://terracottatech.com/terracotta_spring.shtml&quot;&gt;Terracotta for Spring/Spring Web Flow&lt;/a&gt; as well as in using &lt;a href=&quot;http://terracottatech.com/terracotta_DSO.shtml&quot;&gt;Terracotta &lt;span class=&quot;caps&quot;&gt;DSO&lt;/span&gt;&lt;/a&gt; as a generic distributed computing platform.&lt;/p&gt;
&lt;p&gt;During these two days I met a lot of interesting and warm hearted people, and was met with great hospitality throughout the trip. Unfortunately I did not have enough time to see much of the country, but I got a Serbia Travel Guide book as a gift so I guess I have to come back some time (and bring the family).&lt;/p&gt;
&lt;p&gt;Finally I had a really good time (and interesting discussions) going out to lunch with &lt;a href=&quot;http://www.infoq.com/articles/SpringDotNET-QnA&quot;&gt;Aleks Seovic&lt;/a&gt;, the &lt;a href=&quot;http://www.springframework.net/&quot;&gt;Spring.&lt;span class=&quot;caps&quot;&gt;NET&lt;/span&gt;&lt;/a&gt; lead (currently living in Belgrade), talking about &lt;a href=&quot;http://terracottatech.com/&quot;&gt;Terracotta &lt;/a&gt;vs. &lt;a href=&quot;http://tangosol.com/html/index.shtml&quot;&gt;Tangosol&lt;/a&gt;, &lt;a href=&quot;http://www.java.com/en/&quot;&gt;Java &lt;/a&gt;vs. &lt;a href=&quot;http://www.microsoft.com/net/default.mspx&quot;&gt;.&lt;span class=&quot;caps&quot;&gt;NET&lt;/span&gt;&lt;/a&gt;, including more important things than geeky matters.&lt;/p&gt;
&lt;p&gt;All in all, a great trip.&lt;/p&gt;</content>
 </entry>
 
 <entry>
   <title>Hmm...that ain't bad</title>
   <link href="http://jboner.github.com/2006/11/08/hmmthat-aint-bad"/>
   <updated>2006-11-08T00:00:00+00:00</updated>
   <id>hhttp://jboner.github.com/2006/11/08/hmmthat-aint-bad</id>
   <content type="html">&lt;h1&gt;
&lt;p&gt;Hmm&amp;#8230;that ain&amp;#8217;t bad&lt;/p&gt;
&lt;/h1&gt;
&lt;p&gt;
&lt;p&gt;Linear scaling for &lt;span class=&quot;caps&quot;&gt;HTTP&lt;/span&gt; session clustering?&lt;/p&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;p&gt;I didn&amp;#8217;t think it was possible, but the latest benchmark for Terracotta Sessions clustering Tomcat for sure looks promising:&lt;/p&gt;
&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;http://blog.terracottatech.com/archive/sessions-benchmark.png&quot; alt=&quot;Linear Scaling for HTTP Session Clustering&quot; /&gt;&lt;/p&gt;
&lt;p&gt;
&lt;p&gt;&lt;a href=&quot;http://blog.terracottatech.com/archive/2006/10/linear_scaling.html&quot;&gt;Read this post&lt;/a&gt; if you want more details.&lt;/p&gt;
&lt;/p&gt;</content>
 </entry>
 
 <entry>
   <title>Webinar: Grid Computing Simplified Using Network-Attached Memory</title>
   <link href="http://jboner.github.com/2006/10/19/webinar-grid-computing-simplified-using-network-attached-memory"/>
   <updated>2006-10-19T00:00:00+00:00</updated>
   <id>hhttp://jboner.github.com/2006/10/19/webinar-grid-computing-simplified-using-network-attached-memory</id>
   <content type="html">&lt;h1&gt;
&lt;p&gt;Webinar: Grid Computing Simplified Using Network-Attached Memory&lt;/p&gt;
&lt;/h1&gt;
&lt;p&gt;Terracotta is hosting webinar on how to do grid computing using standard Java 5 code.&lt;/p&gt;
&lt;p&gt;This session focuses on the Master/Worker pattern &amp;#8212; one of the most useful parallel programming design patterns in use today.&lt;/p&gt;
&lt;p&gt;You will learn about the identifying characteristics of the pattern and how best to use it in the real world.&lt;/p&gt;
&lt;p&gt;It is a very practical, &amp;#8220;hands-on&amp;#8221; session in which we will present how to build the Master/Worker pattern using standard &lt;span class=&quot;caps&quot;&gt;JDK&lt;/span&gt; 1.5, and how to turn it into a distributed grid implementation by using Network-Attached Memory. Following that, we will cover the production implications of operating and scaling a reliable work management framework.&lt;/p&gt;
&lt;p&gt;Attendees will learn:&lt;/p&gt;
&lt;ul&gt;
	&lt;li&gt;How to identify cases where the Master/Worker pattern is the right solution&lt;/li&gt;
	&lt;li&gt;How to build a distributed Master/Worker pattern with 100% &lt;span class=&quot;caps&quot;&gt;POJO&lt;/span&gt;-based Java&lt;/li&gt;
	&lt;li&gt;How to cluster the &lt;span class=&quot;caps&quot;&gt;JVM&lt;/span&gt; and effectively use Network-Attached Memory&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;More information about the webinar can be found &lt;a href=&quot;http://www.terracottatech.com/webinar_masterworker.shtml&quot;&gt;here&lt;/a&gt;.&lt;/p&gt;</content>
 </entry>
 
 <entry>
   <title>Interviewed by InfoQ</title>
   <link href="http://jboner.github.com/2006/09/25/interviewed-by-infoq"/>
   <updated>2006-09-25T00:00:00+00:00</updated>
   <id>hhttp://jboner.github.com/2006/09/25/interviewed-by-infoq</id>
   <content type="html">&lt;h1&gt;
&lt;p&gt;Interviewed by InfoQ&lt;/p&gt;
&lt;/h1&gt;
&lt;p&gt;&lt;a href=&quot;http://www.infoq.com/&quot;&gt;InfoQ &lt;/a&gt;just posted their &lt;a href=&quot;http://www.infoq.com/news/terracotta-spring&quot;&gt;interview with me&lt;/a&gt;. Topics discussed are:&lt;/p&gt;
&lt;ul&gt;
	&lt;li&gt;Features in &lt;a href=&quot;http://terracottatech.com/terracotta_spring.shtml&quot;&gt;Terracotta for Spring&lt;/a&gt;&lt;/li&gt;
	&lt;li&gt;What it brings to the table vs. &lt;a href=&quot;http://terracottatech.com/terracotta_DSO.shtml&quot;&gt;Terracotta &lt;span class=&quot;caps&quot;&gt;DSO&lt;/span&gt;&lt;/a&gt;&lt;/li&gt;
	&lt;li&gt;Clustering of &lt;span class=&quot;caps&quot;&gt;JMX&lt;/span&gt; data&lt;/li&gt;
	&lt;li&gt;XA compliance and &lt;span class=&quot;caps&quot;&gt;JEE&lt;/span&gt; transactions in general&lt;/li&gt;
	&lt;li&gt;How &lt;a href=&quot;http://terracottatech.com/terracotta_spring.shtml&quot;&gt;Terracotta for Spring&lt;/a&gt; compares to &amp;#8220;Data Grids&amp;#8221; and &lt;span class=&quot;caps&quot;&gt;API&lt;/span&gt;-based clustering products&lt;/li&gt;
&lt;/ul&gt;
</content>
 </entry>
 
 <entry>
   <title>Using AOP to Cluster the JVM: Aspect Leadership Program</title>
   <link href="http://jboner.github.com/2006/09/22/using-aop-to-cluster-the-jvm-aspect-leadership-program"/>
   <updated>2006-09-22T00:00:00+00:00</updated>
   <id>hhttp://jboner.github.com/2006/09/22/using-aop-to-cluster-the-jvm-aspect-leadership-program</id>
   <content type="html">&lt;h1&gt;
&lt;p&gt;Using &lt;span class=&quot;caps&quot;&gt;AOP&lt;/span&gt; to Cluster the &lt;span class=&quot;caps&quot;&gt;JVM&lt;/span&gt;: Aspect Leadership Program&lt;/p&gt;
&lt;/h1&gt;
&lt;p&gt;If you are interested in knowing more about &lt;span class=&quot;caps&quot;&gt;AOP&lt;/span&gt; and how it can be used to simplify distributed computing or want to know more about the inner workings of &lt;a href=&quot;http://terracottatech.com/&quot;&gt;Terrocotta&amp;#8217;s&lt;/a&gt; Distributed Shared Object (&lt;span class=&quot;caps&quot;&gt;DSO&lt;/span&gt;) technology, then you should sign up for the &lt;a href=&quot;http://aspectleadershipprogram.net/index.php&quot;&gt;Aspect Leadership Program&lt;/a&gt; in Vancouver, March 14 &amp;amp; 15 2007.&lt;/p&gt;
&lt;p&gt;I will be giving a talk on &lt;a href=&quot;http://aspectleadershipprogram.net/program.php#Cluster%20JVM&quot;&gt;Using &lt;span class=&quot;caps&quot;&gt;AOP&lt;/span&gt; to Cluster the &lt;span class=&quot;caps&quot;&gt;JVM&lt;/span&gt;&lt;/a&gt; and that is just one out of &lt;a href=&quot;http://aspectleadershipprogram.net/program.php&quot;&gt;many talks&lt;/a&gt; during these two days:&lt;br /&gt;
&lt;blockquote&gt;&lt;br /&gt;
In just two days, the Aspect Leadership Program (&lt;span class=&quot;caps&quot;&gt;ALP&lt;/span&gt;) starts with an introduction to &lt;span class=&quot;caps&quot;&gt;AOP&lt;/span&gt;, and proceeds through key points about the technology and tools, enterprise applications, case studies, refactoring existing applications, future trends and other topics.&lt;/p&gt;
&lt;p&gt;The discussion-oriented coordinated presentations by world-leading experts make it possible to cover so much ground in just two days. Having seven of the world&amp;#8217;s leading experts presenting means that you can get different perspectives on the key issues all in one setting.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;Registration has already &lt;a href=&quot;http://aspectleadershipprogram.net/registration.php&quot;&gt;started&lt;/a&gt;. Hope to see you there.&lt;/p&gt;</content>
 </entry>
 
 <entry>
   <title>How To Implement a Distributed CommonJ WorkManager</title>
   <link href="http://jboner.github.com/2006/09/14/how-to-implement-a-distributed-commonj-workmanager"/>
   <updated>2006-09-14T00:00:00+00:00</updated>
   <id>hhttp://jboner.github.com/2006/09/14/how-to-implement-a-distributed-commonj-workmanager</id>
   <content type="html">&lt;h1&gt;
&lt;p&gt;How To Implement a Distributed CommonJ WorkManager&lt;/p&gt;
&lt;/h1&gt;
&lt;p&gt;In this article I will show you how to implement a distributed version of the &lt;em&gt;CommonJ WorkManager&lt;/em&gt; specification using &lt;a href=&quot;http://www.terracottatech.com/terracotta_spring.shtml&quot;&gt;Terracotta for Spring&lt;/a&gt;. This article is a variation of an article that I wrote for &lt;a href=&quot;http://www.theserverside.com/tss&quot;&gt;TheServerSide.com&lt;/a&gt; titled &lt;em&gt;Distributed Computing Made Easy&lt;/em&gt;, an article that can be found &lt;a href=&quot;http://www.theserverside.com/tt/articles/article.tss?l=DistCompute&quot;&gt;here&lt;/a&gt;.&lt;/p&gt;
&lt;h2&gt;What is CommonJ WorkManager?&lt;/h2&gt;
&lt;p&gt;&lt;em&gt;CommonJ&lt;/em&gt; is a &lt;span class=&quot;caps&quot;&gt;BEA&lt;/span&gt; and &lt;span class=&quot;caps&quot;&gt;IBM&lt;/span&gt; joint specification that provides a standard for executing concurrent tasks in a &lt;span class=&quot;caps&quot;&gt;JEE&lt;/span&gt; environment. It for example has support for the &lt;em&gt;Master/Worker pattern&lt;/em&gt; in its &lt;em&gt;WorkManager &lt;span class=&quot;caps&quot;&gt;API&lt;/span&gt;&lt;/em&gt;.&lt;/p&gt;
&lt;p&gt;From BEA&amp;#8217;s documentation about the specification:&lt;/p&gt;
&lt;blockquote&gt;&amp;#8220;The Work Manager provides a simple &lt;span class=&quot;caps&quot;&gt;API&lt;/span&gt; for application-server-supported concurrent execution of work items. This enables J2EE-based applications (including Servlets and EJBs) to schedule work items for concurrent execution, which will provide greater throughput and increased response time. After an application submits work items to a Work Manager for concurrent execution, the application can gather the results. The Work Manager provides common &amp;#8220;join&amp;#8221; operations, such as waiting for any or all work items to complete. The Work Manager for Application Servers specification provides an application-server-supported alternative to using lower-level threading APIs, which are inappropriate for use in managed environments such as Servlets and EJBs, as well as being too difficult to use for most applications.&amp;quot;&lt;/blockquote&gt;
&lt;p&gt;What we are going to do is to first implement a the specification as a regular single node multi-threaded application, based on the &lt;em&gt;Master/Worker&lt;/em&gt; pattern. We are also going to use the &lt;a href=&quot;http://springframework.org/&quot;&gt;Spring Framework&lt;/a&gt; and implement the &lt;em&gt;Master&lt;/em&gt;, &lt;em&gt;Worker&lt;/em&gt; and &lt;em&gt;Shared Queue&lt;/em&gt; entities as three different &lt;em&gt;Spring&lt;/em&gt; beans; &lt;code&gt;MyWorkManager&lt;/code&gt;, &lt;code&gt;Worker&lt;/code&gt; and &lt;code&gt;WorkQueue&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;We will then use &lt;a href=&quot;http://www.terracottatech.com/terracotta_spring.shtml&quot;&gt;Terracotta for Spring&lt;/a&gt;* to transparently and declaratively, turn this implementation into a multi-node, distributed &lt;em&gt;WorkManager&lt;/em&gt;.&lt;/p&gt;
&lt;h2&gt;Master (WorkManager)&lt;/h2&gt;
&lt;p&gt;&lt;code&gt;MyWorkManager&lt;/code&gt; bean implements the &lt;em&gt;CommonJ WorkManager&lt;/em&gt; interface which has the &lt;span class=&quot;caps&quot;&gt;API&lt;/span&gt; that the user uses to schedule &lt;code&gt;Work&lt;/code&gt; and wait for all &lt;code&gt;Work&lt;/code&gt; to be completed. The &lt;code&gt;MyWorkManager&lt;/code&gt; bean does not have any state, and can therefore be configured as a &lt;em&gt;Prototype&lt;/em&gt; in the &lt;em&gt;Spring&lt;/em&gt; bean config &lt;span class=&quot;caps&quot;&gt;XML&lt;/span&gt; file.&lt;/p&gt;
&lt;p&gt;Here is how we could implement the work manager bean:&lt;/p&gt;
&lt;pre name=&quot;code&quot; class=&quot;java&quot;&gt;
public class MyWorkManager implements WorkManager { 

  // The Work Queue bean, is injected by Spring
  private final WorkQueue m_queue;

  public MyWorkManager(WorkQueue queue) {
    m_queue = queue;
  }

  public WorkItem schedule(final Work work) throws WorkException {
    WorkItem workItem = new MyWorkItem(work, null);
    m_queue.addWork(workItem);
    return workItem;
  }

  public WorkItem schedule(Work work, WorkListener listener) 
    throws WorkException {
    WorkItem workItem = new MyWorkItem(work, listener);
    m_queue.addWork(workItem); // adds work to the shared queue
    return workItem;
  }

  public boolean waitForAll(Collection workItems, long timeout) {
    long start = System.currentTimeMillis();
    do {
      boolean isAllCompleted = true;
      for (Iterator it = workItems.iterator(); 
           it.hasNext() &amp;amp;&amp;amp; isAllCompleted;) {
        int status = ((WorkItem) it.next()).getStatus();
        isAllCompleted = 
            status == WorkEvent.WORK_COMPLETED || 
            status == WorkEvent.WORK_REJECTED;
      }
      if (isAllCompleted) { return true; }
      if (timeout == IMMEDIATE) { return false; }
      if (timeout == INDEFINITE) { continue; }
    } while ((System.currentTimeMillis() - start) &amp;lt; timeout);
    return false;
  }

  public Collection waitForAny(Collection workItems, long timeout) {
    long start = System.currentTimeMillis();
    do {
      synchronized (this) {
        Collection completed = new ArrayList();
        for (Iterator it = workItems.iterator(); it.hasNext();) {
          WorkItem workItem = (WorkItem) it.next();
          if (workItem.getStatus() == WorkEvent.WORK_COMPLETED || 
              workItem.getStatus() == WorkEvent.WORK_REJECTED) {
            completed.add(workItem);
          }
        }
        if (!completed.isEmpty()) { return completed; }
      }
      if (timeout == IMMEDIATE) { return Collections.EMPTY_LIST; }
      if (timeout == INDEFINITE) { continue; }
    } while ((System.currentTimeMillis() - start) &amp;lt; timeout);
    return Collections.EMPTY_LIST;
  }
}
&lt;/pre&gt;
&lt;h2&gt;Shared Queue&lt;/h2&gt;
&lt;p&gt;The &lt;code&gt;MyWorkManager&lt;/code&gt; bean schedules work by adding work to the &lt;code&gt;WorkQueue&lt;/code&gt; bean, which is a simple wrapper around a &lt;code&gt;java.util.concurrent.BlockingQueue&lt;/code&gt; queue. The &lt;code&gt;WorkQueue&lt;/code&gt; bean is the bean that has state, since it holds the queue with all the pending &lt;code&gt;Work&lt;/code&gt;. We need to have a single instance of this queue that can be available to all workers, and we therefore define it as &lt;em&gt;Singleton&lt;/em&gt; in the bean config &lt;span class=&quot;caps&quot;&gt;XML&lt;/span&gt; file.&lt;/p&gt;
&lt;p&gt;The work queue can be implemented like this:&lt;/p&gt;
&lt;p&gt;&lt;/pre&gt;&lt;pre name=&quot;code&quot; class=&quot;java&quot;&gt;&lt;br /&gt;
public class WorkQueue {&lt;br /&gt;
  private final BlockingQueue m_workQueue;&lt;/p&gt;
public WorkQueue() {
m_workQueue = new LinkedBlockingQueue();
}
public WorkQueue(int capacity) {
m_workQueue = new LinkedBlockingQueue(capacity);
}
public MyWorkItem getWork() throws WorkException {
try {
return (MyWorkItem) m_workQueue.take(); // blocks if empty
} catch (InterruptedException e) {
throw new WorkException(e);
}
}
public void addWork(WorkItem workItem) throws WorkException {
try {
m_workQueue.put(workItem);
} catch (InterruptedException e) {
WorkRejectedException we =
new WorkRejectedException(e.getMessage());
((MyWorkItem)workItem).setStatus(WorkEvent.WORK_REJECTED, we);
throw we;
}
}
&lt;p&gt;}&lt;br /&gt;
&lt;/pre&gt;&lt;/p&gt;
&lt;h2&gt;Worker&lt;/h2&gt;
&lt;p&gt;Finally, we have the &lt;code&gt;Worker&lt;/code&gt; bean. This bean uses a thread pool to spawn up N number of worker threads that continuously grabs and executes Work from the &lt;code&gt;WorkQueue&lt;/code&gt;. During the processing of the &lt;code&gt;Work&lt;/code&gt;, its status flag is maintained (can be one of either Accepted, Started, Completed or Rejected), this is needed in order for the &lt;code&gt;MyWorkManager&lt;/code&gt; bean to be able to continuously monitor the status of the Work it has scheduled. The &lt;code&gt;Worker&lt;/code&gt; bean does not have any shared state and is configured as a &lt;em&gt;Prototype&lt;/em&gt; in the bean config &lt;span class=&quot;caps&quot;&gt;XML&lt;/span&gt; file.&lt;/p&gt;
&lt;p&gt;This is what a worker bean implementation can look like. As you can see we choose to make use of the &lt;code&gt;Executor&lt;/code&gt; thread pool implementation in the &lt;code&gt;java.util.concurrent&lt;/code&gt; package:&lt;/p&gt;
&lt;pre name=&quot;code&quot; class=&quot;java&quot;&gt;
public class Worker {

  private transient final WorkQueue  m_queue;
  private transient final ExecutorService m_threadPool = 
      Executors.newCachedThreadPool();
  private volatile boolean m_isRunning  = true;

  public Worker(WorkQueue queue) {
    m_queue = queue;
  }

  public void start() throws WorkException {
    while (m_isRunning) {  
      final MyWorkItem workItem = m_queue.getWork();
      m_threadPool.execute(new Runnable() {
        public void run() {
          try {
            Work work = workItem.getResult();
            workItem.setStatus(WorkEvent.WORK_STARTED, null);
            work.run();
            workItem.setStatus(WorkEvent.WORK_COMPLETED, null);
          } catch (Throwable e) {
            workItem.setStatus(
                WorkEvent.WORK_REJECTED, 
                new WorkRejectedException(e.getMessage()));
          }
        });
      }
    }
  }
}
&lt;/pre&gt;
&lt;h2&gt;Assembly&lt;/h2&gt;
&lt;p&gt;These three beans can now be wired up by the &lt;em&gt;Spring&lt;/em&gt; bean config file:&lt;/p&gt;
&lt;pre name=&quot;code&quot; class=&quot;java&quot;&gt;
&amp;amp;lt;beans&amp;amp;gt;
  &amp;amp;lt;!-- workManager is prototype - not shared --&amp;amp;gt;
  &amp;amp;lt;bean id=&quot;workManager&quot;
        class=&quot;com.jonasboner.commonj.workmanager.MyWorkManager&quot;     
        singleton=&quot;false&quot;&amp;amp;gt;
    &amp;amp;lt;constructor-arg ref=&quot;queue&quot;/&amp;amp;gt;
  &amp;amp;lt;/bean&amp;amp;gt;
    
   &amp;amp;lt;!-- worker is prototype - not shared --&amp;amp;gt;
  &amp;amp;lt;bean id=&quot;worker&quot; 
        class=&quot;com.jonasboner.commonj.workmanager.Worker&quot; 
        singleton=&quot;false&quot;&amp;amp;gt;
    &amp;amp;lt;constructor-arg ref=&quot;queue&quot;/&amp;amp;gt;
  &amp;amp;lt;/bean&amp;amp;gt;

  &amp;amp;lt;!-- the work queue is singleton - can be made shared by Terracotta --&amp;amp;gt;
  &amp;amp;lt;bean id=&quot;queue&quot; 
        class=&quot;com.jonasboner.commonj.workmanager.WorkQueue&quot;/&amp;amp;gt;

&amp;amp;lt;/beans&amp;amp;gt;
&lt;/pre&gt;
&lt;p&gt;We now have a fully functional local, multi-threaded, implementation of the &lt;em&gt;CommonJ WorkManager&lt;/em&gt; specification.&lt;/p&gt;
&lt;h2&gt;Making the WorkManager distributed&lt;/h2&gt;
&lt;p&gt;Now comes the hard part right? Well&amp;#8230;no.&lt;/p&gt;
&lt;p&gt;It turns out that in order to turn this implementation into a distributed &lt;code&gt;WorkManager&lt;/code&gt;, all we have to do is to create a &lt;em&gt;Terracotta&lt;/em&gt; configuration file in which we declare the &lt;em&gt;Spring&lt;/em&gt; beans that we want to share across the cluster:&lt;/p&gt;
&lt;pre name=&quot;code&quot; class=&quot;java&quot;&gt;     
...
&amp;amp;lt;spring&amp;amp;gt;
  &amp;amp;lt;jee-application name=&quot;webAppName&quot;&amp;amp;gt;
    &amp;amp;lt;application-contexts&amp;amp;gt;
      &amp;amp;lt;application-context&amp;amp;gt;
        &amp;amp;lt;paths&amp;amp;gt;
          &amp;amp;lt;path&amp;amp;gt;*/work-manager.xml&amp;amp;lt;/path&amp;amp;gt;
        &amp;amp;lt;/paths&amp;amp;gt;
        &amp;amp;lt;beans&amp;amp;gt;
          &amp;amp;lt;bean name=&quot;queue&quot;/&amp;amp;gt;
        &amp;amp;lt;/beans&amp;amp;gt;
      &amp;amp;lt;/application-context&amp;amp;gt;
    &amp;amp;lt;/application-contexts&amp;amp;gt;
  &amp;amp;lt;/jee-application&amp;amp;gt;
&amp;amp;lt;/spring&amp;amp;gt;
...
&lt;/pre&gt;
&lt;p&gt;Done!&lt;/p&gt;
&lt;p&gt;Now we have a fully distributed, multi-&lt;span class=&quot;caps&quot;&gt;JVM&lt;/span&gt; &lt;em&gt;CommonJ WorkManager&lt;/em&gt;.&lt;/p&gt;
&lt;h2&gt;Client usage&lt;/h2&gt;
&lt;p&gt;Using the distributed work manager is now simply a matter of getting the bean from the application context and invoke &lt;code&gt;schedule(..)&lt;/code&gt;:&lt;/p&gt;
&lt;pre name=&quot;code&quot; class=&quot;java&quot;&gt;
ApplicationContext ctx = 
    new ClassPathXmlApplicationContext(&quot;*/work-manager.xml&quot;);

// get the work manager from the application context
WorkManager workManager = (WorkManager) ctx.getBean(&quot;workManager&quot;);

Set pendingWork = new HashSet();
for (int i = 0; i &amp;lt; nrOfWork; i++) {

  // schedule work
  WorkItem workItem = workManager.schedule(new Work() {
      public void run() {
        ... // do work
      }
  });
                
  // collect the pending work
  pendingWork.add(workItem);
}

// wait for all work to be completed
workManager.waitForAll(pendingWork, WorkManager.INDEFINITE);
&lt;/pre&gt;
&lt;p&gt;To start up a &lt;code&gt;Worker&lt;/code&gt; you simply have to get the &lt;code&gt;Worker&lt;/code&gt; bean from the application context and invoke &lt;code&gt;start()&lt;/code&gt;:&lt;/p&gt;
&lt;p&gt;&lt;/pre&gt;&lt;pre name=&quot;code&quot; class=&quot;java&quot;&gt;&lt;br /&gt;
ApplicationContext ctx = &lt;br /&gt;
    new ClassPathXmlApplicationContext(&amp;#8220;*/work-manager.xml&amp;#8221;);&lt;/p&gt;
&lt;p&gt;// get the worker from the application context&lt;br /&gt;
Worker worker = (Worker) ctx.getBean(&amp;#8220;worker&amp;#8221;);&lt;/p&gt;
&lt;p&gt;// starting worker&lt;br /&gt;
worker.start();&lt;br /&gt;
&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;The usage of the distributed version would roughly be to start up one &lt;code&gt;WorkManager&lt;/code&gt; bean and N number of &lt;code&gt;Worker&lt;/code&gt; beans, each one on a different &lt;span class=&quot;caps&quot;&gt;JVM&lt;/span&gt;.&lt;/p&gt;
&lt;p&gt;That is all there is to it. Now we have a simple, distributed, reliable, high-performant and scalable &lt;em&gt;CommonJ WorkManager&lt;/em&gt; ready for use.&lt;/p&gt;
&lt;p&gt;Enjoy.&lt;/p&gt;
&lt;ul&gt;
	&lt;li&gt;&lt;a href=&quot;http://www.terracottatech.com/downloads.jsp&quot;&gt;RC 1&lt;/a&gt; of &lt;a href=&quot;http://www.terracottatech.com/terracotta_spring.shtml&quot;&gt;Terracotta for Spring&lt;/a&gt; was released some days ago (9/12/2006) and is free for production use for up to two nodes.&lt;/li&gt;
&lt;/ul&gt;</content>
 </entry>
 
 <entry>
   <title>The Art of Minimalistic Design</title>
   <link href="http://jboner.github.com/2006/09/06/the-art-of-minimalistic-design"/>
   <updated>2006-09-06T00:00:00+00:00</updated>
   <id>hhttp://jboner.github.com/2006/09/06/the-art-of-minimalistic-design</id>
   <content type="html">&lt;h1&gt;
&lt;p&gt;The Art of Minimalistic Design&lt;/p&gt;
&lt;/h1&gt;
&lt;p&gt;&lt;a href=&quot;http://en.wikipedia.org/wiki/Minimalism&quot;&gt;Minimalism&lt;/a&gt;:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;&amp;#8220;&amp;#8230;where the work is stripped down to its most fundamental features and core self expression&amp;#8221;.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;The world needs more minimalism.&lt;/p&gt;
&lt;p&gt;Style has always been very important for me. When I talk about style, I mainly mean in terms of expression and art. In the last years, I&amp;#8217;ve started to realize that, my definition of style, and what I&amp;#8217;m really looking for, is minimalism.&lt;/p&gt;
&lt;p&gt;Minimalism plays a very important role in art and design. It is a universal quality that can be found in any kind of art or design form. We can for example  see brands from very different niches, like &lt;a href=&quot;http://www.apple.com/&quot;&gt;Apple&lt;/a&gt; (hardware and software), &lt;a href=&quot;http://37signals.com/&quot;&gt;37signals&lt;/a&gt; (web design), &lt;a href=&quot;http://www.bruno-mathsson-int.se/&quot;&gt;Bruno Mattsson&lt;/a&gt; (furniture) and &lt;a href=&quot;http://www.milesdavis.com/&quot;&gt;Miles Davis&lt;/a&gt; (music),  all have made a successful business out of it and have managed to create very passionate and in some cases almost religiously addicted user communities. Minimalism gets you.&lt;/p&gt;
&lt;p&gt;It is important to understand that minimalism (and simplicity in general) is not equal to plainness. Actually, it is the contrary. My years as a jazz musician told me (the hard way) that simplicity and minimalism is actually one of the hardest thing one can achieve as an artist.  One of the hardest things in the art of musical improvisation is to decide when not to play. To not fear the silence, but to use it as important building blocks in the picture, to paint a story around the silence.&lt;/p&gt;
&lt;p&gt;Few have captured it better than Antoine de Saint-ExupÃ©ry:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;&amp;#8220;Perfection (in design) is achieved not when there is nothing more to add, but rather when there is nothing more to take away.&amp;#8221;&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;Minimalistic design can perhaps be best described as &lt;em&gt;&lt;span class=&quot;caps&quot;&gt;QWAN&lt;/span&gt;&lt;/em&gt;, short for &lt;em&gt;quality without a name&lt;/em&gt;, it is hard to put the finger on it, it has something to do with elegancy, it just feels good, feels right.&lt;/p&gt;
&lt;p&gt;I have always thought of minimalistic design as a synonym for good design. But I like the term better since I think that it captures the essence of good design better, and communicates better what it should be and feel like.&lt;/p&gt;
&lt;p&gt;The last days I have been thinking about questions like; what is really minimalistic design in the context of computer programming, what are the qualities that characterizes it, what does it feel like and how can it be achived?&lt;/p&gt;
&lt;h1&gt;Minimalistic Design in Computer Programming&lt;/h1&gt;
&lt;p&gt;I think that Ward Cunningham &lt;a href=&quot;http://www.artima.com/intv/simplest.html&quot;&gt;captures the essence of minimalistic design&lt;/a&gt; in his informal discussion with Kent Beck:&lt;br /&gt;
&lt;blockquote&gt;&lt;br /&gt;
&amp;#8220;So when I asked [KentBeck], &amp;#8220;What&amp;#8217;s the simplest thing that could possibly work,&amp;#8221; I wasn&amp;#8217;t even sure. I wasn&amp;#8217;t asking, &amp;#8220;What do you know would work?&amp;#8221; I was asking, &amp;#8220;What&amp;#8217;s possible? What is the simplest thing we could say in code, so that we&amp;#8217;ll be talking about something that&amp;#8217;s on the screen, instead of something that&amp;#8217;s ill-formed in our mind.&amp;#8221; I was saying, &amp;#8220;Once we get something on the screen, we can look at it. If it needs to be more, we can make it more.&amp;#8221;&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;&lt;em&gt;&amp;#8220;The simplest thing that could possibly work&amp;#8221;&lt;/em&gt;, sounds simple enough, but&amp;#8230;what does it mean and&amp;#8230;hmm&amp;#8230;is it really simple?&lt;/p&gt;
&lt;p&gt;Let&amp;#8217;s try to break down the statement, and look at what the two parts; &lt;em&gt;&amp;#8220;The Simplest Thing&amp;#8221;&lt;/em&gt; and &lt;em&gt;&amp;#8220;Possibly Work&amp;#8221;&lt;/em&gt; could mean, and if they can to guide us towards minimalism.&lt;/p&gt;
&lt;p&gt;But before we jump into that discussion, let&amp;#8217;s define what &amp;#8220;design&amp;#8221; means in computer programming, both in terms of process and artifact.&lt;/p&gt;
&lt;h2&gt;What is Design?&lt;/h2&gt;
&lt;p&gt;When I talk about design in computer programming, I&amp;#8217;m talking about the Code. I am a firm believer that &lt;a href=&quot;http://www.developerdotstar.com/mag/articles/reeves_design_main.html&quot;&gt;The Code Is the Design&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;This does not mean that &lt;span class=&quot;caps&quot;&gt;UML&lt;/span&gt; diagrams, &lt;span class=&quot;caps&quot;&gt;CRC&lt;/span&gt; cards or whiteboard drawings are bad, useless or unecessary. They are all part of planning and planning is essential. But while the act of planning is highly useful, the plans themself are of limited value. The planning (or design) sessions are best treated as a learning experience where the end product is not a document or a diagram, but increased knowledge and understanding.&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;&amp;#8220;Do the planning but, throw out the plans.&amp;#8221; &lt;br /&gt;
- Mary Poppendieck, &lt;a href=&quot;http://www.poppendieck.com/pdfs/Predictability_Paradox.pdf&quot;&gt;Lean Development &amp;amp; the Predictability Paradox&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;
&lt;blockquote&gt;
&lt;p&gt;&amp;#8220;No plan survives contact with the enemy.&amp;#8221; &lt;br /&gt;
- Field Marshall Helmuth Graf von Moltke&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;The documents or diagrams will be outdated in a short period of time anyway and are best thrown away as soon as possible, since they will only cause frustration when struggling to keep them in sync with the changes of reality. My advice here are, keep design sessions short, try to keep a high-level focus, see the whole and do not get buried in details, use simple tools and don&amp;#8217;t put too much effort into the end artifact (diagram/document) &amp;#8211; since that will make it harder to through away.&lt;/p&gt;
&lt;p&gt;So, let&amp;#8217;s now get back to the dissection of the &amp;#8220;Wardism&amp;#8221; we just chopped up. Starting with part 2.&lt;/p&gt;
&lt;h2&gt;Part 2: &amp;#8220;Possibly Work&amp;#8221;&lt;/h2&gt;
&lt;p&gt;First, I think that it must contain the minimal amount of functionality that solves the (customer&amp;#8217;s) problem, nothing more and nothing less.&lt;/p&gt;
&lt;p&gt;Another way to look at it is; &lt;em&gt;customer satisfaction&lt;/em&gt;, which has also been described as:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;&amp;#8220;the right product, at the right time, for the right price&amp;#8221;.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;Second, if we see &amp;#8220;Work&amp;#8221; as the product, then the natural question is; how do we define the scope of the work, when can we consider ourselves &amp;#8220;done&amp;#8221;?&lt;/p&gt;
&lt;p&gt;Ken Schwaber talks about Scrum&amp;#8217;s definition of &amp;#8220;done&amp;#8221; as an;&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;&amp;#8220;&amp;#8230;increment of functionality that is complete&amp;#8230; [that] must contain all analysis, design, coding, testing, documentation and anything else appropriate for the application&amp;#8221;.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;This would mean that &amp;#8220;possibly work&amp;#8221; could be defined as: an increment of functionality that is complete (in every sense) and that gives the customer satisfaction. I am sort of half satisfied with this definition, but I think it will work for now.&lt;/p&gt;
&lt;p&gt;Now let&amp;#8217;s take a look at part 1.&lt;/p&gt;
&lt;h2&gt;Part 1: &amp;#8220;The Simplest Thing&amp;#8221;&lt;/h2&gt;
&lt;p&gt;What does &amp;#8220;The Simplest Thing&amp;#8221; mean? How can it be achived?&lt;/p&gt;
&lt;p&gt;As we discussed in the beginning of this article; &lt;em&gt;&amp;#8220;simple is not plain&amp;#8221;&lt;/em&gt;. It is more a matter of purification and in the context of code I have found the qualities below to be some of the most important ones:&lt;/p&gt;
&lt;h3&gt;Conceptual Integrity&lt;/h3&gt;
&lt;p&gt;Conceptual Integrity is a quality in art and design, that is described by Fred Brooks, in the context of software, as a a system that is coherent and has been build with a unified vision:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;&amp;#8220;I will contend that conceptual integrity is the most important consideration in system design. It is better to have a system omit certain anomalous features and improvements, but to reflect one set of design ideas, then to have one that contains many good but independent and uncoordinated ideas. â€¦ Simplicity and straightforwardness proceed from conceptual integrity. Every part must reflect the same philosophies and the same balancing of desiderata. Every part must even use the same techniques in syntax and analogous notions in semantics. Ease of use, then, dictates unity of design, conceptual integrity.&amp;#8221;  &lt;br /&gt;
- Fred Brooks, The Mythical Man-Month, p 42, 44&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;I believe that conceptual integrity is one of the key qualities that characterizes minimalistic design, where, as we could learn from the definition above, &amp;#8220;the work is stripped down to its most fundamental features and core self expression&amp;#8221;. It is not an easy thing to achive in in any art form, but especially not in software development with its highly complex nature paired with tight deadlines and customer&amp;#8217;s ever-changing requirements.&lt;/p&gt;
&lt;p&gt;Things that could help here are; a common vision for the project (have to be shared between developers, management and stake holders), having a small, tight, self-managed and adaptive team, steady flow of feedback from customer (short iterations etc.), refactoring and continuous refinement of the code etc.&lt;/p&gt;
&lt;h3&gt;High Cohesion&lt;/h3&gt;
&lt;p&gt;The code should have &lt;em&gt;high cohesion&lt;/em&gt;, which means that a class should have one single and well-defined purpose. This principle was first introduced by Tom DeMarco who defined cohesion as the functional relatedness of the elements of a module. Bob Martin changed the meaning of this quality slightly when he define the &lt;em&gt;Single-Responsibility Principle (&lt;span class=&quot;caps&quot;&gt;SRP&lt;/span&gt;)&lt;/em&gt; which says that:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;&amp;#8220;A class should only have one reason change.&amp;#8221;&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;What this means is that you should only implement one single concern (piece of functionality) in each class (or set of classes) and that all methods and attributes in this class should, one way or the other, contribute to to this specific piece of functionality &amp;#8211; no more and no less.&lt;/p&gt;
&lt;h3&gt;Clarity&lt;/h3&gt;
&lt;p&gt;The code should be clear, easily understood and have a quality that is best described as &lt;a href=&quot;http://en.wikipedia.org/wiki/Self-documenting&quot;&gt;self-documenting&lt;/a&gt;.&lt;/p&gt;
&lt;h3&gt;Modularity&lt;/h3&gt;
&lt;p&gt;The code should be modular in the sense that every concern in the system should, if possible, be made orthogonal to the other concerns. This will help you to minimize &lt;em&gt;code tangling&lt;/em&gt; and &lt;em&gt;code scattering&lt;/em&gt;, and will not only make the code more simple to write, but also to understand, maintain and reuse.&lt;/p&gt;
&lt;p&gt;Here we can be helped by using common OO practices such as, the &lt;a href=&quot;http://www.objectmentor.com/resources/articles/srp.pdf&quot;&gt;Single Responsibility Principle&lt;/a&gt; (see above), the &lt;a href=&quot;http://www.objectmentor.com/resources/articles/dip.pdf&quot;&gt;Dependency Inversion Principle (&lt;span class=&quot;caps&quot;&gt;DIP&lt;/span&gt;)&lt;/a&gt;, the &lt;a href=&quot;http://www.objectmentor.com/resources/articles/ocp.pdf&quot;&gt;Open-Closed Principle (&lt;span class=&quot;caps&quot;&gt;OCP&lt;/span&gt;)&lt;/a&gt; and the &lt;a href=&quot;http://www.objectmentor.com/resources/articles/lsp.pdf&quot;&gt;Liskov Substitution Principle (&lt;span class=&quot;caps&quot;&gt;LSP&lt;/span&gt;)&lt;/a&gt; along with &lt;a href=&quot;http://www.eclipse.org/aspectj/&quot;&gt;Aspect-Oriented Programming (&lt;span class=&quot;caps&quot;&gt;AOP&lt;/span&gt;)&lt;/a&gt; and design patterns such as &lt;a href=&quot;http://en.wikipedia.org/wiki/Strategy_pattern&quot;&gt;Strategy&lt;/a&gt; and &lt;a href=&quot;http://en.wikipedia.org/wiki/Template_method_pattern&quot;&gt;Template Method&lt;/a&gt;.&lt;/p&gt;
&lt;h3&gt;One single authoritative representation of every piece in the system&lt;/h3&gt;
&lt;p&gt;Dave Thomas and Andy Hunt talks captures this in their &lt;a href=&quot;http://www.artima.com/intv/dry.html&quot;&gt;&lt;span class=&quot;caps&quot;&gt;DRY&lt;/span&gt; (Don&amp;#8217;t Repeat Yourself) principle&lt;/a&gt;. We all know that code duplication is bad and should be eliminated, but the &lt;span class=&quot;caps&quot;&gt;DRY&lt;/span&gt; principle takes it even further when it says that:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;&amp;#8220;&amp;#8230;every piece of system knowledge should have one authoritative, unambiguous representation. Every piece of knowledge in the development of something should have a single representation.&amp;#8221;&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;This ties back to both cohesion and orthogonality, since if you have a highly cohesive single representation of every concern in the system, then it is very likely that you can make these concerns orthogonal to each other and get good modularity. It is all connected.&lt;/p&gt;
&lt;h3&gt;Captures the right abstractions&lt;/h3&gt;
&lt;p&gt;The code needs to capture the right abstractions. This means that it has to be based upon a model that naturally reasons with both domain experts and developers. Maintaining a &lt;a href=&quot;http://domaindrivendesign.org/discussion/messageboardarchive/UbiquitousLanguage.html&quot;&gt;Ubiquitous Language&lt;/a&gt; can really help here, along with continuous refactoring and refinement of the model.&lt;/p&gt;
&lt;h1&gt;Final Thoughts&lt;/h1&gt;
&lt;p&gt;Finally, I think that the fact that, when Ward&amp;#8217;s statement was formalized into a rule; &lt;a href=&quot;http://c2.com/xp/DoTheSimplestThingThatCouldPossiblyWork.html&quot;&gt;Do The Simplest Thing That Could Possibly Work&lt;/a&gt;, it was prefixed with the verb &lt;em&gt;&amp;#8220;Do&amp;#8221;&lt;/em&gt;, is of importance. It might sound subtle, but I think it is not. Because simplicity and minimalistic design is truly a process and not an end product.&lt;/p&gt;
&lt;p&gt;We constantly need to challenge the way we work and the code we write, constantly strive for simplification and purification, more pragmatic and effective ways to work, continuously adapt and improve, and keep our humility for one of the most complex crafts there is.&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;&amp;#8220;This complex craft will demand our continual development of the discipline, our learning to compose in larger units, our best use of new tools, our best adaptation of proven engineering management methods, liberal application of common sense, and a God-given humility to recognize our fallibility and limitations.&amp;#8221; &lt;br /&gt;
- Fred Brooks, The Mythical Man-Month after 20 Years, p 289&lt;/p&gt;
&lt;/blockquote&gt;</content>
 </entry>
 
 <entry>
   <title>WebEx: Cluster Your Spring Application in Less Than 30 Min</title>
   <link href="http://jboner.github.com/2006/08/17/webex-how-to-cluster-your-spring-application-in-less-than-30-min"/>
   <updated>2006-08-17T00:00:00+00:00</updated>
   <id>hhttp://jboner.github.com/2006/08/17/webex-how-to-cluster-your-spring-application-in-less-than-30-min</id>
   <content type="html">&lt;h1&gt;
&lt;p&gt;WebEx: Cluster Your Spring Application in Less Than 30 Min&lt;/p&gt;
&lt;/h1&gt;
&lt;p&gt;&lt;a href=&quot;http://terracottatech.com/&quot;&gt;Terracotta&lt;/a&gt; and &lt;a href=&quot;http://interface21.com/&quot;&gt;Interface21&lt;/a&gt; will hold a joint WebEx on how to cluster an arbitrary &lt;a href=&quot;http://www.springframework.org/&quot;&gt;Spring&lt;/a&gt; application or &lt;a href=&quot;http://opensource.atlassian.com/confluence/spring/display/WEBFLOW/Home;jsessionid=aamZesJzuupaxFKcE0&quot;&gt;Spring WebFlow&lt;/a&gt; application transparently (no changes to existing application code) and declaratively in less than 30 minutes.&lt;/p&gt;
&lt;p&gt;&lt;a href=&quot;http://blog.springframework.com/rob/&quot;&gt;Rob Harrop&lt;/a&gt; and me will talk about the benefits and drawbacks with using state-of-the-art clustering technologies such as database, &lt;span class=&quot;caps&quot;&gt;JMS&lt;/span&gt; or custom &lt;span class=&quot;caps&quot;&gt;API&lt;/span&gt; (such as JCache etc.) and then talk about the benefits with and why we need clustering at the &lt;span class=&quot;caps&quot;&gt;JVM&lt;/span&gt; level and how that is done in the &lt;a href=&quot;http://terracottatech.com/terracotta_spring.shtml&quot;&gt;Terracotta for Spring&lt;/a&gt; product.&lt;/p&gt;
&lt;p&gt;If you are in need to scale out you Spring application or are in need for fail-over then you should call in to the WebEx.&lt;/p&gt;
&lt;p&gt;&lt;a href=&quot;https://terracottat.webex.com/mw0302l/mywebex/default.do?siteurl=terracottat&amp;service=6&amp;main_url=%2Fec0507l%2Feventcenter%2Fmainframe.do%3Fmainurl%3Dhttps%253A%252F%252Fterracottat.webex.com%252Fec0507l%252Feventcenter%252Fevent%252FeventAction.do%253FtheAction%253Ddetail%2526confViewID%253D277533545%2526siteurl%253Dterracottat%26siteurl%3Dterracottat&quot;&gt;Here is where you sign up for the WebEx.&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Where and when:&lt;br /&gt;
&lt;blockquote&gt;&lt;br /&gt;
Event:  Transparently Clustered Spring&lt;/p&gt;
&lt;p&gt;Date and Time: 	&lt;br /&gt;
Tuesday, September 12, 2006 8:00 am &lt;br /&gt;
Pacific Daylight Time (&lt;span class=&quot;caps&quot;&gt;GMT&lt;/span&gt; -07:00, San Francisco)&lt;/p&gt;
&lt;p&gt;Panelist(s) Info:&lt;/p&gt;
&lt;ul&gt;
	&lt;li&gt;Jonas BonÃ©r, Sr. Software Engineer, Terracotta&lt;/li&gt;
	&lt;li&gt;Rob Harrop, core developer of the Spring Framework &amp;amp; consultant with Interface 21&lt;/li&gt;
	&lt;li&gt;Sreeni Iyer, Sr. Mgr for Field Engineering, Terracotta&lt;/li&gt;
	&lt;li&gt;Gary Nakamura, VP Sales, Terracotta&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Duration: 	1 hour&lt;/p&gt;
&lt;p&gt;Description: 	Terracotta for Spring provides plug-in capacity, availability and scalability for Spring applications with absolutely no changes to existing code. Single VM Spring applications become clustered applications at load time, by simply configuring Terracotta for Spring to include the application context of your choosing.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;Hope to see you there.&lt;/p&gt;</content>
 </entry>
 
 <entry>
   <title>Distributed Computing Made Easy</title>
   <link href="http://jboner.github.com/2006/05/22/distributed-computing-made-easy"/>
   <updated>2006-05-22T00:00:00+00:00</updated>
   <id>hhttp://jboner.github.com/2006/05/22/distributed-computing-made-easy</id>
   <content type="html">&lt;h1&gt;
&lt;p&gt;Distributed Computing Made Easy&lt;/p&gt;
&lt;/h1&gt;
&lt;p&gt;My latest article for &lt;a href=&quot;http://www.theserverside.com&quot;&gt;TheServerSide.com&lt;/a&gt;, titled &lt;em&gt;Distributed Computing Made Easy&lt;/em&gt;, is published. In this article, I walk through a fairly generic but common distributed computing problem and shows how it can be simplified using &lt;a href=&quot;http://terracottatech.com/&quot;&gt;Clustering at the &lt;span class=&quot;caps&quot;&gt;JVM&lt;/span&gt; level&lt;/a&gt;, the &lt;a href=&quot;http://springframework.org/&quot;&gt;Spring Framework&lt;/a&gt; and &lt;a href=&quot;http://java.sun.com/j2se/1.5.0/docs/api/java/util/concurrent/package-summary.html&quot;&gt;Java 5&amp;#8217;s concurrency libraries&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;The article can be found &lt;a href=&quot;http://www.theserverside.com/tt/articles/article.tss?l=DistCompute&quot;&gt;here&lt;/a&gt;.&lt;/p&gt;</content>
 </entry>
 
 <entry>
   <title>Need to Scale-Out Your Spring Application?</title>
   <link href="http://jboner.github.com/2006/05/09/need-to-scale-out-your-spring-application"/>
   <updated>2006-05-09T00:00:00+00:00</updated>
   <id>hhttp://jboner.github.com/2006/05/09/need-to-scale-out-your-spring-application</id>
   <content type="html">&lt;h1&gt;
&lt;p&gt;Need to Scale-Out Your Spring Application?&lt;/p&gt;
&lt;/h1&gt;
&lt;p&gt;If you do, then you should come to my JavaOne session about &lt;a href=&quot;https://www28.cplan.com/javaone06_cv_124_1/sessions_catalog.jsp?ilc=124-1&amp;ilg=english&amp;isort=1&amp;is=%3CISEARCH%3E&amp;ip=yes&amp;itrack=+&amp;isession_type=+&amp;isession_id=TS-3217&amp;iabstract=&amp;ispeaker=&amp;ispk_company=&quot;&gt;Transparently Clustered Spring &amp;#8212; A Runtime Solution for Javaâ„¢ Technology&lt;/a&gt; and learn how to cluster your &lt;a href=&quot;http://springframework.org/&quot;&gt;Spring&lt;/a&gt; application in minutes, with &lt;strong&gt;zero changes to application code&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt;The session (TS-3217) will be held on Tuesday, 05/16/2006, 02:00 PM &amp;#8211; 03:00 PM and has the following abstract:&lt;/p&gt;
&lt;blockquote&gt;How do you scale a Spring application beyond a single node? How can you guarantee high-availability, eliminate single points of failure, and make sure that you meet your customer SLAs?&lt;br /&gt;
&lt;br /&gt;
Historically speaking, clustering an application is not easy: it takes a significant amount of time and usually requires you to rewrite parts of your application. It also usually perturbs your domain model and breaks object identity.&lt;br /&gt;
&lt;br /&gt;
But does it have to be like that?&lt;br /&gt;
&lt;br /&gt;
In this talk, Jonas BonÃ©r will walk you through how to cluster your Spring application, transparently and naturally, with no changes to your application code, using the Terracotta Spring Runtime.&lt;br /&gt;
&lt;br /&gt;
The Terracotta Spring Runtime allows you to take an arbitrary Spring application, written for a single JVMâ„¢ machine, and cluster it to N nodes while preserving the exact same semantics.&lt;br /&gt;
&lt;br /&gt;
For example:&lt;br /&gt;
&lt;br /&gt;
â€¢ Life-cycle semantics and scope for Spring beans are preserved across the cluster &amp;#8211; within the same logical ApplicationContext (singleton and session scoped beans).&lt;br /&gt;
â€¢ Spring&amp;#8217;s local event mechanism in the ApplicationContext is turned into high-performance asynchronous, distributed and reliable events (messages), but still local within the same logical ApplicationContext.&lt;br /&gt;
â€¢ Clustered beans can be exported using Spring &lt;span class=&quot;caps&quot;&gt;JMX&lt;/span&gt; support, which guarantees a single point of management and coherent view of all the &lt;span class=&quot;caps&quot;&gt;JMX&lt;/span&gt; data in the cluster.&lt;br /&gt;
â€¢ Spring WebFlow&amp;#8217;s web flows are transparently shared across the cluster.&lt;br /&gt;
â€¢ and more&amp;#8230;&lt;br /&gt;
&lt;br /&gt;
The session is backed up by several live demos.&lt;/blockquote&gt;
&lt;p&gt;See you there.&lt;/p&gt;</content>
 </entry>
 
 <entry>
   <title>Domain-Driven Pointcut Design</title>
   <link href="http://jboner.github.com/2006/04/24/domain-driven-pointcut-design"/>
   <updated>2006-04-24T00:00:00+00:00</updated>
   <id>hhttp://jboner.github.com/2006/04/24/domain-driven-pointcut-design</id>
   <content type="html">&lt;h1&gt;
&lt;p&gt;Domain-Driven Pointcut Design&lt;/p&gt;
&lt;/h1&gt;
&lt;h2&gt;What is a pointcut language?&lt;/h2&gt;
&lt;p&gt;Let&amp;#8217;s start with some terminology and definitions. A &lt;em&gt;pointcut language&lt;/em&gt; can be seen as a &lt;a href=&quot;http://en.wikipedia.org/wiki/Domain_Specific_Language&quot;&gt;Domain Specific Language&lt;/a&gt; (&lt;span class=&quot;caps&quot;&gt;DSL&lt;/span&gt;) for defining &lt;em&gt;pointcuts&lt;/em&gt;. A &lt;em&gt;pointcut&lt;/em&gt; is defined as the set of &lt;em&gt;join points&lt;/em&gt; &amp;#8211; a &lt;em&gt;pointcut&lt;/em&gt; picks out &lt;em&gt;join points&lt;/em&gt;, in which a &lt;em&gt;jointpoint&lt;/em&gt; is defined as a well-defined point in the program flow.&lt;/p&gt;
&lt;p&gt;Sounds complicated? It&amp;#8217;s actually pretty simple.&lt;/p&gt;
&lt;p&gt;Let&amp;#8217;s try a little example. You have an instance of a class &lt;code&gt;Foo&lt;/code&gt; and an instance of a class &lt;code&gt;Bar&lt;/code&gt;. The class &lt;code&gt;Bar&lt;/code&gt; has a method with the signature &lt;code&gt;void setFoo(Foo foo)&lt;/code&gt; and the class &lt;code&gt;Foo&lt;/code&gt; has some other method that is invoking this method in &lt;code&gt;Bar&lt;/code&gt;. E.g. something like this:&lt;/p&gt;
&lt;pre name=&quot;code&quot; class=&quot;java&quot;&gt;
class Bar {
  Foo foo;
  void setFoo(Foo f) {
    foo = f;    
  }
}
&lt;/pre&gt;

&lt;pre name=&quot;code&quot; class=&quot;java&quot;&gt;
class Foo {
  Bar bar = new Bar();
  
  void doStuff() {
    bar.setFoo(this); // this is a join point 
  }
}
&lt;/pre&gt;
&lt;p&gt;Then the point where &lt;code&gt;setFoo(..)&lt;/code&gt; is invoked from the class &lt;code&gt;Foo&lt;/code&gt; is a well defined point, e.g. a &lt;em&gt;join point&lt;/em&gt;.&lt;/p&gt;
&lt;p&gt;If we now want to pick out this point in order to do something with it (apply an advice or interceptor to it or something else), then we can create a &lt;em&gt;pointcut&lt;/em&gt; using a &lt;em&gt;pointcut language&lt;/em&gt;. In this particular case we could create a &lt;em&gt;pointcut&lt;/em&gt; defined like this:&lt;/p&gt;
&lt;pre name=&quot;code&quot; class=&quot;java&quot;&gt;
call(void Bar.setFoo(Foo))
&lt;/pre&gt;
&lt;p&gt;Now we have created a &lt;em&gt;pointcut&lt;/em&gt; (AspectJ style), since we have a construct that picks out &lt;em&gt;join points&lt;/em&gt;. The only oddity here is that it only picks out one single &lt;em&gt;join point&lt;/em&gt;.&lt;/p&gt;
&lt;p&gt;Since just working with one &lt;em&gt;join point&lt;/em&gt; at time is fairly useless, we need an expressive way of picking out and managing &amp;#8216;sets&amp;#8217; of &lt;em&gt;join points&lt;/em&gt;.&lt;/p&gt;
&lt;p&gt;Luckily, most &lt;em&gt;pointcut languages&lt;/em&gt; supports using patterns to write more expressive, generic or specific, &lt;em&gt;pointcut&lt;/em&gt; expressions.&lt;/p&gt;
&lt;p&gt;For example, you can use patterns to pick out the call to any method that starts with &lt;em&gt;set&lt;/em&gt; in &lt;code&gt;Bar&lt;/code&gt; using this &lt;em&gt;pointcut&lt;/em&gt;:&lt;/p&gt;
&lt;pre name=&quot;code&quot; class=&quot;java&quot;&gt;
call(* Bar.set*(..))
&lt;/pre&gt;
&lt;p&gt;In this &lt;em&gt;pointcut&lt;/em&gt; we do not care about the parameters to the method, the return type or its name more than it should start with set.&lt;/p&gt;
&lt;p&gt;Generally, &lt;em&gt;pointcut languages&lt;/em&gt; have two families of patterns (mini-languages):&lt;/p&gt;
&lt;ul&gt;
	&lt;li&gt;Signature patterns &amp;#8211; matches signatures (method, field etc.)        &lt;/li&gt; 
	&lt;li&gt;Type patterns &amp;#8211; matches types        &lt;/li&gt; 
&lt;/ul&gt;
&lt;p&gt;(For a more detailed description of the patterns and semantics that the AspectJ pointcut language supports, see the &lt;a href=&quot;http://www.eclipse.org/aspectj/doc/released/progguide/semantics-pointcuts.html#matching&quot;&gt;AspectJ documentation.&lt;/a&gt;)&lt;/p&gt;
&lt;h2&gt;The problems with using signature patterns in pointcuts&lt;/h2&gt;
&lt;p&gt;A pointcut can be seen as an &lt;em&gt;implicit contract&lt;/em&gt; between the target code and the artifact that is using the &lt;em&gt;pointcut&lt;/em&gt; (could be an aspect or an interceptor).&lt;/p&gt;
&lt;p&gt;One problem with using patterns like this is that we are basing the implicit contract on implementation details, details are likely to change during the lifetime of the application.  This is becoming an even bigger problem with the popularity of agile software development methodologies (like XP, Scrum, &lt;span class=&quot;caps&quot;&gt;TDD&lt;/span&gt; etc.), with a high focus on refactoring and responsiveness to customer ever-changing requirements.&lt;/p&gt;
&lt;p&gt;Another problem is that the use of patterns effectively removes all strong typing (can be helped with good tools like &lt;em&gt;&lt;span class=&quot;caps&quot;&gt;AJDT&lt;/span&gt;&lt;/em&gt;, but that will tie you to a specific working environment).&lt;/p&gt;
&lt;p&gt;As a rule of thumb: &lt;br /&gt;
&lt;strong&gt;Your application&amp;#8217;s implementation details will change and you should therefore, as much as possible try to avoid basing implicit contracts, e.g. your pointcuts, upon them.&lt;br /&gt;
&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;So what can we do about it?&lt;/p&gt;
&lt;h2&gt;Metadata to the rescue?&lt;/h2&gt;
&lt;p&gt;When I write metadata I mean data about the target artifact, data that describes the characteristics of the artifact.&lt;/p&gt;
&lt;p&gt;&lt;a href=&quot;http://www.libraries.psu.edu/tas/jca/ccda/tf-meta3.html&quot;&gt;Here is a decent definition&lt;/a&gt; of metadata:&lt;br /&gt;
&lt;blockquote&gt;Metadata are structured, encoded data that describe characteristics of information-bearing entities to aid in the identification, discovery, assessment, and management of the described entities.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;Note: when I write metadata I do not mean any specific kind of metadata, it can be declared in code, text file, &lt;span class=&quot;caps&quot;&gt;XML&lt;/span&gt;, some meta model etc. But to be more specific, let&amp;#8217;s look at Java 5 annotations.&lt;/p&gt;
&lt;p&gt;Since metadata is raising the abstraction level over implementation details, but still captures its characteristics, it seems to be very suitable for helping us addres the issues with &lt;em&gt;signature based patterns&lt;/em&gt; discussed above.&lt;/p&gt;
&lt;p&gt;So, all this sounds promising, but as we will see later, you have to be aware of that not all metadata will help here and you have to define it with care.&lt;/p&gt;
&lt;p&gt;Ramnivas Laddad has written an &lt;a href=&quot;http://www-128.ibm.com/developerworks/java/library/j-aopwork4/&quot;&gt;interesting article&lt;/a&gt; on metadata and pointcuts, where he views metadata as being &lt;em&gt;multi-dimensional signatures&lt;/em&gt;. For example (from the article), a method with the following signature suffers from &lt;em&gt;signature-tangling&lt;/em&gt;:&lt;/p&gt;
&lt;pre name=&quot;code&quot; class=&quot;java&quot;&gt;
public void transactional_authorized_credit(float amount);
&lt;/pre&gt;
&lt;p&gt;This method&amp;#8217;s signature is tangled since it is trying to capture all its roles in it&amp;#8217;s signature, e.g. name. In this case the use of metadata can help to untangle this signature by defining the method like this:&lt;/p&gt;
&lt;pre name=&quot;code&quot; class=&quot;java&quot;&gt;
@Transactional 
@Authorized
public void credit(float amount);
&lt;/pre&gt;
&lt;p&gt;This is definitely an improvement and the benefits are clear; more stable contract, more readable code, opens up for potential use of the metadata by tools etc. But the problem is that it doesn&amp;#8217;t really raise the abstraction level, we still have strong implicit coupling, and the annotations suffers from the &lt;em&gt;@AdviseMeHere&lt;/em&gt; &lt;a href=&quot;http://c2.com/cgi/wiki?CodeSmell&quot;&gt;Code Smell&lt;/a&gt;. I think the main problem is that we are still relying on implementation details, since we have merely restructured the signature.&lt;/p&gt;
&lt;p&gt;He then continues with a discussion on the increased coupling, and suggests using more generic annotations that can be consumed by many different tools. For example that you should prefer using annotations like &lt;code&gt;@ReadOperation&lt;/code&gt; rather than &lt;code&gt;@ReadLock&lt;/code&gt; etc., which I think is an example of a step in the right direction.&lt;/p&gt;
&lt;p&gt;The way I see it: &lt;br /&gt;
&lt;strong&gt;If an artifact F is annotated with an annotation A, then A should only describe characteristics (behaviour, role, responsibilities or properties) that F had before A was added to F&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt;In other words, you should try to avoid using annotations that implies explicit changes of the characteristics of the annotated artifact.&lt;/p&gt;
&lt;p&gt;This raises the questions like, if for example the use of an &lt;code&gt;@Transactional&lt;/code&gt; annotation (or &lt;code&gt;@TransactionAttribute&lt;/code&gt; in &lt;span class=&quot;caps&quot;&gt;EJB&lt;/span&gt; 3) is a bad idea? Well, yes (and no).&lt;/p&gt;
&lt;p&gt;Generally speaking I do consider any annotation that have an &lt;em&gt;Advise-Me-With-Feature-X&lt;/em&gt; kind of flavor, a code smell. However, I do think there is a place for these kind of annotations in well-defined and constrained environments, such as containers and frameworks &amp;#8211; as well as when you think it works for you, stay pragmatic&amp;#8230; :-).&lt;/p&gt;
&lt;p&gt;I do believe that metadata still can help us, but the questions are: What is the proper abstraction level? How to make it work in reality?&lt;/p&gt;
&lt;h2&gt;Metadata and Ubiquitous Languages&lt;/h2&gt;
&lt;p&gt;I am a big fan of &lt;a href=&quot;http://www.domaindrivendesign.org/book/index.html&quot;&gt;&lt;em&gt;Domain-Driven Design&lt;/em&gt;&lt;/a&gt; (and Eric Evans book with the same name). One of the key building blocks in &lt;em&gt;Domain-Driven Design&lt;/em&gt; is the &lt;em&gt;Ubiquitous Language&lt;/em&gt;, and I have found that basing metadata abstractions on a &lt;em&gt;Ubiquitous Language&lt;/em&gt; can get us a long way. More about how and why in a minute, but first, some people might be wondering what a &lt;em&gt;Ubiquitous Languages&lt;/em&gt; is? Eric Evans &lt;a href=&quot;http://domaindrivendesign.org/discussion/messageboardarchive/UbiquitousLanguage.html&quot;&gt;defines it like this&lt;/a&gt;:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;Definition: A language structured around the domain model and used by all team members to connect all the activities of the team with the software.&lt;/p&gt;
&lt;p&gt;Problem:&lt;br /&gt;
A project faces serious problems when its language is fractured. Domain experts use their jargon while technical team members have their own language tuned for discussing the domain in terms of design.&lt;/p&gt;
&lt;p&gt;The terminology of day-to-day discussions is disconnected from the terminology embedded in the code (ultimately the most important product of the software project). And even the same person uses different language in speech and in writing, so that the most incisive impression of the domain often emerges in a transient form that is never captured in the code or even in writing.&lt;/p&gt;
&lt;p&gt;Translation blunts communication and makes knowledge crunching anemic.&lt;/p&gt;
&lt;p&gt;Yet none of these dialects can be a common language because none serves all needs.&lt;/p&gt;
&lt;p&gt;Solution:&lt;br /&gt;
Use the model as a backbone of a language. Commit the team to exercising that language relentlessly in all communication within the team and in the code. Use the same language in diagrams, writing and especially speech.&lt;/p&gt;
&lt;p&gt;Iron out difficulties by experimenting with alternative expressions, which reflect alternative models. Then refactor the code, renaming classes, methods and modules to conform to the new model. Resolve confusion over terms in conversaion, in just the way we come to agree on the meaning of ordinary words.&lt;/blockquote&gt;&lt;/p&gt;
&lt;p&gt;The &lt;em&gt;Ubiquitous Languages&lt;/em&gt; is not only usable in the &lt;em&gt;domain layer&lt;/em&gt;, but can, as Jimmy Nilsson has &lt;a href=&quot;http://www.jnsk.se/weblog/posts/ulreflections.htm&quot;&gt;pointed out&lt;/a&gt;, be very useful in the &lt;em&gt;service layer&lt;/em&gt; or in &lt;em&gt;DSLs&lt;/em&gt;.&lt;/p&gt;
&lt;p&gt;I have found that using the &lt;em&gt;Ubiquitous Language&lt;/em&gt; is equally important when defining metadata. Metadata should be a first class citizen in &lt;em&gt;the model&lt;/em&gt; and therefore has to be part of the &lt;em&gt;Ubiquitous Language&lt;/em&gt;. This is something that is very useful by itself, but becomes even more important in the context of &lt;em&gt;pointcut languages&lt;/em&gt;.&lt;/p&gt;
&lt;h2&gt;Bringing it all together: Domain-Driven Pointcut Design&lt;/h2&gt;
&lt;p&gt;So what is &lt;em&gt;Domain-Driven Pointcut Design&lt;/em&gt; (more than a fancy name for a thing that can be seen as common sense, that you can make yet another buzzword-like acronym out of)? Simply a way of designing &lt;em&gt;pointcuts&lt;/em&gt; that are anchored in &lt;em&gt;the model&lt;/em&gt; and that are more resilient to change.&lt;/p&gt;
&lt;p&gt;My proposal is that you should try as much as possible to to avoid the use of patterns and &lt;em&gt;signature based pointcuts&lt;/em&gt;. What this means in practice is that you need to use match-all signature patterns (since we still have to define a pattern for signatures in the &lt;em&gt;AspectJ pointcut language&lt;/em&gt;), e.g. &lt;code&gt;&#39;* *.*(..)&#39;&lt;/code&gt; for method signatures or &lt;code&gt;&#39;* *.*&#39;&lt;/code&gt; for field signatures etc. You should then instead try to constrain the &lt;em&gt;pointcut language&lt;/em&gt; you are using to three different matching constructs:&lt;/p&gt;
&lt;ul&gt;
	&lt;li&gt;&lt;strong&gt;Types&lt;/strong&gt; &amp;#8211; only fully qualified names, names that are part of the &lt;em&gt;Ubiquitous Language&lt;/em&gt; your team is using. &lt;br /&gt;
For example, &lt;br /&gt;
&lt;code&gt;within(com.biz.domain.Account)&lt;/code&gt; or &lt;br /&gt;
&lt;code&gt;target(com.biz.domain.Customer)&lt;/code&gt; (&lt;em&gt;AspectJ&lt;/em&gt; &lt;br /&gt;
allows you to use regular Java imports, so you can write &lt;br /&gt;
&lt;code&gt;within(Account)&lt;/code&gt; etc.).&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Metadata&lt;/strong&gt; &amp;#8211; only annotations that are part of the &lt;em&gt;Ubiquitous Language&lt;/em&gt; your team is using.&lt;/li&gt; 
&lt;p&gt;For example, &lt;br /&gt;
&lt;code&gt;call(@AccountStateChange * *.*(..))&lt;/code&gt;, &lt;br /&gt;
&lt;code&gt;execution(@Idempotent * *.(..))&lt;/code&gt;, &lt;br /&gt;
&lt;code&gt;@annotation(BillingOperation)&lt;/code&gt;, &lt;br /&gt;
&lt;code&gt;@this(DomainObject)&lt;/code&gt;&lt;br /&gt;
or whatever makes sense in your model.&lt;/p&gt;
&lt;li&gt;&lt;strong&gt;Package patterns&lt;/strong&gt; &amp;#8211; should be used only for narrowing down scope. &lt;br /&gt;
For example &lt;br /&gt;
&lt;code&gt;within(com.biz..)&lt;/code&gt; &lt;br /&gt;
etc. This means that &lt;br /&gt;
&lt;code&gt;within(com.biz..)&lt;/code&gt; is ok, but not &lt;br /&gt;
&lt;code&gt;within(com.*.foo.FooBar)&lt;/code&gt;, &lt;br /&gt;
&lt;code&gt;within(com.biz.Foo*)&lt;/code&gt; or &lt;br /&gt;
&lt;code&gt;within(com.biz.*Bar)&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If you manage to implement this correctly, in your team as well as in your code, then you have a high chance of being able to &lt;strong&gt;define extremely stable pointcuts that are a natural part of the model that both the developers and the domain experts (customers) can understand and reason about&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt;Finally, I do not and believe that &lt;em&gt;signature based pointcuts&lt;/em&gt; are useless, should be banned or anything. You should of course still use &lt;em&gt;signature based pointcuts&lt;/em&gt; when they are the best tool for the job, and when based upon standardized interfaces, classes or annotations then they can be just as stable and communicative.&lt;/p&gt;
&lt;p&gt;Be pragmatic.&lt;/p&gt;
&lt;h2&gt;Afterword&lt;/h2&gt;
&lt;p&gt;This has not been a rant about the &lt;em&gt;AspectJ&lt;/em&gt; pointcut langauge, which is in fact the most elegant, concise and coherent &lt;em&gt;&lt;span class=&quot;caps&quot;&gt;DSL&lt;/span&gt;&lt;/em&gt; I have ever worked with. But an attempt of showing a way of using it, in the context of &lt;em&gt;Domain-Driven Design&lt;/em&gt;, that I have found useful and that addresses some of the problems with staying in sync with the model and the rest of the code throughout the lifetime of the application.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Update&lt;/strong&gt;: Added some example pointcuts in the section titled &lt;em&gt;Bringing it all together: Domain-Driven Pointcut Design&lt;/em&gt;&lt;/p&gt;</content>
 </entry>
 
 <entry>
   <title>Thoughts On Parenthood In The Software Industry</title>
   <link href="http://jboner.github.com/2006/04/18/thoughts-on-parenthood-in-the-software-industry"/>
   <updated>2006-04-18T00:00:00+00:00</updated>
   <id>hhttp://jboner.github.com/2006/04/18/thoughts-on-parenthood-in-the-software-industry</id>
   <content type="html">&lt;h1&gt;
&lt;p&gt;Thoughts On Parenthood In The Software Industry&lt;/p&gt;
&lt;/h1&gt;
&lt;p&gt;I have been thinking some about parenthood in the software industry, in particular, of frameworks and technologies. Men that have been the driving force behind, invented, or founded a particular technology or framework, are often referred to as &lt;em&gt;&amp;#8220;The Father Of&amp;#8230;&amp;#8221;&lt;/em&gt;.&lt;/p&gt;
&lt;p&gt;Is this really a fair and accurate metaphor?&lt;/p&gt;
&lt;p&gt;The &lt;em&gt;father&lt;/em&gt; usually gets away easy with the easy part (of just bootstrapping the whole process).&lt;/p&gt;
&lt;p&gt;While it is the &lt;em&gt;mother&lt;/em&gt; that:&lt;br /&gt;
&lt;ul&gt;&lt;br /&gt;
	&lt;li&gt;has to carry the baby for nine months (product development)&lt;/li&gt;&lt;/p&gt;
	&lt;li&gt;has to do all the labor and go through all the pain in the delivery (shipping and release) process&lt;/li&gt;
	&lt;li&gt;usually has to do most of the maintenance&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;I would like to suggest switching to using the metaphor &lt;em&gt;&amp;#8220;The Mother Of&amp;#8230;&amp;#8221;&lt;/em&gt; (regardless of the sex of the subject).&lt;/p&gt;</content>
 </entry>
 
 <entry>
   <title>Language Of The Year</title>
   <link href="http://jboner.github.com/2006/04/06/language-of-the-year"/>
   <updated>2006-04-06T00:00:00+00:00</updated>
   <id>hhttp://jboner.github.com/2006/04/06/language-of-the-year</id>
   <content type="html">&lt;h1&gt;
&lt;p&gt;Language Of The Year&lt;/p&gt;
&lt;/h1&gt;
&lt;p&gt;One, out of many great advice, coming from the book &lt;a href=&quot;http://www.amazon.com/gp/product/020161622X/sr=8-1/qid=1144104507/ref=pd_bbs_1/104-3443519-2815145?%5Fencoding=UTF8&quot;&gt;The Pragmatic Programmer&lt;/a&gt; (a book that has sort of been my manifesto the last years), is that you should try to learn a new programming language every year.&lt;/p&gt;
&lt;p&gt;You should do this, not because you need to use it in your daily work (even though that is undoubtly the best way of learning a new language), but because it will widen your horizon, help you think and solve (your day-to-day) problems differently and better.&lt;/p&gt;
&lt;p&gt;I have tried to follow this advice the last years (even though I have some gaps) and the last months my interest has been into &lt;a href=&quot;http://en.wikipedia.org/wiki/Declarative_programming&quot;&gt;declarative programming&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;So the &amp;#8220;Language Of The Year&amp;#8221; 2006 is &lt;a href=&quot;http://scala.epfl.ch/&quot;&gt;Scala &lt;/a&gt;along with a deeper dive into &lt;a href=&quot;http://www.mozart-oz.org/&quot;&gt;Oz&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;History:&lt;/p&gt;
&lt;ul&gt;
	&lt;li&gt;Scala and Oz&lt;/li&gt;
	&lt;li&gt;2005 &amp;#8211; nil&lt;/li&gt;
	&lt;li&gt;2004 &amp;#8211; nil&lt;/li&gt;
	&lt;li&gt;2003 &amp;#8211; Groovy&lt;/li&gt;
	&lt;li&gt;2002 &amp;#8211; AspectJ&lt;/li&gt;
	&lt;li&gt;2001 &amp;#8211; Oz&lt;/li&gt;
	&lt;li&gt;2000 &amp;#8211; Ruby&lt;/li&gt;
	&lt;li&gt;1999 &amp;#8211; &lt;span class=&quot;caps&quot;&gt;XML&lt;/span&gt;&lt;/li&gt;
	&lt;li&gt;1998 &amp;#8211; Python and Jython&lt;/li&gt;
	&lt;li&gt;1997 &amp;#8211; Java&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;(The language of 2007 is probably going to be &lt;a href=&quot;http://en.wikipedia.org/wiki/Haskell_programming_language&quot;&gt;Haskell&lt;/a&gt;, but let&amp;#8217;s not get ahead of ourselves).&lt;/p&gt;</content>
 </entry>
 
 <entry>
   <title>The Optimized Null Check Pattern</title>
   <link href="http://jboner.github.com/2006/04/04/the-optimized-null-check-pattern"/>
   <updated>2006-04-04T00:00:00+00:00</updated>
   <id>hhttp://jboner.github.com/2006/04/04/the-optimized-null-check-pattern</id>
   <content type="html">&lt;h1&gt;
&lt;p&gt;The Optimized Null Check Pattern&lt;/p&gt;
&lt;/h1&gt;
&lt;p&gt;A friend of mine showed me The Optimized Null Check Pattern&amp;#8482; yesterday (found in a piece of production code):&lt;/p&gt;
&lt;pre name=&quot;code&quot; class=&quot;java&quot;&gt;
public static boolean isNull(Object obj) {
    boolean isNull;
    try {
        obj.toString();
        isNull = false;
    } catch (NullPointerException e) {
        isNull = true;
    }
    return isNull;
}
&lt;/pre&gt;
&lt;p&gt;Then a colleague pointed out to me that a variation of the pattern (not as clean though) is actually used by Sun.&lt;/p&gt;
&lt;p&gt;From the &lt;code&gt;java.util.logging.LogRecord&lt;/code&gt; class:&lt;/p&gt;
&lt;pre name=&quot;code&quot; class=&quot;java&quot;&gt;
public LogRecord(Level level, String msg) {
    // Make sure level isn&#39;t null, by calling random method.
    level.getClass();
 
    ...
}
&lt;/pre&gt;</content>
 </entry>
 
 <entry>
   <title>Interview</title>
   <link href="http://jboner.github.com/2006/04/04/interview"/>
   <updated>2006-04-04T00:00:00+00:00</updated>
   <id>hhttp://jboner.github.com/2006/04/04/interview</id>
   <content type="html">&lt;h1&gt;
&lt;p&gt;Interview&lt;/p&gt;
&lt;/h1&gt;
&lt;p&gt;JayWay just published their &lt;a href=&quot;http://www.jayway.se/download/JayView10_2006.pdf&quot;&gt;interview of me&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;Topics discussed: &lt;span class=&quot;caps&quot;&gt;AOP&lt;/span&gt; (standardization, future, what is missing, real-world usage etc.) and transparent distributed clustering (&lt;span class=&quot;caps&quot;&gt;DSO&lt;/span&gt; &amp;#8211; Distributed Shared Objects).&lt;/p&gt;</content>
 </entry>
 
 <entry>
   <title>Want To Work At Terracotta?</title>
   <link href="http://jboner.github.com/2006/03/24/want-to-work-at-terracotta"/>
   <updated>2006-03-24T00:00:00+00:00</updated>
   <id>hhttp://jboner.github.com/2006/03/24/want-to-work-at-terracotta</id>
   <content type="html">&lt;h1&gt;
&lt;p&gt;Want To Work At Terracotta?&lt;/p&gt;
&lt;/h1&gt;
&lt;p&gt;We are currently hiring developers to work in a team that is crafting an innovative container/runtime to provide transparent runtime services for Java objects (POJOs).&lt;/p&gt;
&lt;p&gt;Requires knowledge in the following areas: &lt;br /&gt;
&lt;ul&gt;&lt;br /&gt;
	&lt;li&gt;Object-Oriented Programming and design, patterns etc.&lt;/p&gt;
&lt;/li&gt;
	&lt;li&gt;Deep understanding how Java works, including class loading, garbage collection etc.&lt;/li&gt;
	&lt;li&gt;Aspect-Oriented Programming (&lt;span class=&quot;caps&quot;&gt;AOP&lt;/span&gt;)&lt;/li&gt;
	&lt;li&gt;Concurrent and parallel processing techniques&lt;/li&gt;
	&lt;li&gt;Knowledge of distributed programming and component models (sockets, &lt;span class=&quot;caps&quot;&gt;JMS&lt;/span&gt;, &lt;span class=&quot;caps&quot;&gt;RMI&lt;/span&gt;, &lt;span class=&quot;caps&quot;&gt;EJB&lt;/span&gt; etc.)&lt;/li&gt;
	&lt;li&gt;Knowledge of the current state-of-the-art in &lt;span class=&quot;caps&quot;&gt;JEE&lt;/span&gt; today, including standards, best-practices, patterns and anti-patterns, open source and proprietary frameworks etc.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Experience in the follow areas is considered a plus:&lt;br /&gt;
&lt;ul&gt;&lt;br /&gt;
	&lt;li&gt;Bytecode manipulation and analysis&lt;/p&gt;
&lt;/li&gt;
	&lt;li&gt;Dynamic languages such as JavaScript, Smalltalk, &lt;span class=&quot;caps&quot;&gt;CLOS&lt;/span&gt;, Ruby, BeanShell, Groovy and Python&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Culture &lt;br /&gt;
&lt;ul&gt;&lt;br /&gt;
	&lt;li&gt;Must be able to express ideas clearly&lt;/p&gt;
&lt;/li&gt;
	&lt;li&gt;Must be a team player&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Does this describe you?&lt;/p&gt;
&lt;p&gt;Do you like to write application servers instead of use them? Are you dissatisfied with the current state of &lt;span class=&quot;caps&quot;&gt;JEE&lt;/span&gt;, with tons of incoherent &lt;span class=&quot;caps&quot;&gt;XML&lt;/span&gt; config and thousands of APIs? Do you hate complexity when without cause? Do you believe that two heads are better than one and that ideas are what matters.&lt;/p&gt;
&lt;p&gt;If this describes you then come and work at &lt;a href=&quot;http://www.terracottatech.com/&quot;&gt;Terracotta&lt;/a&gt; (based in San Francisco).&lt;/p&gt;
&lt;p&gt;You should have excellent programming abilities and design sense, and be capable of working closely with team members on design and implementation (XP-style).&lt;/p&gt;
&lt;p&gt;If you think this sounds interesting, then drop me an email.&lt;/p&gt;</content>
 </entry>
 
 <entry>
   <title>Keith Jarrett's Solo Concert At The War Memorial Opera House In San Francisco</title>
   <link href="http://jboner.github.com/2006/03/21/keith-jarretts-solo-concert-at-the-war-memorial-opera-house-in-san-francisco"/>
   <updated>2006-03-21T00:00:00+00:00</updated>
   <id>hhttp://jboner.github.com/2006/03/21/keith-jarretts-solo-concert-at-the-war-memorial-opera-house-in-san-francisco</id>
   <content type="html">&lt;h1&gt;
&lt;p&gt;Keith Jarrett&amp;#8217;s Solo Concert At The War Memorial Opera House In San Francisco&lt;/p&gt;
&lt;/h1&gt;
&lt;p&gt;&lt;img alt=&quot;Keith Jarrett In Action&quot; src=&quot;http://www.sfjazz.org/images/concerts/artist/2006/400/jarrett.jpg&quot; /&gt;&lt;/p&gt;
&lt;p&gt;I had the privilege of sitting in the sixth row during Keith Jarrett&amp;#8217;s solo concert at War Memorial Opera House in San Francisco yesterday evening.&lt;/p&gt;
&lt;p&gt;It was a fantastic concert and musical experience, something I will carry with me for a long time.&lt;/p&gt;
&lt;p&gt;Jarrett&amp;#8217;s solo concerts are always completely improvised (meaning both themes, chords, melodies and solos) musical adventures, in which he mixes jazz, classical music, lyricsm, avant-garde and more.&lt;/p&gt;
&lt;p&gt;He is, &lt;span class=&quot;caps&quot;&gt;IMHO&lt;/span&gt;, the world&amp;#8217;s most brilliant pianist and composer, and experiencing his musical brinkmanship and mastery yesterday was indeed, as one reviewer wrote, a &amp;#8220;life-altering&amp;#8221; experience.&lt;/p&gt;
&lt;p&gt;The concert was recorded, so it might be available on CD in a near future.&lt;/p&gt;</content>
 </entry>
 
 <entry>
   <title>Books That Makes You Think</title>
   <link href="http://jboner.github.com/2006/03/20/books-that-makes-you-think"/>
   <updated>2006-03-20T00:00:00+00:00</updated>
   <id>hhttp://jboner.github.com/2006/03/20/books-that-makes-you-think</id>
   <content type="html">&lt;h1&gt;
&lt;p&gt;Books That Makes You Think&lt;/p&gt;
&lt;/h1&gt;
&lt;p&gt;After &lt;a href=&quot;http://jonasboner.com/2006/03/15/what-happened-to-computer-literature-that-makes-you-think-and-challenge-the-way-you-work/&quot;&gt;my  rant&lt;/a&gt; about the lack of computer literature that:&lt;br /&gt;
&lt;blockquote&gt;&amp;#8230;makes you think and challenge the way you work.&lt;/blockquote&gt;&lt;br /&gt;
I thought that I should provide a list of books that have made me start think about what I am doing and how I am doing it. Simply, books that have meant a lot to me.&lt;/p&gt;
&lt;p&gt;This is by no means a complete list (I am sure I have missed a couple o gems) and the books are listed no particular order.&lt;/p&gt;
&lt;p&gt;&lt;a href=&quot;http://www.amazon.com/gp/product/020161622X/qid=1142550672/sr=2-1/ref=pd_bbs_b_2_1/104-6589157-2527103?s=books&amp;v=glance&amp;n=283155&quot;&gt;The Pragmatic Programmer: From Journeyman to Master&lt;/a&gt;&lt;br /&gt;
by Andrew Hunt, David Thomas&lt;/p&gt;
&lt;p&gt;&lt;a href=&quot;http://www.amazon.com/gp/product/0321125215/qid=1142058361/sr=2-1/ref=pd_bbs_b_2_1/103-2054129-4399023?s=books&amp;v=glance&amp;n=283155&quot;&gt;Domain Driven Design&lt;/a&gt;&lt;br /&gt;
by Eric Evans&lt;/p&gt;
&lt;p&gt;&lt;a href=&quot;http://www.amazon.com/gp/product/0321213351/qid=1142550742/sr=2-1/ref=pd_bbs_b_2_1/104-6589157-2527103?s=books&amp;v=glance&amp;n=283155&quot;&gt;Refactoring to Patterns&lt;/a&gt;&lt;br /&gt;
by Joshua Kerievsky&lt;/p&gt;
&lt;p&gt;&lt;a href=&quot;http://www.amazon.com/gp/product/0262610744/qid=1142552985/sr=1-1/ref=sr_1_1/104-6589157-2527103?s=books&amp;v=glance&amp;n=283155&quot;&gt;The Art of the Metaobject Protocol&lt;/a&gt;&lt;br /&gt;
by Gregor Kiczales&lt;/p&gt;
&lt;p&gt;&lt;a href=&quot;http://www.amazon.com/gp/product/0201310058/qid=1142553338/sr=2-1/ref=pd_bbs_b_2_1/104-6589157-2527103?s=books&amp;v=glance&amp;n=283155&quot;&gt;Effective Java&lt;/a&gt;&lt;br /&gt;
by Joshua Bloch&lt;/p&gt;
&lt;p&gt;&lt;a href=&quot;http://www.amazon.com/gp/product/0201485672/ref=cm_bg_d_7/104-6589157-2527103?s=books&amp;v=glance&amp;n=283155&quot;&gt;Refactoring: Improving the Design of Existing Code&lt;/a&gt;&lt;br /&gt;
by Martin Fowler&lt;/p&gt;
&lt;p&gt;&lt;a href=&quot;http://www.amazon.com/gp/product/0201310090/qid=1142553403/sr=2-1/ref=pd_bbs_b_2_1/104-6589157-2527103?s=books&amp;v=glance&amp;n=283155&quot;&gt;Concurrent Programming in Java: Design Principles and Pattern&lt;/a&gt;&lt;br /&gt;
by Doug Lea&lt;/p&gt;
&lt;p&gt;&lt;a href=&quot;http://www.amazon.com/gp/product/0735619670/ref=cm_bg_d_4/104-6589157-2527103?v=glance&amp;n=283155&quot;&gt;Code Complete&lt;/a&gt;&lt;br /&gt;
by Steve McConnell&lt;/p&gt;
&lt;p&gt;&lt;a href=&quot;http://www.amazon.com/gp/product/0201657880/qid=1142549286/sr=2-1/ref=pd_bbs_b_2_1/104-6589157-2527103?s=books&amp;v=glance&amp;n=283155&quot;&gt;Programming Pearls&lt;/a&gt;&lt;br /&gt;
by Jon Bentley&lt;/p&gt;
&lt;p&gt;&lt;a href=&quot;http://www.amazon.com/gp/product/0201633612/ref=pd_sr_ec_ir_b/104-6589157-2527103?n=283155&quot;&gt;Design Patterns: Elements of Reusable Object-Oriented Software&lt;/a&gt;&lt;br /&gt;
by Erich Gamma, Richard Helm, Ralph Johnson, John Vlissides&lt;/p&gt;
&lt;p&gt;&lt;a href=&quot;http://www.amazon.com/gp/product/0135974445/ref=pd_sr_ec_cs_b-koth-pa1/104-6589157-2527103?s=books&amp;st=%2A&amp;v=glance&amp;n=283155&quot;&gt;Agile Software Development, Principles, Patterns, and Practices&lt;br /&gt;
&lt;/a&gt;by Robert C. Martin&lt;/p&gt;
&lt;p&gt;&lt;a href=&quot;http://www.amazon.com/gp/product/0321127420/qid=1142058328/sr=2-1/ref=pd_bbs_b_2_1/103-2054129-4399023?s=books&amp;v=glance&amp;n=283155&quot;&gt;Patterns Of Enterprise Application Architecture&lt;/a&gt;&lt;br /&gt;
by Martin Fowler&lt;/p&gt;
&lt;p&gt;&lt;a href=&quot;http://www.amazon.com/gp/product/0321278658/sr=8-2/qid=1142876835/ref=pd_bbs_2/104-0037309-3509544?%5Fencoding=UTF8&quot;&gt;Extreme Programming Explained : Embrace Change&lt;/a&gt;&lt;br /&gt;
by Kent Beck&lt;/p&gt;
&lt;p&gt;&lt;a href=&quot;http://www.amazon.com/gp/product/0201895420/sr=8-1/qid=1142878037/ref=pd_bbs_1/104-0037309-3509544?%5Fencoding=UTF8&quot;&gt;Analysis Patterns : Reusable Object Models&lt;/a&gt;&lt;br /&gt;
by Martin Fowler&lt;/p&gt;
&lt;p&gt;I am always eager to read more good literature. What are your favourite books, which books have meant a lot to you and your career? If you have some suggestions, please comment.&lt;/p&gt;</content>
 </entry>
 
 <entry>
   <title>Welcome Noah</title>
   <link href="http://jboner.github.com/2006/03/16/welcome-noah"/>
   <updated>2006-03-16T00:00:00+00:00</updated>
   <id>hhttp://jboner.github.com/2006/03/16/welcome-noah</id>
   <content type="html">&lt;h1&gt;
&lt;p&gt;Welcome Noah&lt;/p&gt;
&lt;/h1&gt;
&lt;p&gt;Saturday 17:45 &lt;span class=&quot;caps&quot;&gt;PST&lt;/span&gt;, little Noah decided that it was time to come out and meet the family, seven weeks early, but very welcomed. He will have to stay in the incubator for another two to three weeks, but is getting stronger every day.&lt;/p&gt;</content>
 </entry>
 
 <entry>
   <title>POJOs In Action</title>
   <link href="http://jboner.github.com/2006/03/16/pojos-in-action-2"/>
   <updated>2006-03-16T00:00:00+00:00</updated>
   <id>hhttp://jboner.github.com/2006/03/16/pojos-in-action-2</id>
   <content type="html">&lt;h1&gt;
&lt;p&gt;POJOs In Action&lt;/p&gt;
&lt;/h1&gt;
&lt;p&gt;After &lt;a href=&quot;http://jonasboner.com/2006/03/15/what-happened-to-computer-literature-that-makes-you-think-and-challenge-the-way-you-work/&quot;&gt;yesterday&amp;#8217;s rant&lt;/a&gt; about the low quality in computer literature today, I felt the need to reply to myself.&lt;/p&gt;
&lt;p&gt;One of the books I have been reading lately actually is a book that makes you think and challenge the way you work (if you are working with enterprise Java application development).&lt;/p&gt;
&lt;p&gt;The book I am talking about is &lt;a href=&quot;http://www.amazon.com/gp/product/1932394583/sr=8-1/qid=1142057200/ref=pd_bbs_1/103-2054129-4399023?%5Fencoding=UTF8&quot;&gt;POJOs in Action&lt;/a&gt; by &lt;a href=&quot;http://chris-richardson.blog-city.com/&quot;&gt;Chris Richardson&lt;/a&gt;, a book with a lot of depth and many dimensions.&lt;/p&gt;
&lt;p&gt;Chris is standing on the shoulders of giants, and is basing a lot of his discussion on two recent classics in computer literature: &lt;a href=&quot;http://www.amazon.com/gp/product/0321127420/qid=1142058328/sr=2-1/ref=pd_bbs_b_2_1/103-2054129-4399023?s=books&amp;v=glance&amp;n=283155&quot;&gt;Patterns Of Enterprise Application Architecture&lt;/a&gt; by Martin Fowler and &lt;a href=&quot;http://www.amazon.com/gp/product/0321125215/qid=1142058361/sr=2-1/ref=pd_bbs_b_2_1/103-2054129-4399023?s=books&amp;v=glance&amp;n=283155&quot;&gt;Domain Driven Design&lt;/a&gt; by Eric Evans. POJOs In Action can be seen as a natural practical follow-up to these books.&lt;/p&gt;
&lt;p&gt;Chris has a very pragmatic and relaxed (non-hyped) view on things and has a many interesting discussions and examples that comes from his experience in the field.&lt;/p&gt;
&lt;p&gt;It is a practical book, which discusses, through code samples (which all are test-driven) based on both various standards as well as open source frameworks, not only patterns, design and high-level concepts, but &lt;strong&gt;the process&lt;/strong&gt; of getting them to work and become a natural part in your daily work.&lt;/p&gt;
&lt;p&gt;Highly recommended.&lt;/p&gt;</content>
 </entry>
 
 <entry>
   <title>What Happened To Computer Literature That Makes You Think And Challenge The Way You Work?</title>
   <link href="http://jboner.github.com/2006/03/15/what-happened-to-computer-literature-that-makes-you-think-and-challenge-the-way-you-work"/>
   <updated>2006-03-15T00:00:00+00:00</updated>
   <id>hhttp://jboner.github.com/2006/03/15/what-happened-to-computer-literature-that-makes-you-think-and-challenge-the-way-you-work</id>
   <content type="html">&lt;h1&gt;
&lt;p&gt;What Happened To Computer Literature That Makes You Think And Challenge The Way You Work?&lt;/p&gt;
&lt;/h1&gt;
&lt;p&gt;I like books.&lt;/p&gt;
&lt;p&gt;I enjoy spending time in libraries and bookstores, I can spend hours just browsing around and when my wife is getting bored (usually after 10 min) or when she comes back from her 3 h shopping round, she usually finds me in either the novel section, biography section or the section for computer literature.&lt;/p&gt;
&lt;p&gt;Unfortunately, the visits to the latter one has (the last years) started to disappointment me.&lt;/p&gt;
&lt;p&gt;Why? One of the major reasons is that the &amp;#8220;learn-how-to-use-open-source-framework-X&amp;#8221; type books have completely taken over the place. 90 % of the shelves are occupied by books like: &amp;#8216;X in Action&amp;#8217;, &amp;#8216;Pro Y&amp;#8217;, &amp;#8216;Professional development with W&amp;#8217; or &amp;#8216;Z: developers notebook&amp;#8217; etc. Phew&amp;#8230;&lt;/p&gt;
&lt;p&gt;It seems that everyone is writing a book (and getting it published!) now days. I guess it is not necessarily a bad thing, but it for sure makes it harder to find the real gems, the books worth spending time reading.&lt;/p&gt;
&lt;p&gt;Generally, I have completely stopped reading the &amp;#8220;learn-how-to-use-open-source-framework-X&amp;#8221; type books.&lt;/p&gt;
&lt;p&gt;Most books in this category tend to be very shallow, overly simplified, not thought through, badly written and often combined with too extensive and verbose code samples (there are of course exceptions). Most likely because they have been written under a very tight deadline &amp;#8211; set by the publisher knowing that the topic of the book is expected to be dead in 2 years.&lt;/p&gt;
&lt;p&gt;Books that is simply a walk-through of the APIs and ideas behind a specific framework or technology are generally useless. When I want to learn more about how to use framework X, then I can find the information I need both faster (and cheaper) on the Internet, and then in a format that is both searchable, printable and &amp;#8220;cut-and-paste-able&amp;#8221;. Thanks Google.&lt;/p&gt;
&lt;p&gt;So, what is my definition of a &amp;#8220;good book&amp;#8221; &amp;#8211; a book worth spending time reading?&lt;/p&gt;
&lt;p&gt;In general, books that &lt;strong&gt;makes you think&lt;/strong&gt; and &lt;strong&gt;challenge the way you do things&lt;/strong&gt;, books that &lt;strong&gt;raises questions&lt;/strong&gt;. I generally prefer reading about concepts, things that I do not have to worry being outdated in a year or two. Books that have a place in my library even in 10 years from now.&lt;/p&gt;
&lt;p&gt;So, what happened to good computer literature? Well, fortunately it is still being written and published. It are just so much harder to find.&lt;/p&gt;</content>
 </entry>
 
 <entry>
   <title>Terracotta DSO Explained</title>
   <link href="http://jboner.github.com/2006/02/27/terracotta-dso-explained"/>
   <updated>2006-02-27T00:00:00+00:00</updated>
   <id>hhttp://jboner.github.com/2006/02/27/terracotta-dso-explained</id>
   <content type="html">&lt;h1&gt;
&lt;p&gt;Terracotta &lt;span class=&quot;caps&quot;&gt;DSO&lt;/span&gt; Explained&lt;/p&gt;
&lt;/h1&gt;
&lt;p&gt;Along with the 1.5 release of the &lt;a href=&quot;http://www.terracottatech.com/product_overview.shtml&quot;&gt;Terracotta Server&lt;/a&gt; (shipped today), we have new web site along with a really good flash demo explaining our &lt;span class=&quot;caps&quot;&gt;DSO&lt;/span&gt; (Distributed Shared Objects) technology.&lt;/p&gt;
&lt;p&gt;It takes you through the concepts and ideas behind the technology as well as shows you a couple of live demos, definitely worth the 5 minutes it takes to watch it.&lt;/p&gt;
&lt;p&gt;Just browse to the &lt;a href=&quot;http://www.terracottatech.com/index.shtml&quot;&gt;web site&lt;/a&gt; and click on [View Our Demo].&lt;/p&gt;</content>
 </entry>
 
 <entry>
   <title>Moving To San Francisco</title>
   <link href="http://jboner.github.com/2006/01/13/moving-to-san-francisco"/>
   <updated>2006-01-13T00:00:00+00:00</updated>
   <id>hhttp://jboner.github.com/2006/01/13/moving-to-san-francisco</id>
   <content type="html">&lt;h1&gt;
&lt;p&gt;Moving To San Francisco&lt;/p&gt;
&lt;/h1&gt;
&lt;p&gt;I am writing this blog post in a total chaos of bags, boxes, skiing and outdoor gear, books, pacifiers, computers etc.&lt;/p&gt;
&lt;p&gt;Tomorrow I am catching the plane to San Francisco, nothing special about that apart from that this time I am taking my family with me and we are planning on staying there on a permanent basis (at least for 3-4 years).&lt;/p&gt;
&lt;p&gt;A lot of excitement and anxiety in the air, but I think that we will have a lot of fun. Living in San Francisco, closer to my collegues and friends at &lt;a href=&quot;http://www.terracottatech.com/&quot;&gt;Terracotta&lt;/a&gt;, not more than 3 h from the &lt;a href=&quot;http://www.skiheavenly.com/&quot;&gt;mountains&lt;/a&gt; around Lake Tahoe and being able to enjoy the great &lt;a href=&quot;http://www.google.com/search?q=weather+san+francisco&quot;&gt;California weather&lt;/a&gt; all year around sounds like a good start. :-)&lt;/p&gt;
&lt;p&gt;If you live in the area and are &lt;a href=&quot;http://www.thirstybear.com/&quot;&gt;up for a beer&lt;/a&gt; then drop me a line.&lt;/p&gt;</content>
 </entry>
 
 <entry>
   <title>Highlights 2005</title>
   <link href="http://jboner.github.com/2005/12/31/highlights-2005"/>
   <updated>2005-12-31T00:00:00+00:00</updated>
   <id>hhttp://jboner.github.com/2005/12/31/highlights-2005</id>
   <content type="html">&lt;h1&gt;
&lt;p&gt;Highlights 2005&lt;/p&gt;
&lt;/h1&gt;
&lt;p&gt;2005 has been a good year, both personally and professionally.&lt;/p&gt;
&lt;p&gt;Here is a summary of some of the highlights (in chronological order):&lt;br /&gt;
&lt;ul&gt;&lt;br /&gt;
	&lt;li&gt;Family trip to Vietnam for 6 weeks. Amazing and beutiful country, friendly people, interesting culture, delicious food etc.&lt;/li&gt;&lt;br /&gt;
	&lt;li&gt;The merge of AspectWerkz and AspectJ &lt;a href=&quot;http://www.theserverside.com/news/thread.tss?thread_id=31244&quot;&gt;is announced&lt;/a&gt; .&lt;/li&gt;&lt;br /&gt;
	&lt;li&gt;Sara and I skiing powder in &lt;a href=&quot;http://www.abisko.nu/vinter06/intro/index.asp&quot;&gt;Abisko&lt;/a&gt; (close to the pole circle) for 3 days &amp;#8211; without kids! :-)&lt;/li&gt;&lt;br /&gt;
	&lt;li&gt;Family &lt;a href=&quot;http://www.flickr.com/photos/jboner/sets/1574127/&quot;&gt;trip to California&lt;/a&gt; for 2 weeks.&lt;/li&gt;&lt;br /&gt;
	&lt;li&gt;Release of  the &lt;a href=&quot;http://dev2dev.bea.com/pub/a/2005/08/jvm_aop_1.html&quot;&gt;support for &lt;span class=&quot;caps&quot;&gt;AOP&lt;/span&gt; in the JRockit &lt;span class=&quot;caps&quot;&gt;JVM&lt;/span&gt;&lt;/a&gt;.&lt;/li&gt;&lt;br /&gt;
	&lt;li&gt;Left &lt;a href=&quot;http://bea.com/&quot;&gt;&lt;span class=&quot;caps&quot;&gt;BEA&lt;/span&gt;&lt;/a&gt; and &lt;a href=&quot;http://www.theserverside.com/news/thread.tss?thread_id=36073&quot;&gt;joined Terracotta&lt;/a&gt;.&lt;/li&gt;&lt;br /&gt;
	&lt;li&gt;&lt;a href=&quot;http://static.flickr.com/20/73268814_9fd457e473_o.jpg&quot;&gt;Jacob&lt;/a&gt; turned 2. Amazing little guy, that has gone from hardly speaking at all to someone talking so much that you hardly get anything said (or done), in just 4 months&amp;#8230;&lt;/li&gt;&lt;br /&gt;
	&lt;li&gt;Our family got a US &lt;span class=&quot;caps&quot;&gt;VISA&lt;/span&gt; (we are moving over in mid Jan 2006).&lt;/li&gt;&lt;br /&gt;
	&lt;li&gt;The &lt;a href=&quot;http://jonasboner.com/2005/12/22/aspectj-5-final-is-shipped/&quot;&gt;release of AspectJ 5&lt;/a&gt;.&lt;/li&gt;&lt;/p&gt;
&lt;/ul&gt;
&lt;p&gt;Here is a collage over the family highlights 2005, in chronological order.&lt;/p&gt;
&lt;p&gt;&lt;a href=&quot;http://static.flickr.com/43/79729827_5c4dcbd242_o.jpg&quot;&gt;&lt;img alt=&quot;Highlights 2005&quot; src=&quot;http://static.flickr.com/43/79729827_5c4dcbd242_m.jpg&quot; /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Click on the image for a full-sized view.&lt;/p&gt;</content>
 </entry>
 
 <entry>
   <title>Think AOP Is Dead? - Time To Reconsider...</title>
   <link href="http://jboner.github.com/2005/12/22/think-aop-is-dead-time-to-reconsider"/>
   <updated>2005-12-22T00:00:00+00:00</updated>
   <id>hhttp://jboner.github.com/2005/12/22/think-aop-is-dead-time-to-reconsider</id>
   <content type="html">&lt;h1&gt;
&lt;p&gt;Think &lt;span class=&quot;caps&quot;&gt;AOP&lt;/span&gt; Is Dead? &amp;#8211; Time To Reconsider&amp;#8230;&lt;/p&gt;
&lt;/h1&gt;
&lt;p&gt;If you thought &lt;span class=&quot;caps&quot;&gt;AOP&lt;/span&gt; was dead or at least dying, then it is time to reconsider.&lt;br /&gt;
&lt;span class=&quot;caps&quot;&gt;AOP&lt;/span&gt; is not dead. Not even close.&lt;/p&gt;
&lt;p&gt;After a perhaps healthy backlash, that took away some of the hype and coolness about &lt;span class=&quot;caps&quot;&gt;AOP&lt;/span&gt;, it is steadily getting more and more adoption and traction in the enterprise space at large.&lt;/p&gt;
&lt;p&gt;A lot is happening now. Here are only a few samples:&lt;/p&gt;
&lt;p&gt;Yesterday the release of &lt;a href=&quot;http://eclipse.org/aspectj/&quot;&gt;AspectJ 5&lt;/a&gt; final was &lt;a href=&quot;http://dev.eclipse.org/mhonarc/lists/aspectj-users/msg05174.html&quot;&gt;announced&lt;/a&gt;. This is &lt;span class=&quot;caps&quot;&gt;IMO&lt;/span&gt; the most significant milestone in the history of &lt;span class=&quot;caps&quot;&gt;AOP&lt;/span&gt; (so far). Finally we have a standardized language and toolkit and can, both as developers and users, try to move in the same direction and focus on building new things on top of this platform &amp;#8211; instead of fighting over semantics, APIs and user base.&lt;/p&gt;
&lt;p&gt;&lt;a href=&quot;http://www.aspectprogrammer.org/blogs/adrian/&quot;&gt;Adrian Colyer&lt;/a&gt; and and the &lt;a href=&quot;http://www.springframework.org/&quot;&gt;Spring&lt;/a&gt; team has done a tremendous job unifiying AspectJ 5 and Spring. Adrian showed me some of their work during the Spring Experience conference and they have some very cool and useful things both working and in the pipeline. Stay tuned for the upcoming Spring 2.0 milestones.&lt;/p&gt;
&lt;p&gt;I had the pleasure of attending Rod Johnson&amp;#8217;s talk on Spring 2.0 at JavaPolis, a great talk that actually was mainly a (practical and non salesy) PR talk on &lt;span class=&quot;caps&quot;&gt;AOP&lt;/span&gt;, in which he spent roughly half the time explaining why AspectJ is important for Spring users how they can utilize it in the upcoming 2.0 release of Spring. This is another evidence that Spring is embracing &lt;span class=&quot;caps&quot;&gt;AOP&lt;/span&gt; and AspectJ 5 in a big way.&lt;/p&gt;
&lt;p&gt;The future looks bright&amp;#8230;&lt;br /&gt;
&amp;#8230;but it is still mainly up to you users to make it happen.&lt;/p&gt;
&lt;p&gt;So, go ahead and &lt;a href=&quot;http://www.eclipse.org/ajdt/downloads/&quot;&gt;download AspectJ 5&lt;/a&gt;, play with it, have fun and then start use it for real&amp;#8230;&lt;/p&gt;</content>
 </entry>
 
 <entry>
   <title>AspectJ 5 Final Is Shipped</title>
   <link href="http://jboner.github.com/2005/12/22/aspectj-5-final-is-shipped"/>
   <updated>2005-12-22T00:00:00+00:00</updated>
   <id>hhttp://jboner.github.com/2005/12/22/aspectj-5-final-is-shipped</id>
   <content type="html">&lt;h1&gt;
&lt;p&gt;AspectJ 5 Final Is Shipped&lt;/p&gt;
&lt;/h1&gt;
&lt;p&gt;&lt;a href=&quot;http://eclipse.org/aspectj/&quot;&gt;AspectJ 5&lt;/a&gt; final is finally shipped.&lt;/p&gt;
&lt;p&gt;This means that the &lt;a href=&quot;http://eclipse.org/aspectj/aj5announce.html&quot;&gt;merger&lt;/a&gt; with &lt;a href=&quot;http://aspectwerkz.codehaus.org/&quot;&gt;AspectWerkz&lt;/a&gt; is complete and that the most important chapter in the history of &lt;span class=&quot;caps&quot;&gt;AOP&lt;/span&gt; has been completed (&lt;span class=&quot;caps&quot;&gt;IMHO&lt;/span&gt;). Finally we have a standardized language and toolkit and can, both as developers and users, try to (finally) pull in the same direction. For more info, read Adrian Colyers&amp;#8217; &lt;a href=&quot;http://www.aspectprogrammer.org/blogs/adrian/2005/12/aspectj_150_is.html&quot;&gt;blog announcement&lt;/a&gt; or the &lt;a href=&quot;http://www.eclipse.org/aspectj/doc/released/README-150.html&quot;&gt;release page&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;Go ahead and download the &lt;a href=&quot;http://www.eclipse.org/aspectj/downloads.php&quot;&gt;stand alone compiler&lt;/a&gt; or the &lt;a href=&quot;http://www.eclipse.org/ajdt/downloads/&quot;&gt;&lt;span class=&quot;caps&quot;&gt;AJDT&lt;/span&gt; &lt;span class=&quot;caps&quot;&gt;IDE&lt;/span&gt; toolkit&lt;/a&gt; (with an embedded compiler).&lt;/p&gt;
&lt;p&gt;Time to have some fun.&lt;/p&gt;</content>
 </entry>
 
 <entry>
   <title>Slides From My Session On Transparently Clustered Spring @ The Spring Experience</title>
   <link href="http://jboner.github.com/2005/12/19/slides-from-my-session-on-transparently-clustered-spring-the-spring-experience"/>
   <updated>2005-12-19T00:00:00+00:00</updated>
   <id>hhttp://jboner.github.com/2005/12/19/slides-from-my-session-on-transparently-clustered-spring-the-spring-experience</id>
   <content type="html">&lt;h1&gt;
&lt;p&gt;Slides From My Session On Transparently Clustered Spring @ The Spring Experience&lt;/p&gt;
&lt;/h1&gt;
&lt;p&gt;Some people has asked for them, so here they are: &lt;a href=&quot;http://www.terracottatech.com/spring/TCSR_spring_experience_2005.pdf&quot;&gt;the slides&lt;/a&gt; from &lt;a href=&quot;http://thespringexperience.com/speaker_topic_view.jsp?topicId=206&quot;&gt;my talk&lt;/a&gt; on &lt;a href=&quot;http://www.terracottatech.com/spring/&quot;&gt;Transparently Clustered Spring&lt;/a&gt; at &lt;a href=&quot;http://thespringexperience.com/&quot;&gt;The Spring Experience Conference&lt;/a&gt;.&lt;/p&gt;</content>
 </entry>
 
 <entry>
   <title>Java Killed The Innovation Of Computer Languages</title>
   <link href="http://jboner.github.com/2005/12/19/java-killed-the-innovation-of-computer-languages"/>
   <updated>2005-12-19T00:00:00+00:00</updated>
   <id>hhttp://jboner.github.com/2005/12/19/java-killed-the-innovation-of-computer-languages</id>
   <content type="html">&lt;h1&gt;
&lt;p&gt;Java Killed The Innovation Of Computer Languages&lt;/p&gt;
&lt;/h1&gt;
&lt;p&gt;I had a very interesting discussion with &lt;a href=&quot;http://chris-richardson.blog-city.com/&quot;&gt;Chris Richardson&lt;/a&gt; at the &lt;a href=&quot;http://thespringexperience.com&quot;&gt;Spring Experience&lt;/a&gt; conference last week. Chris is an old &lt;a href=&quot;http://en.wikipedia.org/wiki/Lisp_programming_language&quot;&gt;Lisp&lt;/a&gt; hacker and used to write Lisp compilers and VMs before switching to Java about 10 years ago.&lt;/p&gt;
&lt;p&gt;Here are some quotes from Chris that I find particularly interesting:&lt;br /&gt;
&lt;blockquote&gt;Java killed the innovation of computer languages.&lt;/blockquote&gt;&lt;br /&gt;
&lt;blockquote&gt;AspectJ is one of the few innovations in computer languages the last years.&lt;/blockquote&gt;&lt;br /&gt;
Some comments on these statements:&lt;/p&gt;
&lt;p&gt;&lt;a href=&quot;http://en.wikipedia.org/wiki/Java_programming_language&quot;&gt;Java&lt;/a&gt; has had an enormous impact on enterprise application development the last 10 years and I do believe that Chris is right, it has to some extent &amp;#8220;killed the innovation of computer languages&amp;#8221; (considering commercially used and widely adopted languages). Sure, &lt;a href=&quot;http://en.wikipedia.org/wiki/C_Sharp&quot;&gt;C#&lt;/a&gt; have some more or less innovative ideas (sometimes I wish I was a .&lt;span class=&quot;caps&quot;&gt;NET&lt;/span&gt; developer) and Microsoft is doing some very cool stuff it the labs. But in general nothing has really happened the last 10 years. We have seen many new scripting languages popping up lately (Ruby, Python, Jython, groovy etc.) and even though most of them are both fun and useful &amp;#8211; non of them are really innovating, merely reusing and sometimes reshaping old ideas (not saying that that is not enough &amp;#8211; most of these languages has contributed tremendously on many ways).&lt;/p&gt;
&lt;p&gt;While this is true for commercially adopted languages (for enterprise application, desktop development etc.), things are fortunately different in the academic field and in the labs. The problem here is that not enough effort is made to, wrap up the ideas in something that is (at least to some exent) easy to use, and/or to meet real-world needs of scalability, maintainability, interoperability etc. (due to lack of money or lack of interest?).&lt;/p&gt;
&lt;p&gt;For example, one language that I have been playing with on and off the last years is the &lt;a href=&quot;http://www.mozart-oz.org/&quot;&gt;Mozart Programming System&lt;/a&gt; and its language Oz. Working with it is (as they say themselves) in some ways pure magic. It is an hybrid between object-oriented, functional and declarative languages which is very powerful. It supports high-performant network-transparent distribution (no difference between writing an app for one node or many) transparent sharing of classes, objects, variables, procedures etc., as well as real data-flow threads. It has build-in Separation of Concerns, allowing you to declaratively, in different modules, implement f.e. security, fault handling etc. (I should write a blog post about it later.)&lt;/p&gt;
&lt;p&gt;But the problem is that it is so hard to use (at least for someone like me, that has been doing Java and C the last 10 years), that simply reaching the stage of being able to start playing with it for real takes more time than most people are willing to spend. However, I really encourage you to spend some time learning it.&lt;/p&gt;
&lt;p&gt;I am biased, but I do think that &lt;a href=&quot;http://eclipse.org/aspectj/&quot;&gt;AspectJ&lt;/a&gt; is one of the few really innovating languages that has made it out of the reseach labs into the enterprise without having to do trade-offs between innovation, power, coherent semantics and ease of use.&lt;/p&gt;
&lt;p&gt;Some questions to chew on:&lt;br /&gt;
&lt;ul&gt;&lt;br /&gt;
	&lt;li&gt;What are the requirements for a modern computer language that addresses real-world needs in the enterprise (thinking outside the box)?&lt;/li&gt;&lt;br /&gt;
	&lt;li&gt;What does it take to replace Java (meaning the language, not the platform)?&lt;/li&gt;&lt;br /&gt;
	&lt;li&gt;Is it likely to happen within the next 10 years?&lt;/li&gt;&lt;br /&gt;
	&lt;li&gt;Is it even necessary?&lt;/li&gt;&lt;br /&gt;
	&lt;li&gt;Which are the most important lessons we have learned?&lt;/li&gt;&lt;br /&gt;
	&lt;li&gt;Will the next generation computer languages for enterprise development be build upon the Java Platform (&lt;span class=&quot;caps&quot;&gt;JVM&lt;/span&gt;)?&lt;/li&gt;&lt;br /&gt;
	&lt;li&gt;Which role [can/will/do we want] the academic research to play?&lt;/li&gt;&lt;br /&gt;
	&lt;li&gt;Are we willing to trade &lt;em&gt;Innovation&lt;/em&gt; (and potential long term benefits) for &lt;em&gt;Pragmatism&lt;/em&gt; and &lt;em&gt;Ease-Of-Use&lt;/em&gt; (meaning short term benefits)?&lt;/li&gt;&lt;/p&gt;
&lt;/ul&gt;</content>
 </entry>
 
 <entry>
   <title>JRockit: Still Way Ahead of Its Competition...</title>
   <link href="http://jboner.github.com/2005/12/13/jrockit-still-way-ahead-of-its-competition"/>
   <updated>2005-12-13T00:00:00+00:00</updated>
   <id>hhttp://jboner.github.com/2005/12/13/jrockit-still-way-ahead-of-its-competition</id>
   <content type="html">&lt;h1&gt;
&lt;p&gt;JRockit: Still Way Ahead of Its Competition&amp;#8230;&lt;/p&gt;
&lt;/h1&gt;
&lt;p&gt;&lt;a href=&quot;http://dev2dev.bea.com/jrockit/&quot;&gt;JRockit&lt;/a&gt; is still miles ahead of its competitors, both when it comes to speed and manageability/tool support.&lt;/p&gt;
&lt;p&gt;Today, JRockit released its &lt;a href=&quot;http://www.javalobby.org/java/forums/t59426.html&quot;&gt;latest SPECjbb2000 benchmark&lt;/a&gt; (J2EE benchmark), in which it set a new world record, leaving Sun&amp;#8217;s HotSpot in the dust&amp;#8230;&lt;/p&gt;
&lt;p&gt;It also keeps improving its excellent tool support and manageability. Please read &lt;a href=&quot;http://dev2dev.bea.com/blog/hirt/&quot;&gt;Marcus Hirt&amp;#8217;s&lt;/a&gt; new articles &lt;a href=&quot;http://dev2dev.bea.com/pub/a/2005/12/jrockit-5.html&quot;&gt;JRockit 5.0 The &lt;span class=&quot;caps&quot;&gt;JVM&lt;/span&gt; at Your Fingertips&lt;/a&gt; and &lt;a href=&quot;http://dev2dev.bea.com/pub/a/2005/12/jrockit-mission-control.html&quot;&gt;An Introduction to JRockit Mission Control&lt;/a&gt; as well as &lt;a href=&quot;http://dev2dev.bea.com/blog/sla/&quot;&gt;Staffan Larsen&amp;#8217;s&lt;/a&gt; article about JRockit&amp;#8217;s unique memory leak tool: &lt;a href=&quot;http://dev2dev.bea.com/pub/a/2005/06/memory_leaks.html&quot;&gt;Memory Leaks, Be Gone!&lt;/a&gt;. &lt;/p&gt;
&lt;p&gt;Today, there is really only one single valid excuse for not running JRockit and that is if you are deploying on non-intel architecture, and this reason will be terminated in the upcoming releases (with ports to Sparc, HP-UX etc.). &lt;/p&gt;
&lt;p&gt;(&amp;#8230;and please don&amp;#8217;t start rambling about instability again, that was an old (and then I mean really old) problem, the last releases, including the latest one, have been of very high quality &amp;#8211; which, in my experience, have had better stability than Sun HotSpot&amp;#8217;s comparable versions.)&lt;/p&gt;</content>
 </entry>
 
 <entry>
   <title>JavaPolis In-N-Out</title>
   <link href="http://jboner.github.com/2005/12/13/javapolis-in-n-out"/>
   <updated>2005-12-13T00:00:00+00:00</updated>
   <id>hhttp://jboner.github.com/2005/12/13/javapolis-in-n-out</id>
   <content type="html">&lt;h1&gt;
&lt;p&gt;JavaPolis In-N-Out&lt;/p&gt;
&lt;/h1&gt;
&lt;p&gt;I am heading for &lt;a href=&quot;http://www.javapolis.com/confluence/display/JP05/Home&quot;&gt;JavaPolis&lt;/a&gt; tomorrow morning. Will be spending 24 h in Antwerp, doing two talks, hoping to hang out with some good friends, build new relations, drink the excellent &lt;a href=&quot;http://www.trappistbeer.net/&quot;&gt;Belgian Trappist Beer&lt;/a&gt; (favorite is &lt;a href=&quot;http://www.chimay.be/www/chimay/site8/en/a_abbaye/a0_frameset.htm&quot;&gt;Chimay&lt;/a&gt;) and have some fun&amp;#8230;&lt;/p&gt;
&lt;p&gt;Why the rush? Well, I have simply been travelling way too much lately (JavaOne Japan &amp;#8211; 1 week, Terracotta in SF &amp;#8211; 1 week and The Spring Experience in Miami &amp;#8211; 1 week) and my 2 year old son is starting to be disappointed in me&amp;#8230;&lt;/p&gt;</content>
 </entry>
 
 <entry>
   <title>Stay Out of Jar Hell with Jar Jar Links</title>
   <link href="http://jboner.github.com/2005/12/09/stay-out-of-jar-hell-with-jar-jar-links"/>
   <updated>2005-12-09T00:00:00+00:00</updated>
   <id>hhttp://jboner.github.com/2005/12/09/stay-out-of-jar-hell-with-jar-jar-links</id>
   <content type="html">&lt;h1&gt;
&lt;p&gt;Stay Out of Jar Hell with Jar Jar Links&lt;/p&gt;
&lt;/h1&gt;
&lt;p&gt;
&lt;p&gt;&lt;a href=&quot;http://tonicsystems.com/products/jarjar/&quot;&gt;Jar Jar Links&lt;/a&gt; have been part of my Java toolkit ever since we started using it in &lt;a href=&quot;http://aspectwerkz.codehaus.org/&quot;&gt;AspectWerkz&lt;/a&gt; a couple of years ago. It is one of these little gems that deserves more attention, and is your best way out of &lt;em&gt;Jar Hell&amp;#8482;&lt;/em&gt;.&lt;/p&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;p&gt;Basically it is regular &lt;a href=&quot;http://ant.apache.org/&quot;&gt;Ant&amp;#8217;s Jar task&lt;/a&gt; on steriods, with a lot of small useful features. I will list some of them here.&lt;/p&gt;
&lt;/p&gt;
&lt;h3&gt;Packaging&lt;/h3&gt;
&lt;p&gt;
&lt;p&gt;It works just like the regular Ant Jar task, e.g. you can embed other jars using the &lt;code&gt;zipfileset&lt;/code&gt; element, so start using it is a simple matter. Just replace:&lt;/p&gt;
&lt;/p&gt;
&lt;p&gt;&lt;code lang=&quot;xml&quot;&gt;
&amp;lt;target name=&quot;jar&quot; depends=&quot;compile&quot;&amp;gt;
    &amp;lt;jar jarfile=&quot;dist/example.jar&quot;&amp;gt;
        &amp;lt;fileset dir=&quot;build/main&quot;/&amp;gt;&lt;/p&gt;
&lt;/jar&gt;
&lt;/target&gt;
&lt;p&gt;&lt;/code&gt;&lt;br /&gt;
with:&lt;br /&gt;
&lt;code lang=&quot;xml&quot;&gt;
&amp;lt;target name=&quot;jar&quot; depends=&quot;compile&quot;&amp;gt;
    &amp;lt;taskdef name=&quot;jarjar&quot; 
       classname=&quot;com.tonicsystems.jarjar.JarJarTask&quot; 
       classpath=&quot;lib/jarjar.jar&quot;/&amp;gt;
    &amp;lt;jarjar jarfile=&quot;dist/example.jar&quot;&amp;gt;
        &amp;lt;fileset dir=&quot;build/main&quot;/&amp;gt;&lt;/p&gt;
&lt;/jarjar&gt;
&lt;/target&gt;
&lt;p&gt;&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;
&lt;p&gt;But this did not add much value, where it really starts to shine when you start using its feature of prefixing the dependencies.&lt;/p&gt;
&lt;/p&gt;
&lt;h3&gt;Prefixing of dependencies&lt;/h3&gt;
&lt;p&gt;
&lt;p&gt;This is the heart of Jar Jar&amp;#8217;s value proposition. It allows you to prefix dependencies (the actual package names) that are common and might be used by other libraries. For example in AspectWerkz we prefix the &lt;span class=&quot;caps&quot;&gt;ASM&lt;/span&gt; library since it is very common and used in f.e. &lt;span class=&quot;caps&quot;&gt;CGLIB&lt;/span&gt;, which can create runtime clashes.&lt;/p&gt;
&lt;p&gt;&lt;code lang=&quot;xml&quot;&gt;
...
&amp;lt;!--    define the jarjar task we use to remap ASM --&amp;gt;
&amp;lt;taskdef name=&quot;jarjar&quot; 
  classname=&quot;com.tonicsystems.jarjar.JarJarTask&quot; 
  classpath=&quot;${basedir}/lib/jarjar-0.3.jar&quot;/&amp;gt;
     &amp;lt;!-- we embed jarjar version of ASM in it --&amp;gt;
     &amp;lt;jarjar destfile=&quot;${build.dir}/aw-${version}.jar&quot; 
       manifest=&quot;${lib.dir}/manifest.mf&quot;&amp;gt;
    &amp;lt;fileset dir=&quot;${main.classes}&quot;&amp;gt;
        &amp;lt;exclude name=&quot;**/aspectwerkz/hook/**/*&quot;/&amp;gt;&lt;/p&gt;
&lt;/fileset&gt;
&lt;zipfileset src=&quot;${basedir}/lib/asm-2.1.jar&quot;/&gt;
&lt;rule pattern=&quot;org.objectweb.asm.**&quot; 
       result=&quot;org.codehaus.aspectwerkz.@0&quot;/&gt;
&lt;/jarjar&gt;
&lt;p&gt;&amp;#8230;&lt;br /&gt;
&lt;/code&gt;&lt;/p&gt;
&lt;h3&gt;Remove unwanted dependencies&lt;/h3&gt;
&lt;p&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;
Some days ago I &lt;a href=&quot;http://jonasboner.com/?p=70&quot;&gt;blogged about the problem with Commons Logging&lt;/a&gt; and when software depends on it. Well, using Jar Jar, it is actually a simple matter to remove any unwanted dependency altogether. It might not always be the best solution, f.e. you might actually want to have some logging in place, not just use Commons Logging. But is some cases it is very convenient. (Please note that this feature is still experimental.)&lt;/p&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;p&gt;&lt;a href=&quot;http://sixlegs.com/blog&quot;&gt;Chris Nokleberg&lt;/a&gt; has written more about how to use Jar Jar as a &lt;a href=&quot;http://sixlegs.com/blog/java/dependency-killer.html&quot;&gt;Dependency Killer&lt;/a&gt;.&lt;/p&gt;
&lt;/p&gt;
&lt;h3&gt;Analysis&lt;/h3&gt;
&lt;p&gt;
&lt;p&gt;You can also do some simple, but useful, analysis using Jar Jar. For example finding out which dependencies a library actually has, by either just listing them or use it with graphviz to get a nice graphical view:&lt;/p&gt;
&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;http://sixlegs.com/misc/depfind.png&quot; alt=&quot;Graph of dependencies for a library&quot; /&gt;&lt;/p&gt;
&lt;p&gt;
&lt;p&gt;It can also find and print out all the strings you are using, good if you want to try to eliminate all your &lt;a href=&quot;http://c2.com/cgi/wiki?MagicNumber&quot;&gt;magic values&lt;/a&gt;.&lt;/p&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;p&gt;Read more about these features in the articles &lt;a href=&quot;http://sixlegs.com/blog/java/depfind.html&quot;&gt;Finding dependencies with JarJar&lt;/a&gt; and &lt;a href=&quot;http://sixlegs.com/blog/java/string-dumper.html&quot;&gt;Dumping strings literals&lt;/a&gt;.&lt;/p&gt;
&lt;/p&gt;</content>
 </entry>
 
 <entry>
   <title>The "New Language" Is Already Installed On Your Workstation</title>
   <link href="http://jboner.github.com/2005/12/06/the-new-language-is-already-installed-on-your-workstation"/>
   <updated>2005-12-06T00:00:00+00:00</updated>
   <id>hhttp://jboner.github.com/2005/12/06/the-new-language-is-already-installed-on-your-workstation</id>
   <content type="html">&lt;h1&gt;
&lt;p&gt;The &amp;#8220;New Language&amp;#8221; Is Already Installed On Your Workstation&lt;/p&gt;
&lt;/h1&gt;
&lt;p&gt;Victoria Livshitz is &lt;a href=&quot;http://www.theserverside.com/news/thread.tss?thread_id=37922&quot;&gt;&amp;#8220;Envisioning a New Language&amp;#8221;&lt;/a&gt;, it is an interesting read and I find this quote particulary interesting:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;Give the local virtual machine (&lt;span class=&quot;caps&quot;&gt;LVM&lt;/span&gt;) the ability to act as a container for managed application code. Create a virtual machine and distributed runtime environment that in principle subsume the functionality of the application containers and middleware. While retaining middleware from an interoperability standpoint, the basic needs of distributed applications, such as load balancing, failover, remote communications, mobility of services and their containers, quality of service, and service-level management, should be embedded into a runtime environment for the language&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;This is actually what we are doing at &lt;a href=&quot;http://www.terracottatech.com/&quot;&gt;Terracotta&lt;/a&gt;  in our &lt;a href=&quot;http://www.terracottatech.com/product/dso.html&quot;&gt;Distributed Shared Objects (&lt;span class=&quot;caps&quot;&gt;DSO&lt;/span&gt;)&lt;/a&gt; technology.&lt;/p&gt;
&lt;p&gt;But instead of inventing a new languague (or introducing new APIs), we are using the APIs and bytecode instructions that we already have in Java and instead extending their semantics, in order to have Plain Java code, written for a single VM, work correctly (and scale) in a distributed environment.&lt;/p&gt;
&lt;p&gt;So I would say that the &amp;#8220;New Language&amp;#8221; is already installed on your workstation&amp;#8230;and it is called Java, it just needs to be boosted up some&amp;#8230; ;)&lt;/p&gt;</content>
 </entry>
 
 <entry>
   <title>Transparently Clustered Spring @ The Spring Experience</title>
   <link href="http://jboner.github.com/2005/12/05/transparently-clustered-spring-the-spring-experience"/>
   <updated>2005-12-05T00:00:00+00:00</updated>
   <id>hhttp://jboner.github.com/2005/12/05/transparently-clustered-spring-the-spring-experience</id>
   <content type="html">&lt;h1&gt;
&lt;p&gt;Transparently Clustered Spring @ The Spring Experience&lt;/p&gt;
&lt;/h1&gt;
&lt;p&gt;I am heading down to Miami and &lt;a href=&quot;http://thespringexperience.com&quot;&gt;The Spring Experience&lt;/a&gt; conference to do a talk on &lt;a href=&quot;http://thespringexperience.com/speaker_topic_view.jsp?topicId=206&quot;&gt;Transparently Clustered Spring&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;Basically it is talk about how you can use &lt;a href=&quot;https://www.terracottatech.com&quot;&gt;Terracotta &lt;/a&gt;&amp;#8217;s &lt;span class=&quot;caps&quot;&gt;DSO&lt;/span&gt; technology to cluster your regular Spring applications &lt;strong&gt;Transparently&lt;/strong&gt;, in order to get, better scalability, HA (High Availability), Fail-Over etc.&lt;/p&gt;
&lt;p&gt;The talk will be demo-driven (5 demos) showing among other things:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;how to make Spring Bean&amp;#8217;s lifecycles and state mean the same thing on a cluster as on a single node (e.g. clustered, but local to the same ApplicationContext)&lt;/li&gt;
&lt;li&gt;how to turn Spring&amp;#8217;s ApplicationContext Events into distributed events (but still local within the same ApplicationContext)&lt;/li&gt;
&lt;li&gt;how to avoid using regular messaging and simplify the programming model by:&lt;br /&gt;
&lt;ul&gt;&lt;br /&gt;
&lt;li&gt;turning arbitrary method invocations into asynchronous distributed events&lt;/li&gt;
&lt;li&gt;use a regular java.util.List or java.util.Queue as a message queue (point-to-point or publish-subscribe)&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;and more&amp;#8230;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;I hope to see you there.  :)&lt;/p&gt;</content>
 </entry>
 
 <entry>
   <title>Commons Logging Has To Die</title>
   <link href="http://jboner.github.com/2005/11/24/commons-logging-has-to-die"/>
   <updated>2005-11-24T00:00:00+00:00</updated>
   <id>hhttp://jboner.github.com/2005/11/24/commons-logging-has-to-die</id>
   <content type="html">&lt;h1&gt;
&lt;p&gt;Commons Logging Has To Die&lt;/p&gt;
&lt;/h1&gt;
&lt;p&gt;Unfortunately, I guess it is to late to kill the beast&amp;#8230;&lt;/p&gt;
&lt;p&gt;But I am writing this post anyway, since it &lt;a href=&quot;http://briankuhn.com/?p=33&quot;&gt;keeps on causing harm&lt;/a&gt; and since more enlightment around the actual problems it causes obviously is necessary.&lt;/p&gt;
&lt;p&gt;My first contact with &lt;a href=&quot;http://jakarta.apache.org/commons/logging/&quot;&gt;Commons Logging&lt;/a&gt; was in 2002, where it made the application is was currently working on blow up. After tracking down the problem, I solved it using &lt;a href=&quot;http://eclipse.org/aspectj/&quot;&gt;AspectJ&lt;/a&gt; by hijacking Commons Logging and swap out the dynamic discovery mechanism to a static one (using &lt;a href=&quot;http://logging.apache.org/log4j/docs/&quot;&gt;Log4J&lt;/a&gt; directly), and have been fighting for a &lt;em&gt;Commons-Logging-Free-World&amp;#8482;&lt;/em&gt; ever since.&lt;/p&gt;
&lt;p&gt;If you want to know more about the actual problems it causes and why using class loader hacks to implement a dynamic discovery process is generally a bad idea, read the article &lt;a href=&quot;http://www.qos.ch/logging/classloader.jsp&quot;&gt;Taxonomy of class loader problems encountered when using Jakarta Commons Logging&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;I do understand why Commons Logging was invented, it solves a theoretical problem (I am using the word &amp;#8216;theoretical&amp;#8217; here, since almost everyone that uses it, uses Log4J as the logging implementation anyway), it is just unfortunate that it became so popular so fast.&lt;/p&gt;
&lt;p&gt;However, there are alternatives. If you really need to use a Logging facade (which I do not think you do), you should try out &lt;a href=&quot;http://www.slf4j.org/&quot;&gt;Simple Logging Facade for Java (SLF4J)&lt;/a&gt;, which uses static (compile time) binding of the Logging implementation, e.g. avoids the above stated problems.  They even have a &lt;a href=&quot;http://www.slf4j.org/manual.html#gradual&quot;&gt;gradual migration path&lt;/a&gt; from Commons Logging.&lt;/p&gt;
&lt;p&gt;Sorry if I sound bitter, but I just had to get it out&amp;#8230; :-)&lt;/p&gt;
&lt;hr /&gt;
&lt;p&gt;Update:&lt;/p&gt;
&lt;p&gt;I just remembered that &lt;a href=&quot;http://sixlegs.com/blog&quot;&gt;Chris Nokleberg&lt;/a&gt; has a implemented a &lt;a href=&quot;http://sixlegs.com/blog/java/dependency-killer.html&quot;&gt;fancy way&lt;/a&gt; of getting rid of Commons Logging (and any other annoying dependency for that matter), it is part of his excellent package &lt;a href=&quot;http://tonicsystems.com/products/jarjar/&quot;&gt;JarJar&lt;/a&gt;.&lt;/p&gt;</content>
 </entry>
 
 <entry>
   <title>Thoughts on Harmony</title>
   <link href="http://jboner.github.com/2005/11/23/thoughts-on-harmony"/>
   <updated>2005-11-23T00:00:00+00:00</updated>
   <id>hhttp://jboner.github.com/2005/11/23/thoughts-on-harmony</id>
   <content type="html">&lt;h1&gt;
&lt;p&gt;Thoughts on Harmony&lt;/p&gt;
&lt;/h1&gt;
&lt;p&gt;Floyd Marinescu &lt;a href=&quot;http://dynamicsemantics.blog-city.com/harmonyintelcontribution.htm&quot;&gt;recently blogged&lt;/a&gt; about that the latest contribution from Intel was evidence that God (&lt;span class=&quot;caps&quot;&gt;IBM&lt;/span&gt;?) has big plans for &lt;a href=&quot;http://incubator.apache.org/harmony/&quot;&gt;Harmony&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;I wish I could be that optimistic, and I perhaps should, since Floyd usually does not say things that are not well backed up with facts.&lt;/p&gt;
&lt;p&gt;I really like the idea of an open source &lt;span class=&quot;caps&quot;&gt;JVM&lt;/span&gt;, where companies could contribute with the parts that they can do best (native ports etc.). But just some class library is basically nothing (we already have &lt;a href=&quot;http://www.gnu.org/software/classpath/&quot;&gt;&lt;span class=&quot;caps&quot;&gt;GNU&lt;/span&gt; Classpath&lt;/a&gt;, and who is using that for real&amp;#8230;?).&lt;/p&gt;
&lt;p&gt;What is necessary, and basically the only thing that could make the Harmony project fly is for either &lt;span class=&quot;caps&quot;&gt;BEA&lt;/span&gt; to donate &lt;a href=&quot;http://www.bea.com/content/products/jrockit/index.htm&quot;&gt;JRockit&lt;/a&gt; or &lt;span class=&quot;caps&quot;&gt;IBM&lt;/span&gt; to donate &lt;a href=&quot;http://www-306.ibm.com/software/wireless/wctme/&quot;&gt;J9&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;Why?&lt;/p&gt;
&lt;p&gt;First, it is practically impossible for a distributed non-full-time team to develop an enterprise class &lt;span class=&quot;caps&quot;&gt;JVM&lt;/span&gt; within this century.&lt;/p&gt;
&lt;p&gt;Second, these JVMs are the two single clean room implementations out there (e.g. that are not in any way derived from Sun HotSpot).&lt;/p&gt;
&lt;p&gt;Geir, do you have any cards left to play? ;-)&lt;/p&gt;</content>
 </entry>
 
 <entry>
   <title>I Have Found a New Place in This World</title>
   <link href="http://jboner.github.com/2005/11/22/i-have-found-a-new-place-in-this-world"/>
   <updated>2005-11-22T00:00:00+00:00</updated>
   <id>hhttp://jboner.github.com/2005/11/22/i-have-found-a-new-place-in-this-world</id>
   <content type="html">&lt;h1&gt;
&lt;p&gt;I Have Found a New Place in This World&lt;/p&gt;
&lt;/h1&gt;
&lt;p&gt;I have found a new place in this world.&lt;/p&gt;
&lt;p&gt;I just posted my &lt;a href=&quot;http://blogs.codehaus.org/people/jboner/archives/001245_moving_my_blog.html&quot;&gt;last post&lt;/a&gt; at my &lt;a href=&quot;http://blogs.codehaus.org/people/jboner/&quot;&gt;old blog&lt;/a&gt; at &lt;a href=&quot;http://codehaus.org&quot;&gt;The Codehaus&lt;/a&gt;:&lt;br /&gt;
&lt;blockquote&gt;I decided to move my blog to &lt;a href=&quot;http://jonasboner.com/&quot;&gt;jonasboner.com&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;&lt;a href=&quot;http://blogs.codehaus.org/&quot;&gt;blogs.codehaus.org&lt;/a&gt; has served me well so far, so &lt;span class=&quot;caps&quot;&gt;THANKS&lt;/span&gt;, but was a bit too unflexible for my needs and it felt it was time to move on.&lt;/p&gt;
&lt;p&gt;At the same time I decided to start using &lt;a href=&quot;http://www.feedburner.com&quot;&gt;Feedburner&lt;/a&gt;, so now you can find my feed &lt;a href=&quot;http://feeds.feedburner.com/grubbel&quot;&gt;here and only here&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;So this is the &lt;strong&gt;last post&lt;/strong&gt; at &lt;a href=&quot;http://blogs.codehaus.org/people/jboner/&quot;&gt;http://blogs.codehaus.org/people/jboner/&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;Hope to see you &lt;a href=&quot;http://jonasboner.com/&quot;&gt;here&lt;/a&gt; instead.&lt;/blockquote&gt;&lt;br /&gt;
So, welcome to my new weblog, I promise to try to post more frequently from now on.&lt;br /&gt;
Stay tuned.&lt;/p&gt;</content>
 </entry>
 
 <entry>
   <title>Learning From The Web</title>
   <link href="http://jboner.github.com/2005/11/02/about-time-for-database-vendors-to-learn-from-the-web"/>
   <updated>2005-11-02T00:00:00+00:00</updated>
   <id>hhttp://jboner.github.com/2005/11/02/about-time-for-database-vendors-to-learn-from-the-web</id>
   <content type="html">&lt;h1&gt;
&lt;p&gt;Learning From The Web&lt;/p&gt;
&lt;/h1&gt;
&lt;p&gt;Adam Bosworth has written an &lt;a href=&quot;http://acmqueue.com/modules.php?name=Content&amp;pa=showpage&amp;pid=337&quot;&gt;interesting article&lt;/a&gt; about what we can (or have to) learn from The Web.&lt;/p&gt;
&lt;p&gt;He is talking about how it has teached us how to manage and distribute data and what it means for the Semantic Web and for the databases, and that standards such as Atom and &lt;span class=&quot;caps&quot;&gt;RSS&lt;/span&gt; 2.0 will become increasingly important in the future.&lt;/p&gt;
&lt;p&gt;Here is his conclusion (although I strongly recommend you to read the whole article):&lt;br /&gt;
&lt;blockquote&gt;&lt;br /&gt;
 &lt;span class=&quot;caps&quot;&gt;TIME&lt;/span&gt; &lt;span class=&quot;caps&quot;&gt;FOR&lt;/span&gt; &lt;span class=&quot;caps&quot;&gt;DATABASE&lt;/span&gt; &lt;span class=&quot;caps&quot;&gt;VENDORS&lt;/span&gt; TO &lt;span class=&quot;caps&quot;&gt;STEP&lt;/span&gt; UP TO &lt;span class=&quot;caps&quot;&gt;THE&lt;/span&gt; &lt;span class=&quot;caps&quot;&gt;PLATE&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;Distributed computing has been learning and evolving in response to the lessons of the Web. Formats and protocols are arising to overcome the limitations of XMLâ€”even as &lt;span class=&quot;caps&quot;&gt;XML&lt;/span&gt; in turn arose to overcome the limitations of &lt;span class=&quot;caps&quot;&gt;CORBA&lt;/span&gt; and &lt;span class=&quot;caps&quot;&gt;DCOM&lt;/span&gt;. It is time that the database vendors stepped up to the plate and started to support a native &lt;span class=&quot;caps&quot;&gt;RSS&lt;/span&gt; 2.0/Atom protocol and wire format; a simple way to ask very general queries; a way to model data that encompasses trees and arbitrary graphs in ways that humans think about them; far more fluid schemas that donâ€™t require complex joins to model variations on a theme about anything from products to people to places; and built-in linear scaling so that the database salespeople can tell their customers, in good conscience, for this class of queries you can scale arbitrarily with regard to throughput and extremely well even with regard to latency, as long as you limit yourself to the following types of queries. Then we will know that the database vendors have joined the 21st century.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;The Web is certainly changing the way we are viewing the world in many ways&amp;#8230;&lt;/p&gt;</content>
 </entry>
 
 <entry>
   <title>HotSwap Is A Dead End Road</title>
   <link href="http://jboner.github.com/2005/11/01/improving-hotswap-is-a-dead-end-road"/>
   <updated>2005-11-01T00:00:00+00:00</updated>
   <id>hhttp://jboner.github.com/2005/11/01/improving-hotswap-is-a-dead-end-road</id>
   <content type="html">&lt;h1&gt;
&lt;p&gt;HotSwap Is A Dead End Road&lt;/p&gt;
&lt;/h1&gt;
&lt;p&gt;There has been a lot of discussions about the need for more dynamicity in Java, including the need to support scripting languages.&lt;/p&gt;
&lt;p&gt;Most are around the suggestion of improving the HotSwap facility in Java.&lt;/p&gt;
&lt;p&gt;For exampe:&lt;/p&gt;
&lt;ul&gt;
	&lt;li&gt;&lt;a href=&quot;http://rifers.org/blogs/gbevin/2005/10/31/hotswap_improvement&quot;&gt;How badly do you want Hotswap to improve?&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;ul&gt;
	&lt;li&gt;&lt;a href=&quot;http://blogs.opensymphony.com/plightbo/2005/10/please_vote_the_only_jvm_bug_y.html&quot;&gt;&lt;span class=&quot;caps&quot;&gt;PLEASE&lt;/span&gt; &lt;span class=&quot;caps&quot;&gt;VOTE&lt;/span&gt;: the only &lt;span class=&quot;caps&quot;&gt;JVM&lt;/span&gt; bug you&amp;#8217;ll ever really care about&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;ul&gt;
	&lt;li&gt;&lt;a href=&quot;http://blogs.opensymphony.com/plightbo/2005/10/gaining_traction_hotswap_impro.html&quot;&gt;Gaining traction: HotSwap improvements&lt;/a&gt;,&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;etc.&lt;/p&gt;
&lt;p&gt;I have to admit that a year ago I was probably one of the guys that were swearing the most over HotSwap&amp;#8217;s (and &lt;acronym title=&quot;D/T&quot;&gt;&lt;span class=&quot;caps&quot;&gt;JVM&lt;/span&gt;&lt;/acronym&gt;I&amp;#8217;s shortcomings when trying to stretch its (limited) boundries in AspectWerkz.&lt;/p&gt;
&lt;p&gt;However, today I think that improving HotSwap and the APIs for bytecode instrumentation in general is a dead end road. It simply adds complexiety, memory overhead, performance overhead as well as introduces the multiple agents problem.&lt;/p&gt;
&lt;p&gt;We need to raise the abstraction level and move away from bytecode instrumentation. There is a need for high level VM APIs similar to the one we have been working on in JRockit. For details on the problems with current approaches and how we solve this in a better way, read &lt;a href=&quot;http://dev2dev.bea.com/pub/a/2005/08/jvm_aop_1.html&quot;&gt;this article&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;So, &lt;span class=&quot;caps&quot;&gt;PLEASE&lt;/span&gt;, stop wasting time in discussing how to improve bytecode instrumentation (e.g. HotSwap, &lt;span class=&quot;caps&quot;&gt;JVMTI&lt;/span&gt; etc.) and give us feedback on how to improve the high-level VM &lt;span class=&quot;caps&quot;&gt;API&lt;/span&gt; in JRockit (the prototype is &lt;a href=&quot;http://blogs.codehaus.org/people/avasseur/archives/001198_jrockit_powered_aop_prototype_available.html&quot;&gt;already available&lt;/a&gt;).&lt;/p&gt;
&lt;p&gt;When on the subject on supporting scripting languages in Java is think that &lt;a href=&quot;http://blogs.sun.com/roller/page/gbracha?entry=invokedynamic&quot;&gt;this proposal&lt;/a&gt; for adding a &lt;tt&gt;invokedynamic&lt;/tt&gt; bytecode instruction is a step in the right direction.&lt;/p&gt;</content>
 </entry>
 
 <entry>
   <title>Introducing HyperBeans Ã¢â¬â multi-dimensional POJOs</title>
   <link href="http://jboner.github.com/2005/10/25/introducing-hyperbeans-%25e2%2580%2593-multi-dimensional-pojos"/>
   <updated>2005-10-25T00:00:00+00:00</updated>
   <id>hhttp://jboner.github.com/2005/10/25/introducing-hyperbeans-%e2%80%93-multi-dimensional-pojos</id>
   <content type="html">&lt;h1&gt;
&lt;p&gt;Introducing HyperBeans â€“ multi-dimensional POJOs&lt;/p&gt;
&lt;/h1&gt;
&lt;p&gt;
&lt;p&gt;&lt;br /&gt;
&lt;h2&gt; Introduction &lt;/h2&gt;&lt;/p&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;p&gt;This article is introducing &lt;i&gt;HyperBeans&lt;/i&gt;, a proposal for a framework for &lt;a href=&quot;http://domino.watson.ibm.com/library/cyberdig.nsf/0/2a4097e93456d0cf85256ca9006dac29?OpenDocument&quot;&gt;Symmetric Aspect-Oriented Software Development&lt;/a&gt;  in &lt;b&gt;plain&lt;/b&gt; Java (i.e. no extensions to Java is being used). These ideas are based on the work I have done in &lt;i&gt;&lt;a href=&quot;http://aspectwerkz.codehaus.org/&quot;&gt;AspectWerkz&lt;/a&gt;&lt;/i&gt;  and in the &lt;a href=&quot;http://dev2dev.bea.com/pub/a/2005/08/jvm_aop_2.html&quot;&gt;&lt;span class=&quot;caps&quot;&gt;JVM&lt;/span&gt; support for &lt;span class=&quot;caps&quot;&gt;AOP&lt;/span&gt;&lt;/a&gt;  in &lt;i&gt;JRockit&lt;/i&gt; and are loosely based on the work on &lt;a href=&quot;http://www.cs.virginia.edu/~eos/papers/cs_2004_21.pdf&quot;&gt;&lt;i&gt;Classpects&lt;/i&gt;&lt;/a&gt;  by Kevin Sullivan and Hridesh Rajan.&lt;/p&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;p&gt;If you don&amp;#8217;t care about the research and background behind the ideas presented in this article (and want to see some code right away), then you should skip the rest of this section.&lt;/p&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;p&gt;Aspect-Oriented Software Development (&lt;span class=&quot;caps&quot;&gt;AOSD&lt;/span&gt;) in general, tries, among other things, to address the problem defined as &lt;a href=&quot;http://www.research.ibm.com/hyperspace/Papers/tr21452.ps&quot;&gt;the tyranny of dominant decomposition&lt;/a&gt; , in which the prominent concerns, usually the business logic, dominate the class design, especially the inheritance hierarchy (as observed in &lt;a href=&quot;http://www-128.ibm.com/developerworks/java/library/j-aopwork4/&quot;&gt;this article&lt;/a&gt;).&lt;/p&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;p&gt;Historically there has been (and still is) two different main philosophies in &lt;span class=&quot;caps&quot;&gt;AOSD&lt;/span&gt;, the asymmetric philosophy and the symmetric philosophy.&lt;br /&gt;
A detailed discussion about their semantic and philosophical differences is out of the scope for this article (read &lt;a href=&quot;http://domino.watson.ibm.com/library/cyberdig.nsf/0/2a4097e93456d0cf85256ca9006dac29?OpenDocument&quot;&gt;this article&lt;/a&gt;  for a detailed discussion). &lt;/p&gt;&lt;/p&gt;
&lt;p&gt;
&lt;p&gt;Juri Memmert has written a good &lt;a href=&quot;http://www.jpmdesign.net/wordpress/2005/08/04/symmetry-vs-asymmetry/&quot;&gt;introductory article&lt;/a&gt; on the subject in which he defines the different philosophies like this:&lt;/p&gt;
&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;b&gt;The asymmetric philosophy&lt;/b&gt;&lt;/p&gt;
&lt;p&gt;An asymmetric approach to &lt;span class=&quot;caps&quot;&gt;AOSD&lt;/span&gt; relies on the notion that there is a base body of code that is then augmented with aspects. There is a concept ional difference (and an implementation difference) between the base and the aspects so that an aspect can not serve as the base of another composition. Most asymmetric approaches use language extensions to declare aspects as first class entities.&lt;/p&gt;
&lt;p&gt;&lt;b&gt;The symmetric philosophy&lt;/b&gt;&lt;/p&gt;
&lt;p&gt;A symmetric approach to &lt;span class=&quot;caps&quot;&gt;AOSD&lt;/span&gt; is based on the notion that all concerns in a system are created equal and, in the asymmetric terminology, can serve as both aspect and base in different compositions. Most symmetric approaches use programming languages as-is, although there are approaches that rely on language extensions as well.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;
&lt;p&gt;The leading implementation for asymmetric &lt;span class=&quot;caps&quot;&gt;AOSD&lt;/span&gt; is &lt;a href=&quot;http://www.eclipse.org/aspectj/&quot;&gt;&lt;i&gt;AspectJ&lt;/i&gt;&lt;/a&gt;, while the most well-known symmetric implementation is &lt;a href=&quot;http://www.research.ibm.com/hyperspace/HyperJ/HyperJ.htm&quot;&gt;&lt;i&gt;Hyper/J&lt;/i&gt;&lt;/a&gt; (now part of the &lt;i&gt;Concern Manipulation Environment&lt;/i&gt; (&lt;span class=&quot;caps&quot;&gt;CME&lt;/span&gt;) project).&lt;/p&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;p&gt;Asymmetric &lt;span class=&quot;caps&quot;&gt;AOSD&lt;/span&gt; has so far been the dominant way of implementing &lt;span class=&quot;caps&quot;&gt;AOSD&lt;/span&gt; in Java, and in the enterprise space at large. There has been, and still is, a lot of debate in the research community around whether the symmetric or the asymmetric philosophy is the best way of implementing &lt;span class=&quot;caps&quot;&gt;AOSD&lt;/span&gt; and many papers have been written on the subject (see the reference section at the end).&lt;/p&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;p&gt;When implementing the &lt;i&gt;AspectWerkz&lt;/i&gt; &lt;span class=&quot;caps&quot;&gt;AOP&lt;/span&gt; framework  (which now has been merged with &lt;i&gt;AspectJ&lt;/i&gt;), I have been involved in efforts trying to stretch the boundaries of asymmetric &lt;span class=&quot;caps&quot;&gt;AOSD&lt;/span&gt; and even though I believe that the asymmetric approach is the preferable in many cases, it definitely has some shortcomings. For example, asymmetric &lt;span class=&quot;caps&quot;&gt;AOSD&lt;/span&gt; addresses the problem of &amp;#8216;the tyranny of dominant decomposition&amp;#8217; by allowing us to modularize all concerns, but the dominant concern, using aspects. Since the dominant concern needs to serve as &amp;#8220;base&amp;#8221;, the decision deciding which concern should be seen as the dominant one still needs to be taken, and this is a decision that should not be taken lightly since it is something that is usually very hard to change.&lt;/p&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;p&gt;When taking a look at how a symmetric approach handles the above given problem, I find the symmetric solution very natural and appealing (at least conceptually). In which, if using &lt;i&gt;Hyper/J&lt;/i&gt;&amp;#8217;s terminology, a system is seen as a multi-dimensional hyperspace, which is built up using different hyperslices (dimensions). A hyperslice is the abstraction of a concern, which can be built up by many components.  This gives you the possibility of viewing the hyperspace (your system) differently in regards to different hyperslices (concerns); you have the possibility of looking at the system from the point of view (concern) that you currently want.&lt;/p&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;p&gt;I do not want to take any position in regards to if symmetric &lt;span class=&quot;caps&quot;&gt;AOSD&lt;/span&gt; is a (or will be a) serious contender to asymmetric &lt;span class=&quot;caps&quot;&gt;AOSD&lt;/span&gt;. I simply have not used the symmetric approach to solve real world problems enough yet, but I do believe that it is something worth exploring more. So, let&amp;#8217;s take a look at the proposal.&lt;/p&gt;
&lt;/p&gt;
&lt;h2&gt; HyperBeans â€“ multi-dimensional POJOs &lt;/h2&gt;
&lt;p&gt;
&lt;p&gt;There is no the distinction between classes and aspects, e.g. no need for one concern to serve as &amp;#8220;base&amp;#8221;, all there is is &lt;i&gt;HyperBeans&lt;/i&gt;. &lt;i&gt;HyperBeans&lt;/i&gt; are regular Plain Old Java Objects (POJOs), they are instantiated with &lt;tt&gt;new&lt;/tt&gt; and you work with them just as with regular classes, no configuration file or similar is needed.&lt;/p&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;p&gt;Similarly, there is no the distinction between methods and &lt;i&gt;advice&lt;/i&gt;, there are just regular methods, which can be invoked explicitly (like regular methods) or implicitly (when a join point is being executed &amp;#8211; like a regular &lt;i&gt;advice&lt;/i&gt;).&lt;/p&gt;
&lt;/p&gt;
&lt;h2&gt; Deployment and undeployment &lt;/h2&gt;
&lt;p&gt;
&lt;p&gt;&lt;i&gt;HyperBeans&lt;/i&gt; are instantiated using the &lt;tt&gt;new&lt;/tt&gt; keyword, which will call the constructor and instantiate the HyperBean just like any other &lt;span class=&quot;caps&quot;&gt;POJO&lt;/span&gt;. However, the constructors can also control deployment of the HyperBean.&lt;/p&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;p&gt;Deployment of a HyperBean means that its &amp;#8220;blessed&amp;#8221; methods (&lt;i&gt;advice&lt;/i&gt;) are weaved in, while undeployment means that the woven code is removed.&lt;/p&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;p&gt;You control deployment of a HyperBean by annotating (one or many of) its constructors with the &lt;tt&gt;@Deploy&lt;/tt&gt; annotation. This example shows how to deploy a HyperBean as a singleton (more on other instantiation models later):&lt;/p&gt;
&lt;/p&gt;
&lt;pre&gt;
public class POJO {

    @Deploy
    public POJO() {    	
    }

    ... // remaining methods omitted 
}
&lt;/pre&gt;
&lt;p&gt;
&lt;p&gt;Since there is no such thing as &amp;#8216;destructors&amp;#8217; in Java, we are handling undeployment of HyperBeans by annotating a regular Java method (static or member) with the &lt;tt&gt;@Undeploy&lt;/tt&gt; annotation. This means that when such a method is invoked, the HyperBean will be undeployed. It also give us the possibility of doing additional clean up (closing resources etc.) if necessary:&lt;/p&gt;
&lt;/p&gt;
&lt;pre&gt;
public class POJO {

    @Undeploy
    public void close() {
        // clean up - close resources etc.
    }

    ... // remaining methods omitted 
}
&lt;/pre&gt;
&lt;h2&gt; Instantiation models &lt;/h2&gt;
&lt;p&gt;
&lt;p&gt;You can control instantiation model (also called the deployment scope) of the HyperBean by adding a special argument to the constructor. This special argument defines the deployment scope for the HyperBean and needs to be annotated with the &lt;tt&gt;@Scope&lt;/tt&gt; annotation. What this means is that the HyperBean will be deployed with the scope defined by the type of the argument.&lt;/p&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;p&gt;For example, adding the argument &lt;tt&gt;&lt;code&gt;Scope Thread scope&amp;lt;/tt&amp;gt; to a constructor, (which is annotated with the &amp;lt;tt&amp;gt;&lt;/code&gt;Deploy&lt;/tt&gt; annotation), will deploy the HyperBean with the scope of this particular thread.&lt;/p&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;p&gt;Here are all the currently supported instantiation models:&lt;/p&gt;
&lt;table border=&quot;1&quot;&gt;
&lt;tr&gt;
	&lt;th&gt;Type annotated with @Scope&lt;/th&gt;
	&lt;th&gt;Instantiation model&lt;/th&gt;
&lt;/tr&gt;
&lt;tr&gt;
	&lt;td&gt;&lt;tt&gt;No argument with a @Scope annotation&lt;/tt&gt;&lt;/td&gt;
	&lt;td&gt;Singleton&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
	&lt;td&gt;&lt;tt&gt;Thread&lt;/tt&gt;&lt;/td&gt;
	&lt;td&gt;Per Thread&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
	&lt;td&gt;&lt;tt&gt;Class&lt;/tt&gt;&lt;/td&gt;
	&lt;td&gt;Per Type&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
	&lt;td&gt;&lt;tt&gt;Instance&lt;/tt&gt; (but not of type &lt;tt&gt;Class&lt;/tt&gt; or &lt;tt&gt;Thread&lt;/tt&gt;)&lt;/td&gt;
	&lt;td&gt;Per Instance&lt;/td&gt;
&lt;/tr&gt;
&lt;/table&gt;
&lt;/p&gt;
&lt;p&gt;Here are some examples:&lt;/p&gt;
&lt;pre&gt;
public class POJO {

    /** Singleton deployment */
    @Deploy
    public POJO() {    	
    }

    /** Per Type deployment */
    @Deploy
    public POJO(@Scope Class scope) {    	
    }

    /** Per Instance deployment */
    @Deploy
    public POJO(@Scope SomeType scope) {    	
    }

    /** Per Thread deployment */
    @Deploy
    public POJO(@Scope Thread scope) {    	
    }

    ... // remaining methods omitted 
}
&lt;/pre&gt;
&lt;p&gt;
&lt;p&gt;The current semantics for the &lt;tt&gt;@Scope&lt;/tt&gt; annotation is that it:&lt;br /&gt;
&lt;ul&gt;&lt;br /&gt;
&lt;li&gt;&lt;br /&gt;
Narrows the scope for the matching. For example using the &lt;i&gt;Per Type&lt;/i&gt; instantiation model will add an implicit &lt;tt&gt;&amp;amp;&amp;amp; within(scope)&lt;/tt&gt; to the pointcuts defined in the HyperBean (same as in AspectJ).&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Controls the life cycle. The HyperBean will have the same life cycle as the instance annotated with the &lt;tt&gt;@Scope&lt;/tt&gt; annotation.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Controls state management. Tied to the life cycle.&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/p&gt;
&lt;h2&gt; Control flow management &amp;#8211; no more cflow &lt;/h2&gt;
&lt;p&gt;
&lt;p&gt;There is no more any need for the &lt;i&gt;cflow&lt;/i&gt; pointcut (as defined by &lt;i&gt;AspectJ&lt;/i&gt;). Instead we suggest a simple programmatic model for control flow management of HyperBean deployment. I believe that the same functionality can be reached by a combination of the &lt;i&gt;Per Thread&lt;/i&gt; instantiation model and the deployment/undeployment &lt;span class=&quot;caps&quot;&gt;API&lt;/span&gt;. This approach has similarities with the work done in &lt;i&gt;&lt;a href=&quot;http://caesarj.org/&quot;&gt;CaesarJ&lt;/a&gt;&lt;/i&gt; and its concept of a &lt;tt&gt;deploy {..}&lt;/tt&gt; block. Here is an example:&lt;/p&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;p&gt;You have a HyperBean that can be deployed on a per-thread basis:&lt;/p&gt;
&lt;pre&gt;
class POJO {
    @Deploy
    public POJO(@Scope Thread scope) {    	
    }

    @Undeploy
    public void close() {
    }

    ... // remaining methods omitted 
}
&lt;/pre&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;p&gt;Then you can use the following idiom to achieve fine grained control flow (cflow) management:&lt;/p&gt;
&lt;pre&gt;
POJO hyperBean;
try {
    hyperBean = new POJO(Thread.currentThread()); // deploys the HyperBean to the current control flow ONLY
    
    ... // do stuff - the HyperBean is woven into this control flow ONLY

} finally {
    hyperBean.close(); // undeploy the HyperBean from the control flow
}
&lt;/pre&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;p&gt;If you don&amp;#8217;t want to have this be tangled with your application logic, you can of course put the block in an &lt;i&gt;advice&lt;/i&gt;, (and have its HyperBean be a singleton and instantiated somewhere else):&lt;/p&gt;
&lt;pre&gt;
@Around(..)
public Object cflowAdvice(InvocationContext ctx) {
    POJO hyperBean;
    Object result;
    try {
        hyperBean = new POJO(Thread.currentThread()); 
        result = ctx.proceed();
    } finally {
         hyperBean.close(); 
    }
    return result;
}
&lt;/pre&gt;
&lt;/p&gt;
&lt;h2&gt; Cross-cutting methods &amp;#8211; advice &lt;/h2&gt;
&lt;p&gt;
&lt;p&gt;So how do we define these &amp;#8220;blessed&amp;#8221; methods? They are defined using Java 5 annotations, in a similar fashion to &lt;i&gt;AspectWerkz&lt;/i&gt;&amp;#8216;s and &lt;i&gt;AspectJ 5&lt;/i&gt;&amp;#8217;s annotation style of development, but mixed with the &lt;span class=&quot;caps&quot;&gt;API&lt;/span&gt; defined by the &lt;span class=&quot;caps&quot;&gt;JVM&lt;/span&gt; support for &lt;span class=&quot;caps&quot;&gt;AOP&lt;/span&gt; in &lt;i&gt;JRockit&lt;/i&gt; (as described &lt;a href=&quot;http://dev2dev.bea.com/pub/a/2005/08/jvm_aop_2.html&quot;&gt;in this article&lt;/a&gt; ).&lt;/p&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;p&gt;Let&amp;#8217;s take a look at an example:&lt;/p&gt;
&lt;/p&gt;

&lt;pre&gt;
public class POJO {
 
    @Before(&quot;call(Foo.new(..))&quot;)
    public void doSomething() {
        ... // do something before an instance of Foo is created
    }

    ... // remaining methods omitted 
}
&lt;/pre&gt;
&lt;p&gt;
&lt;p&gt;This is a regular &lt;span class=&quot;caps&quot;&gt;POJO&lt;/span&gt; with one &amp;#8220;blessed&amp;#8221; method that performs some action before an instance of &lt;tt&gt;Foo&lt;/tt&gt; is created.  There&amp;#8217;s really nothing special with this class apart from that one of its method is annotated with a special annotation borrowed from the annotation style syntax in &lt;i&gt;AspectJ&lt;/i&gt;.  Now let&amp;#8217;s take a look at a (slightly) more interesting example, one that makes use of contextual information.&lt;/p&gt;
&lt;/p&gt;
&lt;pre&gt;
public class POJO {
 
    @Before(&quot;call(@test.TraceMe * *.*(..))&quot;)
    public static void traceBefore(@CalleeMethod Method callee) {
        System.out.println(&quot;--&amp;gt; &quot; + callee.toString());
    }
  
    @After(&quot;call(@test.TraceMe * *.*(..))&quot;)
    public static void traceAfter(@CalleeMethod Method callee) {
        System.out.println(&quot;&amp;lt; -- &quot; + callee.toString());
    }

    ... // remaining methods omitted 
}
&lt;/pre&gt;
&lt;p&gt;
&lt;p&gt;This is a regular &lt;span class=&quot;caps&quot;&gt;POJO&lt;/span&gt; with two different &amp;#8220;blessed&amp;#8221; methods (that are tracing &lt;br /&gt;
invocations to all methods that are annotated with the &lt;tt&gt;@Trace&lt;/tt&gt; annotation). Contextual information (like caller and callee method, caller and callee instance, parameters, return value etc.) is retrieved using annotated parameters borrowed from the &lt;i&gt;JRockit&lt;/i&gt; &lt;span class=&quot;caps&quot;&gt;API&lt;/span&gt; for &lt;span class=&quot;caps&quot;&gt;AOP&lt;/span&gt; .&lt;/p&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;p&gt;Here is a list of the current set of defined annotations and their meaning:&lt;/p&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;table border=&quot;1&quot;&gt;
&lt;tr&gt;
	&lt;th&gt;Annotation&lt;/th&gt;
	&lt;th&gt;Exposes&lt;/th&gt;
&lt;/tr&gt;
&lt;tr&gt;
	&lt;td&gt;&lt;tt&gt;@CalleeMethod&lt;/tt&gt;	&lt;/td&gt;
	&lt;td&gt;The callee method (method, constructor, static initializer)	&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
	&lt;td&gt;&lt;tt&gt;@CallerMethod&lt;/tt&gt;	&lt;/td&gt;
	&lt;td&gt;The caller method (method, constructor, static initializer)	&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
	&lt;td&gt;&lt;tt&gt;@Callee&lt;/tt&gt;	&lt;/td&gt;
	&lt;td&gt;The callee instance	&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
	&lt;td&gt;&lt;tt&gt;@Caller&lt;/tt&gt;	&lt;/td&gt;
	&lt;td&gt;The caller instance	&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
	&lt;td&gt;&lt;tt&gt;@Arguments&lt;/tt&gt;	&lt;/td&gt;
	&lt;td&gt;The invocation arguments (wrapped in an object array)	&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
	&lt;td&gt;&lt;tt&gt;@Returning&lt;/tt&gt;	&lt;/td&gt;
	&lt;td&gt;The return value (used in &amp;#8216;after returning advice&amp;#8217;)	&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
	&lt;td&gt;&lt;tt&gt;@Throwing&lt;/tt&gt;	&lt;/td&gt;
	&lt;td&gt;The exception thrown from a method (used in &amp;#8216;after throwing advice&amp;#8217;)	&lt;/td&gt;
&lt;/tr&gt;
&lt;/table&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;p&gt;These annotated arguments (except caller and callee method) also works as &amp;#8216;filters&amp;#8217;, the same is true for all unannotated arguments which are treated as the regular arguments to the method (or the field value about to be set etc.). The methods can be either member or static (dependent on if you want to keep state in the instance or not).&lt;/p&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;p&gt;Since some people might be curious how &amp;#8220;around advice&amp;#8221; (i.e. &amp;#8216;interception&amp;#8217;) is handled, below is an example of how to implement the same functionality as in the class above using an &amp;#8220;around advice&amp;#8221;. Here we introduce the &lt;tt&gt;InvocationContext&lt;/tt&gt; abstraction (similar to the &lt;tt&gt;ProceedingJoinPoint&lt;/tt&gt; abstraction in &lt;i&gt;AspectJ&lt;/i&gt;), which serves as the context on which we can invoke the &lt;tt&gt;proceed()&lt;/tt&gt; method in order to continue with the execution flow.&lt;/p&gt;
&lt;/p&gt;
&lt;p&gt;&lt;/pre&gt;&lt;pre&gt;&lt;br /&gt;
public class &lt;span class=&quot;caps&quot;&gt;POJO&lt;/span&gt; {&lt;/p&gt;
&lt;code&gt;Around(&quot;call(&lt;/code&gt;test.TraceMe * &lt;strong&gt;.&lt;/strong&gt;(..))&amp;quot;)
public Object trace(InvocationContext ctx,
@CalleeMethod Method callee) {
System.out.println(&amp;quot;&amp;#8212;&amp;gt; &amp;quot; + callee.toString());
Object result = ctx.proceed();
System.out.println(&amp;quot;&amp;lt; &amp;#8212; &amp;quot; + callee.toString());
return result;
}
&amp;#8230; // remaining methods omitted
&lt;p&gt;}&lt;br /&gt;
&lt;/pre&gt;&lt;/p&gt;
&lt;h2&gt; Implementation &lt;/h2&gt;
&lt;p&gt;
&lt;p&gt;I have prototyped HyperBeans using the &lt;a href=&quot;http://dev2dev.bea.com/pub/a/2005/08/jvm_aop_1.html&quot;&gt;&lt;span class=&quot;caps&quot;&gt;JVM&lt;/span&gt; support for &lt;span class=&quot;caps&quot;&gt;AOP&lt;/span&gt; in JRockit&lt;/a&gt;. The implementation was actually very simple and shows the power of the JRockit &lt;span class=&quot;caps&quot;&gt;API&lt;/span&gt; (the JRockit prototype is &lt;a href=&quot;http://forums.bea.com/bea/forum.jspa?forumID=600000004&quot;&gt;available now&lt;/a&gt; for those who wants to try it out).&lt;/p&gt;
&lt;/p&gt;
&lt;p&gt;&lt;/pre&gt;&lt;/p&gt;</content>
 </entry>
 
 <entry>
   <title>I'll buy you a beer...</title>
   <link href="http://jboner.github.com/2005/09/13/ill-buy-you-a-beer"/>
   <updated>2005-09-13T00:00:00+00:00</updated>
   <id>hhttp://jboner.github.com/2005/09/13/ill-buy-you-a-beer</id>
   <content type="html">&lt;h1&gt;
&lt;p&gt;I&amp;#8217;ll buy you a beer&amp;#8230;&lt;/p&gt;
&lt;/h1&gt;
&lt;p&gt;I am at &lt;a href=&quot;http://www.ftponline.com/conferences/javaprolive/&quot;&gt;JavaPro Live!&lt;/a&gt; in San Diego &lt;a href=&quot;http://www.ftponline.com/conferences/javaprolive/sessions.aspx#aopdriven&quot;&gt;doing a presentation &lt;/a&gt;titled: &amp;#8220;&lt;span class=&quot;caps&quot;&gt;AOP&lt;/span&gt;-Driven Services: Achieve Modularity and Reusability on the J2EE Platform&amp;#8221; (the talk is 3:15 on Thursday Sept 14:th). If you are attending the conference, stop by and say hi.&lt;/p&gt;
&lt;p&gt;I just finished rewriting the master template for the slides to follow a terra-cotta based color scheme. ;-)&lt;/p&gt;
&lt;p&gt;This is actually my second week as a &lt;a href=&quot;http://www.terracottatech.com/&quot;&gt;Terracotta&lt;/a&gt; employee and I am looking forward to the upcoming months. We are currently sketching on some very interesting new stuff, among other things, stuff tied to transparent runtime environments. More on that later.&lt;/p&gt;
&lt;p&gt;I will, from now on, spend a lot of time in San Francisco, so if you are in the area, drop me an email and I will buy you a beer :-)&lt;/p&gt;
&lt;p&gt;Just a warning: If it turns out that you have not tried Terracotta&amp;#8217;s Virtualization Server and its &lt;a href=&quot;http://www.terracottatech.com/product/dso.html&quot;&gt;Distributed Shared Objects (&lt;span class=&quot;caps&quot;&gt;DSO&lt;/span&gt;)&lt;/a&gt;, you will most likely here me rambling about that it is &lt;b&gt;The&lt;/b&gt; way of doing distributed programming in Java (e.g. clustering and caching) and why you have to give it a try.&lt;/p&gt;</content>
 </entry>
 
 <entry>
   <title>Thanks JRockit</title>
   <link href="http://jboner.github.com/2005/09/04/thanks-jrockit"/>
   <updated>2005-09-04T00:00:00+00:00</updated>
   <id>hhttp://jboner.github.com/2005/09/04/thanks-jrockit</id>
   <content type="html">&lt;h1&gt;
&lt;p&gt;Thanks JRockit&lt;/p&gt;
&lt;/h1&gt;
&lt;p&gt;I guess that most people have &lt;a href=&quot;http://www.theserverside.com/news/thread.tss?thread_id=36073&quot;&gt;not missed&lt;/a&gt; that I am leaving &lt;a href=&quot;http://bea.com/&quot;&gt;&lt;span class=&quot;caps&quot;&gt;BEA&lt;/span&gt;&lt;/a&gt;. Yesterday was actually my last day as an &lt;span class=&quot;caps&quot;&gt;BEA&lt;/span&gt; employee.&lt;/p&gt;
&lt;p&gt;I have had two great years at &lt;span class=&quot;caps&quot;&gt;BEA&lt;/span&gt;.  It has been a great experience to work at the Java Runtime Products Group (&lt;span class=&quot;caps&quot;&gt;JRPG&lt;/span&gt;), (&lt;span class=&quot;caps&quot;&gt;JRPG&lt;/span&gt; is the group that implements the &lt;a href=&quot;http://dev2dev.bea.com/jrockit/&quot;&gt;JRockit &lt;span class=&quot;caps&quot;&gt;JVM&lt;/span&gt;&lt;/a&gt;), not everyone gets the opportunity to work among so many extremely talented and smart people, and that is something that I am very grateful for.  I have learned more in these two years that I had in my whole career.&lt;/p&gt;
&lt;p&gt;I am also grateful for that &lt;span class=&quot;caps&quot;&gt;JRPG&lt;/span&gt; has believed in &lt;span class=&quot;caps&quot;&gt;AOP&lt;/span&gt; and my vision, so much and that they have allowed me to work full-time for two years to materialize them.&lt;/p&gt;
&lt;p&gt;During this two year period we (&lt;a href=&quot;http://blogs.codehaus.org/people/avasseur/&quot;&gt;Alexandre Vasseur&lt;/a&gt; and myself) have had the opportunity to participate in writing three important chapters in this story for &lt;span class=&quot;caps&quot;&gt;AOP&lt;/span&gt; in Java:&lt;/p&gt;
&lt;p&gt;1. &lt;a href=&quot;http://www.theserverside.com/news/thread.tss?thread_id=32534&quot;&gt;The release of AspectWerkz 2.0&lt;/a&gt;&lt;br /&gt;
2. &lt;a href=&quot;http://www.theserverside.com/news/thread.tss?thread_id=31244&quot;&gt;The merge of AspectWerkz and AspectJ&lt;/a&gt;&lt;br /&gt;
3. &lt;a href=&quot;http://dev2dev.bea.com/pub/a/2005/08/jvm_aop_1.html&quot;&gt;The support for &lt;span class=&quot;caps&quot;&gt;AOP&lt;/span&gt; in the JRockit &lt;span class=&quot;caps&quot;&gt;JVM&lt;/span&gt;&lt;/a&gt; (thanks to Joakim Dahlstedt)&lt;/p&gt;
&lt;p&gt;It for sure has been a ride :-)&lt;/p&gt;
&lt;p&gt;Thanks JRockit.&lt;/p&gt;</content>
 </entry>
 
 <entry>
   <title>AOP Roadshow In Japan</title>
   <link href="http://jboner.github.com/2005/08/31/aop-roadshow-in-japan"/>
   <updated>2005-08-31T00:00:00+00:00</updated>
   <id>hhttp://jboner.github.com/2005/08/31/aop-roadshow-in-japan</id>
   <content type="html">&lt;h1&gt;
&lt;p&gt;&lt;span class=&quot;caps&quot;&gt;AOP&lt;/span&gt; Roadshow In Japan&lt;/p&gt;
&lt;/h1&gt;
&lt;p&gt;&lt;b&gt;Japan Tour&lt;/b&gt;&lt;/p&gt;
&lt;p&gt;My Japan tour is starting to come to an end. During the past week I have been traveling in Japan, done two general sessions at &lt;a href=&quot;http://www.beasys.co.jp/news_events/bea_seminar/td_2005.html&quot;&gt;&lt;span class=&quot;caps&quot;&gt;BEA&lt;/span&gt; Tech Day 2005&lt;/a&gt; conferences (Tokyo and Osaka), plus a press conference and a bunch of &lt;span class=&quot;caps&quot;&gt;BEA&lt;/span&gt; internal training sessions.&lt;/p&gt;
&lt;p&gt;I had never been to Japan before, but I am very interested in their culture and I love Japanese food, so this trip has in many ways been a very interesting and fun experience. I had some fears that my visit would turn out similar to the movie &amp;#8216;Lost In Translation&amp;#8217; by Bill Murray (for those that have seen this movie), but my fears were unfounded. Most people spoke good english and I only met very generous people with a lot of heart.&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Excitement around &lt;span class=&quot;caps&quot;&gt;AOP&lt;/span&gt;&lt;/b&gt;&lt;/p&gt;
&lt;p&gt;There is a lot of excitement and interest around &lt;span class=&quot;caps&quot;&gt;AOP&lt;/span&gt; in Japan, both among developers, management and press. They are especially excited about &lt;a href=&quot;http://aspectwerkz.codehaus.org/&quot;&gt;AspectWerkz &lt;/a&gt; (it is actually more popular than &lt;a href=&quot;http://www.eclipse.org/aspectj/&quot;&gt;AspectJ&lt;/a&gt;) and &lt;a href=&quot;http://dev2dev.bea.com/pub/a/2005/08/jvm_aop_1.html&quot;&gt;JRockit support for &lt;span class=&quot;caps&quot;&gt;AOP&lt;/span&gt;&lt;/a&gt;.&lt;/p&gt;
It was fun to learn that they had translated every single article I have written, to Japanese.  On the other hand, the problem having to translate almost every article and book is one of the biggest hurdles on the way to mass adoption of &lt;span class=&quot;caps&quot;&gt;AOP&lt;/span&gt;.
&lt;p&gt;&lt;b&gt;Press conference&lt;/b&gt;&lt;/p&gt;
&lt;p&gt;The press conference I did was an interesting experience, it was my first real press conference, with interviewers lined up, pounding me with questions. It was a lot of fun. The topic for the interview was &lt;a href=&quot;http://dev2dev.bea.com/jrockit/&quot;&gt;JRockit &lt;/a&gt;and its new &lt;span class=&quot;caps&quot;&gt;AOP&lt;/span&gt; support which they seemed very impressed about.&lt;/p&gt;
&lt;p&gt;They have already published, two articles based on the press conference:&lt;br /&gt;
&lt;a href=&quot;http://www.atmarkit.co.jp/news/200508/31/bea.html&quot;&gt;http://www.atmarkit.co.jp/news/200508/31/bea.html&lt;/a&gt;&lt;br /&gt;
&lt;a href=&quot;http://itpro.nikkeibp.co.jp/free/NC/NEWS/20050830/167117/&quot;&gt;http://itpro.nikkeibp.co.jp/free/NC/&lt;span class=&quot;caps&quot;&gt;NEWS&lt;/span&gt;/20050830/167117/&lt;/a&gt;&lt;br /&gt;
and from what I understand, two more articles with the published.&lt;/p&gt;
&lt;p&gt;&lt;b&gt;&lt;span class=&quot;caps&quot;&gt;BEA&lt;/span&gt; Tech Day 2005 in Tokyo&lt;/b&gt;&lt;/p&gt;
&lt;p&gt;The general session I did had around 350 attendees and was simultaneously translated. Things worked out pretty well. People seem to like the talk and came up with a bunch of really good questions at the end. What was even more fun was that there were actually other speakers that talked about AspectWerkz/AspectJ in different contexts.  One guy for example, talked about how to implement the transaction and dependency injection parts of the &lt;span class=&quot;caps&quot;&gt;EJB&lt;/span&gt; 3 specification using AspectWerkz.  In general, one of the main themes of the conference was &lt;span class=&quot;caps&quot;&gt;AOP&lt;/span&gt;.&lt;/p&gt;
&lt;p&gt;Afterwards, during the conference party, I was treated almost like a celebrity, with &amp;#8220;fans&amp;#8221; coming up and wanted to shake hands and have a picture taken with the mysterious &lt;span class=&quot;caps&quot;&gt;AOP&lt;/span&gt; guru from the north pole (Sweden). :-)&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Seasar framework &amp;#8211; a Japanese Spring killer?&lt;/b&gt;&lt;/p&gt;
&lt;p&gt;During lunch at the conference I had the pleasure of having interesting discussion with the founder of the dependency injection and component framework &lt;a href=&quot;http://www.seasar.org/&quot;&gt;Seasar&lt;/a&gt;. For those that have never heard about Seasar, Seasar is a project very similar to &lt;a href=&quot;http://www.springframework.org/&quot;&gt;Spring&lt;/a&gt;, which is more popular than Spring here in Japan. It uses plain bytecode manipulation to do the &lt;span class=&quot;caps&quot;&gt;AOP&lt;/span&gt; part (and not proxies), and is based on the &lt;span class=&quot;caps&quot;&gt;AOP&lt;/span&gt; Alliance interfaces. One cool thing that I found out about the project is that they are not only using but are very excited about Alex&amp;#8217;s and my project &lt;a href=&quot;http://backport175.codehaus.org/&quot;&gt;backport175&lt;/a&gt; (so much that they actually asked to contribute back some code).&lt;/p&gt;
&lt;p&gt;&lt;b&gt;&lt;span class=&quot;caps&quot;&gt;BEA&lt;/span&gt; Tech Day 2005 in Osaka &amp;#8211; Kyoto sightseeing&lt;/b&gt;&lt;/p&gt;
&lt;p&gt;Today I am off for Osaka for my last talks at the &lt;span class=&quot;caps&quot;&gt;BEA&lt;/span&gt; Tech Day 2005 in Osaka tomorrow.  The trip will conclude with one day off,  which I will spend sightseeing in Kyoto. Kyoto is the historical and cultural center in Japan, with many buildings and temples being more than 1000 years old (compared to Tokyo which is only a couple of hundred years old). So that is going to be interesting.&lt;/p&gt;</content>
 </entry>
 
 <entry>
   <title>util.concurrent on steroids</title>
   <link href="http://jboner.github.com/2005/08/30/utilconcurrent-on-steroids"/>
   <updated>2005-08-30T00:00:00+00:00</updated>
   <id>hhttp://jboner.github.com/2005/08/30/utilconcurrent-on-steroids</id>
   <content type="html">&lt;h1&gt;
&lt;p&gt;util.concurrent on steroids&lt;/p&gt;
&lt;/h1&gt;
&lt;p&gt;Steve has written a &lt;a href=&quot;http://blog.terracottatech.com/archive/2005/08/fun_with_dso_an.html#more&quot;&gt;nice article&lt;/a&gt; on how you can coordinate threads in a distributed application, transparently, and with very little effort.&lt;/p&gt;
&lt;p&gt;He does this simply by sharing an instance of a &lt;a href=&quot;http://java.sun.com/j2se/1.5.0/docs/api/java/util/concurrent/CyclicBarrier.html&quot;&gt;CyclicBarrier&lt;/a&gt;, a class in Doug Lea&amp;#8217;s excellent &lt;a href=&quot;http://gee.cs.oswego.edu/dl/classes/EDU/oswego/cs/dl/util/concurrent/intro.html&quot;&gt;util.concurrent package&lt;/a&gt;(now included in &lt;span class=&quot;caps&quot;&gt;JDK&lt;/span&gt; 5.0), across a cluster  Using &lt;a href=&quot;http://www.terracottatech.com/product/dso.html&quot;&gt;Distributed Shared Objects &lt;/a&gt;(&lt;span class=&quot;caps&quot;&gt;DSO&lt;/span&gt;).&lt;/p&gt;
&lt;p&gt;It really shows the power of &lt;span class=&quot;caps&quot;&gt;DSO&lt;/span&gt; in action, by solving an extremely tricky problem in almost no time and no code.&lt;/p&gt;</content>
 </entry>
 
 <entry>
   <title>Time to Throw Out Your Distributed HashMap?</title>
   <link href="http://jboner.github.com/2005/08/19/time-to-throw-out-your-distributed-hashmap"/>
   <updated>2005-08-19T00:00:00+00:00</updated>
   <id>hhttp://jboner.github.com/2005/08/19/time-to-throw-out-your-distributed-hashmap</id>
   <content type="html">&lt;h1&gt;
&lt;p&gt;Time to Throw Out Your Distributed HashMap?&lt;/p&gt;
&lt;/h1&gt;
&lt;p&gt;&lt;a href=&quot;http://www.pcal.net/blog/&quot;&gt;Patrick Calahan&lt;/a&gt; has written an &lt;a href=&quot;http://blog.terracottatech.com/archive/2005/08/object_identity.html&quot;&gt;interesting article&lt;/a&gt; on Distributed Shared Objects (&lt;span class=&quot;caps&quot;&gt;DSO&lt;/span&gt;) and how they compare to traditional ways of doing caching and clustering. &lt;span class=&quot;caps&quot;&gt;DSO&lt;/span&gt; might just as well be the death of the the distributed hashmap, e.g. JCache etc. Seems to be the only sane way of doing clustering in Java.&lt;/p&gt;</content>
 </entry>
 
 <entry>
   <title>JVM support for AOP - part 2</title>
   <link href="http://jboner.github.com/2005/08/08/jvm-support-for-aop-part-2"/>
   <updated>2005-08-08T00:00:00+00:00</updated>
   <id>hhttp://jboner.github.com/2005/08/08/jvm-support-for-aop-part-2</id>
   <content type="html">&lt;h1&gt;
&lt;p&gt;&lt;span class=&quot;caps&quot;&gt;JVM&lt;/span&gt; support for &lt;span class=&quot;caps&quot;&gt;AOP&lt;/span&gt; &amp;#8211; part 2&lt;/p&gt;
&lt;/h1&gt;
&lt;p&gt;The &lt;a href=&quot;http://dev2dev.bea.com/pub/a/2005/08/jvm_aop_2.html&quot;&gt;second part&lt;/a&gt; of our article series about &lt;a href=&quot;http://commerce.bea.com/products/weblogicjrockit/5.0/jr_50.jsp&quot;&gt;JRockit &lt;/a&gt;&lt;span class=&quot;caps&quot;&gt;JVM&lt;/span&gt; support for &lt;span class=&quot;caps&quot;&gt;AOP&lt;/span&gt; is now published.&lt;/p&gt;
&lt;p&gt;This article describes in more detail on how the &lt;span class=&quot;caps&quot;&gt;API&lt;/span&gt; is used, showing some code samples and ends with a discussion in which we try to explain how we address the questions and issues (around the shortcomings of bytecode manipulation) raised in &lt;a href=&quot;http://dev2dev.bea.com/pub/a/2005/08/jvm_aop_1.html&quot;&gt;part one&lt;/a&gt; in the series.&lt;/p&gt;
&lt;p&gt;The prototype will be downloadable soon, stay tuned.&lt;/p&gt;</content>
 </entry>
 
 <entry>
   <title>Use Total Ordering of Objects to Avoid Deadlocks</title>
   <link href="http://jboner.github.com/2005/07/27/use-total-ordering-of-objects-to-avoid-deadlocks"/>
   <updated>2005-07-27T00:00:00+00:00</updated>
   <id>hhttp://jboner.github.com/2005/07/27/use-total-ordering-of-objects-to-avoid-deadlocks</id>
   <content type="html">&lt;h1&gt;
&lt;p&gt;Use Total Ordering of Objects to Avoid Deadlocks&lt;/p&gt;
&lt;/h1&gt;
&lt;p&gt;Writing multi-threaded applications in Java can as you know be quite tricky, and if not done correctly, one can easily end up with deadlock situations.&lt;/p&gt;
&lt;p&gt;For example, take a look at this little code snippet.  This piece of Java code implements a Node, which has three methods (that is of our interest):&lt;/p&gt;
&lt;p&gt;&lt;code lang=&quot;java&quot;&gt;
Object get()
void set(Object o) 
void swap(Node n)
&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;
&lt;p&gt;Here&amp;#8217;s the implementation:&lt;/p&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;p&gt;&lt;code lang=&quot;java&quot;&gt;
public class Node {
    private Object value;
    public synchronized Object get() {
        return value;
    }
    public synchronized void set(Object value) {
        this.value = value;
    }
    public synchronized void swap(Node n) {
        Object tmp = get();
        set(n.get());
        n.set(tmp);
    }
   ... // remaining methods omitted 
}
&lt;/code&gt;&lt;/p&gt;
&lt;/p&gt;
&lt;p&gt;This class might look okay, but it actually suffers from potential deadlock.  For example what happens if, one thread T1 invokes &lt;tt&gt;n1.swap(n2)&lt;/tt&gt; while an other one T2 concurrently invokes &lt;tt&gt;n2.swap(n1) &lt;/tt&gt;?&lt;/p&gt;
&lt;p&gt;Thread T1 acquires the lock for node n1, and thread T2 acquires the lock for node n2.  Thread T1 now invokes &lt;tt&gt;n2.get()&lt;/tt&gt; (in the &lt;tt&gt;swap(..)&lt;/tt&gt; method). This invocation now has to wait since node n2 is locked by thread T2, this while the reverse holds for thread T2 (e.g. it needs to wait for node n1 which is locked by thread T1). Which means that we have a deadlock.&lt;/p&gt;
&lt;p&gt;
&lt;p&gt;This program is however easily fixed by using the technique of totally ordering all objects in the system.&lt;/p&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;p&gt;&lt;code lang=&quot;java&quot;&gt;
public class Node {
    private Object value;
    public synchronized Object get() {
        return value;
    }
    public synchronized void set(Object value) {
        this.value = value;
    }
    private synchronized void doSwap(Node n) {
        Object tmp = get();
        set(n.get());
        n.set(tmp);
    }
    public void swap(Node n) {
        if (this ==  n) {
            return;
        } else if (System.identityHashCode(this) &amp;lt; System.identityHashCode(n)) {
            doSwap(n);
       } else {
           n.doSwap(this);
       }
    }
   ... // remaining methods omitted 
}
&lt;/code&gt;&lt;/code&gt;&lt;/p&gt;&lt;/p&gt;
&lt;p&gt;
&lt;p&gt;In this program, all locks will be acquired in increasing order, guaranteed to be the same for all threads, and thereby avoiding deadlock situations.&lt;/p&gt;
&lt;/p&gt;</content>
 </entry>
 
 <entry>
   <title>Semantics for a Synchronized Block Join Point</title>
   <link href="http://jboner.github.com/2005/07/18/semantics-for-a-synchronized-block-join-point"/>
   <updated>2005-07-18T00:00:00+00:00</updated>
   <id>hhttp://jboner.github.com/2005/07/18/semantics-for-a-synchronized-block-join-point</id>
   <content type="html">&lt;h1&gt;
&lt;p&gt;Semantics for a Synchronized Block Join Point&lt;/p&gt;
&lt;/h1&gt;
&lt;p&gt;As I mentioned in my previous blog entry, the &lt;tt&gt;synchronized&lt;/tt&gt; block is currently not a supported join point in AspectJ 5 (or in any other &lt;span class=&quot;caps&quot;&gt;AOP&lt;/span&gt; framework):&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;Currently you can for example pick out a call to a method that is declared as being synchronized and you can pick out calls to Thread:: notify()/notifyAll()/wait(). Meaning that being able to pick out synchronized blocks are the only missing piece left in order to completely control thread management and locking in Java. The actual bytecode modifications needed to make this work would be fairly simple, but capturing the correct semantics in a good language design would probably be a lot trickier.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;Well, I&amp;#8217;m not a language designer, but I think the problem is interesting so I will spend some time discussing it anyway.&lt;/p&gt;
&lt;p&gt;In bytecode, a synchronized block is represented as a &lt;span class=&quot;caps&quot;&gt;MONITOR&lt;/span&gt; &lt;span class=&quot;caps&quot;&gt;ENTRY&lt;/span&gt; and a &lt;span class=&quot;caps&quot;&gt;MONITOR&lt;/span&gt; &lt;span class=&quot;caps&quot;&gt;EXIT&lt;/span&gt; bytecode instruction pair (however these are not required to be paired).  The first natural approach would to let these two bytecode instructions be join points and treat them similar to field access and modification (&lt;span class=&quot;caps&quot;&gt;PUTFIELD&lt;/span&gt; and &lt;span class=&quot;caps&quot;&gt;GETFIELD&lt;/span&gt; bytecode instructions), meaning simply pick out (and intercept) this single bytecode instruction.&lt;/p&gt;
&lt;p&gt;Just to clarify what I mean, here is an example of how the syntax for the above given semantics could look in the AspectJ pointcut expression language:&lt;br /&gt;
&lt;code lang=&quot;java&quot;&gt;
lock(Type t) &amp;amp;&amp;amp; withincode(* Foo.bar(..)) &amp;amp;&amp;amp; args(t)&lt;/p&gt;
&lt;p&gt;unlock(Type t) &amp;amp;&amp;amp; withincode(* Foo.bar(..)) &amp;amp;&amp;amp; args(t)&lt;br /&gt;
&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;Let us take a look at a synchronized block and how it would be affected:&lt;br /&gt;
&lt;code lang=&quot;java&quot;&gt;
synchronized(obj) {
    // body
}
&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;In bytecode to this is equivalent to (pseudo code):&lt;br /&gt;
&lt;code lang=&quot;java&quot;&gt;
MONITOR ENTRY // lock on obj
    // body
MONITOR EXIT
&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;If we now add around advices to these join points then we could for example get (pseude code):&lt;br /&gt;
&lt;code lang=&quot;java&quot;&gt;
try {
    // a call to the around advice, which calls the lock manager
    aroundAdvice1(obj) --&amp;gt; myLockManager.acquire(obj);&lt;/p&gt;
// body
&lt;p&gt;} finally {&lt;br /&gt;
    aroundAdvice2(obj) &amp;#8212;&amp;gt; myLockManager.release(obj);&lt;br /&gt;
}&lt;br /&gt;
&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;This approach would give you the possibility to completely control how locking is done in Java (including the possibility of enhancing or completely screw up the Java Memory Model (&lt;span class=&quot;caps&quot;&gt;JMM&lt;/span&gt;)). On the other hand it does not allow you to pick out the actual code block that is synchronized (is this something that we want?) .  Therefore this approach is perhaps not intuitive, since in Java source code, what we see is not lock acquisition and release but a code block that is guaranteed to be synchronized. So now let&amp;#8217;s try to approach this problem from the perspective of source code.&lt;/p&gt;
&lt;p&gt;Since AspectJ (and AspectWerkz) already has limited support for the synchronized keyword by allowing calls to methods declared as being synchronized to be matched, let us take a look at the semantics for this join point.&lt;/p&gt;
&lt;p&gt;Here&amp;#8217;s a simple method that is defined as being synchronized:&lt;br /&gt;
&lt;code lang=&quot;java&quot;&gt;
public synchronized void doStuff() { 
    // body
}
&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;What this actually means is:&lt;br /&gt;
&lt;code lang=&quot;java&quot;&gt;
public void doStuff() { 
    synchronized(this) {
        // body
    }
}
&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;The options we have of picking up this synchronized code block (wrapped up in the doStuff() method) is by using a &lt;pre&gt;execution(synchronized void *.doStuff())&lt;/pre&gt; or &lt;pre&gt;call(synchronized void *.doStuff())&lt;/pre&gt; pointcut.&lt;/p&gt;
&lt;p&gt;Using the execution pointcut, the above method body would be transformed to (pseudo code):&lt;br /&gt;
&lt;code lang=&quot;java&quot;&gt;
synchronized(this) {
    // the advice has option of invoking the original body
    myAroundAdvice(this) 
}
&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;I.e. only synchronized body is matched and intercepted.&lt;/p&gt;
&lt;p&gt;If we are using the call pointcut, the same code snippet would be transformed to (pseudo code):&lt;br /&gt;
&lt;code lang=&quot;java&quot;&gt;
...
// the advice has the option of invoking the original synchronized block
myAroundAdvice(this) 
...
&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;I.e. the whole synchronized block, including the locking is matched (and intercepted). (This is conceptionally true, in regards to the locking, which is easy to see if you think of the doStuff() method as being inlined.)&lt;/p&gt;
&lt;p&gt;I will not go into much detail about how these semantic differences should be expressed in the pointcut language here (this post is too long already). But for example, the last discussion would require the possibility of making a distinction between picking out a code block &lt;tt&gt;inclusively&lt;/tt&gt; and &lt;tt&gt;exclusively&lt;/tt&gt; (i.e. picking out the whole code block, or just the body of the code block), which could be expressed something like this:&lt;br /&gt;
&lt;code lang=&quot;java&quot;&gt;
// pick out synchronized block including the locking
block-inclusive(synchronized(Type t)) &amp;amp;&amp;amp; withincode(* Foo.bar(..)) &amp;amp;&amp;amp; args(t)&lt;/p&gt;
&lt;p&gt;// pick out only the synchronized block&amp;#8217;s body, not the locking&lt;br /&gt;
block-exclusive(synchronized(Type t)) &amp;amp;&amp;amp; withincode(* Foo.bar(..)) &amp;amp;&amp;amp; args(t) &lt;br /&gt;
&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;(These &amp;#8220;block&amp;#8221; pointcut descriptors can of course be used in any kind of block, loops, conditional statements etc., if/whenever they are supported in AspectJ.)&lt;/p&gt;
&lt;p&gt;To sum up, the questions are: Do we want the power of intercepting the whole locking mechanism (but not the synchronized body), or is it better to follow the semantics we have for a synchronized method? Which approach addresses the use cases we want?  Is the most intuitive?  Is more orthogonal?&lt;/p&gt;
&lt;p&gt;Thoughts?  Comments?  Ideas?&lt;/p&gt;</content>
 </entry>
 
 <entry>
   <title>DSO: transparent clustering enabled through AOP</title>
   <link href="http://jboner.github.com/2005/06/07/dso-transparent-clustering-enabled-through-aop"/>
   <updated>2005-06-07T00:00:00+00:00</updated>
   <id>hhttp://jboner.github.com/2005/06/07/dso-transparent-clustering-enabled-through-aop</id>
   <content type="html">&lt;h1&gt;
&lt;p&gt;&lt;span class=&quot;caps&quot;&gt;DSO&lt;/span&gt;: transparent clustering enabled through &lt;span class=&quot;caps&quot;&gt;AOP&lt;/span&gt;&lt;/p&gt;
&lt;/h1&gt;
&lt;p&gt;&lt;a href=&quot;http://www.terracottatech.com/index.html&quot;&gt;Terracotta&lt;/a&gt; just recently launched a product (&lt;span class=&quot;caps&quot;&gt;DSO&lt;/span&gt;) for clustering POJOs in a completely transparent way.  What I find interesting in this product is not mainly in the clustering and caching, but how they make use of &lt;span class=&quot;caps&quot;&gt;AOP&lt;/span&gt; and loadtime weaving to make it completely transparent.&lt;/p&gt;
&lt;p&gt;This sounds a lot like techniques that we have been working on in &lt;a href=&quot;http://aspectwerkz.codehaus.org/&quot;&gt;AspectWerkz&lt;/a&gt; last years, and I was not too surprised, although very happy, to learn that they are actually using AspectWerkz. It is really great to see some of the ideas that Alex and I have been working on being used in new and interesting ways and in real-world applications.&lt;/p&gt;
&lt;p&gt;They seem to use techniques that are fairly similar to the &lt;a href=&quot;http://docs.codehaus.org/display/AWARE/UnitOfWorkProtocol&quot;&gt;Unit of Work aspect library&lt;/a&gt; that I wrote for the AWare aspect library. Meaning: record field changes to object graphs within a transaction which can be committed/rolled back./etc. add specific methods that are defined declaratively.&lt;/p&gt;
&lt;p&gt;However they have extended to this idea by adding semantics currently not available in &lt;span class=&quot;caps&quot;&gt;AOP&lt;/span&gt;.  For example being able to pick out synchronized blocks.  This is a great idea, and when you think of it, it is in a way a hole in the current &lt;span class=&quot;caps&quot;&gt;AOP&lt;/span&gt; implementations.  Currently you can for example pick out a call to a method that is declared as being synchronized and you can pick out calls to Thread:: notify()/notifyAll()/wait().  Meaning that being able to pick out synchronized blocks are the only missing piece left in order to completely control thread management and locking in Java. The actual bytecode modifications needed to make this work would be fairly simple, but capturing the correct semantics in a good language design would probably be a lot trickier.&lt;/p&gt;
&lt;p&gt;All and all a very interesting product that shows the beauty of &lt;span class=&quot;caps&quot;&gt;AOP&lt;/span&gt; and loadtime weaving in action, go and check it out.&lt;/p&gt;</content>
 </entry>
 
 <entry>
   <title>JVM support for AOP (technical session at JavaOne 2005)</title>
   <link href="http://jboner.github.com/2005/05/29/jvm-support-for-aop-technical-session-at-javaone-2005"/>
   <updated>2005-05-29T00:00:00+00:00</updated>
   <id>hhttp://jboner.github.com/2005/05/29/jvm-support-for-aop-technical-session-at-javaone-2005</id>
   <content type="html">&lt;h1&gt;
&lt;p&gt;&lt;span class=&quot;caps&quot;&gt;JVM&lt;/span&gt; support for &lt;span class=&quot;caps&quot;&gt;AOP&lt;/span&gt; (technical session at JavaOne 2005)&lt;/p&gt;
&lt;/h1&gt;
&lt;p&gt;If you&amp;#8217;re interested in knowing more about what the future holds for &lt;span class=&quot;caps&quot;&gt;AOP&lt;/span&gt;, then you should come to the technical session TS-7659 (titled &amp;#8220;Runtime Aspects With &lt;span class=&quot;caps&quot;&gt;JVM&lt;/span&gt; Support&amp;#8221;) on Tuesday, June 28 at JavaOne 2005.&lt;/p&gt;
&lt;p&gt;This will be the first time ever to show an enterprise &lt;span class=&quot;caps&quot;&gt;JVM&lt;/span&gt; &amp;#8211; &lt;span class=&quot;caps&quot;&gt;BEA&lt;/span&gt; JRockit &amp;#8211; with native &lt;span class=&quot;caps&quot;&gt;AOP&lt;/span&gt; support. Alex Vasseur and myself have been pushing for this idea since the early days of AspectWerkz when we joined the JRockit group, and it&amp;#8217;s finally taking shape, with now support for AspectJ 5, the de-facto standard for &lt;span class=&quot;caps&quot;&gt;AOP&lt;/span&gt; in Java.&lt;/p&gt;
&lt;p&gt;Credit should also go to Joakim Dahlstedt, the &lt;span class=&quot;caps&quot;&gt;CTO&lt;/span&gt; of JRockit, who has been playing a leading role in the design and implementation.&lt;/p&gt;
&lt;p&gt;We will talk about the current problems with bytecode based weaving (multiple agents, double bookkeeping, performance etc.) and then discuss how these problems can be solved through native &lt;span class=&quot;caps&quot;&gt;JVM&lt;/span&gt; support. We will show a prototype of our implementation, go through the programming model and run a live demo.&lt;/p&gt;
&lt;p&gt;Here is the abstract:&lt;/p&gt;
&lt;p&gt;&amp;#8220;Aspect Oriented Programming (&lt;span class=&quot;caps&quot;&gt;AOP&lt;/span&gt;) is growing in popularity. Adding aspects at runtime, dynamic weaving, can be a very powerful technique for diagnostics, profiling, or debugging of a running system. The concept of adding aspects at runtime (dynamic weaving) is looked upon with skepticism by some. It&amp;#8217;s also easy for multiple instrumenting agents to cause havoc for each other. We have implemented prototype changes to our Javaâ„¢ Virtual Machine (JVMâ„¢ software), &lt;span class=&quot;caps&quot;&gt;BEA&lt;/span&gt; Weblogic JRockit, to support dynamic weaving in the &lt;span class=&quot;caps&quot;&gt;JVM&lt;/span&gt; software in a controlled fashion. We also have modified AspectJ 5 to try out this functionality. We believe our changes to the &lt;span class=&quot;caps&quot;&gt;JVM&lt;/span&gt; software to support dynamic weaving lessens the skepticism about runtime aspects and allows for multiple instrumenting agents to coexist.&lt;br /&gt;
In this session we present the changes we made to the &lt;span class=&quot;caps&quot;&gt;JVM&lt;/span&gt; software to support this functionality and the results of this effort. In addition, we compare this approach to current approaches like &lt;span class=&quot;caps&quot;&gt;JVMTI&lt;/span&gt;/Hotswap and Project JFluid. Finally, we provide a demonstration of various runtime advice you&amp;#8217;ll find useful to analyze the behavior of your applications.&amp;#8221;&lt;/p&gt;
&lt;p&gt;Don&amp;#8217;t miss it, see you there.&lt;/p&gt;</content>
 </entry>
 
 <entry>
   <title>Nordev 2005: Rickard on real world use-cases for AOP etc.</title>
   <link href="http://jboner.github.com/2005/05/20/nordev-2005-rickard-on-real-world-use-cases-for-aop-etc"/>
   <updated>2005-05-20T00:00:00+00:00</updated>
   <id>hhttp://jboner.github.com/2005/05/20/nordev-2005-rickard-on-real-world-use-cases-for-aop-etc</id>
   <content type="html">&lt;h1&gt;
&lt;p&gt;Nordev 2005: Rickard on real world use-cases for &lt;span class=&quot;caps&quot;&gt;AOP&lt;/span&gt; etc.&lt;/p&gt;
&lt;/h1&gt;
&lt;p&gt;I just got back from the Nordic Software Developer Summit (Nordev) 2005.  Strange enough, this is the first good developer conference that we have had in Sweden.&lt;/p&gt;
&lt;p&gt;Altogether it was a really good event with two major tracks, one Java and one .Net. The Java track had interesting talks on topics like: &lt;span class=&quot;caps&quot;&gt;AOP&lt;/span&gt; :-), agile development, hibernate, spring, manageability of the JRockit &lt;span class=&quot;caps&quot;&gt;JVM&lt;/span&gt; etc. (e.g. the usual suspects).&lt;/p&gt;
&lt;p&gt;I was glad to see that &lt;span class=&quot;caps&quot;&gt;AOP&lt;/span&gt; got a lot of focus with talks from myself and Rickard Oberg and a hands-on workshop (on AspectJ).  Both sessions were packed, and people seemed enthusiastic about it.&lt;/p&gt;
&lt;p&gt;Of all things you can be nervous about, I was nervous about speaking in Swedish (my native language).  I had never spoken about &lt;span class=&quot;caps&quot;&gt;AOP&lt;/span&gt; in Swedish so it was interesting and a good exercise :-).  Everything went pretty well except that eclipse died on me when I was starting up the demo (3.1 M6). (The demo was about how to write a unit of work to achieve transparent persistence/replication/transaction management, perhaps something for an upcoming blog post&amp;#8230;).&lt;/p&gt;
&lt;p&gt;I enjoyed Rickard&amp;#8217;s talk.  He based the talk on examples from the &lt;span class=&quot;caps&quot;&gt;CMS&lt;/span&gt; application and he has built, and it was interesting to see both simple and advanced usage of &lt;span class=&quot;caps&quot;&gt;AOP&lt;/span&gt; in the real world application.  He&amp;#8217;s using a pretty extreme approach to &lt;span class=&quot;caps&quot;&gt;AOP&lt;/span&gt; in which every single object is built up a using intertype declarations (introductions/mixins).  I do not think that this model fits every project, and it for sure has drawbacks, but in their &lt;span class=&quot;caps&quot;&gt;CMS&lt;/span&gt; application it has given them extremely high reuse and development speed.&lt;/p&gt;
&lt;p&gt;He showed examples of client aspects, how objects can render themselves differently based on which intertype declarations it has.  For example, if an object in their &lt;span class=&quot;caps&quot;&gt;GUI&lt;/span&gt; (a page, a site, a portlet etc.) has the intertype declaration &lt;span class=&quot;caps&quot;&gt;ACL&lt;/span&gt; applied to it then it will give you the possibility to open up a window to manage security for this object. This object can also be given the Tree intertype declaration, which will allow you to browse the object in a tree structure etc.&lt;/p&gt;
&lt;p&gt;He also showed examples on how they do client synchronization and cluster wide replication, using the same aspect (which simply records the events triggered and then plays it back).  Another interesting aspect that they have is what he calls partial object versioning, in which intertype declarations can be versioned and different versions can be used (in the same object), depending on how system is configured.&lt;/p&gt;
&lt;p&gt;Rickard gave an example of a customer that came in with a new functional requirements very late in the release cycle, requirements that required redesign of some parts of the system, but that he managed to solve in a simple and generic way by applying an aspect.&lt;/p&gt;
&lt;p&gt;Then he talked about the need for tools and that most problems that people are complaining about when it comes to &lt;span class=&quot;caps&quot;&gt;AOP&lt;/span&gt; are actually not related to &lt;span class=&quot;caps&quot;&gt;AOP&lt;/span&gt;, but to the lack of good tools. He showed one interesting tool that they are using on a daily basis, a tool for doing a diff of the system.  This diff showed you which advice and or intertype declarations had been removed or added since the last time the system&amp;#8217;s state was recorded. For example, after a regular refactoring, the diff should be zero.  This is something that we should add to &lt;span class=&quot;caps&quot;&gt;AJDT&lt;/span&gt;.&lt;/p&gt;
&lt;p&gt;All and all a good conference, you should try to get there next year.&lt;/p&gt;</content>
 </entry>
 
 <entry>
   <title>JRockit 5 Memory Leak Tool</title>
   <link href="http://jboner.github.com/2005/03/21/jrockit-5-memory-leak-tool-webex-demo-on-wed"/>
   <updated>2005-03-21T00:00:00+00:00</updated>
   <id>hhttp://jboner.github.com/2005/03/21/jrockit-5-memory-leak-tool-webex-demo-on-wed</id>
   <content type="html">&lt;h1&gt;
&lt;p&gt;JRockit 5 Memory Leak Tool&lt;/p&gt;
&lt;/h1&gt;
&lt;p&gt;The upcoming release of JRockit will contain a new, completely killing, tool for memory leak detection.&lt;/p&gt;
&lt;p&gt;It allows you to (among many other things) to profile an application running in production with (almost) zero overhead (without shutting it down or any preparation whatsoever).&lt;/p&gt;
&lt;p&gt;&lt;a href=&quot;http://jboss.org/jbossBlog/blog/bburke/?permalink=7EBA719FA8D7C246482F5E30A5DEDFB1.txt&quot;&gt;Bill Burke&lt;/a&gt; and &lt;a href=&quot;http://www.almaer.com/blog/archives/000833.html&quot;&gt;Dion Almaer&lt;/a&gt; has already blogged about it.&lt;/p&gt;
&lt;p&gt;Go and check it out. It is free (shipped with JRockit).&lt;/p&gt;</content>
 </entry>
 
 <entry>
   <title>AOSD.05 has started</title>
   <link href="http://jboner.github.com/2005/03/15/aosd05-has-started"/>
   <updated>2005-03-15T00:00:00+00:00</updated>
   <id>hhttp://jboner.github.com/2005/03/15/aosd05-has-started</id>
   <content type="html">&lt;h1&gt;
&lt;p&gt;&lt;span class=&quot;caps&quot;&gt;AOSD&lt;/span&gt;.05 has started&lt;/p&gt;
&lt;/h1&gt;
&lt;p&gt;&lt;a href=&quot;http://aosd.net/2005/index.php&quot;&gt;&lt;span class=&quot;caps&quot;&gt;AOSD&lt;/span&gt; 2005&lt;/a&gt; has just started. I am excited, and have high expectations since last years conference was a real hit, and this year we have a separate industry track with many interesting talks coming up, f.e. Grady Booch is doing a keynote.&lt;/p&gt;
&lt;p&gt;I will be giving a talk on AspectWerkz 2 and the road to AspectJ 5 at the &lt;span class=&quot;caps&quot;&gt;AOSD&lt;/span&gt; 2005 conference hosted at the Intercontinental hotel in Chicago.&lt;/p&gt;
&lt;p&gt;If you are in the area, please drop in and say hi.&lt;/p&gt;
&lt;p&gt;If you are interested you can read the abstract etc. &lt;a href=&quot;http://aosd.net/2005/industry/talk3.php&quot;&gt;here&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;For those who can&amp;#8217;t make it, I will make the slides available online after the conference.&lt;/p&gt;
&lt;p&gt;Hope to see you there&amp;#8230;&lt;/p&gt;</content>
 </entry>
 
 <entry>
   <title>Interception Done Right</title>
   <link href="http://jboner.github.com/2005/01/06/interception-aop-finally-true"/>
   <updated>2005-01-06T00:00:00+00:00</updated>
   <id>hhttp://jboner.github.com/2005/01/06/interception-aop-finally-true</id>
   <content type="html">&lt;h1&gt;
&lt;p&gt;Interception Done Right&lt;/p&gt;
&lt;/h1&gt;
&lt;p&gt;
&lt;p&gt;One of the new features in the &lt;a href=&quot;http://aspectwerkz.codehaus.org/index.html&quot;&gt;AspectWerkz 2&lt;/a&gt; architecture is that it comes with a full-blown interception framework that allows per instance programmatic deployment with most of  the &lt;span class=&quot;caps&quot;&gt;AOP&lt;/span&gt; semantics preserved. You can make use of this using both the regular load-time weaving or the AW Proxy (that I blogged about &lt;a href=&quot;http://blogs.codehaus.org/people/jboner/archives/000914_awproxy_proxy_on_steroids.html&quot;&gt;here&lt;/a&gt;)&lt;/p&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;p&gt;In short you will get: &lt;br /&gt;
&lt;br /&gt;
Per instance programmatic deployment for &lt;i&gt;before&lt;/i&gt;, &lt;i&gt;around&lt;/i&gt;, &lt;i&gt;after&lt;/i&gt;, &lt;i&gt;after finally&lt;/i&gt; and &lt;i&gt;after throwing&lt;/i&gt; advice types for &lt;i&gt;call&lt;/i&gt;, &lt;i&gt;execution&lt;/i&gt;, &lt;i&gt;set&lt;/i&gt; and &lt;i&gt;get&lt;/i&gt; pointcuts as well as the expressiveness of the AspectWerkz&amp;#8217; pointcut pattern language all wrapped up in a very simple and intuitive &lt;span class=&quot;caps&quot;&gt;API&lt;/span&gt;.&lt;/p&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;pre&gt;
    POJO pojo = new POJO();

    // adds tracing to all methods in the &#39;pojo&#39; instance
    ((Advisable) pojo).aw$addAdvice(
        &quot;* *.*(..)&quot;,
        new BeforeAdvice() {
            public Object invoke(JoinPoint jp) {
                System.out.println(&quot;Entering: &quot; + jp.getSignature().toString());
            }
        }
    );
&lt;/pre&gt;
&lt;/p&gt;
&lt;h3&gt;The Advisable interface&lt;/h3&gt;
&lt;p&gt;
&lt;p&gt;From a users perspective pretty much all you need to know about is in the &lt;tt&gt;Advisable&lt;/tt&gt; interface. This interface is added to all the classes that you want to do per instance runtime deployment at. This interface is added to your classes by the framework, but more on that later.&lt;/p&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;p&gt;The &lt;tt&gt;Advisable&lt;/tt&gt; interface basically has two methods that are of interest to you:&lt;br /&gt;
&lt;ul&gt;&lt;br /&gt;
    &lt;li&gt;&lt;br /&gt;
    &lt;tt&gt;void aw_addAdvice(String memberPattern, Advice advice)&lt;/tt&gt; &amp;#8211; this methods allows you to add an &lt;tt&gt;Advice&lt;/tt&gt; instance to the members specified by the &lt;tt&gt;memberPattern&lt;/tt&gt; argument&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;tt&gt;void aw_removeAdvice(String memberPattern, Class adviceClass)&lt;/tt&gt; &amp;#8211; this methods allows you to remove the last advice (that was added last) of the type specified in the &lt;tt&gt;adviceClass&lt;/tt&gt; argument from from the list of aspects applied to the members specified by the &lt;tt&gt;memberPattern&lt;/tt&gt; argument
&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The funny prefix &lt;tt&gt;aw_&lt;/tt&gt; is just to minimize method clashes since these methods are added to your classes on-the-fly.&lt;/p&gt;
&lt;/p&gt;
&lt;h3&gt;The different Advice interfaces&lt;/h3&gt;
&lt;p&gt;
&lt;p&gt;The intercept framework supports all main types of advice defined in &lt;span class=&quot;caps&quot;&gt;AOP&lt;/span&gt; today:&lt;br /&gt;
&lt;ul&gt;&lt;br /&gt;
    &lt;li&gt;&lt;br /&gt;
    &lt;tt&gt;AroundAdvice&lt;/tt&gt; &amp;#8211; works like a regular interceptor and is invoked &amp;#8220;around&amp;#8221; or &amp;#8220;instead-of&amp;#8221; the target method invocation or field access/modification.&lt;br /&gt;
    &lt;br /&gt;
    It has one method you need to implement which has the following signature:&lt;br /&gt;
    &lt;br /&gt;
    &lt;tt&gt;Object invoke(JoinPoint jp)&lt;/tt&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;tt&gt;BeforeAdvice&lt;/tt&gt; &amp;#8211; is invoked before the actual member invocation
&lt;br /&gt;
It has one method you need to implement which has the following signature:
&lt;br /&gt;
&lt;tt&gt;Object invoke(JoinPoint jp)&lt;/tt&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;tt&gt;AfterAdvice&lt;/tt&gt; &amp;#8211; is invoked after the actual member invocation, is invoked both if the method returns normally or with an exception. E.g. can be seen as being invoked in the &lt;tt&gt;finally&lt;/tt&gt; block.
&lt;br /&gt;
jt has one method you need to implement which has the following signature:
&lt;br /&gt;
&lt;tt&gt;Object invoke(JoinPoint jp)&lt;/tt&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;tt&gt;AfterReturning&lt;/tt&gt; &amp;#8211; is invoked after the actual method has returned normally
&lt;br /&gt;
It has one method you need to implement which has the following signature:
&lt;br /&gt;
&lt;tt&gt;Object invoke(JoinPoint jp, Object returnValue)&lt;/tt&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;tt&gt;AfterThrowingAdvice&lt;/tt&gt; &amp;#8211; is invoked after the actual method has returned with an exception
&lt;br /&gt;
It has one method you need to implement which has the following signature:
&lt;br /&gt;
&lt;tt&gt;Object invoke(JoinPoint jp, Throwable exception)&lt;/tt&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;As you can see, all of these methods takes a &lt;tt&gt;JoinPoint&lt;/tt&gt; instance as a parameter. This class contains f.e. contextual information bout the join point (member) we are executing before/after/around. Such as caller and callee instances and types, argument values and types etc. You can also see that some of the advice takes an optional parameter which provides direct access to the return value or the exception instance.&lt;/p&gt;
&lt;/p&gt;
&lt;h3&gt;Preparing your application&lt;/h3&gt;
&lt;p&gt;
&lt;p&gt;To make this work you finally need to tell AspectWerkz which classes you want to make &amp;#8220;advisable&amp;#8221; and to which extent. This is done in the &lt;tt&gt;&lt;span class=&quot;caps&quot;&gt;META&lt;/span&gt;-&lt;span class=&quot;caps&quot;&gt;INF&lt;/span&gt;/aop.xml&lt;/tt&gt; deployment descriptor in which you have to make use of the &lt;tt&gt;&amp;lt;advisable &amp;#8230;/&amp;gt;&lt;/tt&gt; element. This element has two attributes:&lt;br /&gt;
&lt;ul&gt;&lt;br /&gt;
    &lt;li&gt;&lt;br /&gt;
    &lt;tt&gt;expresssion&lt;/tt&gt; &amp;#8211; here you specify the type pattern that picks out the classes that you want to make &amp;#8220;advisable&amp;#8221; and this is done by defining a &lt;tt&gt;within(&amp;lt;&lt;span class=&quot;caps&quot;&gt;TYPE&lt;/span&gt; &lt;span class=&quot;caps&quot;&gt;PATTERN&lt;/span&gt;&amp;gt;)&lt;/tt&gt; pointcut expression&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;tt&gt;pointcut-type&lt;/tt&gt; &amp;#8211; here you defined which pointcut semantics you want the added advice to follow
&lt;br /&gt;
Valid types are:
&lt;ul&gt;
&lt;li&gt;
&lt;tt&gt;call&lt;/tt&gt; &amp;#8211; the advice added to a method will be invoked on the caller side of the method invocation (if you think client-server then it is executing on the client side)
&lt;/li&gt;
&lt;li&gt;
&lt;tt&gt;execution&lt;/tt&gt; &amp;#8211; the advice added to a method will be invoked on the execution side of the method invocation
&lt;/li&gt;
&lt;li&gt;
&lt;tt&gt;set&lt;/tt&gt; &amp;#8211; the advice added to a field will be invoked when a field is modified
&lt;/li&gt;
&lt;li&gt;
&lt;tt&gt;get&lt;/tt&gt; &amp;#8211; the advice added to a field will be invoked when a field is accessed
&lt;/li&gt;
&lt;li&gt;
&lt;tt&gt;all&lt;/tt&gt; &amp;#8211; a combination of all the above pointcut types
&lt;/li&gt;
Or any combination of these separated by a &lt;i&gt;|&lt;/i&gt; character.
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;p&gt;Example:&lt;br /&gt;
&lt;pre&gt;&lt;br /&gt;
    &amp;lt;aspectwerkz&amp;gt;&lt;br /&gt;
        &amp;lt;system id=&amp;#8220;intercept-sample&amp;#8221;&amp;gt;&lt;br /&gt;
            &amp;lt;advisable expression=&amp;#8220;within(my.application.domain.*)&amp;#8221; pointcut-type=&amp;#8220;call|set|get&amp;#8221;/&amp;gt;&lt;br /&gt;
        &amp;lt;/system&amp;gt;&lt;br /&gt;
    &amp;lt;/aspectwerkz&amp;gt;&lt;br /&gt;
&lt;/pre&gt;&lt;/p&gt;
&lt;/p&gt;
&lt;h3&gt;Bringing it all together&lt;/h3&gt;
&lt;p&gt;
&lt;p&gt;So now we have talked about the &lt;tt&gt;Advisable&lt;/tt&gt; interface that we can use to add the advice we want to a specific instance. We have talked about the different &lt;tt&gt;Advice&lt;/tt&gt; interfaces and their differences. Finally we talked about how we tell the AspectWerkz container which classes it should make &amp;#8220;advisable&amp;#8221; and how it should treat them. Let&amp;#8217;s now try to bring it all together in a small example.&lt;/p&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;p&gt;In this example we are taking a regular &lt;span class=&quot;caps&quot;&gt;POJO&lt;/span&gt; and we are adding an advice that will be applied to all methods that are annotated with the annotation &lt;tt&gt;@OneWay&lt;/tt&gt; and turn the otherwise synchronous invocations into ansynchronous invocations.  &lt;br /&gt;
&lt;pre&gt;&lt;br /&gt;
    &amp;#8230;&lt;/p&gt;
&lt;span class=&quot;caps&quot;&gt;POJO&lt;/span&gt; pojo = new &lt;span class=&quot;caps&quot;&gt;POJO&lt;/span&gt;();
((Advisable) pojo).aw_addAdvice(
&amp;#8220;@OneWay * &lt;strong&gt;.&lt;/strong&gt;(..)&amp;#8221;,
new AroundAdvice() {
private Executor m_threadPool = Executors.newCachedThreadPool();
public Object invoke(JoinPoint jp) throws Throwable {
m_threadPool.execute(
new Runnable() {
public void run() {
try {
// proceed with the invocation in a new thread
jp.proceed();
} catch (Throwable e) {
throw new RuntimeException(e);
}
}
}
);
return null;
}
}
);
&amp;#8230;
&lt;p&gt;&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;The &lt;tt&gt;&lt;span class=&quot;caps&quot;&gt;META&lt;/span&gt;-&lt;span class=&quot;caps&quot;&gt;INF&lt;/span&gt;/aop.xml&lt;/tt&gt; file looks like this:&lt;br /&gt;
&lt;pre&gt;&lt;br /&gt;
    &amp;lt;aspectwerkz&amp;gt;&lt;br /&gt;
        &amp;lt;system id=&amp;#8220;intercept-sample&amp;#8221;&amp;gt;&lt;br /&gt;
            &amp;lt;advisable expression=&amp;#8220;within(sample.intercept.&lt;span class=&quot;caps&quot;&gt;POJO&lt;/span&gt;)&amp;#8221; pointcut-type=&amp;#8220;call&amp;#8221;/&amp;gt;&lt;br /&gt;
        &amp;lt;/system&amp;gt;&lt;br /&gt;
    &amp;lt;/aspectwerkz&amp;gt;&lt;br /&gt;
&lt;/pre&gt;&lt;/p&gt;
&lt;/p&gt;
&lt;h3&gt;Resources&lt;/h3&gt;
&lt;p&gt;
&lt;p&gt;I have not written any specific sample application for this article but if you want you can look at and run the tests in the AspectWerkz distribution. You can download the distribution &lt;a href=&quot;http://aspectwerkz.codehaus.org/releases.html&quot;&gt;here&lt;/a&gt;. The tests are in &lt;tt&gt;./src/test/test/intercept/*&lt;/tt&gt; directory and you can run them (along with all the other tests) by invoking &lt;tt&gt;ant test&lt;/tt&gt; when standing in the AspectWerkz distribution&amp;#8217;s root dir.&lt;/p&gt;
&lt;/p&gt;
&lt;p&gt;Enjoy&lt;/p&gt;</content>
 </entry>
 
 <entry>
   <title>Aspect hot deployment in practice: Implementing a JMX monitoring aspect</title>
   <link href="http://jboner.github.com/2004/12/15/aspect-hot-deployment-in-practice-implementing-a-jmx-monitoring-aspect"/>
   <updated>2004-12-15T00:00:00+00:00</updated>
   <id>hhttp://jboner.github.com/2004/12/15/aspect-hot-deployment-in-practice-implementing-a-jmx-monitoring-aspect</id>
   <content type="html">&lt;h1&gt;
&lt;p&gt;Aspect hot deployment in practice: Implementing a &lt;span class=&quot;caps&quot;&gt;JMX&lt;/span&gt; monitoring aspect&lt;/p&gt;
&lt;/h1&gt;
&lt;p&gt;
&lt;p&gt;One of the new features in the &lt;a href=&quot;http://aspectwerkz.codehaus.org/index.html&quot;&gt;AspectWerkz 2&lt;/a&gt; architecture is the ability to deploy and undeploy aspects at runtime, e.g. to do &amp;#8220;hot&amp;#8221; deployment and undeployment.&lt;/p&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;p&gt;The implementation is based on &lt;i&gt;HotSwap&lt;/i&gt; class redefinition and handles &amp;#8216;change sets&amp;#8217; which ensures that the changes to each single join point is atomic. What this means in practice is that if you for example are deploying an aspect which has one before and one after advice, both advising the &lt;b&gt;same&lt;/b&gt; join point (method/field etc.) then either &lt;b&gt;both&lt;/b&gt; of them will be added &lt;b&gt;or none&lt;/b&gt;.&lt;/p&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;p&gt;The &lt;span class=&quot;caps&quot;&gt;API&lt;/span&gt; exposed to the user is very simple and straight forward to use. Everything is handled by the &lt;tt&gt;Deployer&lt;/tt&gt; class, which provides the following &lt;span class=&quot;caps&quot;&gt;API&lt;/span&gt; for runtime deployment and undeployment of aspects:&lt;/p&gt;
&lt;pre&gt;
    DeploymentHandle deploy(Class aspect)
    DeploymentHandle deploy(Class aspect, ClassLoader deployLoader)
    DeploymentHandle deploy(Class aspect, DeploymentScope scope)
    DeploymentHandle deploy(Class aspect, DeploymentScope scope, ClassLoader deployLoader)
    DeploymentHandle deploy(Class aspect, String xmlDef)
    DeploymentHandle deploy(Class aspect, String xmlDef, ClassLoader deployLoader)
    DeploymentHandle deploy(Class aspect, String xmlDef, DeploymentScope scope)
    DeploymentHandle deploy(Class aspect, String xmlDef, DeploymentScope scope, ClassLoader deployLoader)
    void undeploy(Class aspect)
    void undeploy(Class aspect, ClassLoader loader)
    void undeploy(DeploymentHandle deploymentHandle)
&lt;/pre&gt;
&lt;p&gt;As you can see you can for example choose to deploy an annotation defined aspect, e.g. a regular Java5 class with AspectWerkz defined annotations (or a regular Java 1.3/1.4 class with AspectWerkz defined JavaDoc annotations, compiled with AspectWerkz&amp;#8217;s JavaDoc annotation compiler):&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;in the context classloader (just specify the aspect class)&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;in an arbitrary class loader (this option is most useful for application server vendors that can garantuee that all the dependecies can be resolved)&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;within a specific &lt;tt&gt;DeploymentScope&lt;/tt&gt; (more about this in a minute)&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;You can read more about how you can use the &lt;tt&gt;DeploymentHandle&lt;/tt&gt; (that is returned from each of these methods) &lt;a href=&quot;http://aspectwerkz.codehaus.org/dynamic_aop.html&quot;&gt;here&lt;/a&gt;.&lt;/p&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;p&gt;You also deploy a class without any annotation definition and pass in the definition in &lt;span class=&quot;caps&quot;&gt;XML&lt;/span&gt; format. Here you pass in a string containing the &lt;tt&gt;&amp;lt;aspect&amp;gt;&amp;#8230;&amp;lt;/aspect&amp;gt;&lt;/tt&gt; part of the regular &lt;tt&gt;aop.xml&lt;/tt&gt; definition (see the &lt;a href=&quot;http://aspectwerkz.codehaus.org/xml_definition.html&quot;&gt;online docs&lt;/a&gt; for details).&lt;/p&gt;
&lt;/p&gt;
&lt;h2&gt;Use-case: Implementing a &lt;span class=&quot;caps&quot;&gt;JMX&lt;/span&gt; monitoring aspect&lt;/h2&gt;
&lt;p&gt;
&lt;p&gt;Now let&amp;#8217;s take a look at a concrete example on how to hot deploy an aspect that can do some monitoring for us, report that to an &lt;span class=&quot;caps&quot;&gt;JMX&lt;/span&gt; MBean and then undeploy it when the monitoring is done.&lt;/p&gt;
&lt;/p&gt;
&lt;h3&gt;Writing the aspect&lt;/h3&gt;
&lt;p&gt;
&lt;p&gt;So first, here is the (main parts of the) &lt;span class=&quot;caps&quot;&gt;JMX&lt;/span&gt; monitoring aspect:&lt;br /&gt;
&lt;pre&gt;&lt;br /&gt;
    @Aspect&lt;br /&gt;
    public class ResponseTimeAspect {&lt;/p&gt;
&amp;#8230; // methods for registering the MBean lazily, member fields etc.
// the &amp;#8220;invocationsToMonitor&amp;#8221; pointcut will be defined at deployment time
// in the aop.xml file
@Around(&amp;#8220;invocationsToMonitor&amp;#8221;)
public Object monitor(StaticJoinPoint jp) throws Throwable {
Signature signature = jp.getSignature();
long tsStart = System.currentTimeMillis();
Object result = null;
try {
result = jp.proceed();
} finally {
// note: this code is using a &lt;span class=&quot;caps&quot;&gt;JMX&lt;/span&gt; helper method and
// we will have one MBean per jointpoint signature (i.e. method, field, etc.)
long tsElapsed = System.currentTimeMillis() &amp;#8211; tsStart;
ObjectInstance mbeanI = registerMBean(signature);
if (mbeanI != null) {
m_mbeanServer.invoke(
mbeanI.getObjectName(),
&amp;#8220;update&amp;#8221;,
new Object[]{new Long(tsElapsed)},
new String[]{long.class.getName()}
);
}
}
return result;
}
public static void enableMonitoring(String pointcut) { &amp;#8230; }
public static void disableMonitoring() { &amp;#8230; }
}
&lt;p&gt;&lt;/pre&gt;&lt;br /&gt;
As you can see, this aspect is defined using Java5 annotations, but if you are using Java 1.3/1.4 you can define the annnotations using JavaDoc (just put the annotation exactly as it is in the JavaDoc for the method/class) and run &lt;tt&gt;AnnotationC&lt;/tt&gt; on the class. Then the rest in this article should stay the same. You can read more about the &lt;tt&gt;AnnotationC&lt;/tt&gt; compiler &lt;a href=&quot;http://aspectwerkz.codehaus.org/attribute_definition.html#Aspect_annotation_compilation&quot;&gt;here&lt;/a&gt;.&lt;/p&gt;
&lt;/p&gt;
&lt;h3&gt;Hot deploy and undeploy the aspect&lt;/h3&gt;
&lt;p&gt;
&lt;p&gt;As you can see we have added two static methods to the aspect, these will be used to enable and disable the response time aspect. E.g. deploy it and undeploy it.&lt;/p&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;p&gt;First we have the &lt;tt&gt;enableMonitoring&lt;/tt&gt; method, which enables monitoring in our application, e.g. deploys the monitoring aspect. This method can be invoked at &lt;b&gt;any&lt;/b&gt; point in time, all threading issues etc. are handled by the underlying implementation. Here we make use of the &lt;span class=&quot;caps&quot;&gt;XML&lt;/span&gt; definition to resolve the annotation definition in the aspects by defining the &amp;#8220;abstract pointcut&amp;#8221; that we have bound the advice to (the &lt;i&gt;invocationsToMonitor&lt;/i&gt; pointcut).&lt;/p&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;pre&gt;
    public static void enableMonitoring(String pointcut) {
        String xmlDef = &quot;&amp;amp;lt;aspect&amp;amp;gt;&amp;amp;lt;pointcut name=&#39;invocationsToMonitor&#39; expression=&#39;&quot; + pointcut + &quot;&#39;/&amp;amp;gt;&amp;amp;lt;/aspect&amp;amp;gt;&quot;;
        Deployer.deploy(ResponseTimeAspect.class, xmlDef);
    } 
&lt;/pre&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;p&gt;Second we have the &lt;tt&gt;disableMonitoring&lt;/tt&gt; method which disables monitoring for us, e.g. undeploys the aspect from &lt;b&gt;all&lt;/b&gt; the join points where it had been defined (see the concept of &lt;a href=&quot;http://aspectwerkz.codehaus.org/dynamic_aop.html&quot;&gt;&lt;tt&gt;DeploymentHandle&lt;/tt&gt;&lt;/a&gt;s for a more fine grained undeployment &lt;span class=&quot;caps&quot;&gt;API&lt;/span&gt;). &lt;br /&gt;
&lt;pre&gt;&lt;br /&gt;
    public static void disableMonitoring() {&lt;br /&gt;
        Deployer.undeploy(ResponseTimeAspect.class);&lt;br /&gt;
    } &lt;br /&gt;
&lt;/pre&gt;&lt;/p&gt;
&lt;/p&gt;
&lt;h3&gt;Use the aspect to monitor &lt;span class=&quot;caps&quot;&gt;JDBC&lt;/span&gt; &lt;span class=&quot;caps&quot;&gt;SQL&lt;/span&gt; statements&lt;/h3&gt;
&lt;p&gt;
&lt;p&gt;This means that in the user code, all we need to do to enable and disabling this aspect is to invoke these two methods. Here we will monitor the executions of all &lt;span class=&quot;caps&quot;&gt;JDBC&lt;/span&gt; statements:&lt;br /&gt;
&lt;pre&gt;&lt;br /&gt;
    public static void trackResponseTimeOfSqlQueries() {&lt;br /&gt;
        ResponseTimeAspect.enableMonitoring(&amp;#8220;call(* java.sql.Statement+.execute*(..))&amp;#8221;);&lt;/p&gt;
&amp;#8230; // wait a while to do a recording
ResponseTimeAspect.disableMonitoring();
}
&lt;p&gt;&lt;/pre&gt;&lt;/p&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;p&gt;What this pointcut expression (&lt;tt&gt;call(* java.sql.Statement+.execute*(..))&lt;/tt&gt;) actually means is: &lt;br /&gt;
&lt;br /&gt;
&lt;ul&gt;&lt;br /&gt;
    &lt;li&gt;&lt;br /&gt;
Pick out all method calls (&lt;tt&gt;call(..)&lt;/tt&gt;)&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;to alll methods that has a name that starts with &lt;tt&gt;execute&lt;/tt&gt; (&lt;tt&gt;execute*&lt;/tt&gt;)&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;that can have any parameter types (&lt;tt&gt;(..)&lt;/tt&gt;) or return type (&lt;tt&gt;*&lt;/tt&gt;)&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;that are in a concrete class that implements the &lt;tt&gt;java.sql.Statement&lt;/tt&gt; interface (&lt;tt&gt;java.sql.Statement+&lt;/tt&gt;)&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;By basing the pointcut expression on an interface we can be igonrant of how the actual &lt;span class=&quot;caps&quot;&gt;JDBC&lt;/span&gt; driver is implemented (concrete subclasses etc.).&lt;/p&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;p&gt;Note: we could in this specific use-case benefit from retrieving and storing the parameter value to the &lt;tt&gt;execute*(..)&lt;/tt&gt; methods, since it is the actual &lt;i&gt;&lt;span class=&quot;caps&quot;&gt;SQL&lt;/span&gt; query&lt;/i&gt; that we are monitoring the execution time of. This could be done really easy but is out of scope for this article.&lt;/p&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;p&gt;So now after doing some monitoring you should be able to easily see the data in any &lt;span class=&quot;caps&quot;&gt;JMX&lt;/span&gt; console. For example hook in &lt;i&gt;JConsole&lt;/i&gt; which you can read more about in &lt;a href=&quot;http://www.onjava.com/pub/a/onjava/2004/09/29/tigerjmx.html&quot;&gt;this article&lt;/a&gt;.&lt;/p&gt;
&lt;/p&gt;
&lt;h3&gt;Define a deployment scope&lt;/h3&gt;
&lt;p&gt;
&lt;p&gt;Finally, to get this to work in the general case we need to define a &lt;i&gt;deployment scope&lt;/i&gt; which defines the set potential join points that we might want to monitor at runtime.&lt;/p&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;p&gt;This construct gives you (as a application developer or vendor) control over which join points are exposed to the hot deployment &lt;span class=&quot;caps&quot;&gt;API&lt;/span&gt; i.e. helps to prevent usage of the hot deployment facilities in specific areas of the application.&lt;/p&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;p&gt;This can be done either in the &lt;tt&gt;&lt;span class=&quot;caps&quot;&gt;META&lt;/span&gt;-&lt;span class=&quot;caps&quot;&gt;INF&lt;/span&gt;/aop.xml&lt;/tt&gt; file or using annotations in the aspect class. In this case we want to make the aspect generic so we will define it in the external &lt;span class=&quot;caps&quot;&gt;XML&lt;/span&gt; definition:&lt;br /&gt;
&lt;pre&gt;&lt;br /&gt;
    &amp;lt;aspectwerkz&amp;gt;&lt;br /&gt;
        &amp;lt;system id=&amp;#8220;monitoring&amp;#8221;&amp;gt;&lt;br /&gt;
            &amp;lt;deployment-scope name=&amp;#8220;response_time&amp;#8221; expression=&amp;#8220;call(* sample.deployment..&lt;strong&gt;.&lt;/strong&gt;(..))&amp;#8221;/&amp;gt;&lt;br /&gt;
        &amp;lt;/system&amp;gt;&lt;br /&gt;
    &amp;lt;/aspectwerkz&amp;gt;&lt;br /&gt;
&lt;/pre&gt;&lt;/p&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;p&gt;Here we have defined a deployment scope that picks out all method calls in our sample application, which means that we can safely deploy our &lt;tt&gt;ResponseTimeAspect&lt;/tt&gt; at these points. (We could have added something like &lt;tt&gt;&lt;span class=&quot;caps&quot;&gt;AND&lt;/span&gt; within(package..*)&lt;/tt&gt; to the expression if we wanted to narrow down the scope.)&lt;/p&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;p&gt;In the code above we have an implicit contract that says that we can not define the aspect to be deployed outside a deployment scope. If we do not define a pointcut that is wider than this it is all fine. But we can now use this &lt;i&gt;deployment scope&lt;/i&gt; to ensure that we only deploy the aspect at valid points. To do that we need to retrieve a handle to the scope like this (can be done at any point):  &lt;br /&gt;
&lt;pre&gt;&lt;br /&gt;
    DeploymentScope scope = SystemDefinition.getDefinitionFor(loader, systemId).getDeploymentScope(&amp;#8220;response_time&amp;#8221;);&lt;br /&gt;
&lt;/pre&gt;&lt;/p&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;p&gt;Now we can use the &lt;tt&gt;DeploymentScope&lt;/tt&gt; handle to make a more explicit deployment (e.g. knowing exactly the points where our aspect will be applied): &lt;br /&gt;
&lt;pre&gt;&lt;br /&gt;
    Deployer.deploy(ResponseTimeAspect.class, xmlDef, scope);&lt;br /&gt;
&lt;/pre&gt;&lt;/p&gt;
&lt;/p&gt;
&lt;h3&gt;Notes on the impact of the deployment scope preparation&lt;/h3&gt;
&lt;p&gt;
&lt;p&gt;The preparation made by the &lt;i&gt;deployment scope&lt;/i&gt; construct means in practice that we are simply adding a level of indirection by adding a call to a &lt;tt&gt;public static final&lt;/tt&gt; method that redirects to the target join point (method, field, constructor). This method will be inlined by all modern JVMs.&lt;/p&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;p&gt;When we do undeploy of an aspect, if this aspect is the last one that affects the join point, then the class&amp;#8217; bytecode will be reverted to the same state it had before any aspect was deployed.&lt;/p&gt;
&lt;/p&gt;
&lt;h3&gt;Resources&lt;/h3&gt;
&lt;h4&gt;More info about the &lt;span class=&quot;caps&quot;&gt;JMX&lt;/span&gt; ResponseTimeAspect&lt;/h4&gt;
&lt;p&gt;
&lt;p&gt;The code for the monitoring aspect is based on the implementation of the &lt;tt&gt;ResponseTimeAspect&lt;/tt&gt; in the &lt;a href=&quot;http://docs.codehaus.org/display/AWARE&quot;&gt;AWare Project&lt;/a&gt;. The only difference is that in this example I am defining it using Java5 annotations and I have added the two static methods for doing the deployment and undeployment. You can read more about this aspect &lt;a href=&quot;http://docs.codehaus.org/display/AWARE/ResponseTimeAspect&quot;&gt;here&lt;/a&gt; and the source code for it can be found &lt;a href=&quot;https://aspectwerkz-aware.dev.java.net/source/browse/aspectwerkz-aware/components/jmx/main/org/codehaus/aware/jmx/#dirlist&quot;&gt;here&lt;/a&gt;.&lt;/p&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;p&gt;You can also check out the complete AWare project which has some tests for the &lt;span class=&quot;caps&quot;&gt;JMX&lt;/span&gt; module. Info about how to check out the sources can be found &lt;a href=&quot;https://aspectwerkz-aware.dev.java.net/source/browse/aspectwerkz-aware/&quot;&gt;here&lt;/a&gt;.&lt;/p&gt;
&lt;/p&gt;
&lt;h4&gt;More info about the Deployer Module&lt;/h4&gt;
&lt;p&gt;
&lt;p&gt;I have not written any specific sample application for this article but if you want you can look at and run the tests for the deployer module in the AspectWerkz distribution. You can download the distribution &lt;a href=&quot;http://aspectwerkz.codehaus.org/releases.html&quot;&gt;here&lt;/a&gt;. The tests are in &lt;tt&gt;./src/jdk15/test&lt;/tt&gt; directory and you can run them by invoking &lt;tt&gt;ant test:jdk15&lt;/tt&gt; when standing in the AspectWerkz distribution&amp;#8217;s root dir.&lt;/p&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;p&gt;The &lt;tt&gt;aspectwerkz*.jar&lt;/tt&gt; jars and the dependency jars are in the &lt;tt&gt;./lib&lt;/tt&gt; folder in the AspectWerkz distribution.&lt;/p&gt;
&lt;/p&gt;
&lt;p&gt;Enjoy.&lt;/p&gt;</content>
 </entry>
 
 <entry>
   <title>CGLIB with Native Support for AspectWerkz</title>
   <link href="http://jboner.github.com/2004/12/13/cglib-proxies-are-doing-aspectwerkz"/>
   <updated>2004-12-13T00:00:00+00:00</updated>
   <id>hhttp://jboner.github.com/2004/12/13/cglib-proxies-are-doing-aspectwerkz</id>
   <content type="html">&lt;h1&gt;
&lt;p&gt;&lt;span class=&quot;caps&quot;&gt;CGLIB&lt;/span&gt; with Native Support for AspectWerkz&lt;/p&gt;
&lt;/h1&gt;
&lt;h3&gt;The idea&lt;/h3&gt;
&lt;p&gt;
&lt;p&gt;After implementing the &lt;a href=&quot;&quot;&gt;AW Proxy&lt;/a&gt; extension to &lt;a href=&quot;http://aspectwerkz.codehaus.org/&quot;&gt;AspectWerkz&lt;/a&gt; I figured, why not hook into the most widely used proxy implementation out there: &lt;a href=&quot;http://cglib.sourceforge.net/&quot;&gt;cglib&lt;/a&gt; and let AspectWerkz weave in the aspects that are around just before the proxy is returned to the user.&lt;/p&gt;
&lt;/p&gt;
&lt;h3&gt;The solution&lt;/h3&gt;
&lt;p&gt;
&lt;p&gt;So, this is all that this &lt;i&gt;cglib&lt;/i&gt; extension does: asks AspectWerkz if it has anything to say about the class that has just been proxied. If so, then let the AspectWerkz weaver do its job, e.g. weave in the aspects that are currently deployed (on the classpath), and when returns the proxy to the user.&lt;/p&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;p&gt;If no AspectWerkz aspects are found that has pointcuts that matches the class being proxied then nothing happens.&lt;/p&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;p&gt;All that was needed to make this work was to add 30 lines of code to &lt;b&gt;one&lt;/b&gt; single &lt;i&gt;cglib&lt;/i&gt; class.&lt;/p&gt;
&lt;/p&gt;
&lt;h3&gt;How do I use it?&lt;/h3&gt;
&lt;p&gt;
&lt;p&gt;This means that all you need to do to make &lt;b&gt;any&lt;/b&gt; &lt;i&gt;cglib&lt;/i&gt; based applications, and there are many: &lt;a href=&quot;http://geronimo.apache.org/&quot;&gt;Geronimo&lt;/a&gt;, &lt;a href=&quot;https://dynaop.dev.java.net/&quot;&gt;dynaop&lt;/a&gt;, &lt;a href=&quot;http://www.springframework.org/&quot;&gt;Spring &lt;span class=&quot;caps&quot;&gt;AOP&lt;/span&gt;&lt;/a&gt;, &lt;a href=&quot;http://www.hibernate.org/&quot;&gt;Hibernate&lt;/a&gt; etc., become &amp;#8220;aspectwerkz-aware&amp;#8221; is to &lt;b&gt;replace one single class&lt;/b&gt; in the &lt;tt&gt;cglib.jar&lt;/tt&gt; jar.&lt;/p&gt;
&lt;/p&gt;
&lt;p&gt;Or.&lt;/p&gt;
&lt;p&gt;
&lt;p&gt;If you can&amp;#8217;t or don&amp;#8217;t want to patch the &lt;tt&gt;cglib.jar&lt;/tt&gt; jar, you can just put the class &lt;b&gt;before&lt;/b&gt; the jar on the classpath.&lt;/p&gt;
&lt;/p&gt;
&lt;h3&gt;What is the impact on my existing code?&lt;/h3&gt;
&lt;p&gt;
&lt;p&gt;If no AspectWerkz aspects are found that has pointcuts that matches the class being proxied then nothing happens.&lt;br /&gt;
And if AspectWerkz is not available then the whole process is skipped. So basically, there is nothing to loose, it is pay as you go (or should I rather say: gain as you go ;-) ).&lt;/p&gt;
&lt;/p&gt;
&lt;h3&gt;How do I get it?&lt;/h3&gt;
&lt;p&gt;
&lt;p&gt;The single file you need can be found in the AspectWerkz RC2 distribution, which you can find &lt;a href=&quot;http://aspectwerkz.codehaus.org/releases.html&quot;&gt;here&lt;/a&gt;.&lt;/p&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;p&gt;The cglib extension is in the &lt;tt&gt;./src/cglib-ext&lt;/tt&gt; dir (in the root of the AspectWerkz distribution). So step into this directory and type &lt;tt&gt;ant dist&lt;/tt&gt; and it will compile the class and put it in a jar that is to be found in the &lt;tt&gt;./src/cglib-ext/target&lt;/tt&gt;. This jar only contains one single class file (which you either put in the &lt;tt&gt;cglib.jar&lt;/tt&gt; jar or make sure that it is before the jar on the classpath).&lt;/p&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;p&gt;In the &lt;tt&gt;./src/cglib-ext&lt;/tt&gt; dir you also have a sample that you can run. This sample is just a sample taken from the &lt;i&gt;cglib&lt;/i&gt; distribution, but it has a tracing aspect on the classpath, that is weaved into the proxy. You can run this test by invoking &lt;tt&gt;ant samples:trace&lt;/tt&gt;.&lt;/p&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;p&gt;Enjoy.&lt;/p&gt;
&lt;/p&gt;</content>
 </entry>
 
 <entry>
   <title>AWProxy: Proxy on steroids</title>
   <link href="http://jboner.github.com/2004/12/08/awproxy-proxy-on-steroids"/>
   <updated>2004-12-08T00:00:00+00:00</updated>
   <id>hhttp://jboner.github.com/2004/12/08/awproxy-proxy-on-steroids</id>
   <content type="html">&lt;h1&gt;
&lt;p&gt;AWProxy: Proxy on steroids&lt;/p&gt;
&lt;/h1&gt;
&lt;h3&gt;Summary&lt;/h3&gt;
&lt;p&gt;
&lt;p&gt;Proxies are an important piece in the J2EE puzzle. Since it offers a non-intrusive an more transparent way of weaving in advice/interceptors. However, non of the current proxy implementations utilizes all the rich semantics of &lt;span class=&quot;caps&quot;&gt;AOP&lt;/span&gt; (as defined by AspectJ), the expressiveness of a real pointcut pattern language and has the speed of statically compiled code.&lt;/p&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;p&gt;&lt;i&gt;AW Proxy&lt;/i&gt; is here to try to narrow this gap. It makes use of the rich semantics for &lt;span class=&quot;caps&quot;&gt;AOP&lt;/span&gt; in AspectWerkz and is very high-performant due to the use of static compilation (utilizing the AspectWerkz weaver). All wrapped up a very simple and intuitive &lt;span class=&quot;caps&quot;&gt;API&lt;/span&gt;.&lt;/p&gt;
&lt;/p&gt;
&lt;h3&gt;The need for proxies&lt;/h3&gt;
&lt;p&gt;
&lt;p&gt;The two arguments against load-time weaving that we hear the most are:&lt;br /&gt;
&lt;ul&gt;&lt;br /&gt;
    &lt;li&gt;that it is complex &amp;#8211; &lt;br /&gt;
    which is an argument that I don&amp;#8217;t understand, since all you need to do is to add one single VM option when starting up the VM&lt;/p&gt;
&lt;/li&gt;
    &lt;li&gt;that the transparency of proxies is hard to beat &amp;#8211; &lt;br /&gt;
    which is an argument that I can fully agree on    &lt;/li&gt;
&lt;/ul&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;p&gt;Adding the possibility of using &lt;a href=&quot;http://aspectwerkz.codehaus.org/index.html&quot;&gt;AspectWerkz&lt;/a&gt; together with proxies is something that &lt;a href=&quot;http://blogs.codehaus.org/people/avasseur/&quot;&gt;Alex&lt;/a&gt; and I have been talking about for a long time but never implemented due to lack of time. Well, this weekend I took the time and it was actually more simple than I thought.&lt;/p&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;p&gt;After a couple of hours coding (taking the &lt;a href=&quot;http://cglib.sourceforge.net/&quot;&gt;cglib&lt;/a&gt; route with its &amp;#8220;class proxy&amp;#8221;, e.g creating a new class on-the-fly that extends the target class and which methods and constructors delegates to the base class&amp;#8217; methods, but with the difference that they don&amp;#8217;t wrap the arguments but pass them on as they are, which gives a lot better performance), I had a working solution.&lt;/p&gt;
&lt;/p&gt;
&lt;h3&gt;Introducing AW Proxy&lt;/h3&gt;
&lt;p&gt;
&lt;p&gt;The benefit with using the AspectWerkz Proxies compared to the regular load time weaving is that there are no VM options. You simply create your proxy and before it is returned to you it is weaved with the aspects that happens to be available on the classpath at that time.&lt;/p&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;p&gt;The &lt;span class=&quot;caps&quot;&gt;API&lt;/span&gt; is pretty straight forward. All you need is in the &lt;br /&gt;
&lt;pre name=&quot;code&quot; class=&quot;java&quot;&gt;&lt;br /&gt;
    org.codehaus.aspectwerkz.proxy.Proxy&lt;br /&gt;
&lt;/pre&gt; &lt;br /&gt;
class which has the following &lt;span class=&quot;caps&quot;&gt;API&lt;/span&gt;:&lt;br /&gt;
&lt;pre name=&quot;code&quot; class=&quot;java&quot;&gt;&lt;br /&gt;
    // Returns a new proxy for the class specified&lt;br /&gt;
    public static Object newInstance(Class clazz);&lt;/p&gt;
// Returns a new proxy for the class specified, instantiates it by invoking the constructor
// with the argument list specified
public static Object newInstance(Class clazz, Class[] argumentTypes, Object[] argumentValues);
// Same as above, but with optional parameters for caching and making the proxy advisable
public static Object newInstance(Class clazz, boolean useCache, boolean makeAdvisable);
// Same as above, but with optional parameters for caching and making the proxy advisable
public static Object newInstance(Class clazz, Class[] argumentTypes, Object[] argumentValues,
boolean useCache, boolean makeAdvisable);
&lt;p&gt;&lt;/pre&gt;&lt;/p&gt;
&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
The &lt;tt&gt;makeAdvisable&lt;/tt&gt; argument decides if the proxy instance should be &lt;i&gt;advisable&lt;/i&gt;, e.g. be prepared for runtime programmatic deployment. More on this later in this article.
&lt;/li&gt;
&lt;li&gt;
The &lt;tt&gt;useCache&lt;/tt&gt; argument decides if the &lt;tt&gt;Proxy&lt;/tt&gt; should try to reuse an already compiled proxy class for the specific target class.
If you set this flag to &lt;tt&gt;true&lt;/tt&gt; then a cached class it is returned (if found) e.g. no new proxy class is compiled. This has both benefits and drawbacks. The benefits being that if there is a new aspect that has been deployed that advises a &lt;b&gt;new&lt;/b&gt; join point in the target class then these new join points will not be advised (but already advised join points will be redefined).
However if you set this flag to &lt;tt&gt;false&lt;/tt&gt; then a new proxy class will be compiled for each invocation of the method, which will make the invocation a bit slower but you will get a completely new proxy class that is advised at &lt;b&gt;all&lt;/b&gt; matched join points.
&lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;How to use the &lt;span class=&quot;caps&quot;&gt;API&lt;/span&gt;?&lt;/h3&gt;
&lt;p&gt;
&lt;p&gt;Here is a little example on how to use the &lt;tt&gt;Proxy&lt;/tt&gt; &lt;span class=&quot;caps&quot;&gt;API&lt;/span&gt;:&lt;br /&gt;
&lt;pre name=&quot;code&quot; class=&quot;java&quot;&gt;&lt;br /&gt;
    // creates and instantiates a new proxy for the class Target&lt;br /&gt;
    Target target = (Target) Proxy.newInstance(Target.class, false);&lt;br /&gt;
&lt;/pre&gt;&lt;/p&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;p&gt;That&amp;#8217;s it!&lt;/p&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;p&gt;This &lt;tt&gt;target&lt;/tt&gt; instance have now been advised with all the aspects that have been found on the classpath that matches the members of the &lt;tt&gt;Target&lt;/tt&gt; class.&lt;/p&gt;
&lt;/p&gt;
&lt;h3&gt;Utilizing programmatic runtime deployment&lt;/h3&gt;
&lt;p&gt;
&lt;p&gt;As I said before, when you create your proxy it will get automatically weaved with all the matching aspects that are found (on the classpath). This is most of the time what you want and need, but there might be cases when you want to add a specific feature, at runtime, to a specfific instance only. In these cases you need &lt;i&gt;programmatic runtime per instance deployment&lt;/i&gt;.&lt;/p&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;p&gt;One of the new features in the &lt;a href=&quot;http://aspectwerkz.codehaus.org/index.html&quot;&gt;AspectWerkz 2&lt;/a&gt; architecture is that it comes with a full-blown interception framework that allows per instance programmatic deployment with most of  the &lt;span class=&quot;caps&quot;&gt;AOP&lt;/span&gt; semantics preserved.&lt;/p&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;p&gt;In short you will get: &lt;br /&gt;
&lt;br /&gt;
Per instance programmatic deployment for &lt;i&gt;before&lt;/i&gt;, &lt;i&gt;around&lt;/i&gt;, &lt;i&gt;after&lt;/i&gt;, &lt;i&gt;after finally&lt;/i&gt; and &lt;i&gt;after throwing&lt;/i&gt; advice types for &lt;i&gt;call&lt;/i&gt;, &lt;i&gt;execution&lt;/i&gt;, &lt;i&gt;set&lt;/i&gt; and &lt;i&gt;get&lt;/i&gt; pointcuts as well as the expressiveness of the AspectWerkz&amp;#8217; pointcut pattern language all wrapped up in a very simple and intuitive &lt;span class=&quot;caps&quot;&gt;API&lt;/span&gt;.&lt;/p&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;pre name=&quot;code&quot; class=&quot;java&quot;&gt;
    POJO pojo = new POJO();

    // adds tracing to all methods in the &#39;pojo&#39; instance
    ((Advisable) pojo).aw_addAdvice(
        &quot;* *.*(..)&quot;,
        new BeforeAdvice() {
            public Object invoke(JoinPoint jp) {
                System.out.println(&quot;Entering: &quot; + jp.getSignature().toString());
            }
        }
    );
&lt;/pre&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;p&gt;The advice &amp;#8220;interceptors&amp;#8221; that are supported are:&lt;br /&gt;
&lt;ul&gt;&lt;br /&gt;
    &lt;li&gt;&lt;br /&gt;
    &lt;tt&gt;AroundAdvice&lt;/tt&gt; &amp;#8211; works like a regular interceptor and is invoked &amp;#8220;around&amp;#8221; or &amp;#8220;instead-of&amp;#8221; the target method invocation or field access/modification.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;tt&gt;BeforeAdvice&lt;/tt&gt; &amp;#8211; is invoked before the actual member invocation
&lt;/li&gt;
&lt;li&gt;
&lt;tt&gt;AfterAdvice&lt;/tt&gt; &amp;#8211; is invoked after the actual member invocation, is invoked both if the method returns normally or with an exception. E.g. can be seen as being invoked in the &lt;tt&gt;finally&lt;/tt&gt; block.
&lt;/li&gt;
&lt;li&gt;
&lt;tt&gt;AfterReturning&lt;/tt&gt; &amp;#8211; is invoked after the actual method has returned normally
&lt;/li&gt;
&lt;li&gt;
&lt;tt&gt;AfterThrowingAdvice&lt;/tt&gt; &amp;#8211; is invoked after the actual method has returned with an exception
&lt;/li&gt;
&lt;/ul&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;p&gt;Now we can combine use this feature together with the AW Proxies. All proxies that are created implements the &lt;tt&gt;Advisable&lt;/tt&gt; interface which makes it really easy and straightforward to use them together:&lt;/p&gt;
&lt;/p&gt;
&lt;h4&gt;Example&lt;/h4&gt;
&lt;p&gt;
&lt;p&gt;In this example we create and instantiate a new proxy for the class &lt;tt&gt;Target&lt;/tt&gt;, we set it to become &lt;i&gt;advisable&lt;/i&gt; (e.g. it will implement the &lt;tt&gt;Advisable&lt;/tt&gt; interface transparently), and then we add an &lt;i&gt;after returning&lt;/i&gt; advice to all methods that returns an instance of &lt;tt&gt;java.lang.String&lt;/tt&gt;.&lt;br /&gt;
&lt;pre name=&quot;code&quot; class=&quot;java&quot;&gt;&lt;/p&gt;
Advisable target = (Advisable) Proxy.newInstance(Target.class, true, true);
target.aw_addAdvice(
&amp;#8220;String &lt;strong&gt;.&lt;/strong&gt;(..)&amp;#8221;,
new AfterReturningAdvice() {
public void invoke(JoinPoint jp, Object returnValue) {
// do some stuff
}
}
);
&lt;p&gt;&lt;/pre&gt;&lt;/p&gt;
&lt;/p&gt;
&lt;h3&gt;Benefits&lt;/h3&gt;
&lt;h3&gt;Plain proxies with rich &lt;span class=&quot;caps&quot;&gt;AOP&lt;/span&gt; semantics&lt;/h3&gt;
&lt;p&gt;
&lt;p&gt;The benefits are, besides a less intrusive and more transparent approach, that you can utilize the rich semantics for &lt;span class=&quot;caps&quot;&gt;AOP&lt;/span&gt; (well, not all, see below for limitations) that are supported AspectWerkz. Such as, a rich pointcut expression language, before/after/around advice, deployment modules defined by the &lt;tt&gt;&lt;span class=&quot;caps&quot;&gt;META&lt;/span&gt;-&lt;span class=&quot;caps&quot;&gt;INF&lt;/span&gt;/aop.xml&lt;/tt&gt; file etc.&lt;/p&gt;
&lt;/p&gt;
&lt;h3&gt;Very high-performant&lt;/h3&gt;
&lt;p&gt;
&lt;p&gt;You will also get the full performance of the AspectWerkz 2 architecture.&lt;/p&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;p&gt;If you are interested details of the benchmark we made (and/or run it yourself) then you can read &lt;a href=&quot;http://docs.codehaus.org/display/AW/AOP+Benchmark&quot;&gt;this paper&lt;/a&gt;.&lt;/p&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;p&gt;But in short &lt;i&gt;AW Proxy&lt;/i&gt; is roughly:&lt;br /&gt;
&lt;ul&gt;&lt;br /&gt;
    &lt;li&gt;25 times faster than &lt;i&gt;Spring &lt;span class=&quot;caps&quot;&gt;AOP&lt;/span&gt;&lt;/i&gt; for before and after advice&lt;/p&gt;
&lt;/li&gt;
    &lt;li&gt;8 times faster than &lt;i&gt;Spring &lt;span class=&quot;caps&quot;&gt;AOP&lt;/span&gt;&lt;/i&gt; for around advice    &lt;/li&gt;
    &lt;li&gt;16 times faster than &lt;i&gt;dynaop&lt;/i&gt; for before and after advice    &lt;/li&gt;
    &lt;li&gt;5 times faster than &lt;i&gt;dynaop&lt;/i&gt; for around advice    &lt;/li&gt;
    &lt;li&gt;4 times faster than straight &lt;i&gt;cglib&lt;/i&gt; for before and after advice    &lt;/li&gt;
    &lt;li&gt;1.25 times faster than straight &lt;i&gt;cglib&lt;/i&gt; for around advice    &lt;/li&gt;
&lt;/ul&gt;
&lt;/p&gt;
&lt;h3&gt;Limitations&lt;/h3&gt;
&lt;p&gt;
&lt;p&gt;Since it is a proxy approach it only supports &lt;tt&gt;execution&lt;/tt&gt; pointcuts and can only advise methods and constructors that is non-private and non-final. E.g not &lt;tt&gt;call&lt;/tt&gt;, &lt;tt&gt;set&lt;/tt&gt;, &lt;tt&gt;get&lt;/tt&gt; or &lt;tt&gt;handler&lt;/tt&gt; pointcuts. Advice bound to these pointcut types will simply not affect the class being proxied.&lt;/p&gt;
&lt;/p&gt;
&lt;h3&gt;Resources&lt;/h3&gt;
&lt;p&gt;
&lt;p&gt;This new feature will be available in &lt;i&gt;AspectWerkz 2.0 RC2&lt;/i&gt; that will be available soon. Until then you can check out the sources from the &lt;a href=&quot;http://aspectwerkz.codehaus.org/cvs.html&quot;&gt;&lt;span class=&quot;caps&quot;&gt;CVS&lt;/span&gt;&lt;/a&gt; and build it yourself (by invoking &lt;tt&gt;ant dist&lt;/tt&gt;).&lt;/p&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;p&gt;There are some samples in the &lt;tt&gt;./src/samples/examples/proxy&lt;/tt&gt; dir in the distribution. These can be executed by invoking &lt;tt&gt;ant samples:proxy&lt;/tt&gt; from the command line.&lt;/p&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;p&gt;Enjoy.&lt;/p&gt;
&lt;/p&gt;</content>
 </entry>
 
 <entry>
   <title>Off to Vietnam for two months</title>
   <link href="http://jboner.github.com/2004/12/07/off-to-vietnam-for-two-months"/>
   <updated>2004-12-07T00:00:00+00:00</updated>
   <id>hhttp://jboner.github.com/2004/12/07/off-to-vietnam-for-two-months</id>
   <content type="html">&lt;h1&gt;
&lt;p&gt;Off to Vietnam for two months&lt;/p&gt;
&lt;/h1&gt;
&lt;p/&gt;
&lt;p&gt;I am travelling with with my wife and my one year old son in Vietnam for a couple of months. &lt;br /&gt;
&lt;p/&gt;&lt;br /&gt;
It&amp;#8217;s been a pretty hectic fall so I am looking forward resting my mind for a while. Although I am pretty sure that travelling in Vietnam with a little kid is not all relaxing (but for sure interesting :-) ). &lt;br /&gt;
&lt;p/&gt;&lt;br /&gt;
See you in a while.&lt;br /&gt;
&lt;p/&gt;&lt;br /&gt;
/Jonas&lt;br /&gt;
&lt;p/&gt;&lt;/p&gt;</content>
 </entry>
 
 <entry>
   <title>Spring and AspectWerkz: A Happy Marriage v2</title>
   <link href="http://jboner.github.com/2004/12/06/spring-and-aspectwerkz-a-happy-marriage-v2"/>
   <updated>2004-12-06T00:00:00+00:00</updated>
   <id>hhttp://jboner.github.com/2004/12/06/spring-and-aspectwerkz-a-happy-marriage-v2</id>
   <content type="html">&lt;h1&gt;
&lt;p&gt;Spring and AspectWerkz: A Happy Marriage v2&lt;/p&gt;
&lt;/h1&gt;
&lt;p&gt;
&lt;p&gt;The &lt;a href=&quot;http://aspectwerkz.codehaus.org/index.html&quot;&gt;AspectWerkz 2&lt;/a&gt; architecture has been designed to be an &lt;i&gt;Extensible Aspect Container&lt;/i&gt;, which can deploy and run arbitrary aspects. You can read more about the details in &lt;a href=&quot;http://www.theserverside.com/articles/article.tss?l=AspectWerkzP1&quot;&gt;this article&lt;/a&gt;.&lt;/p&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;p&gt;In this article I will show you how you can make use of this container to run &lt;a href=&quot;http://www.springframework.org/&quot;&gt;Spring &lt;span class=&quot;caps&quot;&gt;AOP&lt;/span&gt;&lt;/a&gt; aspects more performant plus utilize the annotation-driven &lt;span class=&quot;caps&quot;&gt;AOP&lt;/span&gt; in AspectWerkz along with its more expressive pointcut pattern language.&lt;/p&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;p&gt;In this article I am using Spring as an example but everything covered here applies to all frameworks that implements the &lt;a href=&quot;http://aopalliance.sourceforge.net/&quot;&gt;&lt;span class=&quot;caps&quot;&gt;AOP&lt;/span&gt; Alliance&lt;/a&gt; interfaces (e.g. dynaop, JoyAop, &lt;span class=&quot;caps&quot;&gt;JAC&lt;/span&gt; etc.).&lt;/p&gt;
&lt;/p&gt;
&lt;h3&gt;Write a regular Spring advice&lt;/h3&gt;
&lt;p&gt;
&lt;p&gt;First we write a regular spring advice that implements authentication:&lt;br /&gt;
&lt;pre&gt;&lt;br /&gt;
    public class AuthenticationAdvice implements MethodBeforeAdvice {&lt;/p&gt;
public void before(Method m, Object[] args, Object target) throws Throwable {
// try to authenticate the user
// if not authenticated throw a SecurityException
}
}
&lt;p&gt;&lt;/pre&gt;&lt;/p&gt;
&lt;/p&gt;
&lt;h3&gt;Define the advice using Java 5 annotations&lt;/h3&gt;
&lt;p&gt;
&lt;p&gt;In this example we will not define this advice using the slightly verbose Spring config file. But make use of Java 5 annotations. In AspectWerkz you have a set of predefined annotations that we can use, f.e. one for each advice type. The advice we have implemented here is a &lt;i&gt;before advice&lt;/i&gt; so we will use the &lt;tt&gt;@Before&lt;/tt&gt; annotation when defining the advice:&lt;br /&gt;
&lt;pre&gt;&lt;br /&gt;
    public class AuthenticationAdvice implements MethodBeforeAdvice {&lt;/p&gt;
@Before(&amp;#8220;authenticationPoints&amp;#8221;)
public void before(Method m, Object[] args, Object target) {
// try to authenticate the user
// if not authenticated throw a SecurityException
}
}
&lt;p&gt;&lt;/pre&gt;&lt;br /&gt;
Here we bind the advice to the pointcut named &lt;tt&gt;&amp;#8220;authenticationPoints&amp;#8221;&lt;/tt&gt;, this pointcut will pick out all the points where we want authentication to take place. However we do not define this pointcut yet. Since we want to make this aspect reusable, then it is better to just compile it, put it in a jar and then at deployment time resolve this definition by defining the pointcut in the external &lt;tt&gt;&lt;span class=&quot;caps&quot;&gt;META&lt;/span&gt;-&lt;span class=&quot;caps&quot;&gt;INF&lt;/span&gt;/aop.xml&lt;/tt&gt; file.&lt;/p&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;p&gt;We can of course choose to not define the pointcut in an external &lt;span class=&quot;caps&quot;&gt;XML&lt;/span&gt; file but directly in the &lt;tt&gt;Before&lt;/tt&gt; annotation like this: &lt;br /&gt;
&lt;pre&gt;&lt;br /&gt;
    &lt;code&gt;Before(&quot;call(&lt;/code&gt;RestrictedOperation * &lt;strong&gt;.&lt;/strong&gt;(..))&amp;quot;)&lt;br /&gt;
    public void before(Method m, Object[] args, Object target) {&lt;br /&gt;
        &amp;#8230; &lt;br /&gt;
    }&lt;br /&gt;
&lt;/pre&gt;&lt;br /&gt;
if we think that that is beneficial. (Then the &lt;tt&gt;&lt;span class=&quot;caps&quot;&gt;META&lt;/span&gt;-&lt;span class=&quot;caps&quot;&gt;INF&lt;/span&gt;/aop.xml&lt;/tt&gt; only have to define the aspect class name, see below for details).&lt;/p&gt;
&lt;/p&gt;
&lt;h3&gt;Define the pointcut in the external aop.xml file&lt;/h3&gt;
&lt;p&gt;
&lt;p&gt;Now all we need to do put this advice to work is to write the little &lt;tt&gt;&lt;span class=&quot;caps&quot;&gt;META&lt;/span&gt;-&lt;span class=&quot;caps&quot;&gt;INF&lt;/span&gt;/aop.xml&lt;/tt&gt; file.&lt;br /&gt;
In this case there are two things that we need to define there:&lt;br /&gt;
&lt;ul&gt;&lt;br /&gt;
    &lt;li&gt;&lt;br /&gt;
    we need to tell the AspectWerkz container that it should deploy this Spring advice as a regular AspectWerkz aspect&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
we need to resolve the definition by defining the &amp;#8220;authenticationPoints&amp;#8221; pointcut
&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Example:&lt;br /&gt;
&lt;pre&gt;&lt;br /&gt;
    &amp;lt;aspectwerkz&amp;gt;&lt;br /&gt;
        &amp;lt;system id=&amp;#8220;spring-extension-sample&amp;#8221;&amp;gt;&lt;br /&gt;
            &amp;lt;aspect class=&amp;#8220;my.application.aspects.AuthenticationAdvice&amp;#8221;&amp;gt;&lt;br /&gt;
                &amp;lt;pointcut name=&amp;#8220;authenticationPoints&amp;#8221; expression=&amp;#8220;call(@RestrictedOperation * &lt;strong&gt;.&lt;/strong&gt;(..))&amp;#8221; /&amp;gt;&lt;br /&gt;
            &amp;lt;/aspect&amp;gt;&lt;br /&gt;
        &amp;lt;/system&amp;gt;&lt;br /&gt;
    &amp;lt;/aspectwerkz&amp;gt;&lt;br /&gt;
&lt;/pre&gt;&lt;br /&gt;
Here we defined the pointcut to pick out all method calls to a method that is marked with the annotation &lt;tt&gt;@RestrictedOperation&lt;/tt&gt;.&lt;/p&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;p&gt;Note that we are using a &lt;i&gt;call&lt;/i&gt; pointcut here, which means that this advice will execute on the client side and not the server (something that is not possible with regular spring aop which only supports &lt;i&gt;execution&lt;/i&gt; pointcuts). This can be beneficial in many situations, it can f.e. can save us a remove call from the client to the server, or take some load off the server if the client is executing in a separate VM, etc.&lt;/p&gt;
&lt;/p&gt;
&lt;h3&gt;Start up the application&lt;/h3&gt;
&lt;p&gt;
&lt;p&gt;The last thing we need to do is to add two VM options:&lt;br /&gt;
&lt;ul&gt;&lt;br /&gt;
    &lt;li&gt;&lt;br /&gt;
    we need to register the &lt;i&gt;Aspect Model&lt;/i&gt; implementation for the Spring extension in the AspectWerkz container. This is done like this:&lt;br /&gt;
    &lt;br /&gt;  &lt;br /&gt;
    &lt;tt&gt;-Daspectwerkz.extension.aspectmodels=org.codehaus.aspectwerkz.transform.spring.SpringAspectModel&lt;/tt&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
we need to register the AspectWerkz weaver agent:
&lt;br /&gt;
&lt;tt&gt;-javaagent:lib/aspectwerkz-jdk5-RC2.jar&lt;/tt&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;You also need to put the &lt;tt&gt;aspectwerkz-core-RC2.jar&lt;/tt&gt;, &lt;tt&gt;aspectwerkz-RC2.jar&lt;/tt&gt;, &lt;tt&gt;aw-ext-spring-0.1.jar&lt;/tt&gt; and &lt;tt&gt;aw-ext-aopalliance-0.1.jar&lt;/tt&gt; plus the regular AspectWerkz dependency jars on the classpath. (You find the aspectwerkz jars in the aspectwerkz distribution and the aw-ext-*.jar jars you need to build yourself (see Resources for details).&lt;/p&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;p&gt;The you can start up the application as usual using &lt;tt&gt;java &amp;#8230;&lt;/tt&gt;.&lt;/p&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;p&gt;We have been thinking of providing a way of defining the spring aspect model on the application level (per &lt;tt&gt;&lt;span class=&quot;caps&quot;&gt;META&lt;/span&gt;-&lt;span class=&quot;caps&quot;&gt;INF&lt;/span&gt;/aop.xml&lt;/tt&gt; file) and not only on the VM level (using a VM option). If there is interest in this, please get back to us and we will add support for this.&lt;/p&gt;
&lt;/p&gt;
&lt;p&gt;&amp;lt;!&amp;#8212;p&amp;gt;&lt;br /&gt;
This means that we can start up the application like this:&lt;br /&gt;
&lt;pre&gt;&lt;/p&gt;
&lt;ol&gt;
	&lt;li&gt;set AspectWerkz version and libs dependancies&lt;br /&gt;
    set &lt;span class=&quot;caps&quot;&gt;VERSION&lt;/span&gt;=2.0.RC1&lt;br /&gt;
    set &lt;span class=&quot;caps&quot;&gt;DEPS&lt;/span&gt;=&amp;#8230; // AspectWerkz dependancies &amp;#8211; see bin/setEnv for the complete list&lt;/li&gt;
&lt;/ol&gt;
&lt;ol&gt;
	&lt;li&gt;all AspectWerkz jar, and dependancies&lt;br /&gt;
    set AW_PATH=aspectwerkz-core-$&lt;span class=&quot;caps&quot;&gt;VERSION&lt;/span&gt;.jar:aspectwerkz-$&lt;span class=&quot;caps&quot;&gt;VERSION&lt;/span&gt;.jar:$&lt;span class=&quot;caps&quot;&gt;DEPS&lt;/span&gt;&lt;/li&gt;
&lt;/ol&gt;
&lt;ol&gt;
	&lt;li&gt;-javaagent option&lt;/li&gt;
	&lt;li&gt;adapth the path to aspectwerkz-core-$&lt;span class=&quot;caps&quot;&gt;VERSION&lt;/span&gt;.jar as required&lt;br /&gt;
    java &lt;del&gt;javaagent:lib/aspectwerkz-jdk5&lt;/del&gt;$&lt;span class=&quot;caps&quot;&gt;VERSION&lt;/span&gt;.jar -cp $AW_PATH:&amp;#8230; \&lt;br /&gt;
         -Daspectwerkz.extension.aspectmodels=org.codehaus.aspectwerkz.transform.spring.SpringAspectModel \ &lt;br /&gt;
         &amp;#8230; my.application.Main &lt;br /&gt;
&lt;/pre&gt;&lt;/li&gt;
&lt;/ol&gt;
&lt;h3&gt;What about my Spring bean config file, dependency injection etc?&lt;/h3&gt;
&lt;p&gt;
&lt;p&gt;Good news is that you can still use your regular Spring bean config file and let Spring do its job, with dependency injection, bean configurations etc.&lt;/p&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;p&gt;AspectWerkz has a pluggable factory mechanism for the aspect instantiation and life-cycle management. We have written a factory for the Spring framework that allows you to use Spring to configure your aspects/advice just as usual. Read more about that &lt;a href=&quot;http://blogs.codehaus.org/people/jboner/archives/000826_spring_and_aspectwerkz_a_happy_marriage.html&quot;&gt;here&lt;/a&gt;.&lt;/p&gt;
&lt;/p&gt;
&lt;h3&gt;Much better performance&lt;/h3&gt;
&lt;p&gt;
&lt;p&gt;Apart from allowing defining your aspects using Java5 annotations and using the semantics of the AspectWerkz pointcut language and weaver, you will also get &lt;b&gt;much&lt;/b&gt; better performance (ranging between 1300 to 50 percent). I won&amp;#8217;t go into detail on that here but you can read more about it in &lt;a href=&quot;http://docs.codehaus.org/display/AW/AOP+Benchmark&quot;&gt;this article&lt;/a&gt;.&lt;/p&gt;
&lt;/p&gt;
&lt;h3&gt;Drawbacks&lt;/h3&gt;
&lt;p&gt;
&lt;p&gt;Everything has tradoffs and there are of course some drawbacks in using the &lt;i&gt;AspectWerkz Extensible Aspect Container&lt;/i&gt; as the runtime environment for your Spring aspects/advice: &lt;br /&gt;
&lt;ul&gt;&lt;br /&gt;
    &lt;li&gt;&lt;br /&gt;
    You will weave the actual target classes &amp;#8211; this is a more intrusive way of doing the weaving, which in some cases perhaps is not beneficial&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
You need a couple of extra VM options when you start up your application.
&lt;/li&gt;
&lt;li&gt;
You need one extra &lt;span class=&quot;caps&quot;&gt;XML&lt;/span&gt; (very tiny) config file &amp;#8211; the &lt;tt&gt;&lt;span class=&quot;caps&quot;&gt;META&lt;/span&gt;-&lt;span class=&quot;caps&quot;&gt;INF&lt;/span&gt;/aop.xml&lt;/tt&gt; file
&lt;/li&gt;
&lt;/ul&gt;
&lt;/p&gt;
&lt;h3&gt;Resources&lt;/h3&gt;
&lt;p&gt;
&lt;p&gt;I have not written any specific sample application for this article but if you want you can look at and run the tests in the AspectWerkz distribution. You can download the distribution &lt;a href=&quot;http://aspectwerkz.codehaus.org/releases.html&quot;&gt;here&lt;/a&gt;. The tests are in &lt;tt&gt;./src/compiler-extensions/spring/src/test&lt;/tt&gt; directory and you can run them by invoking &lt;tt&gt;ant test&lt;/tt&gt; when standing in the &lt;tt&gt;./src/compiler-extensions/spring&lt;/tt&gt; directory. However these tests does not use the Java5 annotation syntax but only the &lt;span class=&quot;caps&quot;&gt;XML&lt;/span&gt; definition to define the aspects. But you should be able to modify the tests as you like.&lt;/p&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;p&gt;The &lt;tt&gt;aspectwerkz*.jar&lt;/tt&gt; jars and the dependency jars are in the &lt;tt&gt;./lib&lt;/tt&gt; folder in the AspectWerkz distribution, but the &lt;tt&gt;aw-ext-*.jar&lt;/tt&gt; jars you need to build yourself. This is done by stepping into the  &lt;tt&gt;./src/compiler-extensions/spring&lt;/tt&gt; directory and type &lt;tt&gt;ant dist&lt;/tt&gt; then the jar will be put into the &lt;tt&gt;./src/compiler-extensions/spring/lib&lt;/tt&gt; directory, use same procedure to build the &lt;span class=&quot;caps&quot;&gt;AOP&lt;/span&gt; Alliance extension jar.&lt;/p&gt;
&lt;/p&gt;
&lt;p&gt;Enjoy.&lt;/p&gt;</content>
 </entry>
 
 <entry>
   <title>Interviewed on javaHispano.org</title>
   <link href="http://jboner.github.com/2004/11/02/interviewed-on-javahispanoorg"/>
   <updated>2004-11-02T00:00:00+00:00</updated>
   <id>hhttp://jboner.github.com/2004/11/02/interviewed-on-javahispanoorg</id>
   <content type="html">&lt;h1&gt;
&lt;p&gt;Interviewed on javaHispano.org&lt;/p&gt;
&lt;/h1&gt;
&lt;p /&gt;
&lt;p&gt;I gave an interview for the spanish Java community site &lt;a href=&quot;http://www.javahispano.org/canyamo.action&quot;&gt;javaHispano.org&lt;/a&gt;.&lt;/p&gt;
&lt;p /&gt;
&lt;p&gt;Here is the &lt;a href=&quot;http://www.javahispano.org/text.viewer.action?file=jonas_en&quot;&gt;english version&lt;/a&gt;.&lt;br /&gt;
&lt;p /&gt;&lt;/p&gt;</content>
 </entry>
 
 <entry>
   <title>Spring and AspectWerkz - A Happy Marriage</title>
   <link href="http://jboner.github.com/2004/08/31/spring-and-aspectwerkz-a-happy-marriage"/>
   <updated>2004-08-31T00:00:00+00:00</updated>
   <id>hhttp://jboner.github.com/2004/08/31/spring-and-aspectwerkz-a-happy-marriage</id>
   <content type="html">&lt;h1&gt;
&lt;p&gt;Spring and AspectWerkz &amp;#8211; A Happy Marriage&lt;/p&gt;
&lt;/h1&gt;
&lt;p /&gt;
&lt;p&gt;&lt;a href=&quot;http://www.springframework.org/&quot;&gt;The Spring Framework&lt;/a&gt; is a very powerful library for J2EE development. It comes with an &lt;span class=&quot;caps&quot;&gt;AOP&lt;/span&gt; implementation (based on proxies) that is in many cases sufficient in terms of what you need to do. However, there are many situations where you need a more full-blown &lt;span class=&quot;caps&quot;&gt;AOP&lt;/span&gt; framework (such as &lt;a href=&quot;http://aspectwerkz.codehaus.org/&quot;&gt;AspectWerkz&lt;/a&gt;) to do the job.&lt;/p&gt;
&lt;p /&gt;
&lt;p&gt;On the other hand &lt;a href=&quot;http://aspectwerkz.codehaus.org/&quot;&gt;AspectWerkz&lt;/a&gt; (even though it has a decent &lt;span class=&quot;caps&quot;&gt;API&lt;/span&gt; for configuration and management) could sometimes benefit from more finegrained and expressive configuration and life-cycle management, which is one thing that &lt;a href=&quot;http://www.springframework.org/&quot;&gt;Spring&lt;/a&gt; does very well.&lt;/p&gt;
&lt;p /&gt;
&lt;p&gt;In short, it is sometimes beneficial to use these two frameworks together and I will now show you a first step on how you can make that happen.&lt;/p&gt;
&lt;p /&gt;
&lt;p&gt;&lt;a href=&quot;http://aspectwerkz.codehaus.org/&quot;&gt;AspectWerkz&lt;/a&gt; has a very open architecture in regards to instantiation, configuration and management of the aspects. Here I will show you how you can make use of this to take control over your aspects using &lt;a href=&quot;http://www.springframework.org/&quot;&gt;Spring&lt;/a&gt;. (The concepts are the same for &lt;a href=&quot;http://picocontainer.org/&quot;&gt;PicoContainer&lt;/a&gt;, &lt;a href=&quot;http://jakarta.apache.org/hivemind/index.html&quot;&gt;HiveMind&lt;/a&gt; or a home-grown IoC implementation.)&lt;/p&gt;
&lt;p /&gt;
&lt;p&gt;In &lt;a href=&quot;http://aspectwerkz.codehaus.org/&quot;&gt;AspectWerkz&lt;/a&gt; instantiation, management and configuration of aspects is handled by an &amp;#8220;aspect container&amp;#8221; and to make it easier for users to provide their own custom implementation it provides the abstract &lt;tt&gt;org.codehaus.aspectwerkz.aspect.AbstractAspectContainer&lt;/tt&gt;&lt;br /&gt;
class which handles all the nitty-gritty details. &lt;br /&gt;
&lt;p /&gt; &lt;br /&gt;
All we have to do is to extend the &lt;tt&gt;org.codehaus.aspectwerkz.aspect.AbstractAspectContainer&lt;/tt&gt; class and implement the abstract &lt;tt&gt;Object createAspect()&lt;/tt&gt; method.&lt;br /&gt;
&lt;p /&gt; &lt;br /&gt;
So let us do that. Here is the implementation of our &lt;tt&gt;SpringAspectContainer&lt;/tt&gt;:&lt;br /&gt;
&lt;pre&gt; &lt;br /&gt;
public class SpringAspectContainer extends AbstractAspectContainer {&lt;/p&gt;
public static final String SPRING_ASPECT_CONTAINER_CONFIG = &amp;#8220;spring-bean-config.xml&amp;#8221;;
/**
&lt;ul&gt;
	&lt;li&gt;The Spring bean factory.&lt;br /&gt;
     */&lt;br /&gt;
    private XmlBeanFactory m_factory = null;&lt;/li&gt;
&lt;/ul&gt;
/**
&lt;ul&gt;
	&lt;li&gt;Creates a new aspect container strategy that uses the Spring framework to manage aspect instantiation and&lt;/li&gt;
	&lt;li&gt;configuaration.&lt;br /&gt;
     *&lt;/li&gt;
	&lt;li&gt;@param crossCuttingInfo the cross-cutting info&lt;br /&gt;
     */&lt;br /&gt;
    public SpringAspectContainer(final CrossCuttingInfo crossCuttingInfo) {&lt;br /&gt;
        super(crossCuttingInfo);&lt;br /&gt;
    }&lt;/li&gt;
&lt;/ul&gt;
/**
&lt;ul&gt;
	&lt;li&gt;Creates a new aspect instance.&lt;br /&gt;
     *&lt;/li&gt;
	&lt;li&gt;@return the new aspect instance&lt;br /&gt;
     */&lt;br /&gt;
    protected Object createAspect() {&lt;br /&gt;
        if (m_factory == null) {&lt;br /&gt;
            InputStream is = null;&lt;br /&gt;
            try {&lt;br /&gt;
                is = ClassLoader.getSystemResourceAsStream(SPRING_ASPECT_CONTAINER_CONFIG);&lt;br /&gt;
                m_factory = new XmlBeanFactory(is);&lt;br /&gt;
            } finally {&lt;br /&gt;
                try {&lt;br /&gt;
                    is.close();&lt;br /&gt;
                } catch (Throwable e) {&lt;br /&gt;
                    throw new WrappedRuntimeException(e);&lt;br /&gt;
                }&lt;br /&gt;
            }&lt;br /&gt;
        }&lt;/li&gt;
&lt;/ul&gt;
// here we are letting Spring instantiate the aspect based on its name
// (the m_infoPrototype field is the CrossCuttingInfo instance passed to the base class)
return m_factory.getBean(m_infoPrototype.getAspectDefinition().getName());
}
&lt;p&gt;}&lt;br /&gt;
&lt;/pre&gt;&lt;/p&gt;
&lt;p /&gt;
&lt;p&gt;This is all that is needed implementation wise. Now we only have to define which aspects should be managed by our new container and configure the aspect in the Spring bean config file.&lt;/p&gt;
&lt;p /&gt;
&lt;p&gt;To tell the &lt;a href=&quot;http://aspectwerkz.codehaus.org/&quot;&gt;AspectWerkz&lt;/a&gt; system that we want to deploy a specific aspect in our custom aspect container we have to specify that in the regular &lt;tt&gt;aop.xml&lt;/tt&gt; file like this:&lt;/p&gt;
&lt;pre&gt; 
&amp;amp;lt;aspect class=&quot;some.package.MyAspect&quot; container=&quot;some.other.package.SpringAspectContainer&quot;&amp;amp;gt;
    ...
&amp;amp;lt;/aspect&amp;amp;gt;
&lt;/pre&gt;
&lt;p /&gt;
For details on the &lt;tt&gt;aop.xml&lt;/tt&gt; file (what it is, how it is used etc.) see the &lt;a href=&quot;http://aspectwerkz.codehaus.org/startup_and_runtime_issues.html#Handling_several_Aspects_across_several_deployed_applications&quot;&gt;online documentation&lt;/a&gt;.
&lt;p /&gt;
&lt;p&gt;To configure the aspect we just configure it like any other &lt;a href=&quot;http://www.springframework.org/&quot;&gt;Spring&lt;/a&gt; bean class:&lt;/p&gt;
&lt;pre&gt; 
&amp;amp;lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&amp;amp;gt;

&amp;amp;lt;!DOCTYPE beans PUBLIC &quot;-//SPRING//DTD BEAN//EN&quot; &quot;http://www.springframework.org/dtd/spring-beans.dtd&quot;&amp;amp;gt;

&amp;amp;lt;beans&amp;amp;gt;
    &amp;amp;lt;bean id=&quot;some.package.MyAspect&quot;
          class=&quot;some.package.MyAspect&quot;
          singleton=&quot;false&quot;
          init-method=&quot;intialize&quot;&amp;amp;gt;
        &amp;amp;lt;property name=&quot;someProperty&quot;&amp;amp;gt;
            ...
        &amp;amp;lt;/property&amp;amp;gt;
        ...
    &amp;amp;lt;/bean&amp;amp;gt;
    ...
&amp;amp;lt;/beans&amp;amp;gt;
&lt;/pre&gt;
&lt;p /&gt;
&lt;p&gt;For details on how to define your bean classes see the &lt;a href=&quot;http://www.springframework.org/documentation.html&quot;&gt;Spring documentation&lt;/a&gt;. But here are some explainations:&lt;br /&gt;
&lt;ul&gt;&lt;br /&gt;
    &lt;li&gt;&lt;br /&gt;
     &lt;b&gt;id&lt;/b&gt; &amp;#8211; specifies the name of the aspect if a custom name is define you that else use the class name of the aspect (which is the default name). Mandatory.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;b&gt;class&lt;/b&gt; &amp;#8211; specifies the class name of the aspect. Mandatory.
&lt;/li&gt;
&lt;li&gt;
&lt;b&gt;singleton&lt;/b&gt; &amp;#8211; specifies if the aspect will be instantiated using the prototype pattern or not. Mandatory.
&lt;/li&gt;
&lt;li&gt;
&lt;b&gt;init-method&lt;/b&gt; &amp;#8211; the init-method is the method that you are using to initialize the aspect. This method will be called when all the properties have been set. Optional.
&lt;/li&gt;
&lt;li&gt;
&lt;b&gt;property&lt;/b&gt; &amp;#8211; the metadata that you want to pass to the aspect (see the Spring documentation for details on how how to define properties). Optional.
&lt;/li&gt;
&lt;/ul&gt;
&lt;p /&gt;
&lt;p&gt;&lt;a href=&quot;http://www.springframework.org/&quot;&gt;Spring&lt;/a&gt; has great support for passing in expressive metadata. It for example allows you to pass in lists, maps of for example strings, primitives or custom objects, and arrange them pretty much as you like.&lt;/p&gt;
&lt;p /&gt;
&lt;p&gt;Here is a more interesting example on how you can configure an aspect using Spring&amp;#8217;s properties. This aspect implements role-based security and is part of the &lt;a href=&quot;http://docs.codehaus.org/display/AWARE&quot;&gt;AWare&lt;/a&gt; aspect library:&lt;/p&gt;
&lt;pre&gt; 
&amp;amp;lt;bean id=&quot;org.codehaus.aware.security.RoleBasedAccessProtocol&quot;
    class=&quot;org.codehaus.aware.security.RoleBasedAccessProtocol&quot;
    singleton=&quot;false&quot;
    init-method=&quot;intialize&quot;&amp;amp;gt;

    &amp;amp;lt;property name=&quot;type&quot;&amp;amp;gt;
        &amp;amp;lt;value&amp;amp;gt;JAAS&amp;amp;lt;/value&amp;amp;gt;
    &amp;amp;lt;/property&amp;amp;gt;

    &amp;amp;lt;property name=&quot;roles&quot;&amp;amp;gt;
        &amp;amp;lt;list&amp;amp;gt;
            &amp;amp;lt;value&amp;amp;gt;admin&amp;amp;lt;/value&amp;amp;gt;
            &amp;amp;lt;value&amp;amp;gt;jboner&amp;amp;lt;/value&amp;amp;gt;
        &amp;amp;lt;/list&amp;amp;gt;
    &amp;amp;lt;/property&amp;amp;gt;

    &amp;amp;lt;property name=&quot;permissions&quot;&amp;amp;gt;
        &amp;amp;lt;list&amp;amp;gt;
            &amp;amp;lt;bean class=&quot;org.codehaus.aware.security.Permission&quot;&amp;amp;gt;
                &amp;amp;lt;property name=&quot;role&quot;&amp;amp;gt;
                    &amp;amp;lt;value&amp;amp;gt;jboner&amp;amp;lt;/value&amp;amp;gt;
                &amp;amp;lt;/property&amp;amp;gt;
                &amp;amp;lt;property name=&quot;className&quot;&amp;amp;gt;
                    &amp;amp;lt;value&amp;amp;gt;org.codehaus.aware.security.SecurityHandlingTest&amp;amp;lt;/value&amp;amp;gt;
                &amp;amp;lt;/property&amp;amp;gt;
                &amp;amp;lt;property name=&quot;methodName&quot;&amp;amp;gt;
                    &amp;amp;lt;value&amp;amp;gt;authorizeMe1&amp;amp;lt;/value&amp;amp;gt;
                &amp;amp;lt;/property&amp;amp;gt;
            &amp;amp;lt;/bean&amp;amp;gt;

           &amp;amp;lt;bean class=&quot;org.codehaus.aware.security.Permission&quot;&amp;amp;gt;
                &amp;amp;lt;property name=&quot;role&quot;&amp;amp;gt;
                    &amp;amp;lt;value&amp;amp;gt;jboner&amp;amp;lt;/value&amp;amp;gt;
                &amp;amp;lt;/property&amp;amp;gt;
                &amp;amp;lt;property name=&quot;className&quot;&amp;amp;gt;
                    &amp;amp;lt;value&amp;gt;org.codehaus.aware.security.SecurityHandlingTest&amp;amp;lt;/value&amp;amp;gt;
                &amp;amp;lt;/property&amp;amp;gt;
                &amp;amp;lt;property name=&quot;methodName&quot;&amp;amp;gt;
                    &amp;amp;lt;value&amp;amp;gt;authorizeMe2&amp;amp;lt;/value&amp;amp;gt;
                &amp;amp;lt;/property&amp;amp;gt;
            &amp;amp;lt;/bean&amp;amp;gt;
        &amp;amp;lt;/list&amp;amp;gt;
    &amp;amp;lt;/property&amp;amp;gt;

&amp;amp;lt;/bean&amp;amp;gt;
 
&lt;/pre&gt;
&lt;p /&gt;
&lt;p&gt;You can find the code for the &lt;tt&gt;SpringAspectContainer&lt;/tt&gt; along with many other reusable aspects in the &lt;a href=&quot;http://docs.codehaus.org/display/AWARE&quot;&gt;AWare&lt;/a&gt; library.  Which is a community-driven &lt;span class=&quot;caps&quot;&gt;OSS&lt;/span&gt; library for reusable aspects for &lt;a href=&quot;http://aspectwerkz.codehaus.org/&quot;&gt;AspectWerkz&lt;/a&gt;.&lt;/p&gt;
&lt;p /&gt;</content>
 </entry>
 
 <entry>
   <title>Tutorial series on how to get started with AspectWerkz</title>
   <link href="http://jboner.github.com/2004/06/11/tutorial-series-on-how-to-get-started-with-aspectwerkz"/>
   <updated>2004-06-11T00:00:00+00:00</updated>
   <id>hhttp://jboner.github.com/2004/06/11/tutorial-series-on-how-to-get-started-with-aspectwerkz</id>
   <content type="html">&lt;h1&gt;
&lt;p&gt;Tutorial series on how to get started with AspectWerkz&lt;/p&gt;
&lt;/h1&gt;
&lt;p/&gt;
&lt;p&gt;Yesterday evening I put together a series of two introductory tutorials on &lt;a href=&quot;http://aspectwerkz.codehaus.org/&quot;&gt;AspectWerkz&lt;/a&gt; (a third is on its way).&lt;br /&gt;
&lt;p/&gt;&lt;br /&gt;
Tutorial one: &lt;a href=&quot;http://docs.codehaus.org/display/AW/Hello+World&quot;&gt;Hello World&lt;/a&gt;.&lt;br /&gt;
&lt;p/&gt;&lt;br /&gt;
Tutorial two: &lt;a href=&quot;http://docs.codehaus.org/display/AW/Hijacking+Hello+World&quot;&gt;Hijacking Hello World&lt;/a&gt;&lt;br /&gt;
&lt;p/&gt;&lt;br /&gt;
I hope that these will help you to get started with &lt;a ref=&quot;http://aspectwerkz.codehaus.org/&quot;&gt;AspectWerkz&lt;/a&gt;. &lt;br /&gt;
&lt;p/&gt;&lt;br /&gt;
Enjoy.&lt;/p&gt;</content>
 </entry>
 
 <entry>
   <title>Slides from AOSD presentations</title>
   <link href="http://jboner.github.com/2004/04/05/slides-from-aosd-presentations"/>
   <updated>2004-04-05T00:00:00+00:00</updated>
   <id>hhttp://jboner.github.com/2004/04/05/slides-from-aosd-presentations</id>
   <content type="html">&lt;h1&gt;
&lt;p&gt;Slides from &lt;span class=&quot;caps&quot;&gt;AOSD&lt;/span&gt; presentations&lt;/p&gt;
&lt;/h1&gt;
&lt;p /&gt;
&lt;p&gt;Just got back from the &lt;span class=&quot;caps&quot;&gt;AOSD&lt;/span&gt; 2004 conference. Great experience. Had fun, attended many great talks, met a lot of interesting people etc.&lt;/p&gt;
&lt;p /&gt;
&lt;p&gt;Here are the links to the talks that I/we gave:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;Invited Talk &amp;#8211; &lt;a href=&quot;http://codehaus.org/~jboner/presentations/InvitedTalkAOSD2004.ppt&quot;&gt;What are the key issues for commercial &lt;span class=&quot;caps&quot;&gt;AOP&lt;/span&gt; use &amp;#8211; how does AspectWerkz address them?&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;AspectWerkz Tutorial &amp;#8211; &lt;a href=&quot;http://codehaus.org/~jboner/presentations/aosd2004_tutorial_aspectwerkz.ppt&quot;&gt;AspectWerkz for Dynamic Aspect-Oriented Programming&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;p /&gt;</content>
 </entry>
 
 <entry>
   <title>BEA sponsors AspectWerkz</title>
   <link href="http://jboner.github.com/2004/03/19/bea-sponsors-aspectwerkz"/>
   <updated>2004-03-19T00:00:00+00:00</updated>
   <id>hhttp://jboner.github.com/2004/03/19/bea-sponsors-aspectwerkz</id>
   <content type="html">&lt;h1&gt;
&lt;p&gt;&lt;span class=&quot;caps&quot;&gt;BEA&lt;/span&gt; sponsors AspectWerkz&lt;/p&gt;
&lt;/h1&gt;
&lt;p&gt;
&lt;p&gt;The &lt;a href=&quot;http://aspectwerkz.codehaus.org/&quot;&gt;AspectWerkz&lt;/a&gt; project is since a a month ago sponsored by &lt;a href=&quot;http://www.bea.com/&quot;&gt;&lt;span class=&quot;caps&quot;&gt;BEA&lt;/span&gt; Systems&lt;/a&gt;. Both &lt;a href=&quot;http://blogs.codehaus.org/people/avasseur/&quot;&gt;Alexandre&lt;/a&gt; and I, the founders and primary contributors to&lt;br /&gt;
AspectWerkz, are full time &lt;span class=&quot;caps&quot;&gt;BEA&lt;/span&gt; employees.  I work with the JRockit engineering team in Stockholm, and our primary objective is to make AspectWerkz the leading &lt;span class=&quot;caps&quot;&gt;AOP&lt;/span&gt; framework for dynamic &lt;span class=&quot;caps&quot;&gt;AOP&lt;/span&gt;, and to ensure that the &lt;a href=&quot;http://commerce.bea.com/showallversions.jsp?family=WLJR&quot;&gt;JRockit &lt;span class=&quot;caps&quot;&gt;JVM&lt;/span&gt;&lt;/a&gt; is far and away the best Java platform for &lt;span class=&quot;caps&quot;&gt;AOP&lt;/span&gt;.&lt;/p&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;p&gt;We are, of course, working on a number of interesting future directions tight to the in tight coordination with the rest of the &lt;span class=&quot;caps&quot;&gt;BEA&lt;/span&gt; products line, but the near term goals are to build deep and unprecedented support for &lt;span class=&quot;caps&quot;&gt;AOP&lt;/span&gt;, including real-time run-time weaving into JRockit Java Virtual Machine, and to make AspectWerkz on top of JRockit the number one choice for dynamic &lt;span class=&quot;caps&quot;&gt;AOP&lt;/span&gt; in enterprise application environments.&lt;/p&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;p&gt;AspectWerkz still is and will remain open source software, licensed under &lt;span class=&quot;caps&quot;&gt;LGPL&lt;/span&gt; and will continue being platform and vendor independent.&lt;/p&gt;
&lt;/p&gt;</content>
 </entry>
 
 <entry>
   <title>AspectWerkz NextGen</title>
   <link href="http://jboner.github.com/2004/03/19/aspectwerkz-nextgen"/>
   <updated>2004-03-19T00:00:00+00:00</updated>
   <id>hhttp://jboner.github.com/2004/03/19/aspectwerkz-nextgen</id>
   <content type="html">&lt;h1&gt;
&lt;p&gt;AspectWerkz NextGen&lt;/p&gt;
&lt;/h1&gt;
&lt;p /&gt;
&lt;p&gt;Today we released the 0.10 Release Candidate 1 of &lt;a href=&quot;http://aspectwerkz.codehaus.org/&quot;&gt;AspectWerkz&lt;/a&gt;.  Which forms the foundation for the next generation of AspectWerkz.&lt;/p&gt;
&lt;p /&gt;
&lt;p&gt;AspectWerkz has gone through a lot of changes, the whole core engine has for example been thrown out and replaced by a much more flexible and performant one.  The join point model is much more expressive and orthogonal we have also implemented a &lt;span class=&quot;caps&quot;&gt;JIT&lt;/span&gt; compiler that is making AspectWerkz much more performant.  On top of that we have a new implementation of true runtime weaving, which allows you to redefine your aspect model including adding &lt;b&gt;new&lt;/b&gt; pointcuts at runtime.&lt;/p&gt;
&lt;p /&gt;
&lt;p&gt;Read more about it &lt;a href=&quot;http://blogs.codehaus.org/projects/aspectwerkz/archives/000651_next_generation_of_the_aspectwerkz_aop_framework_is_released_.html&quot;&gt;here&lt;/a&gt;.&lt;/p&gt;</content>
 </entry>
 
 <entry>
   <title>Presenting on dynamic AOP @ JavaOne</title>
   <link href="http://jboner.github.com/2004/03/03/presenting-on-dynamic-aop-javaone"/>
   <updated>2004-03-03T00:00:00+00:00</updated>
   <id>hhttp://jboner.github.com/2004/03/03/presenting-on-dynamic-aop-javaone</id>
   <content type="html">&lt;h1&gt;
&lt;p&gt;Presenting on dynamic &lt;span class=&quot;caps&quot;&gt;AOP&lt;/span&gt; @ JavaOne&lt;/p&gt;
&lt;/h1&gt;
&lt;p/&gt;
&lt;p&gt;I was lucky and our submission to &lt;a href=&quot;http://servlet.java.sun.com/javaone/&quot;&gt;JavaOne&lt;/a&gt; was accepted. &lt;br /&gt;
&lt;p/&gt;&lt;br /&gt;
This means that &lt;a href=&quot;http://blogs.codehaus.org/people/avasseur/&quot;&gt;Alex&lt;/a&gt; and I will do a presentation on &amp;#8220;Dynamic Aspect-Oriented Programming in J2EE environments&amp;#8221;. &lt;br /&gt;
&lt;p/&gt;&lt;br /&gt;
See you there.&lt;br /&gt;
&lt;p/&gt;&lt;/p&gt;</content>
 </entry>
 
 <entry>
   <title>Doing a tutorial on AOP in enterprise applications at AOSD 2004</title>
   <link href="http://jboner.github.com/2004/02/18/doing-a-tutorial-on-aop-in-enterprise-applications-at-aosd-2004"/>
   <updated>2004-02-18T00:00:00+00:00</updated>
   <id>hhttp://jboner.github.com/2004/02/18/doing-a-tutorial-on-aop-in-enterprise-applications-at-aosd-2004</id>
   <content type="html">&lt;h1&gt;
&lt;p&gt;Doing a tutorial on &lt;span class=&quot;caps&quot;&gt;AOP&lt;/span&gt; in enterprise applications at &lt;span class=&quot;caps&quot;&gt;AOSD&lt;/span&gt; 2004&lt;/p&gt;
&lt;/h1&gt;
&lt;p/&gt;
&lt;p&gt;&lt;a href=&quot;http://blogs.codehaus.org/people/avasseur/&quot;&gt;Alex&lt;/a&gt; and I will do a half-day tutorial on &lt;a href=&quot;http://aspectwerkz.codehaus.org/&quot;&gt;AspectWerkz&lt;/a&gt; at &lt;a href=&quot;http://www.aosd.net/conference.php&quot;&gt;&lt;span class=&quot;caps&quot;&gt;AOSD&lt;/span&gt; 2004&lt;/a&gt;.&lt;br /&gt;
&lt;p/&gt;&lt;br /&gt;
The tutorial will be an introduction to &lt;a href=&quot;http://www.aosd.net/&quot;&gt;&lt;span class=&quot;caps&quot;&gt;AOP&lt;/span&gt;/&lt;span class=&quot;caps&quot;&gt;AOSD&lt;/span&gt;&lt;/a&gt; and &lt;a href=&quot;http://aspectwerkz.codehaus.org/&quot;&gt;AspectWerkz&lt;/a&gt; and to how it can be used in real-world enterprise applications. &lt;br /&gt;
&lt;p/&gt;&lt;br /&gt;
It will include hands-on exercises and we will show you how to implement services like:&lt;br /&gt;
&lt;ul&gt;&lt;br /&gt;
&lt;li&gt;&lt;br /&gt;
Role-based security&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Declarative transaction demarcation&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Transparent persistence&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Asynchronous method invocations&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Caching&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;etc. &lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;for regular POJOs (plain old java objects). &lt;br /&gt;
&lt;p/&gt;&lt;br /&gt;
You can read more about the tutorial &lt;a href=&quot;http://www.aosd.net/2004/tutorials/aspectwerkz.php&quot;&gt;here&lt;/a&gt;.&lt;br /&gt;
&lt;p/&gt;&lt;br /&gt;
We are looking forward to an interesting conference.&lt;br /&gt;
&lt;p/&gt;&lt;br /&gt;
Hope to see you there. &lt;br /&gt;
&lt;p/&gt;&lt;/p&gt;</content>
 </entry>
 
 <entry>
   <title>Role-Based Security the AOP way</title>
   <link href="http://jboner.github.com/2003/12/17/role-based-security-the-aop-way"/>
   <updated>2003-12-17T00:00:00+00:00</updated>
   <id>hhttp://jboner.github.com/2003/12/17/role-based-security-the-aop-way</id>
   <content type="html">&lt;h1&gt;
&lt;p&gt;Role-Based Security the &lt;span class=&quot;caps&quot;&gt;AOP&lt;/span&gt; way&lt;/p&gt;
&lt;/h1&gt;
&lt;p&gt;
&lt;p&gt;Since the last example of the new &amp;#8216;annotation-defined aspects&amp;#8217; model in AspectWerkz seemed to have caused a lot of confusion and made people miss the point, I will in this post try to give a more detailed explaination of the previous &amp;#8216;Role-Based Security&amp;#8217; example.&lt;/p&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;p&gt;First I just want to stress that this new model of defining the aspects will &lt;span class=&quot;caps&quot;&gt;NOT&lt;/span&gt; in any sense make the old pure &lt;span class=&quot;caps&quot;&gt;XML&lt;/span&gt; based and the doclet based approaches obsolete. We are here simply providing a new option. A ticket out of deployment descriptor hell.&lt;/p&gt;
&lt;p /&gt;
&lt;p&gt;I also would like to mention that this new model is completely user-demand driven. It solely exists due to the fact that there is a high need for it. It basically solves three problems that the users have been complaining about:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;Separation of implementation and definition. The implementation and the definition are defined in one single file (in the same bytecode). The aspect definition is thus not separated from the aspect implementation&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
All advices and introductions needed to implement a concern can be implemented in one single class. The problem with the old approach was that you f.e. for a regular Aspect with two advices and an introduction needed four files. One for each advice and introductions as well as one for the definition. This felt like &lt;span class=&quot;caps&quot;&gt;EJB&lt;/span&gt; all over again and the new model addresses this problem in a neat way allowing you to implement everything that is needed in one single class.
&lt;/li&gt;
&lt;li&gt;
Not pure Java. Even though &lt;span class=&quot;caps&quot;&gt;XML&lt;/span&gt; is widely used in the J2EE community and intuitive to most users, it is not Java. It suffers from problems with refactoring, maintainance, reusability etc. This new model tries to address this as well. Even thought the current implementation, based on JavaDoc tags, is not pure Java, it is closer to it and it will be pure Java when we have Java 1.5. Which is what this new model is targetting.
&lt;/li&gt;
&lt;/ul&gt;
&lt;p /&gt;
&lt;p&gt;So lets get back to the &amp;#8216;Role-Based Security&amp;#8217; example of ours.&lt;/p&gt;
&lt;p /&gt;
&lt;p&gt;The first thing we do is to implement an abstract aspect that contains all implementation for this concern (the advices). But we leave the definition of the pointcuts (where to apply the advices) for later:&lt;/p&gt;
&lt;pre&gt;
    /**
     * @Aspect perThread
     */
    public abstract class AbstractRoleBasedAccessController extends Aspect {

        protected Subject m_subject = null;

        protected final SecurityManager m_securityManager = ...

        /** To be defined by the concrete aspect. */
        Pointcut authenticationPoints;

        /** To be defined by the concrete aspect. *
        Pointcut authorizationPoints;

        /** 
         * @Around authenticationPoints 
         */
        public Object authenticateUser(JoinPoint joinPoint) throws Throwable {
            if (m_subject == null) {
               // no subject =&amp;gt; authentication required
               Context ctx = ... // get the principals and credentials
               m_subject = m_securityManager.authenticate(ctx); // throws an exception if not authenticated
            }
            Object result = Subject.doAsPrivileged(
               m_subject, new PrivilegedExceptionAction() {
                  public Object run() throws Exception {
                     return joinPoint.proceed();
                  };
               }, null
            );
            return result;
        }

        /** 
         * @Around authorizationPoints 
         */
        public Object authorizeUser(JoinPoint joinPoint) throws Throwable {
           MethodJoinPoint jp = (MethodJoinPoint)joinPoint;

           if (m_securityManager.checkPermission(
              m_subject, 
              jp.getTargetClass(), 
              jp.getMethod())) {

              // user is authorized =&amp;gt; proceed
              return joinPoint.proceed();
           }
           else {
              throw new SecurityException(...);
           }
        }
    }
&lt;/pre&gt;

&lt;p /&gt;
&lt;p&gt;This aspect is now completely generic and reusable.&lt;/p&gt;
&lt;p /&gt;
&lt;p&gt;Now we can compile this aspect and package it in a library along with the implementation for the security manager etc. The aspect needs to be compiled with a custom compiler which retrieves the attributes and puts them in into the bytecode of the aspect class. This step will not be needed when we are running java 1.5 which has build in support for metadata annontations.&lt;/p&gt;
&lt;p /&gt;
&lt;p&gt;If we now want to use this security aspect of ours we can inherit the abstract aspect using regular class inheritance and define the pointcuts in the concrete subclass:&lt;/p&gt;
&lt;pre&gt;
    /**
     * @Aspect perThread
     */
    public class RoleBasedAccessController extends AbstractRoleBasedAccessController {

       /**
        * @Execution * *..facade.*.*(..)
        */
       Pointcut authenticationPoints;

       /**
        * @Execution * *..service.*.*(..)
        */
       Pointcut authorizationPoints;
    }
&lt;/pre&gt;
&lt;p /&gt;
&lt;p&gt;The system needs to know which aspects we want to use and for this we need to write a &lt;span class=&quot;caps&quot;&gt;TINY&lt;/span&gt; &lt;span class=&quot;caps&quot;&gt;XML&lt;/span&gt; definition file. Here we only need to specify the names of the aspects and NO definition metadata:&lt;/p&gt;
&lt;pre&gt;
    &amp;amp;lt;aspectwerkz&amp;amp;gt;
        &amp;amp;lt;system id=&quot;security-test&quot;&amp;amp;gt;
            &amp;amp;lt;package name=&quot;example&quot;&amp;amp;gt;
                &amp;amp;lt;use-aspect class=&quot;RoleBasedAccessController&quot;&amp;amp;gt;
                    &amp;amp;lt;param name=&quot;type&quot; value=&quot;JAAS&quot;&amp;amp;gt;
                &amp;amp;lt;/use-aspect&amp;amp;gt;
            &amp;amp;lt;/package&amp;amp;gt;
        &amp;amp;lt;/system&amp;amp;gt;
    &amp;amp;lt;/aspectwerkz&amp;amp;gt;
&lt;/pre&gt;
&lt;p /&gt;
&lt;p&gt;Here we can see that I have the possibility of parameterizing the aspect. This can be very useful in some situations since it allows you to use the same aspect with different configurations in different contexts&lt;/p&gt;
&lt;p /&gt;
&lt;p&gt;If you think that inheritance adds too much coupling then you have the option of skipping the concrete subclass completely and define the pointcuts in the &lt;span class=&quot;caps&quot;&gt;XML&lt;/span&gt; definition instead:&lt;/p&gt;
&lt;pre&gt;
    &amp;amp;lt;aspectwerkz&amp;amp;gt;
        &amp;amp;lt;system id=&quot;security-test&quot;&amp;amp;gt;
            &amp;amp;lt;package name=&quot;example&quot;&amp;amp;gt;
                &amp;amp;lt;use-aspect class=&quot;AbstractRoleBasedAccessController&quot;&amp;amp;gt;
                    &amp;amp;lt;pointcut-def name=&quot;authenticationPoints&quot; type=&quot;execution&quot; pattern=&quot;* *..facade.*.*(..)&quot;/&amp;amp;gt;
                    &amp;amp;lt;pointcut-def name=&quot;authorizationPoints&quot; type=&quot;execution&quot; pattern=&quot;* *..service.*.*(..)&quot;/&amp;amp;gt;
                &amp;amp;lt;/use-aspect&amp;amp;gt;
            &amp;amp;lt;/package&amp;amp;gt;
        &amp;amp;lt;/system&amp;amp;gt;
    &amp;amp;lt;/aspectwerkz&amp;amp;gt;
&lt;/pre&gt;
&lt;p /&gt;
&lt;p&gt;If you want to play with this new release (which is a release candidate) you can download it &lt;a href=&quot;http://aspectwerkz.codehaus.org/releases.html&quot;&gt;here&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;&lt;p /&gt;&lt;/p&gt;&lt;/p&gt;</content>
 </entry>
 
 <entry>
   <title>Presentation on dynamic AOP in J2EE environments</title>
   <link href="http://jboner.github.com/2003/12/01/presentation-on-dynamic-aop-in-j2ee-environments"/>
   <updated>2003-12-01T00:00:00+00:00</updated>
   <id>hhttp://jboner.github.com/2003/12/01/presentation-on-dynamic-aop-in-j2ee-environments</id>
   <content type="html">&lt;h1&gt;
&lt;p&gt;Presentation on dynamic &lt;span class=&quot;caps&quot;&gt;AOP&lt;/span&gt; in J2EE environments&lt;/p&gt;
&lt;/h1&gt;
&lt;p/&gt;
&lt;p&gt;On Wednesday (December 3d) I will give a presentation on dynamic &lt;a href=&quot;http://aosd.net/&quot;&gt;Aspect-Oriented Programming&lt;/a&gt; in J2EE environments using &lt;a href=&quot;http://aspectwerkz.codehaus.org/&quot;&gt;AspectWerkz&lt;/a&gt; at &lt;a href=&quot;http://www.bejug.org&quot;&gt;JavaPolis/BeJUG 2003&lt;/a&gt;&lt;br /&gt;
&lt;p/&gt;&lt;br /&gt;
The presentation will aim at give an understanding on how &lt;span class=&quot;caps&quot;&gt;AOP&lt;/span&gt; and AspectWerkz can be used in real-world enterprise applications and will include a couple of hands-on examples.&lt;br /&gt;
&lt;p/&gt;&lt;br /&gt;
If you are in Antwerpen, stop by and say hi.&lt;br /&gt;
&lt;p/&gt;&lt;/p&gt;</content>
 </entry>
 
 <entry>
   <title>Annotation-defined Aspects</title>
   <link href="http://jboner.github.com/2003/10/27/self-defined-aspects-in-aspectwerkz-new-definition-model"/>
   <updated>2003-10-27T00:00:00+00:00</updated>
   <id>hhttp://jboner.github.com/2003/10/27/self-defined-aspects-in-aspectwerkz-new-definition-model</id>
   <content type="html">&lt;h1&gt;
&lt;p&gt;Annotation-defined Aspects&lt;/p&gt;
&lt;/h1&gt;
&lt;p /&gt;
&lt;p&gt;The upcoming release of &lt;a href=&quot;http://aspectwerkz.codehaus.org/&quot;&gt;AspectWerkz&lt;/a&gt; (0.9) will have support for a new definition model in which the aspects are self-defined. This means that implementation and definition is written in the same self-contained component.&lt;/p&gt;
&lt;p /&gt;
&lt;p&gt;In this new definition model the aspects are regular Java classes and the advices and introductions are regular methods and inner classes in these classes. The aspects can be abstract and inheritance is used and treated like regular Java class inheritance. The meta-data (aspect definition) is defined using runtime attributes and is currently compiled in into the class on bytecode level (not needed in Java 1.5.x and above).&lt;/p&gt;
&lt;p /&gt;
&lt;p&gt;This new approach makes it easy to create reusable self-contained aspect components and libraries. It simplifies both the implementation, maintainance and refactorings since &lt;b&gt;everything&lt;/b&gt; is written in one single Java class.&lt;/p&gt;
&lt;p /&gt;
&lt;p&gt;This is an example of a testing aspect defining a virtual mock object mixin that will be introduced (along with its interfaces) on all the classes matching the pattern &lt;tt&gt;foo.baz.&lt;strong&gt;&lt;/tt&gt;. &lt;br /&gt;
&lt;pre&gt;&lt;br /&gt;
/&lt;/strong&gt;*&lt;/p&gt;
&lt;ul&gt;
	&lt;li&gt;@Aspect perInstance&lt;br /&gt;
 */&lt;br /&gt;
public class MockObjectAspect extends Aspect {&lt;/li&gt;
&lt;/ul&gt;
/**
&lt;ul&gt;
	&lt;li&gt;@Introduce foo.baz.*&lt;br /&gt;
     */&lt;br /&gt;
    class MockObject extends MyBase implements Traceable, Metered {&lt;br /&gt;
        // implementation of the mock object&lt;br /&gt;
    }&lt;br /&gt;
}&lt;/li&gt;
&lt;/ul&gt;
&lt;p /&gt;
&lt;p&gt;Here is another example showing a skeleton for an aspect implementing authentication and authorization.&lt;/p&gt;
&lt;p&gt;&lt;/pre&gt;&lt;pre&gt;&lt;br /&gt;
/**&lt;/p&gt;
&lt;ul&gt;
	&lt;li&gt;@Aspect perThread&lt;br /&gt;
 */&lt;br /&gt;
public class RoleBasedAccess extends Aspect {&lt;/li&gt;
&lt;/ul&gt;
private Subject m_subject;
/**
&lt;ul&gt;
	&lt;li&gt;@Call * &lt;strong&gt;..facade.&lt;/strong&gt;.*(..)&lt;br /&gt;
     */&lt;br /&gt;
    Pointcut facadeMethods;&lt;/li&gt;
&lt;/ul&gt;
/**
&lt;ul&gt;
	&lt;li&gt;@Execution * &lt;strong&gt;..service.&lt;/strong&gt;.*(..)&lt;br /&gt;
     */&lt;br /&gt;
    Pointcut needsAuthorization;&lt;/li&gt;
&lt;/ul&gt;
/**
&lt;ul&gt;
	&lt;li&gt;@Before facadeMethods&lt;br /&gt;
     */&lt;br /&gt;
    public void authenticateUser(JoinPoint joinPoint) throws Throwable {&lt;br /&gt;
        Context context = &amp;#8230; // get principals and credentials&lt;br /&gt;
        boolean granted = SecurityManager.authenticateUser(context) // authenticate the user (f.e. using &lt;span class=&quot;caps&quot;&gt;JAAS&lt;/span&gt;)&lt;br /&gt;
        if (granted) {&lt;br /&gt;
            m_subject = &amp;#8230; // set the subject &lt;br /&gt;
        }&lt;br /&gt;
        else {&lt;br /&gt;
            throw new SecurityException(&amp;quot;user not authenticated: &amp;quot; + context);&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;/li&gt;
&lt;/ul&gt;
/**
&lt;ul&gt;
	&lt;li&gt;@Around needsAuthorization&lt;br /&gt;
     */&lt;br /&gt;
    public Object authorizeUser(JoinPoint joinPoint) throws Throwable {&lt;br /&gt;
        MethodJoinPoint jp = (MethodJoinPoint)joinPoint;&lt;br /&gt;
	boolean granted = SecurityManager.checkPermission(m_subject, jp.getTargetClass(), jp.getMethod());&lt;br /&gt;
        if (granted) {&lt;br /&gt;
            // proceed with normal method invocation&lt;br /&gt;
            return joinPoint.proceed();&lt;br /&gt;
        }&lt;br /&gt;
        else {&lt;br /&gt;
            throw new SecurityException(&amp;quot;access denied at &amp;quot; + jp.getTargetClass() + &amp;#8220;.&amp;#8221; + jp.getMethodName());&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&lt;/pre&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p /&gt;
&lt;p&gt;Currently the runtime attributes implementation is based on JavaDoc tags. These are parsed and inserted into the class on bytecode level, so no source files are needed once the classes have been compiled. This is something that will become obsolete when Java 1.5 and the &lt;a href=&quot;http://www.jcp.org/en/jsr/detail?id=175&quot;&gt;&lt;span class=&quot;caps&quot;&gt;JSR&lt;/span&gt;-175&lt;/a&gt; is released, which will bring runtime attributes to the Java language. The extra compilation step needed to annotate the class files will then not be needed. This also means that pure Java refactorings of both implementation and definition in the aspects will be made possible (e.g. tool support).&lt;/p&gt;
&lt;p /&gt;
&lt;p&gt;The new release is scheduled to be released in the middle of end of november.&lt;br /&gt;
&lt;p /&gt;&lt;/p&gt;
&lt;p&gt;Stay tuned.&lt;br /&gt;
&lt;p /&gt;&lt;/p&gt;</content>
 </entry>
 
 <entry>
   <title>AOP for EJB made easy</title>
   <link href="http://jboner.github.com/2003/08/15/aop-for-ejb-made-easy"/>
   <updated>2003-08-15T00:00:00+00:00</updated>
   <id>hhttp://jboner.github.com/2003/08/15/aop-for-ejb-made-easy</id>
   <content type="html">&lt;h1&gt;
&lt;p&gt;&lt;span class=&quot;caps&quot;&gt;AOP&lt;/span&gt; for &lt;span class=&quot;caps&quot;&gt;EJB&lt;/span&gt; made easy&lt;/p&gt;
&lt;/h1&gt;
&lt;p /&gt;
&lt;p&gt;Using the new 0.8 version of &lt;a href=&quot;http://aspectwerkz.codehaus.org/index.html&quot;&gt;AspectWerkz&lt;/a&gt; it is now a breeze to instrument your Servlets, EJBs or WebServices at runtime. &lt;br /&gt;
&lt;p /&gt;&lt;br /&gt;
In this example I will show how you use AspectWerkz together with WebLogic Server. Please note that AspectWerkz is not in any way tied to &lt;span class=&quot;caps&quot;&gt;WLS&lt;/span&gt;, it is a pure generic &lt;span class=&quot;caps&quot;&gt;AOP&lt;/span&gt; solution that works equally good inside any application server.&lt;br /&gt;
&lt;p /&gt;&lt;br /&gt;
Even though I am fed up with using tracing as a way of demonstrating &lt;span class=&quot;caps&quot;&gt;AOP&lt;/span&gt;, this is exactly what I am going to do. This due to simplicity and to be able focus on the important areas. Like, how to get it to work.&lt;br /&gt;
&lt;p /&gt;&lt;br /&gt;
So now I will instrument the examplesServer application in &lt;span class=&quot;caps&quot;&gt;WLS&lt;/span&gt; which contains all interesting parts in J2EE: &lt;span class=&quot;caps&quot;&gt;SLSB&lt;/span&gt;,  &lt;span class=&quot;caps&quot;&gt;SFSB&lt;/span&gt;, &lt;span class=&quot;caps&quot;&gt;CMP&lt;/span&gt;, &lt;span class=&quot;caps&quot;&gt;BMP&lt;/span&gt;, &lt;span class=&quot;caps&quot;&gt;MDB&lt;/span&gt;, Servlets, WebServices etc. &lt;br /&gt;
&lt;p /&gt;&lt;br /&gt;
First I write a simple &lt;span class=&quot;caps&quot;&gt;XML&lt;/span&gt; definition file:&lt;br /&gt;
            &lt;p&gt;&lt;br /&gt;
&lt;pre&gt;&lt;br /&gt;
&amp;lt;!&lt;span class=&quot;caps&quot;&gt;DOCTYPE&lt;/span&gt; aspectwerkz &lt;span class=&quot;caps&quot;&gt;PUBLIC&lt;/span&gt;&lt;br /&gt;
    &amp;#8220;-//AspectWerkz//&lt;span class=&quot;caps&quot;&gt;DTD&lt;/span&gt; 0.8//EN&amp;#8221;&lt;br /&gt;
    &amp;#8220;http://aspectwerkz.codehaus.org/dtd/aspectwerkz_0_8.dtd&amp;#8221;&amp;gt;&lt;/p&gt;
&lt;p&gt;&amp;lt;aspectwerkz id=&amp;#8220;samples&amp;#8221;&amp;gt;&lt;br /&gt;
    &amp;lt;advice-def name=&amp;#8220;log&amp;#8221; class=&amp;#8220;examples.logging.LoggingAdvice&amp;#8221; deployment-model=&amp;#8220;perJVM&amp;#8221;/&amp;gt;&lt;/p&gt;
&amp;lt;aspect name=&amp;#8220;testWLS&amp;#8221;&amp;gt;
&amp;lt;pointcut-def name=&amp;#8220;allMethods&amp;#8221; type=&amp;#8220;method&amp;#8221; pattern=&amp;#8220;* examples..&lt;strong&gt;+.&lt;/strong&gt;(..)&amp;#8221;/&amp;gt;

&amp;lt;bind-advice pointcut=&amp;#8220;allMethods&amp;#8221;&amp;gt;
&amp;lt;advice-ref name=&amp;#8220;log&amp;#8221;/&amp;gt;
&amp;lt;/bind-advice&amp;gt;
&amp;lt;/aspect&amp;gt;
&lt;p&gt;&amp;lt;/aspectwerkz&amp;gt;&lt;br /&gt;
&lt;/pre&gt;&lt;/p&gt;
&lt;/p&gt;
&lt;p&gt;Here you can see that I define one advice that does the tracing, one pointcut matching all methods in the whole examples application and then I simply &amp;#8216;bind&amp;#8217; the advice to the pointcut.&lt;br /&gt;
&lt;p /&gt;&lt;br /&gt;
So now all I have to do is to start up the application server (in this case &lt;span class=&quot;caps&quot;&gt;WLS&lt;/span&gt;) along with AspectWerkz. For this I have many options. First I can post-process my class files before deployment using the &lt;tt&gt;aspectwerkz -offline &amp;#8230;&lt;/tt&gt; compiler. Second there are some more interesting &amp;#8216;online&amp;#8217; alternatives. For all the &amp;#8216;online&amp;#8217; options see the &lt;a href=&quot;http://aspectwerkz.codehaus.org/online.html#Overview&quot;&gt;AspectWerkz documentation&lt;/a&gt;.&lt;br /&gt;
&lt;p /&gt;&lt;br /&gt;
For simplicity I am using the command line tool &lt;tt&gt;bin/aspectwerkz&lt;/tt&gt;  which is a unification of the HotSwap and the Transparent bootclasspath options (see the documention for details). This tool autodetects the version of the &lt;span class=&quot;caps&quot;&gt;JVM&lt;/span&gt; I am using and is chosing the best option for me.&lt;br /&gt;
&lt;p /&gt;&lt;br /&gt;
So what I have to do is to alter the startup script for &lt;span class=&quot;caps&quot;&gt;WLS&lt;/span&gt; a little bit. You have to alter the last part in the script:&lt;br /&gt;
&lt;pre&gt;&lt;br /&gt;
&amp;#8220;&lt;span&gt;JAVA_HOME&lt;/span&gt;\bin\java&amp;#8221; &lt;span&gt;JAVA_VM&lt;/span&gt; &lt;span&gt;MEM_ARGS&lt;/span&gt; &lt;span&gt;JAVA_OPTIONS&lt;/span&gt; &lt;br /&gt;
	-Dweblogic.Name=%SERVER_NAME% -Dweblogic.ProductionModeEnabled=%PRODUCTION_MODE% &lt;br /&gt;
	-Djava.security.policy=&amp;#8220;&lt;span&gt;WL_HOME&lt;/span&gt;\server\lib\weblogic.policy&amp;#8221; weblogic.Server &lt;br /&gt;
&lt;/pre&gt;&lt;br /&gt;
So it looks like this:&lt;br /&gt;
&lt;pre&gt;&lt;br /&gt;
set ASPECTWERKZ_HOME=C:\src\aspectwerkz&lt;br /&gt;
set AW_OPT=-Daspectwerkz.definition.file=C:\bea\weblogic81\mydomain\aspectwerkz.xml&lt;/p&gt;
&lt;p&gt;&lt;span&gt;ASPECTWERKZ_HOME&lt;/span&gt;\bin\aspectwerkz &lt;span&gt;JAVA_VM&lt;/span&gt; &lt;span&gt;MEM_ARGS&lt;/span&gt; &lt;span&gt;JAVA_OPTIONS&lt;/span&gt; &lt;span&gt;AW_OPT&lt;/span&gt; &lt;br /&gt;
	-Dweblogic.Name=%SERVER_NAME% -Dweblogic.ProductionModeEnabled=%PRODUCTION_MODE% &lt;br /&gt;
	-Djava.security.policy=&amp;#8220;&lt;span&gt;WL_HOME&lt;/span&gt;\server\lib\weblogic.policy&amp;#8221; weblogic.Server &lt;br /&gt;
&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;As you can see here I am simply setting the &lt;tt&gt;ASPECTWERKZ_HOME&lt;/tt&gt;, passing the definition file to the &lt;span class=&quot;caps&quot;&gt;JVM&lt;/span&gt; using the &lt;tt&gt;-Daspectwerkz.definition.file&lt;/tt&gt; &lt;span class=&quot;caps&quot;&gt;JVM&lt;/span&gt; option and then replacing the regular call to &lt;tt&gt;%JAVA_HOME%\bin\java&lt;/tt&gt; with the call to &lt;tt&gt;%ASPECTWERKZ_HOME%\bin\aspectwerkz&lt;/tt&gt;.&lt;br /&gt;
&lt;p /&gt;&lt;br /&gt;
So now all we have to do is to start up the server and run the examples. &lt;br /&gt;
Running, for example, the &lt;span class=&quot;caps&quot;&gt;CMP&lt;/span&gt; example produces the folllowing output (as you can see AspectWerkz has no problem with the ejbc generated stubs):&lt;br /&gt;
&lt;p /&gt;&lt;br /&gt;
&lt;pre&gt;&lt;br /&gt;
&amp;#8230;&lt;br /&gt;
&lt;del&gt;&amp;#8594; examples.ejb20.basic.containerManaged.containerManaged_9ufdc1_HomeImpl_WLSkel::invoke&lt;br /&gt;
  &amp;#8212;&amp;gt; examples.ejb20.basic.containerManaged.containerManaged_9ufdc1_HomeImpl::create&lt;br /&gt;
    &amp;#8212;&amp;gt; examples.ejb20.basic.containerManaged.containerManaged_9ufdc1__WebLogic_CMP_RDBMS::&lt;i&gt;WL_initialize&lt;br /&gt;
      &amp;#8212;&amp;gt; examples.ejb20.basic.containerManaged.containerManaged_9ufdc1&lt;/i&gt;WebLogic_CMP_RDBMS::&lt;i&gt;WL_initialize&lt;br /&gt;
        &amp;#8212;&amp;gt; examples.ejb20.basic.containerManaged.containerManaged_9ufdc1&lt;/i&gt;WebLogic_CMP_RDBMS::__WL_initialize_persistent&lt;br /&gt;
        &amp;lt; -&lt;/del&gt; examples.ejb20.basic.containerManaged.containerManaged_9ufdc1__WebLogic_CMP_RDBMS::&lt;i&gt;WL_initialize_persistent&lt;br /&gt;
      &amp;lt;&amp;#8212; examples.ejb20.basic.containerManaged.containerManaged_9ufdc1&lt;/i&gt;WebLogic_CMP_RDBMS::&lt;i&gt;WL_initialize&lt;br /&gt;
    &amp;lt;&amp;#8212; examples.ejb20.basic.containerManaged.containerManaged_9ufdc1&lt;/i&gt;WebLogic_CMP_RDBMS::&lt;i&gt;WL_initialize&lt;br /&gt;
    &amp;#8212;&amp;gt; examples.ejb20.basic.containerManaged.containerManaged_9ufdc1&lt;/i&gt;WebLogic_CMP_RDBMS::setEntityContext&lt;br /&gt;
      &lt;del&gt;&amp;#8594; examples.ejb20.basic.containerManaged.AccountBean::setEntityContext&lt;br /&gt;
        &amp;#8212;&amp;gt; examples.ejb20.basic.containerManaged.AccountBean::id&lt;br /&gt;
        &amp;lt; -&lt;/del&gt; examples.ejb20.basic.containerManaged.AccountBean::id&lt;br /&gt;
        &lt;del&gt;&amp;#8594; examples.ejb20.basic.containerManaged.AccountBean::log&lt;br /&gt;
setEntityContext called (12773520, PK = nullctx)&lt;br /&gt;
        &amp;lt; -&lt;/del&gt; examples.ejb20.basic.containerManaged.AccountBean::log&lt;br /&gt;
      &amp;lt;&amp;#8212; examples.ejb20.basic.containerManaged.AccountBean::setEntityContext&lt;br /&gt;
    &amp;lt;&amp;#8212; examples.ejb20.basic.containerManaged.containerManaged_9ufdc1__WebLogic_CMP_RDBMS::setEntityContext&lt;br /&gt;
    &lt;del&gt;&amp;#8594; examples.ejb20.basic.containerManaged.containerManaged_9ufdc1__WebLogic_CMP_RDBMS::__WL_setEJBContext&lt;br /&gt;
    &amp;lt; -&lt;/del&gt; examples.ejb20.basic.containerManaged.containerManaged_9ufdc1__WebLogic_CMP_RDBMS::&lt;i&gt;WL_setEJBContext&lt;br /&gt;
    &amp;#8212;&amp;gt; examples.ejb20.basic.containerManaged.containerManaged_9ufdc1&lt;/i&gt;WebLogic_CMP_RDBMS::&lt;i&gt;WL_setup&lt;br /&gt;
    &amp;lt; &amp;#8212; examples.ejb20.basic.containerManaged.containerManaged_9ufdc1&lt;/i&gt;WebLogic_CMP_RDBMS::&lt;i&gt;WL_setup&lt;br /&gt;
    &amp;#8212;&amp;gt; examples.ejb20.basic.containerManaged.containerManaged_9ufdc1&lt;/i&gt;WebLogic_CMP_RDBMS::&lt;i&gt;WL_setBusy&lt;br /&gt;
    &amp;lt; &amp;#8212; examples.ejb20.basic.containerManaged.containerManaged_9ufdc1&lt;/i&gt;WebLogic_CMP_RDBMS::&lt;i&gt;WL_setBusy&lt;br /&gt;
    &amp;#8212;&amp;gt; examples.ejb20.basic.containerManaged.containerManaged_9ufdc1&lt;/i&gt;WebLogic_CMP_RDBMS::ejbCreate&lt;br /&gt;
      &lt;del&gt;&amp;#8594; examples.ejb20.basic.containerManaged.AccountBean::ejbCreate&lt;br /&gt;
        &amp;#8212;&amp;gt; examples.ejb20.basic.containerManaged.AccountBean::log&lt;br /&gt;
AccountBean.ejbCreate( id = 12773520, PK = 10020, initial balance = $ 3000.0)&lt;br /&gt;
        &amp;lt; -&lt;/del&gt; examples.ejb20.basic.containerManaged.AccountBean::log&lt;br /&gt;
        &lt;del&gt;&amp;#8594; examples.ejb20.basic.containerManaged.containerManaged_9ufdc1__WebLogic_CMP_RDBMS::setAccountId&lt;br /&gt;
        &amp;lt; -&lt;/del&gt; examples.ejb20.basic.containerManaged.containerManaged_9ufdc1__WebLogic_CMP_RDBMS::setAccountId&lt;br /&gt;
        &lt;del&gt;&amp;#8594; examples.ejb20.basic.containerManaged.containerManaged_9ufdc1__WebLogic_CMP_RDBMS::setBalance&lt;br /&gt;
        &amp;lt; -&lt;/del&gt; examples.ejb20.basic.containerManaged.containerManaged_9ufdc1__WebLogic_CMP_RDBMS::setBalance&lt;br /&gt;
        &lt;del&gt;&amp;#8594; examples.ejb20.basic.containerManaged.containerManaged_9ufdc1__WebLogic_CMP_RDBMS::setAccountType&lt;br /&gt;
        &amp;lt; -&lt;/del&gt; examples.ejb20.basic.containerManaged.containerManaged_9ufdc1__WebLogic_CMP_RDBMS::setAccountType&lt;br /&gt;
      &amp;lt;&amp;#8212; examples.ejb20.basic.containerManaged.AccountBean::ejbCreate&lt;br /&gt;
      &lt;del&gt;&amp;#8594; examples.ejb20.basic.containerManaged.containerManaged_9ufdc1__WebLogic_CMP_RDBMS::pkCheck&lt;br /&gt;
      &amp;lt; -&lt;/del&gt; examples.ejb20.basic.containerManaged.containerManaged_9ufdc1__WebLogic_CMP_RDBMS::pkCheck&lt;br /&gt;
      &lt;del&gt;&amp;#8594; examples.ejb20.basic.containerManaged.containerManaged_9ufdc1__WebLogic_CMP_RDBMS::__WL_getPrimaryKey&lt;br /&gt;
      &amp;lt; -&lt;/del&gt; examples.ejb20.basic.containerManaged.containerManaged_9ufdc1__WebLogic_CMP_RDBMS::&lt;i&gt;WL_getPrimaryKey&lt;br /&gt;
    &amp;lt;&amp;#8212; examples.ejb20.basic.containerManaged.containerManaged_9ufdc1&lt;/i&gt;WebLogic_CMP_RDBMS::ejbCreate&lt;br /&gt;
    &lt;del&gt;&amp;#8594; examples.ejb20.basic.containerManaged.containerManaged_9ufdc1__WebLogic_CMP_RDBMS::__WL_getEJBContext&lt;br /&gt;
    &amp;lt; -&lt;/del&gt; examples.ejb20.basic.containerManaged.containerManaged_9ufdc1__WebLogic_CMP_RDBMS::&lt;i&gt;WL_getEJBContext&lt;br /&gt;
    &amp;#8212;&amp;gt; examples.ejb20.basic.containerManaged.containerManaged_9ufdc1&lt;/i&gt;WebLogic_CMP_RDBMS::&lt;i&gt;WL_setLoadUser&lt;br /&gt;
    &amp;lt; &amp;#8212; examples.ejb20.basic.containerManaged.containerManaged_9ufdc1&lt;/i&gt;WebLogic_CMP_RDBMS::&lt;i&gt;WL_setLoadUser&lt;br /&gt;
    &amp;#8212;&amp;gt; examples.ejb20.basic.containerManaged.containerManaged_9ufdc1&lt;/i&gt;WebLogic_CMP_RDBMS::ejbPostCreate&lt;br /&gt;
      &lt;del&gt;&amp;#8594; examples.ejb20.basic.containerManaged.AccountBean::ejbPostCreate&lt;br /&gt;
        &amp;#8212;&amp;gt; examples.ejb20.basic.containerManaged.AccountBean::id&lt;br /&gt;
          &amp;#8212;&amp;gt; examples.ejb20.basic.containerManaged.containerManaged_9ufdc1__WebLogic_CMP_RDBMS::__WL_getMethodState&lt;br /&gt;
          &amp;lt; -&lt;/del&gt; examples.ejb20.basic.containerManaged.containerManaged_9ufdc1__WebLogic_CMP_RDBMS::&lt;i&gt;WL_getMethodState&lt;br /&gt;
          &amp;#8212;&amp;gt; examples.ejb20.basic.containerManaged.containerManaged_9ufdc1&lt;/i&gt;WebLogic_CMP_RDBMS::&lt;i&gt;WL_getMethodState&lt;br /&gt;
          &amp;lt; &amp;#8212; examples.ejb20.basic.containerManaged.containerManaged_9ufdc1&lt;/i&gt;WebLogic_CMP_RDBMS::&lt;i&gt;WL_getMethodState&lt;br /&gt;
        &amp;lt;&amp;#8212; examples.ejb20.basic.containerManaged.AccountBean::id&lt;br /&gt;
        &lt;del&gt;&amp;#8594; examples.ejb20.basic.containerManaged.AccountBean::log&lt;br /&gt;
AccountBean.ejbPostCreate (12773520, PK = 10020)&lt;br /&gt;
        &amp;lt; -&lt;/del&gt; examples.ejb20.basic.containerManaged.AccountBean::log&lt;br /&gt;
      &amp;lt;&amp;#8212; examples.ejb20.basic.containerManaged.AccountBean::ejbPostCreate&lt;br /&gt;
      &amp;#8212;&amp;gt; examples.ejb20.basic.containerManaged.containerManaged_9ufdc1&lt;/i&gt;WebLogic_CMP_RDBMS::&lt;i&gt;WL_create&lt;br /&gt;
        &amp;#8212;&amp;gt; examples.ejb20.basic.containerManaged.containerManaged_9ufdc1&lt;/i&gt;WebLogic_CMP_RDBMS::&lt;i&gt;WL_setBeanParamsForCreateArray&lt;br /&gt;
        &amp;lt; &amp;#8212; examples.ejb20.basic.containerManaged.containerManaged_9ufdc1&lt;/i&gt;WebLogic_CMP_RDBMS::&lt;i&gt;WL_setBeanParamsForCreateArray&lt;br /&gt;
      &amp;lt;&amp;#8212; examples.ejb20.basic.containerManaged.containerManaged_9ufdc1&lt;/i&gt;WebLogic_CMP_RDBMS::&lt;i&gt;WL_create&lt;br /&gt;
    &amp;lt;&amp;#8212; examples.ejb20.basic.containerManaged.containerManaged_9ufdc1&lt;/i&gt;WebLogic_CMP_RDBMS::ejbPostCreate&lt;br /&gt;
    &lt;del&gt;&amp;#8594; examples.ejb20.basic.containerManaged.containerManaged_9ufdc1__WebLogic_CMP_RDBMS::__WL_setBusy&lt;br /&gt;
    &amp;lt; -&lt;/del&gt; examples.ejb20.basic.containerManaged.containerManaged_9ufdc1__WebLogic_CMP_RDBMS::&lt;i&gt;WL_setBusy&lt;br /&gt;
    &amp;#8212;&amp;gt; examples.ejb20.basic.containerManaged.containerManaged_9ufdc1&lt;/i&gt;WebLogic_CMP_RDBMS::ejbStore&lt;br /&gt;
      &lt;del&gt;&amp;#8594; examples.ejb20.basic.containerManaged.containerManaged_9ufdc1__WebLogic_CMP_RDBMS::&lt;i&gt;WL_store&lt;br /&gt;
        &amp;#8212;&amp;gt; examples.ejb20.basic.containerManaged.AccountBean::ejbStore&lt;br /&gt;
          &amp;#8212;&amp;gt; examples.ejb20.basic.containerManaged.AccountBean::id&lt;br /&gt;
            &amp;#8212;&amp;gt; examples.ejb20.basic.containerManaged.containerManaged_9ufdc1&lt;/i&gt;WebLogic_CMP_RDBMS::__WL_getMethodState&lt;br /&gt;
            &amp;lt; -&lt;/del&gt; examples.ejb20.basic.containerManaged.containerManaged_9ufdc1__WebLogic_CMP_RDBMS::&lt;i&gt;WL_getMethodState&lt;br /&gt;
            &amp;#8212;&amp;gt; examples.ejb20.basic.containerManaged.containerManaged_9ufdc1&lt;/i&gt;WebLogic_CMP_RDBMS::&lt;i&gt;WL_getMethodState&lt;br /&gt;
            &amp;lt; &amp;#8212; examples.ejb20.basic.containerManaged.containerManaged_9ufdc1&lt;/i&gt;WebLogic_CMP_RDBMS::&lt;i&gt;WL_getMethodState&lt;br /&gt;
          &amp;lt;&amp;#8212; examples.ejb20.basic.containerManaged.AccountBean::id&lt;br /&gt;
          &lt;del&gt;&amp;#8594; examples.ejb20.basic.containerManaged.AccountBean::log&lt;br /&gt;
AccountBean.ejbStore (12773520, PK = 10020)&lt;br /&gt;
          &amp;lt; -&lt;/del&gt; examples.ejb20.basic.containerManaged.AccountBean::log&lt;br /&gt;
        &amp;lt;&amp;#8212; examples.ejb20.basic.containerManaged.AccountBean::ejbStore&lt;br /&gt;
        &amp;#8212;&amp;gt; examples.ejb20.basic.containerManaged.containerManaged_9ufdc1&lt;/i&gt;WebLogic_CMP_RDBMS::&lt;i&gt;WL_getMethodState&lt;br /&gt;
        &amp;lt; &amp;#8212; examples.ejb20.basic.containerManaged.containerManaged_9ufdc1&lt;/i&gt;WebLogic_CMP_RDBMS::&lt;i&gt;WL_getMethodState&lt;br /&gt;
      &amp;lt;&amp;#8212; examples.ejb20.basic.containerManaged.containerManaged_9ufdc1&lt;/i&gt;WebLogic_CMP_RDBMS::&lt;i&gt;WL_store&lt;br /&gt;
    &amp;lt;&amp;#8212; examples.ejb20.basic.containerManaged.containerManaged_9ufdc1&lt;/i&gt;WebLogic_CMP_RDBMS::ejbStore&lt;br /&gt;
    &lt;del&gt;&amp;#8594; examples.ejb20.basic.containerManaged.containerManaged_9ufdc1__WebLogic_CMP_RDBMS::__WL_isBusy&lt;br /&gt;
    &amp;lt; -&lt;/del&gt; examples.ejb20.basic.containerManaged.containerManaged_9ufdc1__WebLogic_CMP_RDBMS::&lt;i&gt;WL_isBusy&lt;br /&gt;
    &amp;#8212;&amp;gt; examples.ejb20.basic.containerManaged.containerManaged_9ufdc1&lt;/i&gt;WebLogic_CMP_RDBMS::&lt;i&gt;WL_setLoadUser&lt;br /&gt;
    &amp;lt; &amp;#8212; examples.ejb20.basic.containerManaged.containerManaged_9ufdc1&lt;/i&gt;WebLogic_CMP_RDBMS::&lt;i&gt;WL_setLoadUser&lt;br /&gt;
    &amp;#8212;&amp;gt; examples.ejb20.basic.containerManaged.containerManaged_9ufdc1&lt;/i&gt;WebLogic_CMP_RDBMS::&lt;i&gt;WL_isCreatorOfTx&lt;br /&gt;
    &amp;lt; &amp;#8212; examples.ejb20.basic.containerManaged.containerManaged_9ufdc1&lt;/i&gt;WebLogic_CMP_RDBMS::__WL_isCreatorOfTx&lt;br /&gt;
  &amp;lt;&amp;#8212; examples.ejb20.basic.containerManaged.containerManaged_9ufdc1_HomeImpl::create&lt;br /&gt;
&amp;lt;&amp;#8212; examples.ejb20.basic.containerManaged.containerManaged_9ufdc1_HomeImpl_WLSkel::invoke&lt;br /&gt;
&amp;#8230;&lt;br /&gt;
&lt;/pre&gt;&lt;br /&gt;
&lt;p&gt;&lt;br /&gt;
You can find the AspectWerkz 0.8 distribution &lt;a href=&quot;http://aspectwerkz.codehaus.org/releases.html&quot;&gt;here&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;&lt;/pre&gt;&lt;/p&gt;</content>
 </entry>
 
 <entry>
   <title>AspectWerkz makes AOP seamless</title>
   <link href="http://jboner.github.com/2003/08/13/aspectwerkz-makes-aop-seamless"/>
   <updated>2003-08-13T00:00:00+00:00</updated>
   <id>hhttp://jboner.github.com/2003/08/13/aspectwerkz-makes-aop-seamless</id>
   <content type="html">&lt;h1&gt;
&lt;p&gt;AspectWerkz makes &lt;span class=&quot;caps&quot;&gt;AOP&lt;/span&gt; seamless&lt;/p&gt;
&lt;/h1&gt;
&lt;p&gt;
&lt;p&gt;Today me and &lt;a href=&quot;http://blogs.codehaus.org/people/avasseur/&quot;&gt;Alex&lt;/a&gt; released the 0.8 version of &lt;a href=&quot;http://aspectwerkz.codehaus.org&quot;&gt;AspectWerkz&lt;/a&gt;.&lt;/p&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;p&gt;This new release have been tested and verified to work for servlets and all types of EJBs under WebLogic Server 7 and 8.1 using both online and offline mode.&lt;/p&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;p&gt;JMangler has been thrown out for a completely new and customized solution that apart from solving some of the shortcomings and bugs in JMangler also provides completely new and interesting possibilities. Here are the options currently supported for online mode:&lt;/p&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;ul&gt;
&lt;li&gt;
HotSwap&lt;br/&gt;
A first &lt;span class=&quot;caps&quot;&gt;JVM&lt;/span&gt; launchs your target application in a second &lt;span class=&quot;caps&quot;&gt;JVM&lt;/span&gt;.
The first &lt;span class=&quot;caps&quot;&gt;JVM&lt;/span&gt; hooks AspectWerkz in the second one just before the &lt;i&gt;main class&lt;/i&gt; (and all dependencies) gets loaded,
and then connects to the stdout / stderr / stdin stream ot the
second &lt;span class=&quot;caps&quot;&gt;JVM&lt;/span&gt; to make them appear as usual thru the first &lt;span class=&quot;caps&quot;&gt;JVM&lt;/span&gt;.&lt;br/&gt;
&lt;/li&gt;
&lt;li&gt;
Transparent bootclasspath&lt;br/&gt;
For &lt;span class=&quot;caps&quot;&gt;JVM&lt;/span&gt; or java version like 1.3 which don&amp;#8217;t support &lt;i&gt;class replacement at runtime (HotSwap)&lt;/i&gt;, this option
allows for same mechanism by putting an enhanced class loader in the target
application VM bootclasspath.&lt;br/&gt;
&lt;/li&gt;
&lt;li&gt;
Native HotSwap&lt;br/&gt;
A native C &lt;span class=&quot;caps&quot;&gt;JVM&lt;/span&gt; extension running in the target application VM handles the replacement of the
class loader by the enhanced one.&lt;br/&gt;
&lt;/li&gt;
&lt;li&gt;
Remote HotSwap&lt;br/&gt;
The application VM is launched &lt;i&gt;suspended&lt;/i&gt;. The replacement of the enhanced class loader is done
thru a separate manual process, which can easily be scripted.&lt;br/&gt;
&lt;/li&gt;
&lt;li&gt;
Prepared bootclasspath&lt;br/&gt;
The enhanced class loader is builded and packaged as a jar file in a first separate manual process, which can easily be scripted.
The application VM is launched with options to use this enhanced class loader.&lt;br/&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Auto detection of java 1.3 and java 1.4&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;p&gt;Some of the other new features are:&lt;/p&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;span class=&quot;caps&quot;&gt;JDK&lt;/span&gt; 1.3 compatibility.
&lt;/li&gt;
&lt;li&gt;
Runtime attributes &amp;#8594; &lt;span class=&quot;caps&quot;&gt;XML&lt;/span&gt; compiler (no more metaData dir and meta-data compilers needed, one aspectwerkz.xml per application).
&lt;/li&gt;
&lt;li&gt;
Offline compiler refactored. Now support rollback on error facility.
&lt;/li&gt;
&lt;li&gt;
Released under a &lt;span class=&quot;caps&quot;&gt;BSD&lt;/span&gt;-style license.
&lt;/li&gt;
&lt;li&gt;
Non-reentrancy option for join points.
&lt;/li&gt;
&lt;li&gt;
Definition validator.
&lt;/li&gt;
&lt;li&gt;
Documentation updated and reorganized.
&lt;/li&gt;
&lt;/ul&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;p&gt;The new release can be downloaded from &lt;a href=&quot;http://aspectwerkz.codehaus.org/releases.html&quot;&gt;here&lt;/a&gt;.&lt;/p&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;p&gt;A more detailed paper describing the new online architecture can be downloaded from &lt;a href=&quot;http://aspectwerkz.codehaus.org/downloads&quot;&gt;here&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;.&lt;/p&gt;</content>
 </entry>
 
 <entry>
   <title>JMangler changes its license back to LGPL</title>
   <link href="http://jboner.github.com/2003/06/30/jmangler-changes-its-license-back-to-lgpl"/>
   <updated>2003-06-30T00:00:00+00:00</updated>
   <id>hhttp://jboner.github.com/2003/06/30/jmangler-changes-its-license-back-to-lgpl</id>
   <content type="html">&lt;h1&gt;
&lt;p&gt;JMangler changes its license back to &lt;span class=&quot;caps&quot;&gt;LGPL&lt;/span&gt;&lt;/p&gt;
&lt;/h1&gt;
&lt;p&gt;&lt;a href=&quot;http://javalab.iai.uni-bonn.de/research/jmangler/&quot;&gt;JMangler&lt;/a&gt; will change its license back to &lt;span class=&quot;caps&quot;&gt;LGPL&lt;/span&gt; (for some reason they changed to &lt;span class=&quot;caps&quot;&gt;GPL&lt;/span&gt; for the 3.x release).&lt;br /&gt;
&lt;p/&gt;&lt;br /&gt;
The change to &lt;span class=&quot;caps&quot;&gt;GPL&lt;/span&gt; did of course cause a lot of problems for &lt;a href=&quot;http://aspectwerkz.codehaus.org/&quot;&gt;AspectWerkz&lt;/a&gt; (which is licensed under &lt;span class=&quot;caps&quot;&gt;LGPL&lt;/span&gt; and uses JMangler) and I have since then tried to persuade the JMangler team to change its license back to &lt;span class=&quot;caps&quot;&gt;LGPL&lt;/span&gt;. &lt;br /&gt;
&lt;p/&gt;&lt;br /&gt;
Yesterday I recieved this letter from Michael Austermann, the creator of JMangler:&lt;br /&gt;
&lt;blockquote&gt;&lt;br /&gt;
Hi Jonas,&lt;/p&gt;
&lt;p&gt;I&amp;#8217;ve been to canda for the last weeks and was unable to read/reply any&lt;br /&gt;
emails. We will change the the licence of JMangler back to &lt;span class=&quot;caps&quot;&gt;LGPL&lt;/span&gt; within the&lt;br /&gt;
next one or two weeks.&lt;/p&gt;
&lt;p&gt;Michael&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;So for all of you who have hesitated in using &lt;a href=&quot;http://aspectwerkz.codehaus.org/&quot;&gt;AspectWerkz&lt;/a&gt; in a commercial application: go for it, you will love it. :-)&lt;br /&gt;
&lt;p/&gt;&lt;/p&gt;</content>
 </entry>
 
 <entry>
   <title>AspectWerkz ruthlessly refactored</title>
   <link href="http://jboner.github.com/2003/06/20/aspectwerkz-ruthlessly-refactored"/>
   <updated>2003-06-20T00:00:00+00:00</updated>
   <id>hhttp://jboner.github.com/2003/06/20/aspectwerkz-ruthlessly-refactored</id>
   <content type="html">&lt;h1&gt;
&lt;p&gt;AspectWerkz ruthlessly refactored&lt;/p&gt;
&lt;/h1&gt;
&lt;p&gt;After two weeks of Ruthless Refactoring&amp;#8482; I can now announce that AspectWerkz version 0.6.3 has been released.&lt;br /&gt;
&lt;p/&gt;&lt;br /&gt;
I have among other things rewritten the whole definition and weave model implementation to support a much more powerful join point model. The join point model now have the essence of the AspectJ model.&lt;/p&gt;
&lt;p/&gt;
&lt;p&gt;Here are some of the features/changes:&lt;br /&gt;
                    &lt;ul&gt;&lt;br /&gt;
                        &lt;li&gt;&lt;br /&gt;
                            Completely new definition model. Aspects, advices,&lt;br /&gt;
                            introductions and pointcuts are now completely orthogonal&lt;br /&gt;
                            and the model now has the essence of the AspectJ model.&lt;br /&gt;
                            See the documentation for details.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
Abstract aspects definitions as well as pointcut expressions
(e.g.
&lt;code&gt;((pc1 OR pc2) AND !pc3)&lt;/code&gt; and similar).
&lt;/li&gt;
&lt;li&gt;
Multiple weave models.
&lt;/li&gt;
&lt;li&gt;
Multiple AspectWerkz system can run in the same &lt;span class=&quot;caps&quot;&gt;JVM&lt;/span&gt; concurrently.
&lt;/li&gt;
&lt;li&gt;
setField and getField now works for get and set java.util.*
collection fields (e.g. add/get/remove/size and so on).
&lt;/li&gt;
&lt;li&gt;
Advice and introduction container is now pluggable. I.e. the
user can provide its own custom implementation (f.e. to enable
persistence).
&lt;/li&gt;
&lt;li&gt;
The transparent persistence of advices and introductions have
been moved to the sandbox.
&lt;/li&gt;
&lt;li&gt;
Many bug fixes.
&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Here is an example of the new definition:&lt;br /&gt;
&lt;pre&gt;&lt;br /&gt;
&amp;lt;aspectwerkz&amp;gt;&lt;br /&gt;
    &amp;lt;!&amp;#8212; ========= &lt;del&gt;-&amp;gt;&lt;br /&gt;
    &amp;lt;!&lt;/del&gt;-  Define the advices                           &lt;del&gt;-&amp;gt;&lt;br /&gt;
    &amp;lt;!&lt;/del&gt;- ========= &amp;#8212;&amp;gt;&lt;br /&gt;
    &amp;lt;advice-def name=&amp;#8220;log&amp;#8221;&lt;br /&gt;
                advice=&amp;#8220;advices.LoggingAdvice&amp;#8221;&lt;br /&gt;
                deployment-model=&amp;#8220;perInstance&amp;#8221;/&amp;gt;&lt;/p&gt;
&amp;lt;advice-def name=&amp;#8220;cache&amp;#8221;
advice=&amp;#8220;advices.CachingAdvice&amp;#8221;
deployment-model=&amp;#8220;perClass&amp;#8221;/&amp;gt;
&amp;lt;advice-def name=&amp;#8220;persistent&amp;#8221;
advice=&amp;#8220;advices.PersistenceAdvice&amp;#8221;
deployment-model=&amp;#8220;perJVM&amp;#8221;/&amp;gt;
&amp;lt;advices-def name=&amp;#8220;log_and_cache&amp;#8221;&amp;gt;
&amp;lt;advice-ref name=&amp;#8220;log&amp;#8221;/&amp;gt;
&amp;lt;advice-ref name=&amp;#8220;cache&amp;#8221;/&amp;gt;
&amp;lt;/advices-def&amp;gt;
&amp;lt;!&amp;#8212; ========= &amp;#8212;&amp;gt;
&amp;lt;!&amp;#8212;  Define the introductions                     &amp;#8212;&amp;gt;
&amp;lt;!&amp;#8212; ========= &amp;#8212;&amp;gt;
&amp;lt;introduction-def name=&amp;#8220;serializable&amp;#8221;
interface=&amp;#8220;java.io.Serializable&amp;#8221;/&amp;gt;
&amp;lt;introduction-def name=&amp;#8220;mixin&amp;#8221;
interface=&amp;#8220;mixins.Mixin&amp;#8221;
implementation=&amp;#8220;mixins.MixinImpl&amp;#8221;
deployment-model=&amp;#8220;perInstance&amp;#8221;/&amp;gt;
&amp;lt;!&amp;#8212; ========= &amp;#8212;&amp;gt;
&amp;lt;!&amp;#8212;  Define the abstract aspects                  &amp;#8212;&amp;gt;
&amp;lt;!&amp;#8212; ========= &amp;#8212;&amp;gt;
&amp;lt;abstract-aspect name=&amp;#8220;MyAbstractAspect&amp;#8221;&amp;gt;
&amp;lt;advice pointcut=&amp;#8220;setters &lt;span class=&quot;caps&quot;&gt;AND&lt;/span&gt; !getters&amp;#8221;&amp;gt;
&amp;lt;advices-ref name=&amp;#8220;log_and_cache&amp;#8221;/&amp;gt;
&amp;lt;/advice&amp;gt;
&amp;lt;advice pointcut=&amp;#8220;persistentFields&amp;#8221;&amp;gt;
&amp;lt;advice-ref name=&amp;#8220;persistent&amp;#8221;/&amp;gt;
&amp;lt;/advice&amp;gt;
&amp;lt;/aspect&amp;gt;
&amp;lt;!&amp;#8212; ========= &amp;#8212;&amp;gt;
&amp;lt;!&amp;#8212;  Define the aspects                           &amp;#8212;&amp;gt;
&amp;lt;!&amp;#8212; ========= &amp;#8212;&amp;gt;
&amp;lt;aspect name=&amp;#8220;MyAspect&amp;#8221; extends=&amp;#8220;MyAbstractAspect&amp;#8221;&amp;gt;
&amp;lt;introduction class=&amp;#8220;domain.*&amp;#8221;&amp;gt;
&amp;lt;introduction-ref name=&amp;#8220;serializable&amp;#8221;/&amp;gt;
&amp;lt;introduction-ref name=&amp;#8220;mixin&amp;#8221;/&amp;gt;
&amp;lt;/introduction&amp;gt;
&amp;lt;pointcut-def name=&amp;#8220;setters&amp;#8221; type=&amp;#8220;method&amp;#8221; pattern=&amp;#8220;String domain.&lt;strong&gt;.set&lt;/strong&gt;(..)&amp;#8221;/&amp;gt;
&amp;lt;pointcut-def name=&amp;#8220;getters&amp;#8221; type=&amp;#8220;method&amp;#8221; pattern=&amp;#8220;String domain.&lt;strong&gt;.get&lt;/strong&gt;(..)&amp;#8221;/&amp;gt;
&amp;lt;pointcut-def name=&amp;#8220;persistentFields&amp;#8221; type=&amp;#8220;setField&amp;#8221; pattern=&amp;#8220;* domain.&lt;strong&gt;.&lt;/strong&gt;&amp;#8221;&amp;gt;
&amp;lt;/aspect&amp;gt;
&lt;p&gt;&amp;lt;/aspectwerkz&amp;gt;&lt;br /&gt;
&lt;/pre&gt;&lt;br /&gt;
You can download the new release from the &lt;a href=&quot;http://aspectwerkz.codehaus.org/releases.html&quot;&gt;releases page&lt;/a&gt;&lt;br /&gt;
&lt;p/&gt;&lt;br /&gt;
Enjoy.&lt;br /&gt;
&lt;p/&gt;&lt;/p&gt;</content>
 </entry>
 
 <entry>
   <title>JBoss AOP is lacking a decent join point model</title>
   <link href="http://jboner.github.com/2003/06/18/jboss-aop-is-lacking-a-decent-join-point-model"/>
   <updated>2003-06-18T00:00:00+00:00</updated>
   <id>hhttp://jboner.github.com/2003/06/18/jboss-aop-is-lacking-a-decent-join-point-model</id>
   <content type="html">&lt;h1&gt;
&lt;p&gt;JBoss &lt;span class=&quot;caps&quot;&gt;AOP&lt;/span&gt; is lacking a decent join point model&lt;/p&gt;
&lt;/h1&gt;
&lt;p /&gt;
&lt;p&gt;There has been a lot of talk about the JBoss &lt;span class=&quot;caps&quot;&gt;AOP&lt;/span&gt; implementation (and its lack of performance) lately.&lt;/p&gt;
&lt;p /&gt;
&lt;p&gt;When looking at it you can see (as &lt;a href=&quot;http://freeroller.net/page/rickard/20030618#test_of_jboss_aop_performance&quot;&gt;Rickard&lt;/a&gt; and many others already have pointed out) that it has soo many major flaws that it is impossible for it to perform anything but bad.&lt;/p&gt;
&lt;p /&gt;
&lt;p&gt;Apart from this, one of the things that first hit me was that the implementation is completely lacking a  decent join point model (meaning some sort of &amp;#8220;language&amp;#8221; in which you can define which methods/fields etc. should be advised). They are simply advising &lt;strong&gt;everything&lt;/strong&gt; and are then (at runtime) making the desicions if an advice should be invoked or not. This is really amazing, since the key to a good &lt;span class=&quot;caps&quot;&gt;AOP&lt;/span&gt; framework lies in how expressive and rich the join point model is. Those that have used &lt;span class=&quot;caps&quot;&gt;AOP&lt;/span&gt; for some time will agree on that it is not enough to pick out join points on class level only:&lt;br /&gt;
&lt;pre&gt;&lt;br /&gt;
 &amp;lt;!&amp;#8212; from the JBoss tutorial &amp;#8212;&amp;gt;&lt;br /&gt;
 &amp;lt;interceptor-pointcut class=&amp;#8220;com.acme.&lt;span class=&quot;caps&quot;&gt;POJO&lt;/span&gt;.*&amp;#8221;&amp;gt;&lt;br /&gt;
       &amp;lt;interceptors&amp;gt;&lt;br /&gt;
         &amp;lt;interceptor factory=&amp;#8220;jboss.security.SecurityFactory&amp;#8221;/&amp;gt;&lt;br /&gt;
         &amp;lt;interceptor-ref name=&amp;#8220;Logging&amp;#8221;/&amp;gt;&lt;br /&gt;
       &amp;lt;/interceptors&amp;gt;&lt;br /&gt;
   &amp;lt;/interceptor-pointcut&amp;gt;&lt;br /&gt;
&lt;/pre&gt;&lt;br /&gt;
but there is a need for a much more finegrained selection mechanism, or you will run into a scaling wall pretty quick.&lt;/p&gt;
&lt;p /&gt;
&lt;p&gt;In my world I would like to (at least) be able to pick out join points based on:&lt;br /&gt;
&lt;ul&gt;&lt;br /&gt;
	&lt;li&gt;&lt;br /&gt;
		class pattern&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;method pattern, including return type and parameter types&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;field pattern, including field type&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;a mechanism to combine these patterns in an expression (e.g. ((pc1 || pc2) &amp;amp;&amp;amp; pc3) and similar)&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;(Of course &lt;a href=&quot;http://aspectwerkz.codehaus.org/&quot;&gt;AspectWerkz&lt;/a&gt; supports all of these.)&lt;/p&gt;
&lt;p /&gt;
&lt;p&gt;I could not resist to implement and run the same performance tests that &lt;br /&gt;
&lt;a href=&quot;http://freeroller.net/page/rickard/20030618#test_of_jboss_aop_performance&quot;&gt;Rickard&lt;/a&gt; &lt;br /&gt;
did for JBoss &lt;span class=&quot;caps&quot;&gt;AOP&lt;/span&gt;. Here are the results using &lt;a href=&quot;http://aspectwerkz.codehaus.org/&quot;&gt;AspectWerkz&lt;/a&gt; (running on my Pentium 4 2.54 Mhz box):&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;overhead for non-advised method: 0.0 ms/call (since non-advised calls are not advised)&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;overhead for advised method but no advice (advice removed): 0.0003 ms/call&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;invoking a method with one advice that does nothing: 0.0005 ms/call&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;invoking a method with a chain of ten advices that does nothing: 0.0024 ms/call&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;overhead for a call to an introduced method: 0.0004 ms/call&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;</content>
 </entry>
 
 <entry>
   <title>AspectWerkz has moved to codehaus.org</title>
   <link href="http://jboner.github.com/2003/05/13/aspectwerkz-has-moved-to-codehausorg"/>
   <updated>2003-05-13T00:00:00+00:00</updated>
   <id>hhttp://jboner.github.com/2003/05/13/aspectwerkz-has-moved-to-codehausorg</id>
   <content type="html">&lt;h1&gt;
&lt;p&gt;AspectWerkz has moved to codehaus.org&lt;/p&gt;
&lt;/h1&gt;
&lt;p/&gt;
&lt;p&gt;The move to codehaus.org is now completed.&lt;br /&gt;
&lt;p/&gt;&lt;br /&gt;
The new &lt;span class=&quot;caps&quot;&gt;URL&lt;/span&gt; is&lt;br /&gt;
&lt;a href=&quot;http://aspectwerkz.codehaus.org&quot;&gt;aspectwerkz.codehaus.org&lt;/a&gt;.&lt;br /&gt;
&lt;p/&gt;&lt;/p&gt;</content>
 </entry>
 
 <entry>
   <title>AspectWerkz 0.5 has been released</title>
   <link href="http://jboner.github.com/2003/05/12/aspectwerkz-05-has-been-released"/>
   <updated>2003-05-12T00:00:00+00:00</updated>
   <id>hhttp://jboner.github.com/2003/05/12/aspectwerkz-05-has-been-released</id>
   <content type="html">&lt;h1&gt;
&lt;p&gt;AspectWerkz 0.5 has been released&lt;/p&gt;
&lt;/h1&gt;
&lt;p&gt;The AspectWerkz 0.5 release is a huge improvement compared to previous versions. Apart from many bug fixes it is also packed with new features. &lt;br /&gt;
&lt;p/&gt;&lt;br /&gt;
You can download the new release from the &lt;a href=&quot;http://aspectwerkz.codehaus.org/releases.html&quot;&gt;releases page&lt;/a&gt;.&lt;br /&gt;
&lt;p/&gt;&lt;br /&gt;
New features:&lt;br /&gt;
  &lt;ul&gt;&lt;br /&gt;
                        &lt;li&gt;&lt;br /&gt;
                            Caller side pointcuts. I.e. the possibility to advise a&lt;br /&gt;
                            method invocation (caller side) apart from method&lt;br /&gt;
                            execution (callee side).&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
Fine-grained pattern language for picking out pointcuts.
It is now possible to pick out method pointcuts by method name,
parameter types and return type as well as field pointcuts
by field name and field type. All these support regular
expressions.
&lt;/li&gt;
&lt;li&gt;
Runtime attributes for advices (method/field/throws).
I.e. add meta-data to your methods/field/classes as
JavaDoc tags which are picked up by the weaver and
used as rule set when doing the transformations.
&lt;/li&gt;
&lt;li&gt;
Class selectors for aspects. I.e. pick out classes using
regular expressions.
&lt;/li&gt;
&lt;li&gt;
Advising of static fields.
&lt;/li&gt;
&lt;li&gt;
Parameters to advices. The possibility to pass
parameters to advices through the &lt;span class=&quot;caps&quot;&gt;XML&lt;/span&gt; definition.
&lt;/li&gt;
&lt;li&gt;
Advice stacks. The possibility to define stacks/chains
of advices that can be reused throughout the &lt;span class=&quot;caps&quot;&gt;XML&lt;/span&gt; definition.
&lt;/li&gt;
&lt;li&gt;
MethodAdvice has been renamed to AroundAdvice and the
FieldAdvices has been renamed to PreAdvice and PostAdvice.
&lt;/li&gt;
&lt;li&gt;
Creation and registration of new advices at runtime
(in previous releases you could only add/remove/reorder
existing advices).
&lt;/li&gt;
&lt;li&gt;
Ant task for offline post-processing.
&lt;/li&gt;
&lt;li&gt;
More flexible startup and definition handling process.
&lt;/li&gt;
&lt;li&gt;
Multiple pattern declarations for pointcuts.
&lt;/li&gt;
&lt;li&gt;
More documentation and examples.
&lt;/li&gt;
&lt;li&gt;
Many many bugfixes.
&lt;/li&gt;
&lt;/ul&gt;</content>
 </entry>
 
 <entry>
   <title>Cedric's AOP challange: a solution using caller pointcuts and decoupled advices.</title>
   <link href="http://jboner.github.com/2003/05/10/cedrics-aop-challange-a-solution-using-caller-pointcuts-and-decoupled-advices"/>
   <updated>2003-05-10T00:00:00+00:00</updated>
   <id>hhttp://jboner.github.com/2003/05/10/cedrics-aop-challange-a-solution-using-caller-pointcuts-and-decoupled-advices</id>
   <content type="html">&lt;h1&gt;
&lt;p&gt;Cedric&amp;#8217;s &lt;span class=&quot;caps&quot;&gt;AOP&lt;/span&gt; challange: a solution using caller pointcuts and decoupled advices.&lt;/p&gt;
&lt;/h1&gt;
&lt;p&gt;Last night I implemented caller pointcuts for &lt;a href=&quot;http://aspectwerkz.sourceforge.net/&quot;&gt;AspectWerkz&lt;/a&gt;.  I just had to, since they have been haunting me since I read &lt;a href=&quot;http://www.freeroller.net/page/cbeust&quot;&gt;Cedric&amp;#8217;s&lt;/a&gt; little &lt;a href=&quot;http://beust.com/aop-thread.html&quot;&gt;&lt;span class=&quot;caps&quot;&gt;AOP&lt;/span&gt; challange&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;So for what it is worth, here is my way of implementing Cedric&amp;#8217;s caching problem using &lt;a href=&quot;http://aspectwerkz.sourceforge.net/&quot;&gt;AspectWerkz&lt;/a&gt; and caller pointcuts.&lt;/p&gt;
&lt;p&gt;I&amp;#8217;ll start with the cache advice. The &lt;tt&gt;CachingAdvice&lt;/tt&gt;w caches the result and if the cache is used it reports it to the &lt;tt&gt;CacheStatistics&lt;/tt&gt; instance:&lt;/p&gt;
&lt;pre&gt;
public class CachingAdvice extends MethodAdvice {

    protected Map m_cache = new StaticBucketMap(1000);

    public CachingAdvice() {
        super();
    }

    public Object execute(final JoinPoint joinPoint) 
        throws Throwable {
        MethodJoinPoint jp = (MethodJoinPoint)joinPoint;

        final Long hash = new Long(calculateHash(jp));
        final Object cachedResult = m_cache.get(hash);

        if (cachedResult != null) { 
            System.out.println(&quot;using cache&quot;);
            CacheStatistics.addCacheInvocation(
                jp.getMethodName(), jp.getParameterTypes());
            return cachedResult;
        }

        final Object result = joinPoint.proceed();

        m_cache.put(hash, result);
        return result;
    }

    private long calculateHash(final MethodJoinPoint jp) {
        int result = 17;
        result = 37 * result + jp.getMethodName().hashCode();
        Object[] parameters = jp.getParameters();
        for (int i = 0, j = parameters.length; i &amp;lt; j; i++) {
            result = 37 * result + parameters[i].hashCode();
        }
        return result;
    }
}
&lt;/pre&gt;
&lt;p&gt;The second advice I had to implement was the &lt;tt&gt;InvocationCounterAdvice&lt;/tt&gt;. This advice will be applied on the caller side of the method invocation and simply reports the each invocation to the &lt;tt&gt;CacheStatistics&lt;/tt&gt; instance:&lt;/p&gt;
&lt;p&gt;&lt;/pre&gt;&lt;pre&gt;&lt;br /&gt;
public class InvocationCounterAdvice extends PreAdvice {&lt;/p&gt;
public InvocationCounterAdvice() {
super();
}
public void execute(final JoinPoint joinPoint)
throws Throwable {
CallSideJoinPoint jp = (CallSideJoinPoint)joinPoint;
CacheStatistics.addMethodInvocation(
jp.getMethodName(), jp.getParameterTypes());
joinPoint.proceed();
}
&lt;p&gt;}&lt;br /&gt;
&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;The next step was to define these advices and define where they should be applied:&lt;/p&gt;
&lt;pre&gt;
&amp;amp;lt;advice name=&quot;caching&quot;
    class=&quot;examples.caching.CachingAdvice&quot;
    deploymentModel=&quot;perInstance&quot;/&amp;amp;gt;

&amp;amp;lt;advice name=&quot;invocationCounter&quot;
    class=&quot;examples.caching.InvocationCounterAdvice&quot;
    deploymentModel=&quot;perInstance&quot;/&amp;amp;gt;

&amp;amp;lt;aspect class=&quot;examples.caching.Pi&quot;&amp;amp;gt;
    &amp;amp;lt;pointcut type=&quot;method&quot; pattern=&quot;getPiDecimal&quot;&amp;amp;gt;
        &amp;amp;lt;advice-ref name=&quot;caching&quot;/&amp;amp;gt;
    &amp;amp;lt;/pointcut&amp;amp;gt;
&amp;amp;lt;/aspect&amp;amp;gt;

&amp;amp;lt;aspect class=&quot;.*&quot;&amp;amp;gt;
    &amp;amp;lt;pointcut type=&quot;callSide&quot; pattern=&quot;examples.caching.Pi#getPiDecimal&quot;&amp;amp;gt;
        &amp;amp;lt;advice-ref name=&quot;invocationCounter&quot;/&amp;amp;gt;
    &amp;amp;lt;/pointcut&amp;amp;gt;
&amp;amp;lt;/aspect&amp;amp;gt;
&lt;/pre&gt;
&lt;p&gt;In this file I first defined the advices by giving them a name, specifying the class and the deployment model (scope). The second thing was to define the aspects and the pointcuts. By setting an aspect&amp;#8217;s class attribute to &amp;#8220;.*&amp;#8221; I am telling the system to apply its pointcuts to &lt;strong&gt;all&lt;/strong&gt; classes. The pointcuts are defined by specifying the type (method/field/callSide etc.), the pattern (which methods should be advised) and last but not least the references to the advices that should be applied to these poincuts. The call side pointcut&amp;#8217;s pattern consists of the target class and the target method concatenated by a &amp;#8220;#&amp;#8221;. (I don&amp;#8217;t know if this is the best syntax to describe this, but it works for now.)&lt;/p&gt;
&lt;p&gt;Last I had to implement the &lt;tt&gt;CacheStatistics&lt;/tt&gt; class (code not shown here) and a client class:&lt;/p&gt;
&lt;pre&gt;
public class CacheTest {
    public static void main(String[] args) {

        Pi.getPiDecimal(3);
        Pi.getPiDecimal(4);
        Pi.getPiDecimal(3);

        int methodInvocations = CacheStatistics.
            getNrOfMethodInvocationsFor(
                &quot;getPiDecimal&quot;, new Class[]{int.class});
        int cacheInvocations = CacheStatistics.
            getNrOfCacheInvocationsFor(
                &quot;getPiDecimal&quot;, new Class[]{int.class});

        double hitRate = methodInvocations / cacheInvocations;
        System.out.println(&quot;Hit rate: &quot; + hitRate );
    }
}
&lt;/pre&gt;
&lt;p&gt;When running the client class it produces the following output:&lt;br /&gt;
&lt;pre&gt;&lt;br /&gt;
using method&lt;br /&gt;
using method&lt;br /&gt;
using cache&lt;br /&gt;
Hit rate: 3.0&lt;br /&gt;
&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;The complete example can be checked out along with the aspectwerkz distribution from the AspectWerkz&amp;#8217;s &lt;a href=&quot;http://aspectwerkz.sourceforge.net/cvs.html&quot;&gt;&lt;span class=&quot;caps&quot;&gt;CVS&lt;/span&gt; repository&lt;/a&gt; (please note that this feature is still very much in development). You can run the example using &lt;a href=&quot;http://maven.apache.org&quot;&gt;Maven&lt;/a&gt; by executing: &lt;tt&gt;maven aspectwerkz:samples:caching&lt;/tt&gt;&lt;/p&gt;</content>
 </entry>
 
 <entry>
   <title>AOP standardization process started</title>
   <link href="http://jboner.github.com/2003/05/10/aop-standardization-process-started"/>
   <updated>2003-05-10T00:00:00+00:00</updated>
   <id>hhttp://jboner.github.com/2003/05/10/aop-standardization-process-started</id>
   <content type="html">&lt;h1&gt;
&lt;p&gt;&lt;span class=&quot;caps&quot;&gt;AOP&lt;/span&gt; standardization process started&lt;/p&gt;
&lt;/h1&gt;
&lt;p/&gt;
&lt;p&gt;Renaud Pawlak, the creator of &lt;a href=&quot;http://jac.aopsys.com/&quot;&gt;&lt;span class=&quot;caps&quot;&gt;JAC&lt;/span&gt;&lt;/a&gt; has started up a project called &lt;a href=&quot;http://aopi.sourceforge.net&quot;&gt;&lt;span class=&quot;caps&quot;&gt;AOPI&lt;/span&gt;&lt;/a&gt;, which has the goal of specifying a common set of components for aspect-oriented systems.&lt;br /&gt;
&lt;br/&gt;
From the homepage:&lt;br /&gt;
&lt;blockquote&gt;&lt;br /&gt;
&lt;span class=&quot;caps&quot;&gt;AOPI&lt;/span&gt; are a standard specification of a set of useful or mandatory componants for aspect-oriented systems. &lt;br /&gt;
&lt;p/&gt;&lt;br /&gt;
The idea is to have a common set of abstract components specifications, distributed under a Public Domain licence, that will help all the projets to communicate and reuse components coming from other projects.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;This is a really good idea an I&amp;#8217;m looking forward to some interesting discussions on the mailinglist. :-)&lt;br /&gt;
&lt;p/&gt;&lt;/p&gt;</content>
 </entry>
 
 <entry>
   <title>AOP in practice: Transparent persistence for POJOs</title>
   <link href="http://jboner.github.com/2003/05/10/aop-in-practice-transparent-persistence-for-pojos"/>
   <updated>2003-05-10T00:00:00+00:00</updated>
   <id>hhttp://jboner.github.com/2003/05/10/aop-in-practice-transparent-persistence-for-pojos</id>
   <content type="html">&lt;h1&gt;
&lt;p&gt;&lt;span class=&quot;caps&quot;&gt;AOP&lt;/span&gt; in practice: Transparent persistence for POJOs&lt;/p&gt;
&lt;/h1&gt;
&lt;p/&gt;
&lt;p&gt;Using the new techniques of &lt;span class=&quot;caps&quot;&gt;AOP&lt;/span&gt; and the&lt;br /&gt;
&lt;a href=&quot;http://aspectwerkz.sourceforge.net/&quot;&gt;AspectWerkz&lt;/a&gt; framework there is actually&lt;br /&gt;
pretty easy to implement transparent persistence for Plain Old Java Objects (POJOs).&lt;br /&gt;
&lt;br/&gt;
This example has been implemented using &lt;a href=&quot;http://www.coyotegulch.com/jisp/&quot;&gt;&lt;span class=&quot;caps&quot;&gt;JISP&lt;/span&gt;&lt;/a&gt;&lt;br /&gt;
as persistence engine and can be checked out along with the&lt;br /&gt;
&lt;a href=&quot;http://aspectwerkz.sourceforge.net/&quot;&gt;AspectWerkz&lt;/a&gt;&lt;br /&gt;
distribution from the &lt;a href=&quot;http://aspectwerkz.sourceforge.net/cvs.html/&quot;&gt;&lt;span class=&quot;caps&quot;&gt;CVS&lt;/span&gt;&lt;/a&gt;.&lt;br /&gt;
(Please note that the&lt;br /&gt;
&lt;a href=&quot;http://aspectwerkz.sourceforge.net/documentation.html&quot;&gt;documentation&lt;/a&gt;&lt;br /&gt;
is not all up to date).&lt;/p&gt;
&lt;p/&gt;
&lt;p&gt;First we have to write a &lt;code&gt;PostAdvice&lt;/code&gt; similar to this one:&lt;/p&gt;
&lt;pre&gt;
public final class DirtyFieldCheckAdvice extends PostAdvice {

    public DirtyFieldCheckAdvice() {
        super();
    }

    // is being executed when a field has become dirty
    public void execute(final JoinPoint joinPoint) {
        FieldJoinPoint jp = (FieldJoinPoint)joinPoint;
        try {
            PersistenceManager.store(jp.getTargetObject());
        } catch (PersistenceManagerException e) {
            throw new WrappedRuntimeException(e);
        }
    }
}
&lt;/pre&gt;
&lt;p&gt;So, what did we just do? Well, we created a field advice that extended the&lt;br /&gt;
&lt;code&gt;PostAdvice&lt;/code&gt; class. The &lt;code&gt;PostAdvice&lt;/code&gt; abstract base&lt;br /&gt;
class guarantees that the &lt;code&gt;void execute(final JoinPoint joinPoint)&lt;/code&gt;&lt;br /&gt;
will be executed after that a field has been set (modified).&lt;/p&gt;
&lt;p/&gt;
&lt;p&gt;The second step is to tell the&lt;br /&gt;
&lt;a href=&quot;http://aspectwerkz.sourceforge.net/&quot;&gt;AspectWerkz&lt;/a&gt;&lt;br /&gt;
system which fields we want to monitor. This is specified in the &lt;span class=&quot;caps&quot;&gt;XML&lt;/span&gt; definition file:&lt;/p&gt;
&lt;pre&gt;
&amp;amp;lt;aspectwerkz&amp;gt;

    &amp;amp;lt;introduction name=&quot;persistable&quot;
        interface=&quot;aspectwerkz.extension.persistence.Persistable&quot;/&amp;amp;gt;

    &amp;amp;lt;advice name=&quot;makePersistent&quot;
        class=&quot;aspectwerkz.extension.persistence.DirtyFieldCheckAdvice&quot;
        deploymentModel=&quot;perJVM&quot;/&amp;amp;gt;

    &amp;amp;lt;aspect class=&quot;domain.*&quot;&amp;amp;gt;
        &amp;amp;lt;introduction-ref name=&quot;persistable&quot;/&amp;amp;gt;
        &amp;amp;lt;pointcut type=&quot;setField&quot; pattern=&quot;.*&quot;&amp;amp;gt;
            &amp;amp;lt;advice-ref name=&quot;makePersistent&quot;/&amp;amp;gt;
        &amp;amp;lt;/pointcut&amp;amp;gt;
    &amp;amp;lt;/aspect&amp;amp;gt;


&amp;amp;lt;/aspectwerkz&amp;amp;gt;
&lt;/pre&gt;
&lt;p&gt;In this file we first define an&lt;br /&gt;
&lt;a href=&quot;http://aspectwerkz.sourceforge.net/documentation.html#Introductions&quot;&gt;Introduction&lt;/a&gt;&lt;br /&gt;
for the &amp;#8220;marker interface&amp;#8221;&lt;br /&gt;
&lt;code&gt;aspectwerkz.extension.persistence.Persistable&lt;/code&gt; (that will be used by the&lt;br /&gt;
persistence engine to select which objects that should be treated as persistent and&lt;br /&gt;
which should be treated as transient).&lt;/p&gt;
&lt;p/&gt;
&lt;p&gt;Next we define our&lt;br /&gt;
&lt;a href=&quot;http://aspectwerkz.sourceforge.net/documentation.html#Advices&quot;&gt;Advice&lt;/a&gt;&lt;br /&gt;
by giving it a name, mapping it to the full&lt;br /&gt;
class name of the advice and specifying the&lt;br /&gt;
&lt;a href=&quot;http://aspectwerkz.sourceforge.net/documentation.html#Deployment models&quot;&gt;deploymentModel&lt;/a&gt;,&lt;br /&gt;
(which can be seen as the &amp;#8220;scope&amp;#8221; for the advice). There are four different&lt;br /&gt;
deployment models available:&lt;br /&gt;
&lt;ul&gt;&lt;br /&gt;
    &lt;li&gt;&lt;br /&gt;
        &lt;code&gt;perJVM&lt;/code&gt; &amp;#8211; one sole instance per Java Virtual&lt;br /&gt;
        Machince. Basically the same thing as a singleton class.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;perClass&lt;/code&gt; &amp;#8211; one instance per class.
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;perInstance&lt;/code&gt; &amp;#8211; one instance per class instance.
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;perThread&lt;/code&gt; &amp;#8211; one instance per thread.
&lt;/li&gt;
&lt;/ul&gt;
&lt;p/&gt;
&lt;p&gt;Then we have to specify where to apply the &lt;code&gt;Introduction&lt;/code&gt;, i.e.&lt;br /&gt;
which objects we want to mark as persistable and which fields we want to monitor&lt;br /&gt;
using our &lt;code&gt;Advice&lt;/code&gt;.&lt;br /&gt;
&lt;p/&gt;&lt;br /&gt;
This is done by by defining an&lt;br /&gt;
&lt;a href=&quot;http://aspectwerkz.sourceforge.net/documentation.html#Aspects&quot;&gt;Aspect&lt;/a&gt;&lt;br /&gt;
with a pattern matching the classes we want to make persistable.&lt;br /&gt;
In this &lt;code&gt;Aspect&lt;/code&gt; we added a reference to our &lt;code&gt;Introduction&lt;/code&gt; (which tells the system that it should apply this &lt;code&gt;Introduction&lt;/code&gt; to all classes matching the pattern for the &lt;code&gt;Aspect&lt;/code&gt;)&lt;br /&gt;
and defined a &lt;code&gt;Pointcut&lt;/code&gt; for the fields we are interested in (in this case all fields in all classes matching the pattern).&lt;br /&gt;
&lt;p/&gt;&lt;br /&gt;
The &lt;a href=&quot;http://aspectwerkz.sourceforge.net/documentation.html#Pointcuts&quot;&gt;Pointcut&lt;/a&gt;&lt;br /&gt;
is defined by specifying the type of the &lt;code&gt;Advice&lt;/code&gt; (in this&lt;br /&gt;
case &lt;code&gt;setField&lt;/code&gt; since we want to know when a field has been modified), the &lt;code&gt;pattern&lt;/code&gt; for the fields we want the monitor (as a regular expression) and last but not least a reference to to the &lt;code&gt;Advice&lt;/code&gt;s we want to apply to this &lt;code&gt;Pointcut&lt;/code&gt;.&lt;/p&gt;
&lt;p/&gt;
&lt;p&gt;The only thing that is left for getting the whole thing to work is to implement the persistence manager class. This example is based on&lt;br /&gt;
&lt;a href=&quot;http://www.coyotegulch.com/jisp/&quot;&gt;&lt;span class=&quot;caps&quot;&gt;JISP&lt;/span&gt;&lt;/a&gt; but the implementation is made pluggable so it would be a simple task to add support for &lt;span class=&quot;caps&quot;&gt;RDBMS&lt;/span&gt;, &lt;span class=&quot;caps&quot;&gt;JDO&lt;/span&gt; or whatever.&lt;/p&gt;
&lt;p/&gt;
&lt;p&gt;If you choose to check out the example, you can run it using&lt;br /&gt;
&lt;a href=&quot;http://maven.apache.org/&quot;&gt;Maven&lt;/a&gt; by executing&lt;br /&gt;
&lt;code&gt;maven aspectwerkz:samples:transparentpersistence&lt;/code&gt;.&lt;br /&gt;
&lt;p/&gt;&lt;/p&gt;</content>
 </entry>
 
 
</feed>