5

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.

svaor
  • 2,205
  • 2
  • 19
  • 41
Tripti
  • 75
  • 1
  • 4

4 Answers4

3

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.

svaor
  • 2,205
  • 2
  • 19
  • 41
  • Sadly ehcache incorporates only a simple property name replace, even in 3.8.0 . I mean that configurations in other systems allow to use many property names and even default value e.g. ${global.property,subsystem.property:100000} . – rychu Jul 25 '19 at 12:05
1

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?

Rocky Hu
  • 1,326
  • 4
  • 17
  • 32
1

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.

Tripti
  • 75
  • 1
  • 4
0

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();
}
braveterry
  • 3,724
  • 8
  • 47
  • 59
  • 1
    Sorry pressed enter too early! I tried doing that too, but the problem is how to hook this CacheManager to hibernate? I have the following code in my hibernate context file: `truenet.sf.ehcache.hibernate.SingletonEhCacheProvider` So hibernate starts using the default ehcache.xml (available in the jar) and creates Caches using that instead of using the CacheManger I created. Is there any way I can tell hibernate to use my CacheManager instead of creating the new one? Thanks, Tripti. – Tripti Mar 26 '12 at 15:47