Externarlize ehcache.xml to use properties from an external properties file

I want to place placeholders in an ehcache.xml file (for example, $ {}) so that values ​​can be replaced from an external properties file (.properties) at runtime. Sort of:

ehcache.xml (in the classpath):

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

ehcache.properties (outside the war / classpath):

 ... default_TTI=21600 ... 

The goal is to be able to reconfigure the cache without having to rebuild the application. Spring PropertyPlaceHolder will only work with Spring bean definiton for ehcache, which I don't want (I need to save ehcache.xml as a file)

There are similar posts here, but nothing led me to a solution. I've been looking for a week already!

Im using Spring 2.5.6, Hibernate 3.2.6 and Ehcache 2.4.6

Any help or idea is pretty much explained !!

Thanks a lot, Tripti.

+4
source share
4 answers

If you just want to read the configuration 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(); } 
0
source

As a workaroud solution, you can set property values ​​to the system area (System.setProperty (...)). EhCahe uses these properties to allow placeholders when parsing the configuration file.

+2
source

I finally got a solution! Thank you for pointing me in this direction. I used this when starting the context:

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

in combination with sleep configuration:

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

This creates a singleton CacheManager from the ehcache.xml file outside the context path. I did this earlier, but accidentally created another CacheManager before that, using ehcache.xml by default in the classpath.

Thanks, Tripti.

+1
source

svaor, I follow what you mean, I define a bean as follows:

 <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 defines the system.properties file that is in the classpath

I also create ehcache.xml in the classpath, in ehcache.xml it has the following code:

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

but when I deploy my project, I believe that it cannot use the name system.project_name in system.properties, why?

+1
source

Source: https://habr.com/ru/post/1403165/


All Articles