Reputation: 10565
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?
Upvotes: 5
Views: 1417
Reputation: 3081
You can consider another option, namely Netty along with LMAX Disruptor, both of them written in pure java. Netty is an asynchronous event-driven network application framework for rapid development of maintainable high performance protocol servers & clients.
What is the Disruptor?
LMAX aims to be the fastest trading platform in the world. Clearly, in order to achieve this we needed to do something special to achieve very low-latency and high-throughput with our Java platform. Performance testing showed that using queues to pass data between stages of the system was introducing latency, so we focused on optimising this area.
The Disruptor is the result of our research and testing. We found that cache misses at the CPU-level, and locks requiring kernel arbitration are both extremely costly, so we created a framework which has "mechanical sympathy" for the hardware it's running on, and that's lock-free...
(this taken from https://lmax-exchange.github.io/disruptor/)
Upvotes: 0
Reputation: 3446
Kontraktor is a new actor library designed for java 8. https://github.com/RuedigerMoeller/kontraktor
Upvotes: 0
Reputation: 57304
You might also want to take a look at esper, which is not quite as low-level as the actor frameworks being mentioned, more like a generalized event-processing system you'd build on top of an actor framework. Very mature, complete, and I think developed for complex-event processing in financial transactions.
Upvotes: 0
Reputation: 1278
More recently akka provides an actor framework for Scala and is based on Erlang.
Upvotes: 5
Reputation: 45714
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.
Upvotes: 2
Reputation: 24340
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.
Upvotes: 3