2017-08-04 106 views
0

我下面這個教程中lagom執行流程 - https://www.lagomframework.com/documentation/1.3.x/scala/ServiceImplementation.html無法理解

我創建了一個記錄的維修

//logged takes a ServerServiceCall as argument (serviceCall) and returns a ServerServiceCall. 
    //ServerServiceCall's compose method creates (composes) another ServerServiceCall 
    //we are returing the same ServerServiceCall that we received but are logging it using println 
    def logged[Request,Response](serviceCall:ServerServiceCall[Request,Response]) = { 
    println("inside logged"); 

    //return new ServerServiceCall after logging request method and uri 
    ServerServiceCall.compose({ 
    requestHeader=>println(s"Received ${requestHeader.method} ${requestHeader.uri}") 
    serviceCall 
    } 
)} 

我用記錄如下:

override def hello3 = logged(ServerServiceCall 
    { (requestHeader,request) => 
    println("inside ssc") 
    val incoming:Option[String] = requestHeader.getHeader("user"); 

    val responseHeader = ResponseHeader.Ok.withHeader("status","authenticated") 
    incoming match { 
     case Some(s) => Future.successful((responseHeader,("hello3 found "+s))) 
     case None => Future.successful((responseHeader,"hello3 didn't find user")) 
    } 
    }) 

我預計inside ssc將首先打印,然後打印在日誌中,但它是相反的。不應該首先評估傳遞給函數的參數嗎?

我明白了。爲什麼?

inside logged Received POST /hello3 inside ssc

回答

2

logged是,你寫一個函數,一個ServiceCall,並有自己的ServiceCall裝飾它。稍後,Lagom使用請求標頭調用服務調用。您在服務調用已裝飾的點上正在記錄inside logged,然後它已被返回到Lagom,因此在調用之前,這就是爲什麼它首先被調用的原因。這也許可以解釋它:

def logged[Request, Response](serviceCall:ServerServiceCall[Request, Response]) = { 
    println("3. inside logged method, composing the service call") 
    val composed = ServerServiceCall.compose { requestHeader=> 
    println("6. now Lagom has invoked the logged service call, returning the actual service call that is wrapped") 
    serviceCall 
    } 

    println("4. Returning the composed service call") 
    composed 
} 

override def hello3 = { 
    println("1. create the service call") 
    val actualServiceCall: ServerServiceCall[Request, Response] = ServerServiceCall { (requestHeader, request) => 
    println("7. and now the actual service call has been invoked") 
    val incoming:Option[String] = requestHeader.getHeader("user"); 

    val responseHeader = ResponseHeader.Ok.withHeader("status","authenticated") 
    incoming match { 
     case Some(s) => Future.successful((responseHeader,("hello3 found "+s))) 
     case None => Future.successful((responseHeader,"hello3 didn't find user")) 
    } 
    } 

    println("2. wrap it in the logged service call") 
    val loggedServiceCall = logged(actualServiceCall) 

    println("5. return the composed service call to Lagom") 
    loggedServiceCall 
} 

要記住的重要一點是,hello3方法的調用是服務調用的不調用,hello3只返回一個ServiceCall這Lagom將使用調用它。

+0

想要嘗試代碼,但它沒有編譯:( –

+0

仍然在編寫代碼,但有一些下面的問題(請原諒有限的scala知識) - 1.什麼時候hello3被調用?2)Lagom執行打印但它創建actualServiceCall時不執行ServerServiceCall內的代碼?爲什麼?此時僅創建ServerServiceCall的對象嗎?當lagom使用其調用函數時,傳遞給該代碼塊的代碼會被執行嗎? 3)編寫serviceCall時執行了創建serviceCall時傳遞給它的代碼塊嗎? –

+0

啊!我的一些疑惑在第一段中解釋 - https://www.lagomframework.com/documentation/1.3.x/scala/ServiceImplementation.html。仍然不能編譯代碼:( –