views:

420

answers:

2

I work on financial applications in Java and getting concurrency right is pain. Erlang and the actors model is supposed to be a good fit for massively concurrent applications but I can't figure out how to do it in Java. I know there are libraries such as Jetlang, FunctionalJava, kilim, etc., but they don't usually go beyond simplistic examples.

Say I need to process three or four different events, like calculating some number from market data feeds, order/trade feeds and 'outputting' some derivative of this data. Most of the time, these events or streams of data need to be processed in order (at least in order with respect to some key...for example, all orders for a specific symbol have to be processed in order, but in parallel with respect to unrelated symbols)

I create a normal Java object with methods which mutate state. Rather than letting those methods directly change state, I put their parameters (by converting them to a command object) in a fifo queue (erlang's mailbox), and a react() method which processes that queue. This way, all updates have to go through a single queue and the react() method can only be accessed one update at a time. Theoretically this should save me the need to lock or synchronize on this method.

However, this queue is basically an producer/consumer queue, which means it is a blocking queue. Blocking is very bad for scalability. Also, having a single queue means all my update command objects (of different types) come out of the queue with some overly generic super type (like Object) and I have to cast them back to the right type and let react() process them.

Once this actorized object produces an output, to be consumed by another such object, I go through the same process. In other words, I've changed the programming model from object oriented, with methods which return results, to some sort of continuation passing nightmare where all my methods become asynchronous.

Any ideas how I can approach this?

+2  A: 

Use one of the excellent Actors libraries that have appeared recently. Alex Miller wrote a good two part piece for Javaworld on Actors.

I also personally quite like Actor's Guild.

GaryF
+2  A: 

You might also want to look into Scala's actors (you could see them as a kind of Java library), see for instance: The busy Java developer's guide to Scala: Dive deeper into Scala concurrency.

Fabian Steeg