private Integer getLongestDuration(List<Project> project) {
if (project== null || project.isEmpty())
return 0;
List<Integer> results = new ArrayList<>();
for (Project m : project) {
scanRoute(m, results, Integer.valueOf(0));
}
return Collections.max(results);
}
private void scanRoute(Project m, List<Integer> results, Integer time) {
Integer maxDuration = repoCall.calculateDuration(m.getId());
time += (maxDuration == null ? 0 : maxDuration);
List<Project> output = findElements(m); //DB Call
results.add(time);
if (!output.isEmpty()) {
for (Project successor : output) {
scanRoute(successor, results, time);
}
}
}
The problem is when too many elements scanRoute() takes too much time causing it to timeout as a response. I want to optimize this method so that there should be fewer DB round trips and the timeout problem can be solved.