Java 未在所有Tomcat实例中加载使用RMI EhCache数据的复制缓存
我正在尝试使用RMI实现EhCache复制缓存。我有一个SpringWeb应用程序,它使用EhCache进行数据缓存。Ehcache只保存很少的Java.Util.Map,在整个应用程序视图页面中都使用它。当用户从前端屏幕创建一条记录(比如某个业务JavaPOJO对象)时,该记录将被插入到数据库中,随后,EhCache保存的映射将被更新 后来,我们在同一台机器的3个tomcat实例中部署了这个web应用程序。应用程序可通过Apache HTTP负载平衡器访问 我面临的问题是,EhCache数据正在一个tomcat实例上加载。但在另外两个上没有。使用端口号单独访问应用程序时,应用程序运行正常 相同的SpringWeb应用程序在Tomcat实例(900190022003)上运行。配置为侦听的EhCache RMI(400140024003) 请查找我配置的ehCache.xml文件 在Tomcat实例1上Java 未在所有Tomcat实例中加载使用RMI EhCache数据的复制缓存,java,tomcat,replication,rmi,ehcache,Java,Tomcat,Replication,Rmi,Ehcache,我正在尝试使用RMI实现EhCache复制缓存。我有一个SpringWeb应用程序,它使用EhCache进行数据缓存。Ehcache只保存很少的Java.Util.Map,在整个应用程序视图页面中都使用它。当用户从前端屏幕创建一条记录(比如某个业务JavaPOJO对象)时,该记录将被插入到数据库中,随后,EhCache保存的映射将被更新 后来,我们在同一台机器的3个tomcat实例中部署了这个web应用程序。应用程序可通过Apache HTTP负载平衡器访问 我面临的问题是,EhCache数据正
<?xml version="1.0" encoding="UTF-8"?>
<ehcache name="WidgetCache" updateCheck="false">
<defaultCache maxElementsInMemory="10000" eternal="false"
timeToIdleSeconds="0" timeToLiveSeconds="0" overflowToDisk="true"
diskPersistent="false" diskExpiryThreadIntervalSeconds="120"
memoryStoreEvictionPolicy="LRU" />
<cacheManagerPeerListenerFactory
class="net.sf.ehcache.distribution.RMICacheManagerPeerListenerFactory"
properties="hostName=10.237.31.33, port=40001, socketTimeoutMillis=2000" />
<cacheManagerPeerProviderFactory
class="net.sf.ehcache.distribution.RMICacheManagerPeerProviderFactory"
properties="peerDiscovery=manual,
rmiUrls=//10.237.31.33:40002/widgets|//10.237.31.33:40002/lobCache|//10.237.31.33:40002/lobFilterCache|//10.237.31.33:40002/glossarylobFilterCache|//10.237.31.33:40002/bbpUserListCache|//10.237.31.33:40002/userRoleCache|//10.237.31.33:40002/glossary|//10.237.31.33:40003/widgets|//10.237.31.33:40003/lobCache|//10.237.31.33:40003/lobFilterCache|//10.237.31.33:40003/glossarylobFilterCache|//10.237.31.33:40003/bbpUserListCache|//10.237.31.33:40003/userRoleCache|//10.237.31.33:40003/glossary" />
<cache name="userRoleCache" maxElementsInMemory="100" eternal="false"
timeToIdleSeconds="0" timeToLiveSeconds="0" memoryStoreEvictionPolicy="LFU">
<cacheEventListenerFactory
class="net.sf.ehcache.distribution.RMICacheReplicatorFactory"
properties="replicateAsynchronously=true, replicatePuts=true, replicateUpdates=true,replicateUpdatesViaCopy=false, replicateRemovals=true" />
</cache>
<diskStore path="java.io.tmpdir" />
我在以下方法中得到NullPointerException
@Override
public List<BbpUser> loadUsersfromUserListCache() throws InventoryException {
// TODO Auto-generated method stub
Cache cache = cacheManager.getCache("userRoleCache");
Element elementt = cache.get("getAllRoles");
return (List<BbpUser>) elementt.getObjectValue();
}
@覆盖
public List loadUsersfromUserListCache()引发InventoryException{
//TODO自动生成的方法存根
Cache Cache=cacheManager.getCache(“userRoleCache”);
Element elementt=cache.get(“getAllRoles”);
return(List)elementt.getObjectValue();
}
当replicateUpdatesViaCopy设置为false时,条目将从其他实例的缓存中删除。您需要检查null并从数据存储(或任何您有最新值的地方)加载它
您可以将replicateUpdatesViaCopy设置为true,并检查在访问其他实例时是否仍获得NPE
要获得完整的详细信息,您可以查看在哪一行抛出NullPointerException?return(List)elementt.getObjectValue()//这是一行抛出的NullPointerException。该NullPointerException发生在两个tomcat实例中。在第三个方面,应用程序运行时没有任何问题。同一个应用程序部署在所有三个tomcat实例中。在给定时间点,缓存数据仅在一个实例中可用。例如,假设我正在从Tomcat1访问应用程序。Tomcat1中的应用程序从缓存填充页面中的数据。同时,当我单独从Tomcat2访问应用程序时,Tomcat2中的应用程序会从缓存中填充数据。但在Tomcat1实例中,如果我导航到任何页面,就会得到NullPointerException。
<?xml version="1.0" encoding="UTF-8"?>
<ehcache name="WidgetCache" updateCheck="false">
<defaultCache maxElementsInMemory="10000" eternal="false"
timeToIdleSeconds="0" timeToLiveSeconds="0" overflowToDisk="true"
diskPersistent="false" diskExpiryThreadIntervalSeconds="120"
memoryStoreEvictionPolicy="LRU" />
<cacheManagerPeerListenerFactory
class="net.sf.ehcache.distribution.RMICacheManagerPeerListenerFactory"
properties="hostName=10.237.31.33, port=40003, socketTimeoutMillis=2000" />
<cacheManagerPeerProviderFactory
class="net.sf.ehcache.distribution.RMICacheManagerPeerProviderFactory"
properties="peerDiscovery=manual,
rmiUrls=//10.237.31.33:40001/widgets|//10.237.31.33:40001/lobCache|//10.237.31.33:40001/lobFilterCache|//10.237.31.33:40001/glossarylobFilterCache|//10.237.31.33:40001/bbpUserListCache|//10.237.31.33:40001/userRoleCache|//10.237.31.33:40001/glossary|//10.237.31.33:40002/widgets|//10.237.31.33:40002/lobCache|//10.237.31.33:40002/lobFilterCache|//10.237.31.33:40002/glossarylobFilterCache|//10.237.31.33:40002/bbpUserListCache|//10.237.31.33:40002/userRoleCache|//10.237.31.33:40002/glossary" />
<cache name="userRoleCache" maxElementsInMemory="100" eternal="false"
timeToIdleSeconds="0" timeToLiveSeconds="0" memoryStoreEvictionPolicy="LFU">
<cacheEventListenerFactory
class="net.sf.ehcache.distribution.RMICacheReplicatorFactory"
properties="replicateAsynchronously=true, replicatePuts=true, replicateUpdates=true,replicateUpdatesViaCopy=false, replicateRemovals=true" />
</cache>
<diskStore path="java.io.tmpdir" />
<bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager" p:cache-manager-ref="ehcache" />
<bean id="ehcache" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean"
p:config-location="classpath:ehcache.xml" p:shared="true"/>
public class PortfolioUserDetailsServiceImpl implements PortfolioUserDetailsService {
private Logger logger = LoggerFactory
.getLogger(PortfolioUserDetailsServiceImpl.class);
private UserDao userDao;
@Autowired
private CacheManager cacheManager;
private Ehcache userRoleCache;
@PostConstruct
public void init() {
// Load our widgets cache:
userRoleCache = cacheManager.getEhcache("userRoleCache");
// Create an EHCache Element to hold the widget
Element element = new Element("getAllRoles", userDao.getAllRoles());
// Add the element to the cache
userRoleCache.put(element);
}
@Override
public List<BbpUser> loadUsersfromUserListCache() throws InventoryException {
// TODO Auto-generated method stub
Cache cache = cacheManager.getCache("userRoleCache");
Element elementt = cache.get("getAllRoles");
return (List<BbpUser>) elementt.getObjectValue();
}