Akka (Actor) – Hello World

Theory

In this our main aim is to a very basic hello world in Akka Actor and also realize the basic diff. between on how the actor arch. is diff.

Let’s write the code first

First add the following in the maven file:

<dependency>
	        <groupId>com.typesafe.akka</groupId>
	        <artifactId>akka-actor_2.11</artifactId>
	        <version>2.4.4</version>
</dependency>

Now write the main HelloWorldActor.java file:

import akka.actor.AbstractActor;
import akka.actor.Props;
import akka.japi.pf.ReceiveBuilder;

public class HelloWorldActor extends AbstractActor {
public HelloWorldActor() {
    receive(ReceiveBuilder
        .match(PrintMessage.class, this::sayHello)
        .build());
}

private void sayHello(final PrintMessage message) {
    System.out.println(message.getMessage());
}


public static Props props() {
    return Props.create(HelloWorldActor.class);
}

public static class PrintMessage{
    private String message;
    public PrintMessage(String message) {
        this.message = message;
    }

    public String getMessage() {
        return this.message;
    }
}
}

and in HelloWorldActorMain.java file write :


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

public class HelloWorldActorMain {
	
	public static void main(String[] args) {
		ActorSystem as = ActorSystem.create("hello-world");
		ActorRef helloWorldActor = as.actorOf(HelloWorldActor.props());
		helloWorldActor.tell(new HelloWorldActor.PrintMessage("Hello World"), null);
	}
}

Let’s Understand Actor Perspective

  • We created two files here one is
    • HelloWorldActor – this represents the actor class
      • on constructur we create the behaviour for the actor ie for eg: when actor recieves an Object of PrintMessage class as a Message it will call the sayHello function
      • every actor must describe its behavior on what all messages it can process.
    • HelloWorldActorMain – this is for the main function
  • In main we first created a actor system which we generally create single only per JVM
  • Then we created a ActorRef (it does not represent the HelloWorldActor class object). This is a generic object of ActorRef which takes your messages , queues it and process the actual message on HelloWorldActor object.
  • Now sent a message to Actor – saying “Hello World” in PrintMessage object form.
  • and internally actor call a function sayHello and passing the PrintMessage object as an argument
  • we must realize here that normally we directly call function sayHello of a class and here we are calling function of ActorRef of HelloWorldActor to pass a Message
  • Also one very important thing is diff. here which is normally when we call sayHello of a class same thread go and does the work, but here diff./same thread actually does the work of processing the message
  • When we call the sayHello function our message go into a Queue type object and some other thread read and works on this.

Summary

In this hello world – we write a basic Hello World in Akka (Actor based System)

Also we realized how Actor based development is diff. from the normal function call between classes.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s