scalaactorremote-actors

How to terminate Scala Remote Actor client?


I'm playing with Remote Actors but I'm facing some difficulties.

Consider this server:

object Server {
    def main(args: Array[String]) {
        val server = new Server
        server.start
    }
}

class Server extends Actor {
    RemoteActor.alive(12345)
    RemoteActor.register('server, this)

    def act() {
        while(true) {
            receive { case x => println(x) }
        }
    }
}

I have written a simple client:

object Client {
    def main(args: Array[String]) {
        val server = RemoteActor.select(Node("localhost", 12345), 'server)
        server ! "Hey!"
    }
}

As expected, the server prints "Hey!".

However, unexpectedly, the client application never terminates!

It looks like many thread have been started on the client application, but they keep running after my main function finishes!

What can I do to terminate the client application? And more: what if I want my client to be able to start and stop connections? How can I achieve this?

Some additional info (based on replies): I'm using scala 2.8.0.final, and what I'm talking about here is a standalone server and a standalone client. They should be launched like $ scala Server and $ scala Client. What I want to happen is that the application "Client" should finish, but it never happens.

Thanks!


Solution

  • Do not send messages to other actors from outside an actor. While Scala will happily accept it, it has consequences such as you are seeing. Here's an equivalent code that works:

    import scala.actors.remote._
    
    object Act extends scala.actors.Actor {
      def act = {
            val server = RemoteActor.select(Node("localhost", 12345), 'server)
            server ! "Hey!"
      }
    }
    
    object Client {
        def main(args: Array[String]) {
          Act.start()
        }
    }
    

    Or, more simply,

    import scala.actors.Actor.actor
    import scala.actors.remote._
    
    object Client {
        def main(args: Array[String]) {
          actor {
            val server = RemoteActor.select(Node("localhost", 12345), 'server)
            server ! "Hey!"
          }        
        }
    }