Akka Actors in Java - Part 1



Whenever we are dealing with the large and enterprise applications we always need to focus more on synchronizations and locks. This stuffs are more complex and also is time-consuming. But with Akka, we can easily write asynchronous code without the need for locks and synchronization. Akka is based on the message-driven terminology so that we don't need to wait for a return value instead the receiver will reply the sender with a message that will contain the result. And all actors are monitored by the parents so that it will also be easier to notify and stop the actors if the failure happens. In order to use Actors in our system lets first add akka-actor dependency in our pom.xml

<dependencies>
    <dependency>
        <groupId>com.typesafe.akka</groupId>
        <artifactId>akka-actor_2.12</artifactId>
        <version>2.5.11</version>
    </dependency>
</dependencies>

//As we discussed all the actors will be in the hierarchy so lets create first the actor system
//in which our actors will be resides
//actors who share the common configuration should be in the same Actor system
ActorSystem system = ActorSystem.create("system");


In order to create a Akka actor we need to extend our actor with AbstractActor abstract class
and should also need to implement the createReceive() method for handling the incoming messages.

import akka.actor.AbstractActor;
public class SimpleActor extends AbstractActor {
  public Receive createReceive() {
    return receiveBuilder()
        .match(String.class, r -> {
          System.out.println("Message received: "+r);
        })
        .match(Integer.class, r -> {
          System.out.println("Integer received: "+r);
        })
        .build();
  }
}

here this actor will look for two types of messages one is for String and another for Integer
which means if this actor will receive the string message then it will print
message Received:: <message>

Once the actor is created we then need to include it in the actor system like this
ActorRef simpleActor = system.actorOf(Props.create(SimpleActor.class), "simpleActor");

There are three kinds of methods to send a message to the actor tell(), ask() and forward(). For now, we are using tell() because it does not expect any response from the actor. To send the message with tell we need a message and actorsRef like this

simpleActor.tell(<Message>, <ActorRef>);

import akka.actor.ActorRef;
import akka.actor.ActorSystem;
import akka.actor.Props;

public class SimpleActorSystem{
  public static void main(String[] args) {
    ActorSystem system = ActorSystem.create("system");
    ActorRef simpleActor = system.actorOf(Props.create(SimpleActor.class), "simpleActor");
    simpleActor.tell("hi there", simpleActor);
    simpleActor.tell(1, simpleActor);
  }
}
So in the above example we have sent two messages to our actor one is String and another is Integer and once the actor receives the message then it will print the output accordingly.
Output::Message received: hi there
Integer received: 1
Happy Coding \,,/

Comments

Popular posts from this blog

Functional Programming in Java - Part 1

Communication between two Actor's In scala

Stopping the Actor in Akka