我正在使用groovy和Gpars建立异步连接。我收到了对我的API的巨大请求,我正在使用JSON Path拆分JSON。 $。$ {jsonobjstpath} .. [i] [j],其中i和j是0:20范围内的值,并在其上循环。我能够获取正确的拆分json。 我使用GParsExecutorsPool.withPool将这些JSON批次发送到我的API。但是gpar正在等待响应。 假设处理一个请求的API花费10秒,则gpar等待10秒发送控制器循环。我的代码在下面。
import groovyx.gpars.GParsExecutorsPool;
import groovyx.gpars.GParsPool;
import jsr166y.ForkJoinPool;
import jsr166y.RecursiveTask;
def invoke(mega) {
def size=mega.get("size"); //get size of actual JSON objects
def body=mega.get("content.body"); // Load JSON body
int counter=Math.ceil(size/20); //Get Number of loops to run
def Path="/AsyncInmp"; // Path to call function
def Name="SplitJsonObject"; //Name of Function
int i=0;
int j=19;
while(j<=size) {
msg.put("i",i); //Send i value to function
msg.put("j",j); // Send j value to function
callPolicy(Path,Name,body); //Call function json path to get split json, receiving JSON with i and j values
def split_body=resp.body;//response from split json
def Path2="/AsyncInmp"; //path to connection function
def Name2="connect"; //name of connection function
GParsExecutorsPool.withPool {
(0..<1).eachParallel { k ->
callPolicy(Path2, Name2,split_body) //Call function to connect using gpars, This is not working
}
}
j=j+20;
i=i+20;
}
return true;
}
- 所以,一旦我的json拆分请求准备好,我该如何使用gpar进行异步调用
- 我如何收集所有异步呼叫的响应
答案 0 :(得分:2)
您正在withPool
循环内调用while
,并且在eachParallel
中使用大小为1的范围,我想这些东西的结合实际上会使您的代码在单个代码中表现线程方式。
将其更改为以下内容:
import java.util.concurrent.CopyOnWriteArrayList
def futures = [] as CopyOnWriteArrayList
GParsExecutorsPool.withPool {
while(...) {
...
futures << {
callPolicy(Path2, Name2,split_body)
}.async().call()
}
}
// wait for all requests to complete
def results = futures*.get() // or futures.collect { it.get() } if this breaks
// results is now a list of return values from callPolicy
我尚未测试或运行此代码,但是它应该使您了解如何继续前进。
<-评论后编辑->
一个工作示例:
@Grab('org.codehaus.gpars:gpars:1.0.0')
import groovyx.gpars.*
import java.util.concurrent.*
import java.util.concurrent.atomic.*
import static groovyx.gpars.dataflow.Dataflow.task
random = new Random()
sequence = new AtomicInteger(-1)
def promises = [] as CopyOnWriteArrayList
GParsPool.withPool(25) { pool ->
10.times { index ->
promises << task {
callPolicy(index)
}
}
}
def results = promises*.get()
results.each { map ->
println map
}
def callPolicy(index) {
Thread.sleep(random.nextInt(100) % 100)
[index: index, sequence: sequence.incrementAndGet(), time: System.currentTimeMillis()]
}
产生以下类型的输出:
~> groovy solution.groovy
[index:0, sequence:9, time:1558893973348]
[index:1, sequence:1, time:1558893973305]
[index:2, sequence:8, time:1558893973337]
[index:3, sequence:5, time:1558893973322]
[index:4, sequence:7, time:1558893973337]
[index:5, sequence:4, time:1558893973320]
[index:6, sequence:3, time:1558893973308]
[index:7, sequence:6, time:1558893973332]
[index:8, sequence:0, time:1558893973282]
[index:9, sequence:2, time:1558893973308]
~>
在这里我们可以看到返回了结果,并且以多线程方式进行了调用,因为sequence
和index
的值都不是顺序的。