Search code examples
javascalaparallel-processingactorreactive-programming

Parallel program running in scala


I am able to create actors and run them locally. But now I want the actors to run code on connected computers on network. Say I want to create actors to find factorial of a number.

object FactorialAll extends Actor{
   def receive = {
     case (n:Int)=>
         for(i<-1 to n){
          var factorActor = context.actorOf(Prop[FactorActor],"factorActor")
          factorActor ! Factorial(i)
         }
     case Result(n:Int,fact:BigInt)=>
        println("factorial of " + n + " is "+fact)
   }
}

and we send some int to this Actor.

How can I create and run FactorActor instances to run on network.


Solution

  • To just send message to a remote actor, you need Akka Remoting: http://doc.akka.io/docs/akka/current/scala/remoting.html

    Then, you can get remote actor and send a message like:

    val selection =
      context.actorSelection("akka.tcp://actorSystemName@host:1234/user/actorName")
    
    selection ! "fooBar"