I have a requirement to iterate a large number of records using entity list iterator in Freemarker. But, while using a recursive macro, it gives StackOverflow error when it reaches 1000 records only. Below is the code snippet for the same.
<#macro recurseTest value>
${value.partyId!}
${value.firstName!}
${value.lastName!}
<#local tmp = eli.next()!/>
<#if tmp?has_content>
<@recurseTest tmp/>
</#if>
</#macro>
<#assign eli = EntityQuery.use(delegator).from("Person").queryIterator()/>
<br>
<@recurseTest eli.next()!/>
${eli.close()}
Regardless of FreeMarker, it's not a normal practice to process 1000s of elements with recursion (except in some functional languages that have no usual loops construct, but has tail recursion optimizations). Try to use #list
. If you can't get a List
or Iterator
from that query API (but look into it if you can), as a last resort, you can do this hack (poor man's do { ... } while (cond)
):
<#list 1..100000000 as _>
...
<#if !tmp?has_content><#break></#if>
</#list>
(With high enough incompatible_improvements
FreeMarker configuration setting you can write <#list 1.. as _>
, but I don't know what is it set to there. So I have specified some arbitrary high number that would cause timeout or out-of-memory in practice anyway.)