01: Simple Akka tutorial in Java step by step

Why use Akka? Akka handles all of the underlying threading complexities by allowing you to just focus on dividing your tasks into actors, defining messages to pass among actors, and wiring together their communication logic.

The real power of Akka is realized when you distribute actors across multiple machines. You could spin off hundreds of Akka servers to run your actors, and the Akka toolkit seamlessly handles the distribution and communication of the messages among the actors.

Step 1: Create a simple Java based Maven project. “-B” is to run the maven archetype plugin in non-intercative mode. In order to learn how to set up maven, look at Setting up Java, Maven, and Eclipse.

Step 2: Import the “simple-akka” into eclipse.

File –> Import –> “Existing Maven Projects” –> “select the pom.xml” file that gets created from the step 1 in the folder “c:\projects\simple-akka”.

Step 3: Open the “pom.xml” file within the imported project and add the “akka-actor_2.10” library as dependency.

Step 4: Let’s write a very simple Akka example where a “Producer” creates numbers from 1 to 10, and a “Consumer”, receives these numbers and just print them. The “Producer” and “Consumer” are asynchronous.

Producer.java in package com.mytutorial

Consumer.java in package com.mytutorial

Step 5: Let’s run the “Producer.java” and demonstrate the asynchronous nature of this akka program.

Output:

As you can see, the printing is not in order.

Step 6: Let’s add a 1 second time delay in generating the numbers in the “Producer.java”.

Output:

Now, you get the output

Some points to remember:

1) What is an ActorSystem in Akka? As per the ActorSystem API, it extends “ActorRefFactory“, and it is a hierarchical group of actors which share common configuration like dispatchers, deployments, remote capabilities and addresses. It is also the entry point for creating or looking up actors .

2) What is an ActorRef in Akka? As per the ActorRef API, it is an immutable and serializable handle to an actor. So, it is thread-safe and fully shareable. “Consumer.java” is an actor in out example.

3) What is a Props class? It is a configuration object used for creating an Actor. it is immutable, hence thread-safe and fully shareable.

4) What is an UntypedActor? As per the API, this class is the Java cousin to the Actor Scala interface. In other words, UntypedActor is simply the name for Actor but as the Java API. Subclass this abstract class to create a MDB-style untyped actor.

5) What is an Actor in Akka? Akka is based on the Actor Model, which is a higher level of abstraction for writing concurrent and distributed systems. It alleviates the developer from having to deal with explicit locking and thread management, making it easier to write correct concurrent and parallel systems.

6) What is a tell method in Akka? tell means “fire-and-forget”, e.g. send a message asynchronously and return immediately. The “tell” method was invoked on the ActorRef “printNumbersConsumer”.

7)system.shutdown();” shuts down the Akka actor system.

8) In Java, if you are not inside an actor or do not want to pass the sender, use ActorRef.noSender() instead.

9) Akka can not only do inter thread processing as shown above, but also can do inter process or inter JVM with akka remoting.

Relevant Akka Tutorial Links

1) Simple Akka tutorial in Java with 2 Actors.


Java Interview FAQs

800+ Java Interview Q&As

Top