Scenario: the application use freemarker with a TemplateExceptionHandler.RETHROW_HANDLER policy, but occasionally needs to produce large XML (20~200Mb) and goes out of memory.
In FreemarkerResult there are two possible behaviours (line 191):
- Buffer-behaviour: the whole template is processed and if everything is OK it is flushed to the output, otherwise an exception is thrown and handled at higher level before any output has been sent. This is intended to be used when TemplateExceptionHandler.RETHROW_HANDLER is active
- Flush-behaviour: template is processed and flushed according to freemarker library policies, used with any other TemplateExceptionHandler
Since TemplateExceptionHandler cannot be switched for a given request (it is a global configuration embedded in FreemarkerManager) there is no way to force a Flush-behaviour. (you can only force a Buffer-behaviour using isWriteIfCompleted)
I implemented a more flexible solution that let you force the behaviour in both ways:
where useBufferedWriter is a parameter that can be modified per request