java - 运行时修改Ehcache配置

标签 java spring ehcache terracotta

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

最佳答案

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

关于java - 运行时修改Ehcache配置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11441606/

相关文章:

java - 嵌入式事务管理 JPA

hibernate - 在 L2 缓存 Ehcache 中缓存 Hibernate Collection 时出现异常

java - 从我的二级 ehcache 中检索项目后出现 "org.hibernate.LazyInitializationException"异常

java - 如何设置JMX监控的net.sf.ehcache.CacheManager名称?

java - 在android中获取2D ArrayList内的值?

java - 使用Java中的方法对单链表进行排序

java - Graphics2D 放置具有特定角坐标的图像

spring - 将请求参数放在 url 中并使用 Spring Webflow 传递它

java - 如何在避免 NoSuchElementException 错误的同时获取可选值?

java - POST 到 Spring MVC Controller 结果为 "HttpMessageNotReadableException: Could not read JSON: No suitable constructor found"