0

How can I get the port the Actor is listening on?

I have the following code

import scala.actors.Actor
import scala.actors.Actor._
import scala.actors.remote.RemoteActor._

object DispatchActor extends Actor 
{
  def act() 
  {
    alive(8888)
    register('myService, self)
    println("Listening on port " + howToGetPort)

 ..

  }
}

How can I print the port which the Actor is listening on?

The second question is about calling the alive() and register() methods. These methods are defined in RemoteActor object. How is it possible to call them on an Actor object.

Thanks.

1 Answer 1

1

I don't think the Actor API gives you any way to know which port a particular Actor is listening on (a bit similar to Java RMI now I think of it) so the standard approach in this case is to remember what port you specified when you called alive.

As far as the second part of your question goes, you are not calling the methods 'on' your Actor object, you have already imported the relevant defintions from RemoteActor at the top of your object definition - so you could write your code like this:

import scala.actors.Actor
import scala.actors.Actor._
import scala.actors.RemoteActor

class DispatchActor(port: Int) extends Actor 
{
  def act() 
  {
    RemoteActor.alive(port)
    RemoteActor.register('myService, self)
    println("Listening on port " + port)
  }
}

Note that I've changed your object definition to a class definition to allow it to be parameterised.

2
  • Interesting. So can we think of alive() and register() as static methods in RemoteActor?
    – Nabegh
    Apr 30, 2012 at 18:52
  • If you're coming from Java-land, yes, kind of. Methods in Scala are always attached to an object, these are actually methods on the singleton object RemoteActor.
    – rxg
    Apr 30, 2012 at 18:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.