简体   繁体   中英

Modifying Ehcache configuration at runtime

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">

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.

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").

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM