1

Started using scala and really like the language, but I seem to have a hard time understanding how actors work. I previously did a lot of my remote stuff using NIO, but now I want to try scala's remote actors.

I'm trying to create a very simple server and client (see the code below).

  1. The server is started
  2. A remote actor proxy is made (followed this example: http://www.scala-lang.org/docu/files/actors-api/actors_api_guide_6.html)
  3. I send a message to the server through the proxy, and it gets through
  4. I want to shut down my application..... But how!? :). It keeps going forever

code:

package testactors

import scala.collection.mutable.ArrayBuffer
import scala.actors._
import scala.actors.Actor._
import scala.actors.remote._
import scala.actors.remote.RemoteActor._

class Server extends Actor {
    start

    @volatile private var toLive = true

    def act() {

        alive(9000)
        register('server, self)

        while (toLive) {
            receiveWithin(100) {
                case TIMEOUT =>
                case e => println(e)
            }
        }

        println("Server died")
    }

    def killActor() {
        toLive = false
    }

}

object Main {

    def main(args: Array[String]): Unit = {

        val remoteSrc = new Server

        println("Connecting to remote actor")
        var localSrc = select(Node("localhost", 9000), 'server)
        localSrc ! "hello"
        Thread.sleep(1000)

        println("Killing remote actor")
        remoteSrc.killActor

    }

}

The output is:

Connecting to remote actor
hello
Killing remote actor
Server died

But it keeps on going forever..

UPDATE

SORRY! Duplicate, found the answer the 3rd time searching: Is it bad practice to send an actor a message from something which is not an actor?

Community
  • 1
  • 1

0 Answers0