2013-07-21 16 views
0

我想知道如何在Scala中使用Akka。難道我得到它的正確的,我需要做的是不是這樣的:Akka簡而言之

class Class1 { 
    def someMethod1 = { 
    //.... some operations.... 
    "someStringData" 
    } 

    def someMethod2(param1: Int, param2: Double, param3: BigInt) = { 
    //.... some operations.... 
    new someClass 
    } 
} 

//............................. 
object Application extends App { 
    val c = new Class1 
    val stringData = c.someMethod1 
    val someClass = c.someMethod2 
} 

我必須這樣做:

case object SomeMethod 
case class SomeClass(a: Int, b: Double, c: BigInt) 
case class SomeReturnClass(d: Boolean) 

class Class1 extends Actor{ 
    def receive = { 
    case SomeMethod => { 
     //.... some operation.... 
     sender ! "someStringData" 
    } 

    case SomeClass(a, b, c) => { 
    //...some operations.... 
    val result: Boolean = ..... // some operations.... 
    sender ! new SomeReturnClass(result) 
    } 
    } 
} 

//............................. 

object Application extends App { 
    val system = ActorSystem("HelloSystem") 
    val helloActor = system.actorOf(Props[Class1], name = "helloactor") 
    val stringData: String = helloActor ! someMethod1 
    val someClass: SomeReturnClass = helloActor ! someMethod2 
} 

回答

3

你已經得到了基本思路正確,唯一的錯誤是如何你試圖得到演員的答案:請看看ask pattern。演員在某種意義上就像「主動對象」,但並不是每一個對象都應該被翻譯成演員;在執行演員時使用普通對象組成並不罕見。

+0

但我不會在這裏未來。 –

+0

你是什麼意思?你的代碼不會編譯,因爲「!」不會返回你所假設的。 –

+0

你說得對。但是,我看過這些文檔,這就是爲什麼我問這個問題。 –

相關問題