An option could be like;
- create a contextObject which keeps track of the processed records by having a startIndex and batchSize,
so you start with startIndex of 0 and batchsize 1000 in your first execution, update your contextObject then and pass it through to the java action so that it starts in the 2nd run by retrieving it from the updated startIndex and batch (1000-1000)
-execution should stop when there is no retrieve results
In my experience, you can't solve a stack overflow error with batches: batching solves heap space error and GC overhead exceeded errors. If you have a stack overflow error, you probably have a recursive microflow which does not terminate nicely.
Furthermore, you shouldn't use executeMicroflowInBatches: since Mendix 4, Mendix supports the batching pattern natively. See section 5.2.1 of this page for an example of how to implement batching in Mendix.