0

我正在尝试运行服务器和远程演员。两者已成功启动并运行。但是,服务器并没有收到远程工作人员发送的消息。

import akka.actor._
import akka.actor.Props
import akka.event.Logging
import com.typesafe.config.ConfigFactory
import java.security.MessageDigest
import akka.actor.{Actor, ActorSystem, Props}
import akka.routing.RoundRobinRouter

object Project {

  // Define cases
  case class register()

  case class remoteWorkerActive()

  // Main that accepts String argument to determine if the actor
  // is the server or a worker

  def main(args: Array[String]) {

    println("I have " + args.length + " argument(s)")
    println(args(0))
    // Declare configurations for server and worker remote akka actors
    //Attach configuration file of the Server
    val serverConfiguration = ConfigFactory.parseString(
      """
        akka{
          actor{
            provider = "akka.remote.RemoteActorRefProvider"
          }
          remote{
            enabled-transports = ["akka.remote.netty.tcp"]
            netty.tcp{
              hostname = "127.0.0.1"
              port = 2575 
            }
          }
        }""")

    //Attach configuration file of the Worker
    val workerConfiguration = ConfigFactory.parseString(
      """
        akka{
          actor{
            provider = "akka.remote.RemoteActorRefProvider"
          }
          remote{
            enabled-transports = ["akka.remote.netty.tcp"]
            netty.tcp{
              hostname = "127.0.0.1"
              port = 0
            }
          }
        }""")

    // Based on the input argument, declare an actor system as either
    // Server or Worker
    if (!args(0).isEmpty) {
      //Check if the argument is a valid IP address for a worker
      if (args(0).contains('.')) {
        //Create the Worker ActorSystem with the above configuration
        val system = akka.actor.ActorSystem(
          "Remote", ConfigFactory.load(workerConfiguration))

        //Create the worker actor
        val remote = system.actorOf(Props(
          new Remote(args(0))), name = "remote")
        remote ! register()
        println("This actor is a worker")
      }
      else {
        //Create the Server ActorSystem with the above configuration
        val system = akka.actor.ActorSystem(
          "Server", ConfigFactory.load(serverConfiguration))

        //Create the server actor
        val server = system.actorOf(Props[Server], name = "server")
        println("This actor is a server")
        server ! "test successful";
      }
    }
  }

  class Server extends Actor {
    def receive = {
      case remoteWorkerActive() =>
        println("Registration of worker successful")
        sender ! "You are a registered worker"
      case msg: String =>
        println(s"'$msg'")
      case _ =>
        println("Invalid message")
    }
  }

  class Remote(ip_address: String) extends Actor {
    println(ip_address)
    val master = context.actorSelection(
      "akka.tcp://Server@" + ip_address + ":2575/user/Server")

    def receive = {
      case register() =>
        println("Trying to register with the server")
        master ! remoteWorkerActive()
        println("Message sent")
      case msg: String =>
        println(s"'$msg'")
    }
  }
}

我究竟做错了什么?另外,有什么方法可以跟踪正在发送的消息吗?主要用于调试。

4

1 回答 1

1

你有:

val server = system.actorOf(Props[Server], name = "server")

和:

val master = context.actorSelection(
  "akka.tcp://Server@" + ip_address + ":2575/user/Server")

如果 Akka ActorPaths 区分大小写,这将失败。尝试name = "Server"

于 2016-01-07T09:19:29.043 回答