Question

I am using ehcache in Spring framework. I am using ehcache.xml to initialize ehcache. However I want to add certain properties such as terracottaconfig at runtime. For this purpose I overridden the class EhCacheManagerFactoryBean. Currently I am overriding method getObject() of this class (I don't know whether this is a correct one to override as other method setResource() and afterPropertiesSet() are called before class is initialized with ehcache.xml file.)

This is my spring configuration file

<bean id="cacheManager"
class="com.dexknows.util.CustomEhCacheManagerFactoryBean">
<property name="configLocation">
   <value>file:///${vp_data_dir}/ehcache.xml</value>
</property>     
<property name="terracottaConfigUrl" value="#{dexProperties['terracottaConfig.Url']}"/>
</bean>

and this is the class method that I overridden

public class CustomEhCacheManagerFactoryBean extends EhCacheManagerFactoryBean {

    private String terracottaConfigUrl;
    private Resource resourceConfiguration;

    @Override
    public void setConfigLocation(Resource configLocation) {            
        super.setConfigLocation(configLocation);
    }

    @Override
    public void afterPropertiesSet() throws IOException, CacheException {

        super.afterPropertiesSet();
    }


    @Override
    public CacheManager getObject() {

        CacheManager manager = super.getObject();

        TerracottaClientConfiguration terracottaClientConfiguration = new TerracottaClientConfiguration();
        terracottaClientConfiguration.setRejoin(true);
        terracottaClientConfiguration.setUrl(terracottaConfigUrl);



        manager.getConfiguration().setDynamicConfig(true);
        manager.getConfiguration().terracotta(terracottaClientConfiguration);

Configuration().terracotta(terracottaClientConfiguration)));
    return manager;

    }

    public String getTerracottaConfigUrl() {
        return terracottaConfigUrl;
    }

    public void setTerracottaConfigUrl(String terracottaConfigUrl) {
        this.terracottaConfigUrl = terracottaConfigUrl;
    }

}

I am getting following exception:

Error creating bean with name 'cacheManager': FactoryBean threw exception on object creation; nested exception is java.lang.IllegalStateException: net.sf.ehcache.config.Configuration.dynamicConfig can't be changed dynamically

I have set dynamicConfig="true"

<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:noNamespaceSchemaLocation="ehcache.xsd"
         updateCheck="false"
         monitoring="autodetect"
         dynamicConfig="true"
         name="xyz">
Was it helpful?

Solution

After analyzing the source code of EhCacheManagerFactoryBean class, I found that 'terracottaConfigConfiguration' can't be changed dynamically. Only properties mentioned in enum DynamicProperty can be changed dynamically. Still, If some one find the documentation, mentioning the same, it will be helpful.

OTHER TIPS

There are only a few dynamic properties:

  • timeToIdleSeconds
  • timeToLiveSeconds
  • maxElementsInMemory
  • maxElementsOnDisk

They are mentioned in the official Javadocs of net.sf.ehcache.config.CacheConfiguration ("Dynamic Configuration").

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