Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mkhq/827341 to your computer and use it in GitHub Desktop.
Save mkhq/827341 to your computer and use it in GitHub Desktop.
two remote actors, register one send ping and reply with pong
import akka.remoteinterface.RemoteSupport
import akka.remote.netty.NettyRemoteSupport
import akka.actor.Actor.actorOf
import akka.actor.{Actor}
import org.specs._
import java.util.concurrent.{CountDownLatch, TimeUnit}
object ServiceState {
val latch = new CountDownLatch(1)
}
case class Register(serviceName:String, hostName:String, port:Int)
case class Ping()
case class Pong()
class PingPongWithRegisterService(server:RemoteSupport) extends Actor {
self.homeAddress = Some(server.address)
def receive:Receive = {
case msg @ Register(serviceName, hostName, port) =>
log.debug(self.homeAddress + " received msg: " + msg)
val actor = server.actorFor(serviceName, hostName, port)
actor ! Ping()
case Ping() =>
log.debug(self.homeAddress + " received ping!")
self.reply(Pong())
case Pong() =>
log.debug(self.homeAddress + " received pong!")
ServiceState.latch.countDown
}
}
object PingPongWithRegisterServiceSpec extends Specification {
var servers = List[RemoteSupport]()
def startServer(hostname:String, port:Int):RemoteSupport = {
val s = new NettyRemoteSupport()
s.start(hostname, port)
s.optimizeLocal.set(false)
servers = s :: servers
s
}
"a service" should {
doAfter {
servers.foreach(_.shutdown)
Actor.remote.shutdown
Actor.registry.shutdownAll
Thread.sleep(1000)
}
"req. and reply" in {
val s1 = startServer("localhost", 9094)
val s2 = startServer("localhost", 9095)
val a1 = actorOf(new PingPongWithRegisterService(s1))
val a2 = actorOf(new PingPongWithRegisterService(s2))
// val a1id = "uuid:" + a1.uuid
// val a2id = "uuid:" + a2.uuid
val a1id = "test1"
val a2id = "test2"
s1.register(a1id, a1)
s2.register(a2id, a2)
val ref = Actor.remote.actorFor(a1id, "localhost", 9094)
ref ! Register(a2id, "localhost", 9095)
ServiceState.latch.await(2000, TimeUnit.MILLISECONDS)
ServiceState.latch.getCount must be(0L)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment