return result;
}
private static Sequence invokeRequestsInParallel(final List<String> endpoints, final BindingVariable hostVar, final PreparedQueryRequest request, final DynamicContext dynEnv) {
final int numEndpoints = endpoints.size();
final ExecutorService ex = ExecutorFactory.newFixedThreadPool(numEndpoints, "parallelRemoteExec");
final Sequence[] results = new Sequence[numEndpoints];
//final AtomicReferenceArray<Sequence> resultsRef = new AtomicReferenceArray<Sequence>(results);
try {
for(int i = 0; i < numEndpoints; i++) {
final String endpoint = endpoints.get(i);
final int at = i;
Runnable r = new Runnable() {
public void run() {
final Sequence res;
try {
res = invokeRequest(endpoint, request);
} catch (XQueryException e) {
throw new IllegalStateException("An error caused while evaluating CompiledQueryRequest#"
+ request.getIdentifier() + " at " + endpoint, e);
}
results[at] = res;
}
};
ex.execute(r);
}
} finally {
ExecutorUtils.shutdownAndAwaitTermination(ex);
}
assert (ex.isTerminated());
return new PipelinedSequence(dynEnv, results);
}