Getting Started With Akka Remoting

DZone 's Guide to

Getting Started With Akka Remoting

Need help getting started with Akka remoting? We've got you covered.

· Java Zone ·
Free Resource

This article was first published on the Knoldus blog.

When we start with Akka, we generally start with a one actor system on our local machine, but when we talk about a business application, we can have multiple parts of an application and those parts can run on a different machine or node.

Akka remoting is a communication module to connect the actor system in a 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:

libraryDependencies += "com.typesafe.akka" %% "akka-remote" % "2.5.22"                  

Now, let’s start by adding the application.conf file.

akka {
actor {
provider = remote
remote {
enabled-transports = ["akka.remote.netty.tcp"]
netty.tcp {
hostname = ""
port = 2552

The example above only illustrates the minimum properties you must add to enable remoting.

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

Each actor system requires a unique port if shared on the same host or they can use the same port if they are on different machines.

Now, let’s start creating a Worker.

class Worker extends Actor {
def receive = {
case msg: String =>
println(s"Worker received message: $msg”)

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.

object System extends App {
val config = ConfigFactory.load.getConfig("System")

val system = ActorSystem("System", config)

val worker = system.actorOf(Props[Worker], "remote-worker")

println(s"Worker actor path is ${worker.path}")


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 is used to look up an actor on a remote node with actorSelection(path).

val selection = context.actorSelection("akka.tcp://actorSystemName@")

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

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

akka.[protocol]://@[hostname]:[port]/[actor path]


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

Now, to create an 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:

akka {
actor {
deployment {
/sampleActor {
remote = "akka.tcp://sampleActorSystem@"

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

val actor = system.actorOf(Props[Worker], "sampleActor")

That’s all for this blog. In our next blog, we will look at examples of Akka Clustering.

akka ,akka remoting ,cluster ,java ,remote ,remoting ,tutorial

Published at DZone with permission of Prabhat Kashyap , DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}