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?
The actor model has most famously been associated with Erlang, a functional, dynamically typed language invented in 1986 at Ericsson. Erlang was designed for creating applications (such as telephone switches) that must run nonstop.
An actor is a computational entity that, in response to a message it receives, can concurrently: send a finite number of messages to other actors; create a finite number of new actors; designate the behavior to be used for the next message it receives.
The basic Actor Model design pattern is simple. When you hear of an actor, think of it as a computer process or a function. It's some code that you're going to pass a message to, kind of like calling a function. Basically you send the actor instructions and it returns some information back to you.
The main difference is that actors are completely isolated from each other and they will never share memory. It's also worth noting that an actor can maintain a private state that can never be changed directly by another actor.
More recently akka provides an actor framework for Scala and is based on Erlang.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With