DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
  1. DZone
  2. Coding
  3. Java
  4. Getting Started With Akka Remoting

Getting Started With Akka Remoting

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

Prabhat Kashyap user avatar by
Prabhat Kashyap
·
May. 22, 19 · Tutorial
Like (2)
Save
Tweet
Share
6.35K Views

Join the DZone community and get the full member experience.

Join For Free

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 = "127.0.0.1"
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

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

val selection = context.actorSelection("akka.tcp://actorSystemName@10.0.0.1:2552/user/actorName")


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

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@127.0.0.1:2553"
}
}
}
}


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 (toolkit)

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

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Implementing Adaptive Concurrency Limits
  • DevOps Roadmap for 2022
  • A ChatGPT Job Interview for a Scrum Master Position
  • What Should You Know About Graph Database’s Scalability?

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: