Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to do actors (erlang) in java?

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?

like image 698
Shahbaz Avatar asked Apr 27 '09 08:04

Shahbaz


People also ask

Does Erlang use actor model?

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.

What is an actor in coding?

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.

What is actor design pattern?

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.

Do actors share memory?

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.


2 Answers

More recently akka provides an actor framework for Scala and is based on Erlang.

like image 63
manku Avatar answered Oct 11 '22 12:10

manku


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.

like image 33
GaryF Avatar answered Oct 11 '22 12:10

GaryF