【问题标题】:Modifying Ehcache configuration at runtime在运行时修改 Ehcache 配置
【发布时间】:2012-07-11 14:23:18
【问题描述】:

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

这是我的spring配置文件

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

【问题讨论】:

    标签: java spring ehcache terracotta


    【解决方案1】:

    只有几个动态属性:

    • timeToIdleSeconds
    • timeToLiveSeconds
    • maxElementsInMemory
    • maxElementsOnDisk

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

    【讨论】:

      【解决方案2】:

      分析EhCacheManagerFactoryBean类的源码后,发现'terracottaConfiguration'不能动态改变。 只有枚举 DynamicProperty 中提到的属性才能动态更改。不过,如果有人找到文档并提及相同内容,那将会很有帮助。

      【讨论】:

        猜你喜欢
        • 2020-06-12
        • 1970-01-01
        • 1970-01-01
        • 2018-12-19
        • 2010-10-14
        • 2017-05-11
        • 1970-01-01
        • 1970-01-01
        • 2023-03-18
        相关资源
        最近更新 更多