簡體   English   中英

akka向遠程角色發送閉包+在Scala Akka遠程角色之間發送消息(案例類)時出錯

[英]akka sending a closure to remote actor + Error in sending messages (case class) between Scala Akka remote actors

新查詢:我試圖將DSum()作為參數從localActor傳遞給RemoteActor,DSum()將在Remote節點進行一些計算。 我無法將其發送給RemoteActor。 是否可能?(下面的代碼)

完成:我正在嘗試連接Remote actor和local actor,並嘗試使用case類發送對象,但是當從localActor調用時,它無法獲取RemoteActor的Message類(Common.Message(msg))。正在獲取“ case _ => println(“從本地”)接收到未知消息“

 1.package.scala

package object check {
    trait Context
    case object Start
    case class Message(msg: String)
    case class CxtDA(cxtA: List[CxtA])
    case class RCxt(var cxtA: List[CxtA], var cxtB: List[CxtB], var v1: Int, var v2: String) extends Context
    case class CxtA(var cxtC: List[CxtC], var v1: Int) extends Context
    case class CxtB(var cxtC: List[CxtC], var v1: Int) extends Context
    case class CxtC(var v1: String, var v2: Int) extends Context
    case class Task(var t1: DSum()) 

}


2. Remote Actor

package com.akka.remote

import java.io.File

import akka.actor._
import com.typesafe.config.ConfigFactory
import check._

/**
 * Remote actor which listens on port 5150
 */


class RemoteActor extends Actor {

    override def toString: String = {
        return "You printed the Local";
    }
    def receive = {
    case msg: String => {
      println("remote received " + msg + " from " + sender)
      sender ! "hi"
    }
    case Message(msg) =>
        println("RemoteActor received message "+ msg)
        sender ! Message("Hello from server")

    case CxtDA(cxtA) =>
        println("cxtA "+ cxtA)

    case Task(taskA) =>
            println ("recieved closure")


    case _ => println("unknown msg")
  }
}



object RemoteActor{


   def main(args: Array[String]) {
     //get the configuration file from classpath
    val configFile = getClass.getClassLoader.getResource("remote_application.conf").getFile
    // //parse the config
    val config = ConfigFactory.parseFile(new File(configFile))
    // //create an actor system with that config
    val system = ActorSystem("RemoteSystem" , config)
    // //create a remote actor from actorSystem
     val remoteActor = system.actorOf(Props[RemoteActor], name="remote")
     println("remote is ready")
    remoteActor ! Message("Hello from active remote")

   }
 }


3.Local Actor

package com.akka.local

import java.io.File

import akka.actor.{Props, Actor, ActorSystem}
import com.typesafe.config.ConfigFactory
import check._
import scala.util.Random

/**
 * Local actor which listens on any free port
 */
trait CxtTask {
    type CxtT <: Context
    def work(ctx: CxtT): CxtT
}


class DSum extends CxtTask with Serializable{
  override type CxtT = CxtA
    def work(ctx: CxtA): CxtA = {
    val sum = ctx.cxtC.foldRight(0)((v, acc) => v.v2 + acc)
    ctx.cxtC= List()
    ctx.v1 = sum
    println("ctx: " + ctx)
    ctx

  }
}

class LocalActor extends Actor{ 
    // import Common._

  @throws[Exception](classOf[Exception])
    val  remoteActor = context.actorSelection("akka.tcp://RemoteSystem@127.0.0.1:5150/user/remote")
    println("That 's remote:" + remoteActor)
    remoteActor ! "hi"
    var counter = 0  

    override def toString: String = {
        return "You printed the Local";
    }

  def receive = {   

    case msg:String => {
      println("got message from remote" + msg)
    }
    case Start =>
        println("inside Start.local "+ remoteActor)
        remoteActor ! Message("Hello from the LocalActor")


    case Message(msg) =>
         println("LocalActor received message: "+ msg)
        if (counter < 5) {
            sender ! Message("Hello back to you")
            counter += 1
        }

    case CxtDA(cxtA) =>
            remoteActor ! CxtDA(cxtA)

    case Task(t1) =>
            remoteActor ! Task(t1)


  }
}


 object LocalActor {

   def main(args: Array[String]) {

    val configFile = getClass.getClassLoader.getResource("local_application.conf").getFile
    val config = ConfigFactory.parseFile(new File(configFile))
    val system = ActorSystem("ClientSystem",config)
    val localActor = system.actorOf(Props[LocalActor], name="local")
    localActor ! Start

    def createRndCxtC(count: Int):List[CxtC] = (for (i <- 1 to count) yield CxtC(Random.nextString(5), 3)).toList

    def createRndCxtB(count: Int): List[CxtB] = (for (i <- 1 to count) yield CxtB(createRndCxtC(count), Random.nextInt())).toList

    def createRndCxtA(count: Int): List[CxtA] = (for (i <- 1 to count) yield CxtA(createRndCxtC(count), Random.nextInt())).toList

    val tree = RCxt(createRndCxtA(2),createRndCxtB(2),1,"")
    val workA = new DSum()
    tree.cxtA.foreach(ctxa =>workA.work(ctxa))
    localActor ! Task(new DSum())
  }
}

[Remote actor output][1]


  [1]: https://i.stack.imgur.com/mtmvU.jpg

關鍵是您為每個參與者定義了兩種不同的協議:

  • 駐留在RemoteActor.scala文件中的Common對象
  • 駐留在LocalActor.scala文件中的Common對象

因此,當在本地Actor中發送Common.Message時,基本上是在創建與遠程Actor的Common.Message類型不同的消息。 因此,演員無法對其進行處理。

作為Akka的一種良好做法,每當演員有特定的消息procotol時,都應在其伴隨對象中定義。 但是,如果您有多個共享同一協議的參與者(它們的行為是通過處理這些類型的消息來定義的),則應將該協議放在一個對象中並從參與者中導入。

我希望這是有幫助的。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM