Reputation: 434
I would like to know how can I invalidate the cache created by the Cache Mediator just by calling a proxy.
According to the doc we can do that by using a JMX Monitoring tool. But we do not want to do something like this.
So I've made a custom mediator
import javax.cache.CacheException;
import javax.cache.Caching;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.synapse.MessageContext;
import org.apache.synapse.mediators.AbstractMediator;
public class CacheInvalidateMediator extends AbstractMediator
{
private static final Log log = LogFactory.getLog(CacheInvalidateMediator.class);
public boolean mediate(MessageContext synCtx)
{
try
{
Caching.getCacheManager("cacheManager").getCache("mediatorCache").removeAll();
log.info("Total mediator cache has been invalidated.");
}
catch(CacheException cacheException)
{
throw new CacheException("Error occurred while invalidating mediator cache. " + cacheException);
}
return true;
}
}
It works for EI 6.2.0 but now for EI 6.6.0 it is deprecated and I can't figure how to do the same thing now. Do you have any idea ?
Regards.
EDIT
I tried to do an iteration on getCaches(). It seems that I do not have the right name for the cache manager.
public class CacheInvalidateMediator extends AbstractMediator {
private static final Log log = LogFactory.getLog(CacheInvalidateMediator.class);
public boolean mediate(MessageContext context) {
try {
log.info("JAVA REMOVING CACHE...");
log.info(Caching.getCacheManager("cacheManager"));
log.info(Caching.getCacheManager("cache"));
Iterable<Cache<?, ?>> iterable1 = Caching.getCacheManagerFactory().getCacheManager("__default__").getCaches();
log.info("iterate :");
for(Cache<?, ?> cache : iterable1) {
log.info("cache : "+cache.getName());
cache.removeAll();
}
Iterable<Cache<?, ?>> iterable2 = Caching.getCacheManagerFactory().getCacheManager("cache").getCaches();
log.info("iterate :");
for(Cache<?, ?> cache : iterable2) {
log.info("cache : "+cache.getName());
cache.removeAll();
}
log.info("Total mediator cache has been invalidated.");
log.info("JAVA CACHE INVALIDATE");
} catch (Exception e) {
log.info(e.toString());
}
return true;
}
}
Based on github project, I've tried to use some constants, but anything seems to work.
Upvotes: 0
Views: 225
Reputation: 3469
The caching implementation was totally rewritten in the later releases. Hence using javax CacheManager to clear the cache would not work in EI 6.6.0. But still there is a MBean implementation to invalidate the cache. https://github.com/wso2/carbon-mediation/blob/master/components/mediators/cache/org.wso2.carbon.mediator.cache/src/main/java/org/wso2/carbon/mediator/cache/MediatorCacheInvalidator.java
You should try out an implementation to invoke this MBean to invalidate the cache.
Upvotes: 1