我的Coherence tangosol-coherence-override.xml配置的一部分不起作用。
我可以启动服务器,放置并获取数据,但无法通过CacheLoader获取数据。
谢谢。
CacheLoader的存根
package com.example;
import com.tangosol.net.cache.AbstractCacheLoader;
import java.util.HashMap;
public class DataLoader extends AbstractCacheLoader {
private HashMap<String, String> hashMap;
public DataLoader() {
hashMap = new HashMap<String, String>();
init();
}
private void init() {
hashMap.put("A", "a");
hashMap.put("B", "b");
hashMap.put("C", "c");
hashMap.put("D", "d");
hashMap.put("E", "f");
hashMap.put("F", "g");
}
public Object load(Object key) {
return hashMap.get(key);
}
}
tangosol-coherence-override.xml
<coherence>
<cluster-config>
<member-identity>
<cluster-name>thecluster</cluster-name>
</member-identity>
<multicast-listener>
<!--<address>224.3.6.0</address>-->
<port>9100</port>
<time-to-live>0</time-to-live>
</multicast-listener>
<caching-scheme-mapping>
<cache-mapping>
<cache-name>ABC</cache-name>
<scheme-name>distributed-rxwbm</scheme-name>
</cache-mapping>
</caching-scheme-mapping>
</cluster-config>
<caching-schemes>
<distributed-scheme>
<scheme-name>distributed-rwbm</scheme-name>
<backing-map-scheme>
<read-write-backing-map-scheme>
<internal-cache-scheme>
<local-scheme/>
</internal-cache-scheme>
<cachestore-scheme>
<class-scheme>
<class-name>com.example.DataLoader</class-name>
<init-params>
<init-param>
<param-type>java.lang.String</param-type>
<param-value>{cache-name}</param-value>
</init-param>
</init-params>
</class-scheme>
</cachestore-scheme>
</read-write-backing-map-scheme>
</backing-map-scheme>
</distributed-scheme>
</caching-schemes>
</coherence>
启动服务器
package com.example;
import com.tangosol.net.DefaultCacheServer;
import com.tangosol.net.DefaultConfigurableCacheFactory;
public class HelloWorld {
public static void main(String[] args) {
DefaultConfigurableCacheFactory factory;
factory = new DefaultConfigurableCacheFactory();
DefaultCacheServer dcs = new DefaultCacheServer(factory);
dcs.startAndMonitor(5000);
}
}
读取数据
package com.example;
import com.tangosol.net.CacheFactory;
import com.tangosol.net.NamedCache;
public class Reader {
public static void main(String[] args) {
CacheFactory.ensureCluster();
NamedCache cache = CacheFactory.getCache("ABC");
System.out.println("Value in cache is: " + cache.get("A"));
System.out.println("Value in cache is: " + cache.get("B"));
System.out.println("Value in cache is: " + cache.get("C"));
System.out.println("Value in cache is: " + cache.get("D"));
}
}
最佳答案
您的配置错误。您不能以这种方式合并操作配置和缓存配置(至少并非没有技巧)。您应该将其拆分为两个文件:tangosol-coherence-override.xml
和coherence-cache-config.xml
,如下所示:
tangosol-coherence-override.xml:
<?xml version="1.0" encoding="UTF-8"?>
<coherence xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://xmlns.oracle.com/coherence/coherence-operational-config"
xsi:schemaLocation="http://xmlns.oracle.com/coherence/coherence-operational-config coherence-operational-config.xsd">
<cluster-config>
<member-identity>
<cluster-name>thecluster</cluster-name>
</member-identity>
<multicast-listener>
<!--<address>224.3.6.0</address>-->
<port>9100</port>
<time-to-live>0</time-to-live>
</multicast-listener>
</cluster-config>
</coherence>
coherence-cache-config.xml:
<?xml version="1.0" encoding="UTF-8"?>
<cache-config
xmlns="http://xmlns.oracle.com/coherence/coherence-cache-config"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.oracle.com/coherence/coherence-cache-config coherence-cache-config.xsd">
<caching-scheme-mapping>
<cache-mapping>
<cache-name>ABC</cache-name>
<scheme-name>distributed-rxwbm</scheme-name>
</cache-mapping>
</caching-scheme-mapping>
<caching-schemes>
<distributed-scheme>
<scheme-name>distributed-rxwbm</scheme-name>
<backing-map-scheme>
<read-write-backing-map-scheme>
<internal-cache-scheme>
<local-scheme/>
</internal-cache-scheme>
<cachestore-scheme>
<class-scheme>
<class-name>com.example.DataLoader</class-name>
<init-params>
<init-param>
<param-type>java.lang.String</param-type>
<param-value>{cache-name}</param-value>
</init-param>
</init-params>
</class-scheme>
</cachestore-scheme>
</read-write-backing-map-scheme>
</backing-map-scheme>
<autostart>true</autostart>
</distributed-scheme>
</caching-schemes>
</cache-config>
您的
DataLoader
缺少接受String
参数(用于缓存名称)的构造函数,如果您的意图正确,则应考虑在
Reader
节点上禁用存储,即通过设置tangosol.coherence.distributed.localstorage=false
。