Deprecated: Function set_magic_quotes_runtime() is deprecated in /home/raedan/public_html/textpattern/lib/txplib_db.php on line 14
State of Flow: Fly Scala!::journal

Fly Scala!

Channing Walton - Tuesday October 28, 2008

Porting the Java client library for the Fly Object Space to Scala.

Fly is a lightweight Object Space that can distribute and coordinate information on clusters of computers in the form of Objects. You can read more about fly at

Fly’s client library is written in Java, however I have been working on a Scala version, and there is a Ruby version in the works too.

The Scala port started as a syntactic conversion of the Java client, mostly a brute force conversion of Java syntax into Scala syntax. Since the Java client is small, this only took a few hours. It would have been quicker if I knew more about Scala, but this was my first real Scala project.

After the syntactic port, came more Scala-like idioms. Pattern matching was the first major departure from the Java version. The Java API includes the FlyPrime interface which provides basic operations on the space for writing, reading and taking objects that match a template:

<T> T read(T template, long waitTime);

The Scala version of this method, defined in a trait, looks like this:

def read[T <: AnyRef](template:T, waitTime:Long):Option[T]

The difference is that it returns an Option[T], which means that clients are made aware of the possibility that an object matching the supplied template may not be found. In the Java version, null is returned. In the Scala version, None is returned and can be matched like this:, 0L) match { case None => ... do something about not finding an entry case Some(x) => ... work with x, the matching object }

You can do this in Java with libraries like JEDI, but its clumsy.

Scala is a functional language, and so techniques unavailable to the Java API can be used. The Notifly interface in Java sets up a NotifyHandler to be notified when an object matching a template is added to the space:

boolean notify(Object template, NotifyHandler handler, long leaseTime);

The Scala Notifly trait supports three methods:

def notify(template:AnyRef, handler:NotifyHandler, leaseTime:Long):Boolean def notify(template: AnyRef, leaseTime: Long) (block: => Unit): Boolean def notify(template: AnyRef, leaseTime:Long, actor: Actor):Boolean

The first method is the same as the Java one.

The second makes use of some Scala syntactic sugar that enables a closure to be invoked when a matching object is added to the space. It is used like this:

fly.notify(template, 1000L) { println("Block Template matched!") }

(Thanks to Phillip Haller for the suggestion.)

The third method in the Notifly trait supplies an Actor to be notified when a matching object is added to the space. More about actors here

A simple example is:

val myActor = actor { loop { react { case FlyPrime.ACTOR_MESSAGE => println("Actor received a message") } } } fly.notify(template, 1 * 1000L, myActor)

The Scala version of Fly is about 2/3rds the size of the Java one, and runs at a comparable speed, which is not surprising.

During my experience of the port I found myself wishing that I could use Scala at work. Java seems so basic and verbose by comparison. I have been looking for a language like Scala for quite a while: a language supporting functional programming and object oriented programming.

A big attraction to Scala is that it runs on a JVM, and can make use of Java libraries almost invisibly. One can learn a new language fairly quickly (unless its Haskell and you don’t have a brain the size of a planet), but far greater effort and time is spent learning how to use the frameworks, libraries and idioms that come with it. With Scala, the world of Java frameworks and libraries is there for you to use, and you can slowly migrate towards a more functional style over time which has a lot of benefits.

I have also started work on an Eclipse plugin for viewing the status of objects in a Fly space. Its just a simple view at the moment but will grow in the coming months. The update site is

I welcome feedback about the port, the source is provided in the zip which can be downloaded here. I am new to Scala and I am sure that there are many improvements that can be made.