Oracle Coherence直读配置

时间:2012-03-12 07:28:04

标签: java caching oracle-coherence

我的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"));
    }
}

1 个答案:

答案 0 :(得分:2)

  1. 您的配置错误。您不能以这种方式合并操作配置和缓存配置(至少不是没有技巧)。您应该将其拆分为两个文件:tangosol-coherence-override.xmlcoherence-cache-config.xml,如下所示:

    Tangosol的相干-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>
    

    相干缓存-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>
    
  2. 您的DataLoader缺少接受String参数的构造函数(对于缓存名称),
  3. 如果我认为你有意,你应该考虑在Reader节点上禁用存储,即设置tangosol.coherence.distributed.localstorage=false