2016-11-11 44 views
0

顆粒狀方法假設我有一個阿卡演員三種方法,我需要從一個演員的客戶端調用:調用在阿卡

class MyActor extends Actor { 
    def receive = { 
    case req1: Request1 => sender ! method1() 
    case req2: Request2 => sender ! method2() 
    case req3: Request3 => sender ! method3() 
    } 
} 

我需要調用不同的組合使用這些方法,例如:

方法1,方法2

方法3

方法1,方法2,方法3

我可以在客戶端的操作方法來調用method1

def invokeMethod1() { 
     val system = ActorSystem("system") 
     implicit val timeout = Timeout(120 seconds) 
     val actor = system.actorOf(Props[MyActor], name = "myactor") 
     val future = actor ? Request1 
     val result = Await.result(future, timeout.duration) 
} 

什麼是調用不同的組合方法,最好的策略是什麼?我應該有invokeMethod1,invokeMethod2invokeMethod3(注意每個人初始化演員系統)?或者我應該在演員身上結合使用方法,例如method1and2?有沒有其他的首選方式?

回答

-1

我建議使用用於循環,

def invokeMetod1:Future[Int] = ??? 
    def invokeMetod2:Future[Int] = ??? 
    def invokeMetod3:Future[Int] = ??? 
    val a = 
    for{ 
     _1 <- invokeMetod1 
     _3 <- invokeMetod3 
     _2 <- invokeMetod2 
    }yield 
     _1 + _2 + _3 

它可以順序執行(invokeMetod1然後invokeMetod3然後invokeMetod2)