package test;
import akka.actor.{ Actor, Props, ActorSystem, ExtendedActorSystem }
import com.typesafe.config.ConfigFactory
import akka.remote._
object MyApp extends App {
val actorSystem1 = ActorSystem("actorSystem1", ConfigFactory.parseString("""
akka {
actor {
provider = "akka.remote.RemoteActorRefProvider"
}
remote {
netty.tcp {
hostname = "127.0.0.1"
port = "2555"
}
}
}
"""))
val provider = actorSystem1.asInstanceOf[ExtendedActorSystem].provider
val boundPort = provider.getDefaultAddress.port.get
System.out.println(boundPort + "!!!")
val actorSystem2 = ActorSystem("actorSystem2", ConfigFactory.parseString("""
akka {
actor {
provider = "akka.remote.RemoteActorRefProvider"
}
remote {
netty.tcp {
hostname = "127.0.0.1"
port = "2554"
}
}
}
"""))
actorSystem1.actorOf(Props(new Actor {
def receive = {
case x: String =>
Thread.sleep(1000)
println("RECEIVED MESSAGE: " + x)
}
}), "simplisticActor")
val remoteActor = actorSystem2.actorSelection("akka.tcp://actorSystem1@127.0.0.1:" + boundPort + "/user/simplisticActor")//127.0.0.1不能写成localhost
remoteActor ! "TEST 1"
remoteActor ! "TEST 2"
remoteActor ! "TEST 3"
Thread.sleep(5000)
actorSystem1.shutdown()
actorSystem2.shutdown()
}
全部从官网同一个akka版本里的
akka-actor_2.10-2.3.11.jar
akka-remote_2.10-2.3.11.jar
config-1.2.1.jar
netty-3.8.0.Final.jar
protobuf-java-2.5.0.jar