2017-03-08 4 views
2

백엔드 호출에서 비동기를 구현하려고하는데, 읽고있는 중이며 GParse가 이것을 구현하는 좋은 라이브러리 인 것처럼 보입니다.하지만이 방법을 구현할 수있는 방법은 분명하지 않습니다. 올바른 길. 누구든지 나를 도울 수 있습니까? 이건 내 예입니다GParse call BackendService async

def itemsResults 
    def locationResults 
    def itemsNearLocation 
    GParsPool.withPool { 
     itemsResults = {searchMyItems()}.async().call() 
     locationResults = {getLocations()}.async().call() 
     itemsNearLocation = {getItemsNear(locationResults)}.async().call() // i need locationresults answer in order to call this service 

    } 
    model.putAll(["items": itemsResults, 
        "itemsNearLocation":itemsNearLocation]) 

    return "myView" 

그래서, 내가 전에 비동기라는 응답 중 하나를 사용하는 데 필요한 세 번째에 다음이 API를 호출과 전화, 그리고 마지막으로 내 모델이 추가해야합니다. 내가 어떻게 이걸 얻을 수 있니?

+1

가 (HTTP [데이터 흐름]에 대한 좋은 사례가 될 수 있습니다. gpls.org/1.0.0/guide/guide/dataflow.html) –

답변

3

당신은 내가 (하지만 그것을 시도하지) 믿습니다 작동해야이 같은이 ... 뭔가 GPars의 데이터 흐름을 사용할 수 있습니다 : // WWW :

import groovyx.gpars.dataflow.Dataflows 
import static groovyx.gpars.dataflow.Dataflow.task 

final def df = new Dataflows() 

task { 
    df.itemsResults = searchMyItems() 
} 

task { 
    df.locationResults = getLocations() 
} 

task { 
    df.itemsNearLocation = getItemsNear(df.locationResults) 
} 

def lastTask = task { 
    model.putAll(["items": df.itemsResults, 
        "itemsNearLocation":df.itemsNearLocation]) 
} 

lastTask.join() 
+0

'final'을 var 이름으로 사용할 수 있습니까? :) – injecteer

+0

@injecteer, 잘 잡으세요. 그것은 예약 된 키워드입니다. 빠른 회신일지도 모릅니다. 그러나 OP가 찾고있는 것이 어떻게 달성 될 수 있는지를 보여주는 것입니다. – Rao

+0

@injecteer hehehe, fixed ;-) –