如何向邻近的jvm中的akka​​系统发送消息?

 人走茶凉 发布于 2022-12-23 17:26

我在一个JVM中使用HelloActor启动了akka系统,并尝试从另一个JVM中的客户端向其发送消息.什么都行不通.我应该如何正确地做到这一点?这是代码:

简单服务器

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.")
    }

简单客户端

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)
}

配置文件

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
      }
    }
  }
}

它假设ServerClient之前启动.现在我收到"akka:// mySystem/deadLetters"(println(selection.anchorPath))时试图让演员这样system.actorSelection("akka://HelloSystem@127.0.0.1:2552/HelloActor")

那么如何在另一个JVM中从ActorSystem中检索actor呢?或者直到我创建一个集群,指定种子,领导等等,这是不可能的?

撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有