Ehcache不能在同一台机器上的两个tomcat之间复制

时间:2016-03-09 09:37:10

标签: java hibernate tomcat caching ehcache

我试图在同一台机器上的两个tomcat之间设置缓存复制。缓存工作完美,但复制没有。

对于我的测试,我在服务器1上调用count方法。然后我在服务器2上添加一个实体并在服务器2上调用count方法。最后我再次调用服务器1上的count方法:缓存命中并且找不到我添加的实体。

Tomcat版本:6,Java版本:1.6,Ehcache版本:1.3,OS:linux

登录启动时(两台服务器都相同):

DEBUG net.sf.ehcache.store.MemoryStore - Initialized net.sf.ehcache.store.LruMemoryStore for myCacheSample
DEBUG net.sf.ehcache.store.LruMemoryStore - myCacheSample Cache: Using SpoolingLinkedHashMap implementation
DEBUG net.sf.ehcache.Cache - Initialised cache: myCacheSample
DEBUG net.sf.ehcache.distribution.RMICacheManagerPeerListener - Adding myCacheSample to RMI listener
DEBUG net.sf.ehcache.distribution.RMICacheManagerPeerListener - 0 RMICachePeers bound in registry for RMI listener

代码添加:

public void persist(MyEntity myEntity) {
    getEntityManager().persist(myEntity);
}

缓存设置的代码计数:

public int count(String criteriaStr) {
    Criteria criteria = ((HibernateEntityManager) getEntityManager()).getSession().createCriteria(MyEntity.class);
    criteria.setCacheable(true).setCacheRegion("myCacheSample");
    criteria.add(Restrictions.eq("criteriaStr", criteriaStr));
    return (Integer) criteria.setProjection(Projections.rowCount()).uniqueResult();
}

代码删除:

public void remove(MyEntity myEntity) {
    getEntityManager().remove(myEntity);
    getEntityManager().flush();
}

服务器1 ehcache.xml(对于服务器2:端口40001和40002已切换):

<cacheManagerPeerProviderFactory
    class="net.sf.ehcache.distribution.RMICacheManagerPeerProviderFactory"
    properties="peerDiscovery=manual,
    rmiUrls=//localhost:40002/myCacheSample"/>

<cacheManagerPeerListenerFactory
    class="net.sf.ehcache.distribution.RMICacheManagerPeerListenerFactory"
    properties="
    hostName=localhost,
    port=40001,
    socketTimeoutMillis=2000
"/>

<defaultCache
    diskExpiryThreadIntervalSeconds="120"
    diskPersistent="false"
    eternal="false"
    maxElementsInMemory="1000"
    memoryStoreEvictionPolicy="LRU"
    overflowToDisk="true"
    timeToIdleSeconds="120"
    timeToLiveSeconds="120">

<cacheEventListenerFactory
    class="net.sf.ehcache.distribution.RMICacheReplicatorFactory"
    properties="
        asynchronousReplicationIntervalMillis=1000,
        replicateAsynchronously=true,
        replicatePuts=false,
        replicateRemovals=true,
        replicateUpdates=true,
        replicateUpdatesViaCopy=false
    "/>

<cache
    eternal="false"
    maxElementsInMemory="1000"
    timeToLiveSeconds="300"
    timeToIdleSeconds="300"
    name="myCacheSample"
    overflowToDisk="false"/>

感谢您的回答!

1 个答案:

答案 0 :(得分:0)

3个tomcat实例的RMI复制的工作配置希望它适合您。

    Server 1

<cacheManagerPeerProviderFactory
    class="net.sf.ehcache.distribution.RMICacheManagerPeerProviderFactory"
    properties="peerDiscovery=manual, rmiUrls=//localhost:40002/apicache|//localhost:40003/apicache" />

<cacheManagerPeerListenerFactory
    class="net.sf.ehcache.distribution.RMICacheManagerPeerListenerFactory"
    properties="hostName=192.168.1.152, port=40001, socketTimeoutMillis=2000" />

Server 2        

<cacheManagerPeerProviderFactory
    class="net.sf.ehcache.distribution.RMICacheManagerPeerProviderFactory"
    properties="peerDiscovery=manual, rmiUrls=//localhost:40001/apicache|//localhost:40003/apicache" />

<cacheManagerPeerListenerFactory
    class="net.sf.ehcache.distribution.RMICacheManagerPeerListenerFactory"
    properties="hostName=192.168.1.152, port=40002, socketTimeoutMillis=2000" />

Server 3

<cacheManagerPeerProviderFactory
    class="net.sf.ehcache.distribution.RMICacheManagerPeerProviderFactory"
    properties="peerDiscovery=manual, rmiUrls=//localhost:40001/apicache|//localhost:40002/apicache" />

<cacheManagerPeerListenerFactory
    class="net.sf.ehcache.distribution.RMICacheManagerPeerListenerFactory"
    properties="hostName=192.168.1.152, port=40003, socketTimeoutMillis=2000" />