ContentsTopSchedulersRemote Actors

Contents

Index

Remote Actors

This section describes the remote actors API. Its main interface is the RemoteActor object in package scala.actors.remote. This object provides methods to create and connect to remote actor instances. In the code snippets shown below we assume that all members of RemoteActor have been imported; the full list of imports that we use is as follows:

import scala.actors._
import scala.actors.Actor._
import scala.actors.remote._
import scala.actors.remote.RemoteActor._

Starting remote actors

A remote actor is uniquely identified by a Symbol. This symbol is unique to the JVM instance on which the remote actor is executed. A remote actor identified with name 'myActor can be created as follows.

class MyActor extends Actor {
  def act() {
    alive(9000)
    register('myActor, self)
    // ...
  }
}

Note that a name can only be registered with a single (alive) actor at a time. For example, to register an actor A as 'myActor, and then register another actor B as 'myActor, one would first have to wait until A terminated. This requirement applies across all ports, so simply registering B on a different port as A is not sufficient.

Connecting to remote actors

Connecting to a remote actor is just as simple. To obtain a remote reference to a remote actor running on machine myMachine, on port 8000, with name 'anActor, use select in the following manner:

val myRemoteActor = select(Node("myMachine", 8000), 'anActor)

The actor returned from select has type AbstractActor which provides essentially the same interface as a regular actor, and thus supports the usual message send operations:

myRemoteActor ! "Hello!"
receive {
  case response => println("Response: " + response)
}
myRemoteActor !? "What is the meaning of life?" match {
  case 42   => println("Success")
  case oops => println("Failed: " + oops)
}
val future = myRemoteActor !! "What is the last digit of PI?"

Note that select is lazy; it does not actually initiate any network connections. It simply creates a new AbstractActor instance which is ready to initiate a new network connection when needed (for instance, when ! is invoked).


November 30, 2010

ContentsTopSchedulersRemote Actors

Contents

Index