我遇到一个问题,即在应用无法启动后,ehcache没有清理其监听器。基本上在启动失败后,它会尝试在remoteObjectPort 40003上建立一个监听器,并且由于端口已经在使用而失败:
Caused by: java.rmi.server.ExportException: Port already in use: 40003; nested exception is:
java.net.BindException: Address already in use
...
即使应用程序已停止,侦听器仍在这些端口上运行:
[root@server logs]# netstat -tulpn | grep 4000
tcp 0 0 0.0.0.0:40001 0.0.0.0:* LISTEN 14946/java
tcp 0 0 0.0.0.0:40003 0.0.0.0:* LISTEN 14946/java
这是我的ehcache.xml文件中的相关配置:
<cacheManagerPeerProviderFactory
class="net.sf.ehcache.distribution.RMICacheManagerPeerProviderFactory"
properties="peerDiscovery=automatic,
multicastGroupAddress=230.0.0.1,
multicastGroupPort=40002,
timeToLive=64"
/>
<cacheManagerPeerListenerFactory
class="net.sf.ehcache.distribution.RMICacheManagerPeerListenerFactory"
properties="port=40001,remoteObjectPort=40003,
socketTimeoutMillis=20000"/>
有没有办法在我的Application.java中创建一个@PreDestroy方法,我可以明确地杀死在容器中运行的任何正在运行的ehcache监听器?
编辑:这是我在春天初始化EhCache的方式:
@EnableCaching
...
public class Application extends SpringBootServletInitializer implements WebApplicationInitializer {
...
@Bean
public EhCacheCacheManager gdaCacheManager() {
return new EhCacheCacheManager(ehCacheCacheManager().getObject());
}
@Bean
public EhCacheManagerFactoryBean ehCacheCacheManager() {
EhCacheManagerFactoryBean factory = new EhCacheManagerFactoryBean();
factory.setConfigLocation(new ClassPathResource("cache/ehcache.xml"));
factory.setShared(true);
return factory;
}
我还在hibernate配置中设置了以下属性,以启用ehcache的二级缓存:
properties.put("hibernate.cache.use_second_level_cache", "true");
properties.put("hibernate.cache.use_query_cache", "true");
properties.put("hibernate.cache.region.factory_class", "org.hibernate.cache.ehcache.SingletonEhCacheRegionFactory");
properties.put("net.sf.ehcache.configurationResourceName", "/cache/ehcache.xml");