9 Feb 23:47
Dynamic Templates
Mark-12345 <marks1900-post01 <at> yahoo.com.au>
2012-02-09 22:47:34 GMT
2012-02-09 22:47:34 GMT
I thought I would share the following code with the Apache Velocity
community.
Problem Description: I wanted to use Dynamic Templates though I found the
following had massive performance implications.
-- Velocity Call Snippet --
String template = "message.to: ${message.to} ";
StringWriter velocityWriter = new StringWriter();
velocityEngine.evaluate( velocityContext, velocityWriter, "LOG", template );
return velocityWriter.toString();
-- Velocity Call Snippet --
As such, I needed an alternate solution and guessed that if I could cache
the Velocity templates used I could see big performance gains.
I initially looked to the
org.apache.velocity.runtime.resource.loader.StringResourceLoader class to
solve my problems. The StringResourceLoader class requires an instance of
the StringResourceRepository class, and the only implementation of
StringResourceRepository is StringResourceRepositoryImpl. Unfortunately,
since the number of unique templates will grow over time, in time the
StringResourceRepositoryImpl.resources Map will cause an out of memory
error.
Why doesn't StringResourceRepositoryImpl use a cache with a maximum size?
Is a cache even needed as the Velocity ResourceCacheImpl will cache
Templates?
(Continue reading)
RSS Feed