Akka Actors vs. Java EJBs from a High-Level Concurrency Perspective
When comparing architectural principles and coding styles in Akka Actors and Java EE 7 Enterprise JavaBeans, specifically stateless session beans and JMS message-driven beans, there are commonalities, e.g. all three types are capable of both vertical and horizontal scaling, and some differences, e.g. in exception and transaction handling, Dr Gerald Loeffler concludes in a recent introductory talk when explaining and comparing the three approaches from a high-level concurrency view.
For his talk Gerald, a Senior Integration Architect, defines high-level concurrency as a programming paradigm where the concurrency is handled at the level of components and is abstracted away as much as possible, above handling individual threads or individual tasks and submitting those to thread pools.
He structures his talk around the idea of the Active Objects design pattern, loosely related to the classic design pattern but with the same kind of ideas. The examples are in Scala but he believes the talk is equally relevant for Java.
A simple mental model from an Active Objects perspective is for Gerald that:
- Akka Actors receive messages as arbitrary objects, enqueued in a mailbox. They are explicitly managed and addressed individually via a common interface.
- Stateless Session Beans expose asynchronous methods executed in an application server with lifecycle controlled by the container.
- JMS Message-Driven Beans receive and process messages of predefined types from a JMS queue/topic. They are container-managed in the same way as session beans.
A common behaviour is that all three types execute asynchronously on threads, typically from a pool, with serialized access to the same instance.
Gerald uses three patterns for his comparison with code examples for all three types:
- Local One-Way Asynchronous Request where an asynchronous request is sent from a requestor to a responder, who will not respond.
- Request Asynchronous Out-of-Context Request where a requestor sends a message to a responder who sends back a return message, and when received the requestor has to re-establish the context.
- Request Asynchronous In-Context Request where the context in some way has been preserved by the requestor when the return message is received.
Finally, Gerald concludes with a short summary of commonalities, e.g. all three approaches uses thread pools and are capable of both vertical and horizontal scaling, and some important differences, e.g. in exception handling, supervisor strategies and transaction handling.
Akka is an open source toolkit and runtime for building concurrent and event-driven applications on the JVM.
Enterprise Java Beans, EJB, is part of the larger Java EE specification, currently Java EE 7.
slides with code
Mike Hartington Jul 26, 2015