Akka Actor Model

Posted on Posted in Architect, Java

Akka Actor Model 

Dealing with highconcurrency, highly scalable applications.

Again, just want to emphasize this site does not aim to give a complete comprehension of given topic, but rather an overview, or review of certain topics. It is still recommended to do a reading on the actual documentation.

Recommended Reading:

http://doc.akka.io/docs/akka/current/java/guide/tutorial_1.html

What is Actor Architecture? In a very oversimplified explanation, it is framework to handle highly concurrent applications. Actor aims to eliminate the call stack problem:

  • ProcessA calls a method on ProcessB
  • ProcessB has a very long method that takes more than 5 seconds to complete
  • ProcessA is just waiting for ProcessB to end
  • ProcessB ends and returns a result
  • ProcessA resumes its operations

(Photo reference: http://doc.akka.io/docs/akka/current/java/guide/actors-motivation.html#the-illusion-of-a-call-stack)

With Actor Architecture, Actors are the processes and they just pass around messages, in the scenario above, ProcessA will just the task to ProcessB and continue on.

With that said, lets dive in. Here are some basic jargons/requirements for Actor Architecture:

  1. AbstractActor -classes require to extend AbstractActor to act as an Actor
  2. Receive -method to map method signature ( method from AbstractActor)
  3. Props – configuration class to specify options of creation of actors
  4. ActorSystem – is similar to Spring context, manages lifecycles of Actors
  5. tell – fire and forget mechanism of Actors

Basic Hello world using Actors:

Breaking down the codes:

ActorSystem, creates an hierarchy of your actors that are under whatever name you give it.

Props creates an instance of DemoActor, with possible configuration (none in this case)

DemoActor extends Abstract Actor, with the required method.

RecieveBuilder, creates a Receive for you, which really is just defining the parameters signature of your Actor. In this case if you can accept a parameter String.class, which in turn will just print out “Demoing + parameter”

Your output in the code above will result in the below, notice that “Am I Blocking” is written first even though it is written in the codes last, it is because actors are not blocking, we fired a request to demoactor and the execution continued.

Am I Blocking?
Demoing This is the message sent

Leave a Reply