2014-09-03 15 views
7

Tôi bắt đầu hệ thống akka với HelloActor trong một JVM và cố gắng gửi tin nhắn cho nó từ máy khách trong một JVM khác. Và không có gì hiệu quả. Làm thế nào tôi nên làm điều đó một cách chính xác? Đây là mã:Làm thế nào để gửi tin nhắn đến hệ thống akka trong jvm lân cận?

Simple server

package akkaSample.severalSystems 

    import akka.actor.{Props, Actor, ActorSystem} 
    import com.typesafe.config.ConfigFactory 

    class HelloActor extends Actor { 
     override def preStart(): Unit = { 
      println("Hello actor started") 
     } 

     def receive = { 
      case "mew" => println("I said mew") 
      case "hello" => println("hello back at you") 
      case "shutdown" => context.stop(self) 
      case _  => println("huh?") 
     } 
    } 

    object Server extends App { 
     val root = ConfigFactory.load() 
     val one = root.getConfig("systemOne") 
     val system = ActorSystem("HelloSystem", one) 
     val helloActor = system.actorOf(Props[HelloActor], "HelloActor") 
     println (system) 
     println("Remote application started.") 
    } 

Simple Khách hàng

package akkaSample.severalSystems.client 

import akka.actor.ActorSystem 
import com.typesafe.config.ConfigFactory 
import scala.util.control.Breaks._ 

class Client { 

} 

object Client extends App { 
    println("started") 

    val root = ConfigFactory.load() 
    val two = root.getConfig("systemTwo") 
    val system = ActorSystem("mySystem", two) 
    //What should be there to access HelloActor? 
    val selection = ... 
    selection ! "mew" 
    println(selection.anchorPath) 
} 

tập tin cấu hình

systemOne { 
    akka { 
    remote { 
     enabled-transports = ["akka.remote.netty.tcp"] 
     netty.tcp { 
     port = 2552 
     } 
    } 
    } 
} 

systemTwo { 
    akka { 
    remote { 
     enabled-transports = ["akka.remote.netty.tcp"] 
     netty.tcp { 
     port = 2553 
     } 
    } 
    } 
} 

Giả sử rằng Máy chủ bắt đầu trước Khách hàng. Bây giờ tôi nhận được "akka: // mySystem/deadLetters" (println(selection.anchorPath)) khi cố gắng để có được diễn viên như vậy system.actorSelection("akka://[email protected]:2552/HelloActor")

Vậy làm thế nào để truy xuất diễn viên từ ActorSystem trong một JVM khác? Hoặc nó không thể cho đến khi tôi tạo ra một cụm, chỉ định hạt giống, nhà lãnh đạo và như vậy?

Trả lời

5

Có vẻ như bạn đã bỏ lỡ thêm RemoteActorRefProvider trong cấu hình. cấu hình diễn viên của bạn nên được như sau:

systemOne { 
    akka { 
    actor { 
     provider = "akka.remote.RemoteActorRefProvider" 
    } 
    remote { 
     enabled-transports = ["akka.remote.netty.tcp"] 
     netty.tcp { 
     port = 2552 
     } 
    } 
    } 
} 

systemTwo { 
    akka { 
    actor { 
     provider = "akka.remote.RemoteActorRefProvider" 
    } 
    remote { 
     enabled-transports = ["akka.remote.netty.tcp"] 
     netty.tcp { 
     port = 2553 
     } 
    } 
    } 
} 

lựa chọn diễn viên được thực hiện sử dụng

context.actorSelection("akka.tcp://[email protected]:2552/user/actorName") 

Bạn không cần phải tạo ra một Akka Cụm có bật tính năng truy cập từ xa.

Thay đổi cấu hình này RemoteActorRefProvider yêu cầu akka-remote làm phụ thuộc.

Các vấn đề liên quan