使用RMI EhCache数据的复制缓存未在所有Tomcat实例中加载

时间:2015-01-28 16:32:35

标签: java tomcat replication rmi ehcache

我正在尝试使用RMI实现EhCache复制缓存。我有一个使用EhCache进行数据缓存的Spring Web应用程序。 Ehcache拥有很少的Java.Util.Map,用于整个应用程序视图页面。当用户从前端屏幕创建记录(比如一些商业JAVA POJO对象)时,该记录将被插入到数据库中,随后,EhCache保存的地图将被更新。

稍后,我们在同一台机器中的 3个tomcat实例中部署了此Web应用程序。可以通过Apache HTTP Load Balancer访问应用程序。

Promblem我面临的是,EhCache数据正在一个tomcat实例上加载。但不是其他两个。当使用端口号单独访问应用程序时,应用程序运行正常。

相同的Spring Web应用程序在Tomcat实例(9001,9002,9003)上运行。 EhCache RMI配置为侦听(4001,4002,4003)。

请找到我配置的ehCache.xml文件

在Tomcat实例1上

<?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" />

在Tomcat实例2上

<?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=40002, 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: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" />

在Tomcat实例3上

<?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" />

请告知我在这里缺少的东西。

以下是在spring-servlet.xml中配置的

<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"/>

来自Java业务类

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);

}

我在以下方法上得到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();
}

1 个答案:

答案 0 :(得分:0)

当replicateUpdatesViaCopy设置为false时,将从其他实例上的缓存中删除条目。您需要检查null并从数据存储区加载它(或者您拥有最新值时)。

您可以将replicateUpdatesViaCopy设置为true,并检查您是否仍在访问其他实例时获取NPE。

有关完整的详细信息,您可以浏览http://www.ehcache.org/generated/2.10.0/html/ehc-all/index.html#page/Ehcache_Documentation_Set/co-rmi_configuring_cache_replicators.html