Lightbend Activator

Akka Remote Samples with Scala

Activator will be EOL-ed on May 24, 2017.

We’re making it easier and simpler for developers to get started with Lightbend technologies. This unfortunately means that future releases of Play, Akka and Scala will no longer include Activator support, and Lightbend’s Activator server will be decommissioned by the end of 2017. Instead of supporting Activator to create and set up development projects, we'll be supporting standard Giter8 templates for sbt users and Maven archetypes for Maven users. So going forward,

To create new Lightbend projects

Instead of using the Activator command, make sure you have sbt 0.13.13 (or higher), and use the “sbt new” command, providing the name of the template. For example, “$ sbt new akka/hello-akka.g8”. You can find a list of templates here.

Also, as a convenience, the Lightbend Project Starter allows you to quickly create a variety of example projects that you just unzip and run.

To create new templates

If you want to create new templates, you can now do that in Giter8.

To migrate templates from Activator to Giter8

If you created Activator templates in the past, please consider migrating them to Giter8 with this simple process.

Akka Remote Samples with Scala

Akka Team
Source
September 30, 2016
akka remote scala sample

Akka Remote Samples with Scala

How to get "Akka Remote Samples with Scala" on your computer

There are several ways to get this template.

Option 1: Choose akka-sample-remote-scala in the Lightbend Activator UI.

Already have Lightbend Activator (get it here)? Launch the UI then search for akka-sample-remote-scala in the list of templates.

Option 2: Download the akka-sample-remote-scala project as a zip archive

If you haven't installed Activator, you can get the code by downloading the template bundle for akka-sample-remote-scala.

  1. Download the Template Bundle for "Akka Remote Samples with Scala"
  2. Extract the downloaded zip file to your system
  3. The bundle includes a small bootstrap script that can start Activator. To start Lightbend Activator's UI:

    In your File Explorer, navigate into the directory that the template was extracted to, right-click on the file named "activator.bat", then select "Open", and if prompted with a warning, click to continue:

    Or from a command line:

     C:\Users\typesafe\akka-sample-remote-scala> activator ui 
    This will start Lightbend Activator and open this template in your browser.

Option 3: Create a akka-sample-remote-scala project from the command line

If you have Lightbend Activator, use its command line mode to create a new project from this template. Type activator new PROJECTNAME akka-sample-remote-scala on the command line.

Option 4: View the template source

The creator of this template maintains it at https://github.com/akka/akka.

Option 5: Preview the tutorial below

We've included the text of this template's tutorial below, but it may work better if you view it inside Activator on your computer. Activator tutorials are often designed to be interactive.

Preview the tutorial

In order to showcase the remote capabilities of Akka we thought a remote calculator could do the trick. This sample demonstrates both remote deployment and look-up of remote actors.

Lookup Remote Actors

This sample involves two actor systems.

  • CalculatorSystem listens on port 2552 and starts one actor, the CalculatorActor that provides a service for arithmetic operations.
  • LookupSystem listens on port 2553 and starts one actor, the LookupActor that sends operations to the remote calculator service.

Open LookupApplication.scala.

There you see how the two actor systems and actors are started. In this first step they are running in the same JVM process, but you can run them in separate processes as described later. Note that this changes nothing in the configuration or implementation.

The two actor systems use different configuration, which is where the listen port is defined. The CalculatorSystem uses calculator.conf and the LookupSystem uses remotelookup.conf.

Note that the configuration files also import the common.conf. This enables the remoting by installing the RemoteActorRefProvider and chooses the default remote transport. Be sure to replace the default IP 127.0.0.1 with the real address the system is reachable by if you deploy onto multiple machines!

The CalculatorActor does not illustrate anything exciting. More interesting is the LookupActor. It takes a String path as constructor parameter. This is the full path, including the remote address of the calculator service. Observe how the actor system name of the path matches the remote system’s name, as do IP and port number. Top-level actors are always created below the "/user" guardian, which supervises them.


"akka.tcp://CalculatorSystem@127.0.0.1:2552/user/calculator"

First it sends an Identify message to the actor selection of the path. The remote calculator actor will reply with ActorIdentity containing its ActorRef. Identify is a built-in message that all Actors will understand and automatically reply to with a ActorIdentity. If the identification fails it will be retried after the scheduled timeout by the LookupActor.

Note how none of the code is specific to remoting, this also applies when talking to a local actor which might terminate and be recreated. That is what we call Location Transparency.

Once it has the ActorRef of the remote service it can watch it. The remote system might be shutdown and later started up again, then Terminated is received on the watching side and it can retry the identification to establish a connection to the new remote system.

Run the Lookup Sample

To run this sample, go to the Run tab, and start the application main class sample.remote.calculator.LookupApplication if it is not already started.

In the log pane you should see something like:


Started LookupSystem
Calculating 74 - 42
Sub result: 74 - 42 = 32
Calculating 15 + 71
Add result: 15 + 71 = 86

The two actor systems are running in the same JVM process. It can be more interesting to run them in separate processes. Stop the application in the Run tab and then open two terminal windows.

Start the CalculatorSystem in the first terminal window with the following command (on one line):


<path to activator dir>/activator 
  "run-main sample.remote.calculator.LookupApplication Calculator"		

Start the LookupSystem in the second terminal window with the following command (on one line):


<path to activator dir>/activator 
  "run-main sample.remote.calculator.LookupApplication Lookup"		

Thereafter you can try to shutdown the CalculatorSystem in the first terminal window with 'ctrl-c' and then start it again. In the second terminal window you should see the failure detection and then how the successful calculation results are logged again when it has established a connection to the new system.

Create Remote Actors

This sample involves two actor systems.

  • CalculatorWorkerSystem listens on port 2552
  • CreationSystem listens on port 2554 and starts one actor, the CreationActor that creates remote calculator worker actors in the CalculatorWorkerSystem and sends operations to them.

Open CreationApplication.scala.

There you see how the two actor systems and actors are started. In this first step they are running in the same JVM process, but you can run them in separate processes as described later.

The two actor systems use different configuration, which is where the listen port is defined. The CalculatorWorkerSystem uses calculator.conf and the CreationSystem uses remotecreation.conf.

Note that the configuration files also import the common.conf. This enables the remoting by installing the RemoteActorRefProvider and chooses the default remote transport. Be sure to replace the default IP 127.0.0.1 with the real address the system is reachable by if you deploy onto multiple machines!

The CreationActor creates a child CalculatorActor for each incoming MathOp message. The configuration contains a deployment section that matches these child actors and defines that the actors are to be deployed at the remote system. The wildcard (*) is needed because the child actors are created with unique anonymous names.


akka.actor.deployment {
  /creationActor/* {
    remote = "akka.tcp://CalculatorWorkerSystem@127.0.0.1:2552"
  }
}

Error handling, i.e. supervision, works exactly in the same way as if the child actor was a local child actor. In addtion, in case of network failures or JVM crash the child will be terminated and automatically removed from the parent even though they are located on different machines.

Run the Creation Sample

To run this sample, go to the Run tab, and start the application main class sample.remote.calculator.CreationApplication if it is not already started.

In the log pane you should see something like:


Started CreationSystem
Calculating 7135 / 62
Div result: 7135 / 62 = 115.08
Calculating 0 * 9
Mul result: 0 * 9 = 0

The two actor systems are running in the same JVM process. It can be more interesting to run them in separate processes. Stop the application in the Run tab and then open two terminal windows.

Start the CalculatorWorkerSystem in the first terminal window with the following command (on one line):


<path to activator dir>/activator 
  "run-main sample.remote.calculator.CreationApplication CalculatorWorker"		

Start the CreationSystem in the second terminal window with the following command (on one line):


<path to activator dir>/activator 
  "run-main sample.remote.calculator.CreationApplication Creation"		

Thereafter you can try to shutdown the CalculatorWorkerSystem in the first terminal window with 'ctrl-c' and then start it again. In the second terminal window you should see the failure detection and then how the successful calculation results are logged again when it has established a connection to the new system.

×

Welcome to the Lightbend Enterprise Suite


You are excited about Reactive applications. And you want to build, manage and monitor them easily and reliably in your environment.
We get it. So we built Lightbend Enterprise Suite to help you do that, and more, with a range of powerful Application Management, Intelligent Monitoring, Enterprise Integration and Advanced Tooling features.