Getting Started with Akka Remoting

When we start with Akka we generally start with one actor system on our local machine but when we talk about the business application we can have multiple parts of an application and those part can run on different machine or node. Akka Remoting is a communication module to connect the actor system in peer to peer fashion. It also serves as the foundation for Akka Clustering.

So let’s begin with the implementation.

To start working with Akka remoting we need to add Akka Remoting’s dependency.

Now to start with Akka Remoting let’s start by adding configuration generally application.conf file.

The example above only illustrates the bare minimum of properties you have to add to enable remoting.

First, we should change the provider from local to remote. Second, we added tcp on enable-transport list. At last, we hast hostname and port.

Each Actor System requires a unique port if shared on the same host or they can use the same port if on different machines.

Now let’s start creating a worker.

Our worker actor will print a message when receiving any string message.

Now let’s create our Actor System using the configuration that we defined earlier.

Code is similar to any local actor system the only difference is this is a remote actor because of the configuration we provided.

Akka has two ways of using remoting:

  • Lookup
  • Creation

Lookup

Lookup is used to look up an actor on a remote node with actorSelection(path).

Using the above example we can obtain ActorSelection to an actor on a remote node.

ActorPath provides in the above example can be defined as below if we break it down into its different parts. Now we can use actorSelection in the same way as we do with a local remote.

Creation

Creation is used to create an actor on a remote node using actorOf(…)

Now to create actor using Akka remoting we need to add one more configuration in our application.conf to describe the deployment for remote Akka service. For example:

Once you have configured the properties above you would do the following in code:

That’s all for this blog. In our next blog, we will example Akka Clustering.

Resources:

https://doc.akka.io/docs/akka/current/remoting.html



Knoldus-blog-footer-image

Written by 

Prabhat is a Sr. Software Consultant with more than 2 years of experience in C, C++, Java, MySQL, and Scala. His interests are in Cyber Security, Web Development, and New technologies. Prabhat developed software and website on different platforms which include VB6, VB.NET, ASP.NET, PHP, Wordpress, OpenCart, SMF, VBulletin, HTML5, MyBB. Prabhat is focused and result oriented, self-motivated and team-oriented and effective team player.

Leave a Reply

Knoldus Pune Careers - Hiring Freshers

Get a head start on your career at Knoldus. Join us!