I am new to scala and trying to use the Actor model. I have worked my way with using actors on the same machine. Now, I want to use remote actors to go a step further. As I have only one box to play with, I am planning to start a scala process which will act as a remote actor
remote.scala looks like
import scala.actors._
import scala.actors.remote._
import scala.actors.remote.RemoteActor._
import Actor._
import scala.math._
class remoteActor extends Actor{
def act(){
alive(9010)
register('myActor, self)
while (true)
{
println("Started Remote Actor")
receive {
case (caller :Actor, index :Int, length :Int) =>
{ // Do some stuff
}
}
}
}
}
object hello {
def main(args: Array[String]): Unit = {
println("Hello")
val act = new remoteActor
act.start
}
}
The main program which will use this remote actor
actor.scala
import scala.actors._
import scala.actors.remote._
import scala.actors.remote.RemoteActor._
import Actor._
class masterActor extends Actor{
def act()
{
val myRemoteActor = select(Node("localhost", 9010), 'myActor)
myRemoteActor ! (self,3,2)
}
}
object hello {
def main(args: Array[String]): Unit = {
val sample = new masterActor
sample.start
}
}
The problem is when I run remote.scala using eclipse (by installing the scala plugin for eclipse) I get the
Hello
Started Remote Actor
message printed in the eclipse console. But when I run the same program from Windows command line after installing scala.
C:\Program Files (x86)\scala\bin>scala remote.scala
then the messages do not get printed. However, if I change the remote.scala file to just include the hello world message, (i.e. remote.scala looks like)
object hello {
def main(args: Array[String]): Unit = {
println("Hello")
}
}
then the Hello message gets printed on the Windows command prompt. Why is Windows command prompt not printing the messages in case of original remote.scala?
What I want is to start this scala process where the remote actor is registered and the process is still alive. I then want to start the main.scala program from another command prompt so that it sends messages to the remote actor to perform the computations. How can I make sure that the remote actor is still alive and registered and the process is running?
Don't include the actor reference in your message. The receiving pattern should be:
And the message should be sent as
myRemoteActor ! (3,2)
In the receiver you can just write
sender
to refer to the original message sender. There are other useful functions, e.g.reply(msg)
- replies to the original sender withmsg