java 如何在不重启服务器的情况下清除 ehcache
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10912087/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to clear ehcache without server restart
提问by Vishal
Though I guess its highly unlikely - but is there any way to clear the ehcache without restarting the server? I need to clear the cache for some testing - I cannot change the code and cannot afford to restart server at multiple times.
虽然我猜它不太可能 - 但是有什么方法可以在不重新启动服务器的情况下清除 ehcache 吗?我需要清除缓存以进行某些测试 - 我无法更改代码并且无法多次重新启动服务器。
PS: I am using apache-tomcat-5.5.25 Please let me know. Thanks, psvm
PS:我正在使用 apache-tomcat-5.5.25 请告诉我。谢谢,psvm
回答by jeha
Do you expose Ehcache via JMX? Then you could clear the cache using JMX operations by using a tool like e.g. jvisualvm. Look for MBeans like net.sf.ehcache.CacheManager
which provide a clearAll()
operation.
您是否通过 JMX 公开 Ehcache?然后,您可以使用 jvisualvm 之类的工具使用 JMX 操作清除缓存。寻找像net.sf.ehcache.CacheManager
提供clearAll()
操作的MBean 。
回答by Nisarg Panchal
Using spring+hibernate and exposing mbean:
使用 spring+hibernate 并暴露 mbean:
import org.hibernate.Cache;
import org.hibernate.SessionFactory;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
@Component("CacheManagerMBean")
public class CacheManagerMBean {
private static final org.slf4j.Logger logger = LoggerFactory.getLogger(CacheManagerMBean.class);
@Resource(name = "sessionFactory")
private SessionFactory sessionFactory;
public void clearCache() {
Cache cache = sessionFactory.getCache();
if (null != cache) {
logger.info("Clearing cache...");
cache.evictAll();
cache.evictAllRegions();
logger.info("Clearing cache...Done!");
} else {
logger.error("No second level cache available for session-factory");
}
}
}
XML Config:
XML 配置:
<bean id="jmxExporterCacheManagerMBean" class="org.springframework.jmx.export.MBeanExporter">
<property name="beans">
<map>
<entry key="CacheManager:type=SecondLevelCacheManager">
<ref bean="CacheManagerMBean"/>
</entry>
</map>
</property>
</bean>
And then connect to the java process using jconsole and use Mbean method invocation - to clear the second level cache!
然后使用jconsole连接java进程,使用Mbean方法调用——清除二级缓存!