API / Apache FreeMarker Interview questions
Explain the lifecycle of a FreeMarker Template object?
A Template instance moves through a small number of well-defined stages, and understanding them explains why FreeMarker performs well under load.
- Request: application code calls
configuration.getTemplate(name, locale). - Cache lookup: the Configuration's internal template cache is checked using a key built from the name, locale, and encoding.
- Cache hit (fresh): if a cached entry exists and is still within the configured freshness window, the existing compiled
Template(AST) is returned immediately - no I/O, no re-parsing. - Cache miss or stale: the configured
TemplateLoaderis asked for the source; if it has changed (or was never loaded), FreeMarker parses the FTL text into an AST, wraps it in a newTemplate, and stores it back in the cache. - Processing:
template.process(dataModel, writer)may be called any number of times, from any number of threads, against that same compiledTemplate- each call creates its ownEnvironment, so calls do not interfere with each other.
Because the expensive step - parsing - only happens on a cache miss, and the resulting Template is immutable and thread-safe, one compiled template can safely serve a very high volume of concurrent process() calls.
More Related questions...