GParse 异步调用后端服务

GParse call BackendService async

我正在尝试在后端调用中实现异步,我一直在阅读,似乎 GParse 是一个很好的库来实现它,但我不清楚如何以正确的方式实现它.谁能帮我?这是我的例子:

    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"

所以,我需要调用 2 个 api 调用,然后在第三个中我需要使用之前称为异步的响应之一,最后将其添加到我的模型中。我怎样才能做到这一点?

你可以为此使用 GPars 数据流......我相信这样的东西应该有效(虽然没有尝试过):

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()