How to interrupt Scala Remote Actor client?

I play with Remote Actors, but I have to face 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 wrote 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 ends!

It looks like a lot of threads were running in the client application, but they continue to work after the completion of my main function!

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

Additional info (based on answers): I am using scala 2.8.0.final, and what I'm talking about is a standalone server and a standalone client. They should run as $ scala Server and $ scala Client . I want the Client application to end, but that will never happen.

Thanks!

+4
source share
3 answers

Do not send messages to other players by the actor. While Scala will gladly accept it, it has consequences such as you see. The equivalent code works here:

 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 simply put,

 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!" } } } 
+3
source

In the client code, wrap the selections and related items in the Actor.actor block, then call exit () at the end of the block. You don't seem to need to do this, but this is the only way I found that it terminates all the threads that the select call starts.

+1
source

The following code works for me:

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

All Articles