I'm trying to disable a distributed ehcache at runtime. This is needed to facilitate rolling deploys to our several servers so new version of code wouldn't interfere with servers awaiting update to the newer code.
I came up with the following class
@Component
@ManagedResource(objectName = "net.sf.ehcache:category=CustomCacheManager,name=cache-ms", description = "Custom managing of ehcache")
public class CacheJmxManagerService {
@Autowired
private CacheManager cacheManager;
@ManagedOperation
public void enableCaches(){
String[] cacheNames = cacheManager.getCacheNames();
for (String cacheName : cacheNames) {
cacheManager.getCache(cacheName).setDisabled(false);
}
}
@ManagedOperation
public void disableCaches(){
String[] cacheNames = cacheManager.getCacheNames();
for (String cacheName : cacheNames) {
cacheManager.getCache(cacheName).setDisabled(true);
}
}
}
<bean id="cacheManager"
class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
<property name="configLocation">
<value>${sslsConfPath}ehcache-#{applicationName}.xml</value>
</property>
<property name="shared" value="true"/>
</bean>
The bean is how the ehcache manager is configured in the application context.
However when I execute them by jmx and run a jmeter test to test the load, the results are the same. Our calls are just as fast with or without the cache. Is this the right way to disable ehcache in Spring ?
ehcache
provides its own JMX-exposable beans. Also, the@Transactional
annotation is a bit puzzling as you do no database operations. – beerbajayMaxEntries
attributes to0
, but actually exposing the disable functionality would be better. – beerbajay