2017-03-08 5 views
2

私はバックエンドコールで非同期を実装しようとしていますが、私は読んできましたが、GParseはこれを実装するための良いライブラリだと思われますが、これをどのように実装できますか正しい方法。誰でも助けてくれますか?これは私の例である:GParseコール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" 

だから、私はのに、ASYNCと呼ばれる応答のいずれかを使用する必要が三番目で、その後2のAPIコールとを呼び出し、最後に私のモデルにこれを追加する必要があります。どうすればこれを達成できますか?

+1

は、[データフロー](HTTPのために良い場合もございます。 gpars.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

@インジェクターhehehe、固定;-) –