Here is my pseudo-code:
class Builder implements Callable<T> {
T obj;
ManagedExecutorService pool;
Builder (T obj, ManagedExecutorService pool){
this.obj = obj;
this.pool = pool;
}
T call(){
build();
}
private void build(){
// skip if already traversed
return isTraversed(obj);
// call db and get obj's one-to-many relationships
Collection<T> colOfChildObj = DbUtil.getChildrenPOJO(obj);
for (<Collection>T childObj : colOfChildObj){
this.pool.submit(new Builder(childObj, this.pool));
}
// set children as-is, when the submit above completes,
// it will update childObj and thus will reflect
// obj.childObj.itsChidren etc. For this though the caller
// has to wait until all submits are processed
obj.setChildren(colOfChildObj);
}
}
Since Java-ee does not support ForkJoinPool - that is out of the question. So how do I do it with either ManagedThreadFactory and/or ManagedExecutorService? My real challenge is due to not being able to call pool.shutdown() or pool.awaitTermination in Java-ee. So, from the caller, if I do:
class Caller () {
T getObjGraph(T rootObj){
pool.submit(new Builder(rootObj));
T objGraph = pool.get();
return objGraph;
}
}
Then my method does not wait for all the pool.submit(new Builder(childObj, pool)) and thus my object does not have everything set and is incomplete. I thought of putting all Futures returned by pool.submit into a blocking queue - but then I don't know how to inform the caller that my tree traversal is complete. I do have a counter that reaches 0 when the tree traversal is complete but since the client is submitting a top level node, I'm not sure how to make it wait there in Java-ee without while(isCounter = 0) - which is a CPU hog.
Any pointers?