views:

75

answers:

1

I'm playing with RemoteActors. Now I wonder, what happens if I shut down an RemoteActor. The actor was made available with RemoteActor.alive and RemoteActor.register. I can't find the inverse of either of both: alive and register.

How do I properly shut down an RemoteActor?

Update

To make it more obvious, I made a 'small' example. Neither of the following 2 programs terminate, the JVM keeps running. All user created actors and main are finished.

package test

import scala.actors.{OutputChannel, AbstractActor, Actor} , Actor._
import scala.actors.remote.RemoteActor , RemoteActor._


object ActorTestA{
  def main(args :Array[String]) {
    RemoteActor.classLoader = getClass().getClassLoader()
    println("Start A")
    val a = new A().start
    println("Fin A")
  }
}

object ActorTestB{
  def main(args :Array[String]) {
    RemoteActor.classLoader = getClass().getClassLoader()
    println("Start B")
    val b = new B().start
    b !? 'start
    println("Fin B")
  }
}

case class M1(sendRef :AbstractActor, m2 :M2)
case class M2(v1:String, v2 :String, sendRef :AbstractActor)
case class M3(m2 :M2)

object A { val portToUse = 20000 }
class A extends Actor {
  alive(A.portToUse)
  register('A, this)
  val proxy = select(actors.remote.Node("localhost", A.portToUse), 'A)

  def act = { 
    loop { react {
      case M1(sendRef, m2) =>
        println("A receives M1")
        sendRef ! M3(m2)
        self ! 'exit
      case 'exit => 
        println("A exits")
        exit
      case any => println("Unknown Msg: "+any)
    } } 
  }
}


class B extends Actor {
  val portToUse = 20001 
  alive(portToUse)
  register('B, this)
  val proxy = select(actors.remote.Node("localhost", portToUse), 'B)

  var resultTo :OutputChannel[Any] = _
  def act = { 
    loop { react {
      case 'start => 
        println("B starts")
        val a = select(actors.remote.Node("localhost", A.portToUse), 'A)
        a ! M1(proxy, M2("some","val", proxy))
        resultTo = sender
      case M3(M2(v1,v2,sendRef))=>
        println("B receives M3")
        resultTo ! sendRef
        self ! 'exit
      case 'exit => 
        println("B exits")
        exit
      case any => println("Unknown Msg: "+any)
    } } 
  }
}

Output for A is:

Start A
Fin A
A receives M1
A exits

And for B is:

Start B
B starts
B receives M3
B exits
Fin B

The debugger says for A Program, that following 4 non-daemon threads are still up:

  • PlainSocketImpl.socketAccept
  • SocketInputStream.socketRead0
  • ForkJoinScheduler.liftedTree1
  • DestroyJavaVM
+1  A: 

Is Actor.link and Actor.trapExit what you are looking for?

EDIT Try this. I moved everything into the act methods. Also sending proxies between remote actors seemed to cause problems. There's some discussion about this possible bug here: http://www.scala-lang.org/node/6779 ; see the comment from Stefan Kuhn.

package test

import scala.actors.{OutputChannel, AbstractActor, Exit, Debug, Actor} , Actor._
import scala.actors.remote.RemoteActor , RemoteActor._


object ActorTestA{
  def main(args :Array[String]) {
    println("Start A")
    val a = new A().start
    println("Fin A")
  }
}

object ActorTestB{
  def main(args :Array[String]) {
    println("Start B")
    val b = new B().start
    b ! 'start
    println("Fin B")
  }
}

case class AA(hostname: String, port: Int, symbol: Symbol) {
  def proxy = select(actors.remote.Node(hostname, port), symbol)
}
case class M1(sendRef :AA, m2 :M2)
case class M2(v1:String, v2 :String, sendRef :AA)
case class M3(m2 :M2)

object A { val portToUse = 20000 }
class A extends Actor {
  def act = { 
    alive(A.portToUse)
    register('A, this)
    RemoteActor.classLoader = getClass().getClassLoader()
    val proxy = select(actors.remote.Node("localhost", A.portToUse), 'A)

    loop { react {
      case M1(sendRef, m2) =>
        println("A receives M1")
        sendRef.proxy ! M3(m2)
        self ! 'exit
      case 'exit => 
        println("A exits")
        exit
      case any => println("Unknown Msg: "+any)
    } } 
  }
}


class B extends Actor {
  def act = { 
    RemoteActor.classLoader = getClass().getClassLoader()
    val portToUse = 20001 
    alive(portToUse)
    register('B, this)
    var proxy = AA("localhost", portToUse, 'B)
    var resultTo :Option[OutputChannel[Any]] = None

    loop { react {
      case 'start => 
        println("B starts")
        val a = select(actors.remote.Node("localhost", A.portToUse), 'A)
        a ! M1(proxy, M2("some","val", proxy))
        resultTo = Some(sender)
      case M3(M2(v1,v2,sendRef))=>
        println("B receives M3") 
        resultTo match {  
          case Some(ch) => ch ! sendRef.proxy;  
          case None => println("ch missing!?")
        }
        self ! 'exit 
      case 'exit => 
        println("B exits")
        exit
      case any => println("Unknown Msg: "+any)
    } } 
  }
}
Magnus
no, Actor.link and trapExit are used to notify a linked actor if another linked actor terminates. This doesn't work for RemoteActors right now.
Stefan K.
I had to move the RemoteActor.classLoader = getClass().getClassLoader() outside the act methods to be able to run your code. It works. Sending proxies is the root of the problem. Thanks.
Stefan K.