Java 在运行时修改Ehcache配置
我正在Spring框架中使用ehcache。我正在使用ehcache.xml初始化ehcache。但是,我想在运行时添加某些属性,例如terracottaconfig。为此,我重写了类EhCacheManagerFactoryBean。目前,我正在重写该类的方法getObject()(我不知道这是否是要重写的正确方法,因为在使用ehcache.xml文件初始化该类之前调用了其他方法setResource()和afterPropertieSet() 这是我的spring配置文件Java 在运行时修改Ehcache配置,java,spring,ehcache,terracotta,Java,Spring,Ehcache,Terracotta,我正在Spring框架中使用ehcache。我正在使用ehcache.xml初始化ehcache。但是,我想在运行时添加某些属性,例如terracottaconfig。为此,我重写了类EhCacheManagerFactoryBean。目前,我正在重写该类的方法getObject()(我不知道这是否是要重写的正确方法,因为在使用ehcache.xml文件初始化该类之前调用了其他方法setResource()和afterPropertieSet() 这是我的spring配置文件 <bean
<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>
我得到以下例外情况:
创建名为“cacheManager”的bean时出错:FactoryBean在创建对象时引发异常;嵌套异常为java.lang.IllegalStateException:net.sf.ehcache.config.Configuration.dynamicConfig无法动态更改
我已设置dynamicConfig=“true”
在分析EhCacheManagerFactoryBean类的源代码后,我发现“terracottaConfigConfiguration”无法动态更改只能动态更改枚举DynamicProperty中提到的属性。尽管如此,如果有人找到了文档,提到了同样的内容,这将是很有帮助的。在分析了EhCacheManagerFactoryBean类的源代码后,我发现“terracottaConfigConfiguration”不能动态更改只能动态更改枚举DynamicProperty中提到的属性。尽管如此,如果有人找到了文档,提到了相同的内容,这将非常有用。只有几个动态属性:
timeToIdleSeconds
timeToLiveSeconds
maxElementsInMemory
maxElementsOnDisk
官方(“动态配置”)中提到了它们。只有几个动态属性:
timeToIdleSeconds
timeToLiveSeconds
maxElementsInMemory
maxElementsOnDisk
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;
}
}
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="ehcache.xsd"
updateCheck="false"
monitoring="autodetect"
dynamicConfig="true"
name="xyz">