在运行时修改Ehcache配置

时间:2012-07-11 21:14:47

标签: java spring ehcache terracotta

我在Spring框架中使用ehcache。我正在使用ehcache.xml来初始化ehcache。但是我想在运行时添加某些属性,例如terracottaconfig。为此,我重写了EhCacheManagerFactoryBean类。目前我正在重写此类的方法getObject()(我不知道这是否是一个正确的覆盖方法,因为在使用ehcache.xml文件初始化类之前调用​​其他方法setResource()和afterPropertiesSet()。)

这是我的弹簧配置文件

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

这是我重写的类方法

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;
    }

}

我遇到以下异常:

创建名为'cacheManager'的bean时出错:FactoryBean在创建对象时抛出异常;嵌套异常是java.lang.IllegalStateException:net.sf.ehcache.config.Configuration.dynamicConfig无法动态更改

我设置了dynamicConfig =“true”

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

2 个答案:

答案 0 :(得分:3)

在分析了EhCacheManagerFactoryBean类的源代码后,我发现'terracottaConfigConfiguration'无法动态更改。 只能动态更改枚举DynamicProperty中提到的属性。不过,如果有人找到文档,提到相同的文档,那将会有所帮助。

答案 1 :(得分:1)

只有少数动态属性:

  • timeToIdleSeconds
  • timeToLiveSeconds
  • maxElementsInMemory
  • maxElementsOnDisk

官方Javadocs of net.sf.ehcache.config.CacheConfiguration(“动态配置”)中提到了它们。

相关问题