Question

I want to place property place holders in the ehcache.xml file (like ${}) so that the values can be replaced from external properties file (.properties) at runtime. Something like:

ehcache.xml (in classpath):

 <defaultCache
maxElementsInMemory="20000"
eternal="false"
timeToIdleSeconds="${default_TTI}"
timeToLiveSeconds="86400"
overflowToDisk="true"
... />

ehcache.properties (outside of the war/classpath):

...
default_TTI=21600
...

The purpose is to be able to change the cache configuration without the need to rebuild the app. Spring's PropertyPlaceHolder will only work with Spring bean definiton for ehcache which I dont want (need to keep the ehcache.xml as a file)

There are similar posts here but nothing got me to solution. I've been searching for a week now!!

Im using Spring 2.5.6,Hibernate 3.2.6 and Ehcache 2.4.6

Any help or idea is greatly Appriciated!!

Thanks a lot, Tripti.

Was it helpful?

Solution

If you just want to read the config in from disk at startup, you can do the following in EHCache 2.5:

InputStream fis = 
    new FileInputStream(new File("src/config/ehcache.xml").getAbsolutePath());
try 
{
  CacheManager manager = new CacheManager(fis);
} 
finally 
{
  fis.close();
}

OTHER TIPS

As a workaroud solution you can set property values to system scope (System.setProperty(...)). EhCahe uses these properties to resolve placeholders during parsing its configuration file.

I got the solution finally! Thanks to braveterry for pointing me in that direction. I used this at context startup:

Inputstream = new FileInputStream(new File("src/config/ehcache.xml").getAbsolutePath()); 
cacheManager = CacheManager.create(stream);

in conjuction with hibernate configuration:

<prop key="hibernate.cache.provider_class">net.sf.ehcache.hibernate.SingletonEhCacheProvider</prop>

This creates a singleton CacheManager from ehcache.xml file outside the context classpath. I was doing this same earlier but was accidently creating another CacheManager before this one using the default ehcache.xml in the classpath.

Thanks, Tripti.

svaor, I follow what you mean, I define a bean like this:

<bean class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
        <property name="targetClass" value="java.lang.System" />
        <property name="targetMethod" value="setProperty" />
        <property name="arguments">
            <list>
                <value>system.project_name</value>
                <value>${system.project_name}</value>
            </list>
        </property>
    </bean>

system.project_name define in system.properties file which locate in classpath

I also create a ehcache.xml in classpath, in ehcache.xml has the code like this:

<diskStore path="${java.io.tmpdir}/${system.project_name}/cache" />

but when I deploy my project, I find it cann't use the system.project_name define in system.properties, why?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top