2
我想在同一臺機器上的兩個tomcat之間設置一個高速緩存複製。緩存工作正常,但複製沒有。Ehcache不在同一臺機器上的兩個tomcat之間複製
對於我的測試,我調用服務器1.計數方法,然後我在服務器2添加一個實體,並呼籲服務器2的計數方法最後,我再次呼籲對服務器1的計數方法:緩存命中並找不到我添加的實體。
的Tomcat版本:6,Java版本:1.6,的Ehcache版本:1.3,操作系統: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"/>
感謝您的解答!
當兩個tomcat位於不同的服務器上時它起作用嗎? – avianey
不,但我不確定網絡管理(防火牆,端口等)。這就是爲什麼我之前在同一臺機器上測試,而不是在多播模式下。 – elfdev