ecache配置rmi

ecache的集群支持多种方式,这里通过RMI实现。

测试方式:

本机,两个不同的进程(java两个不同的工程)


工程1的ehcache.xml



<?xml version="1.0" encoding="UTF-8"?>
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="ehcache.xsd">
<cacheManagerPeerProviderFactory
class="net.sf.ehcache.distribution.RMICacheManagerPeerProviderFactory"
properties="peerDiscovery=manual,
rmiUrls=//localhost:40000/UserCache" />

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

<defaultCache maxElementsInMemory="10000" eternal="false"
timeToIdleSeconds="120" timeToLiveSeconds="120" overflowToDisk="true"
diskSpoolBufferSizeMB="30" maxElementsOnDisk="10000000"
diskPersistent="false" diskExpiryThreadIntervalSeconds="120"
memoryStoreEvictionPolicy="LRU">
<cacheEventListenerFactory
class="net.sf.ehcache.distribution.RMICacheReplicatorFactory" />
</defaultCache>

<cache name="UserCache" maxElementsInMemory="1000" eternal="false"
timeToIdleSeconds="100000" timeToLiveSeconds="100000"
overflowToDisk="false">
<cacheEventListenerFactory
class="net.sf.ehcache.distribution.RMICacheReplicatorFactory" />
</cache>
</ehcache>



rmiUrls=//localhost:40000/UserCache (ip+port+缓存名称)
表示本服务缓存数据改变的时候,通知其他服务也更新,可配置多个
properties="hostName=localhost,port=40001,socketTimeoutMillis=120000"
表示本服务监听的一个监听端口,当其他服务的缓存数据改变的时候,就通过其通知服务同步更新数据
工程1的java代码:


package tutorial;

import java.net.URL;
import java.util.List;

import net.sf.ehcache.Cache;
import net.sf.ehcache.CacheManager;
import net.sf.ehcache.Element;

public class UsingCacheCluster {

public static void main(String[] args) throws Exception {
URL url = UsingCacheCluster.class.getClassLoader().getResource(
"config/ehcache.xml");
CacheManager manager = new CacheManager(url);
//取得Cache
Cache cache = manager.getCache("UserCache");
int i=0;
while(true) {
//System.out.println("write:"+t);
Element element = new Element("Write"+i, "Write"+i++);
cache.put(element);
Thread.sleep(4500);
List list = cache.getKeys();
for (String str : list) {
System.out.print(str+" || ");
}
System.out.println();
}

//Element element1 = cache.get("key1");
//System.out.println(element1.getValue());
}

}



工程2的ehcache.xml配置
只需将工程1的ehcache.xml配置文件中,上面的列举说明的两个属性的端口调换


工程2的java代码:

package tutorial;

import java.net.URL;
import java.util.List;

import net.sf.ehcache.Cache;
import net.sf.ehcache.CacheManager;
import net.sf.ehcache.Element;

public class UsingCacheCluster {

public static void main(String[] args) throws Exception {
URL url = UsingCacheCluster.class.getClassLoader().getResource(
"config/ehcache.xml");
CacheManager manager = new CacheManager(url);
//取得Cache
Cache cache = manager.getCache("UserCache");

String t="";
int k = 100;
while(true) {
Thread.sleep(4500);
cache.put(new Element("read"+k,"read"+k++));
List<String> list = cache.getKeys();
for (String str : list) {
System.out.print(str+" || ");
}
System.out.println();
}
}

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值