I have an async method as shown below which calls my Task class and my Task class does all the work.
@Override
public Future<DataResponse> executeAsync(DataKey key) {
Future<DataResponse> future = null;
try {
Task task = new Task(key, restTemplate);
future = executor.submit(task);
} catch (Exception ex) {
// logging exception here
}
return future;
}
Below is my Task
class which does all the work:
public class Task implements Callable<DataResponse> {
private DataKey key;
private RestTemplate restTemplate;
public Task(DataKey key, RestTemplate restTemplate) {
this.key = key;
this.restTemplate = restTemplate;
}
@Override
public DataResponse call() throws Exception {
// some code here
}
}
Now I need to call executeAsync
method in parallel and then make a List<DataResponse>
object and return it.
@Override
public List<DataResponse> executeSync(DataKey key) {
List<DataResponse> responseList = new ArrayList<DataResponse>();
// make a List of DataKey using single key passed to this method.
List<DataKey> keys = new ArrayList<DataKey>();
for(DataKey key : keys) {
}
}
How can I call executeAsync
method in parallel and return back responseList
? In my keys
list maximum I will have six DataKey
object.