Hazelcast IMap有时会返回null值对象,即使它存在于地图

时间:2017-01-12 05:54:12

标签: java hazelcast hazelcast-imap

我在Hazelcast中有一个带有键的地图,其值与IMap相似。现在我和我有两个节点。问题是当我试图从Hazelcast地图获取值对象时,有时它返回我NULL并且下一次调用,它工作正常。由于这个工作正常一段时间,我认为代码没有问题。在这里,我将从hazelcast.xml文件共享我的地图配置。

<map name="MY_HAZELCAST_MAP">
       <in-memory-format>OBJECT</in-memory-format>
       <backup-count>1</backup-count>
       <async-backup-count>0</async-backup-count>
       <time-to-live-seconds>1000</time-to-live-seconds>
       <eviction-percentage>25</eviction-percentage>
       <max-size policy="PER_NODE">3000</max-size>
       <cache-deserialized-values>ALWAYS</cache-deserialized-values>
       <merge-policy>com.hazelcast.map.merge.PassThroughMergePolicy</merge-policy>
</map>

如果我配置错误,请告诉我。使用hazelcast-all-3.7.3.jar

这是hazelcast.xml文件

<?xml version="1.0" encoding="UTF-8"?>
<hazelcast xsi:schemaLocation="http://www.hazelcast.com/schema/config hazelcast-config-3.6.xsd"
           xmlns="http://www.hazelcast.com/schema/config"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <properties>
        <property name="instance.name">MY_INSTANCE</property>
        <property name="hazelcast.logging.type">log4j</property>
    </properties>
    <group>
        <name>dev1</name>
        <password>dev-pass</password>
    </group>
    <management-center enabled="false">http://localhost:8080/mancenter</management-center>
    <network>
        <port auto-increment="false" port-count="100">51253</port>
        <outbound-ports>
            <ports>0</ports>
        </outbound-ports>
        <join>
            <multicast enabled="false"/>
            <tcp-ip enabled="true">
                <member-list>
                    <member>192.168.1.87:51253</member>
                </member-list>
            </tcp-ip>
            <discovery-strategies>
            </discovery-strategies>
        </join>
        <ssl enabled="false"/>
        <socket-interceptor enabled="false"/>
        <symmetric-encryption enabled="false">
            <algorithm>PBEWithMD5AndDES</algorithm>
            <salt>thesalt</salt>
            <password>thepass</password>
            <iteration-count>19</iteration-count>
        </symmetric-encryption>
    </network>
    <partition-group enabled="false"/>
    <executor-service name="default">
        <pool-size>16</pool-size>
        <queue-capacity>0</queue-capacity>
    </executor-service>
    <queue name="default">
        <max-size>0</max-size>
        <backup-count>1</backup-count>
        <async-backup-count>0</async-backup-count>
        <empty-queue-ttl>-1</empty-queue-ttl>
    </queue>
    <map name="MY_HAZELCAST_MAP">
           <in-memory-format>OBJECT</in-memory-format>
           <backup-count>1</backup-count>
           <async-backup-count>0</async-backup-count>
           <time-to-live-seconds>1000</time-to-live-seconds>
           <eviction-percentage>25</eviction-percentage>
           <max-size policy="PER_NODE">3000</max-size>
           <cache-deserialized-values>ALWAYS</cache-deserialized-values>
           <merge-policy>com.hazelcast.map.merge.PassThroughMergePolicy</merge-policy>
    </map>
    <map name="default">
        <in-memory-format>BINARY</in-memory-format>
        <backup-count>1</backup-count>
        <async-backup-count>0</async-backup-count>
        <time-to-live-seconds>0</time-to-live-seconds>
        <max-idle-seconds>0</max-idle-seconds>
        <eviction-policy>NONE</eviction-policy>
        <max-size policy="PER_NODE">0</max-size>
        <eviction-percentage>25</eviction-percentage>
        <min-eviction-check-millis>100</min-eviction-check-millis>
        <merge-policy>com.hazelcast.map.merge.PutIfAbsentMapMergePolicy</merge-policy>
        <cache-deserialized-values>INDEX-ONLY</cache-deserialized-values>
    </map>

    <multimap name="default">
        <backup-count>1</backup-count>
        <value-collection-type>SET</value-collection-type>
    </multimap>

    <list name="default">
        <backup-count>1</backup-count>
    </list>

    <set name="default">
        <backup-count>1</backup-count>
    </set>

    <jobtracker name="default">
        <max-thread-size>0</max-thread-size>
        <queue-size>0</queue-size>
        <retry-count>0</retry-count>
        <chunk-size>1000</chunk-size>
        <communicate-stats>true</communicate-stats>
        <topology-changed-strategy>CANCEL_RUNNING_OPERATION</topology-changed-strategy>
    </jobtracker>

    <semaphore name="default">
        <initial-permits>0</initial-permits>
        <backup-count>1</backup-count>
        <async-backup-count>0</async-backup-count>
    </semaphore>

    <reliable-topic name="default">
        <read-batch-size>10</read-batch-size>
        <topic-overload-policy>BLOCK</topic-overload-policy>
        <statistics-enabled>true</statistics-enabled>
    </reliable-topic>

    <ringbuffer name="default">
        <capacity>10000</capacity>
        <backup-count>1</backup-count>
        <async-backup-count>0</async-backup-count>
        <time-to-live-seconds>30</time-to-live-seconds>
        <in-memory-format>BINARY</in-memory-format>
    </ringbuffer>

    <serialization>
        <portable-version>0</portable-version>
    </serialization>
    <services enable-defaults="true"/>
    <lite-member enabled="false"/>
</hazelcast>

有关示例代码,请参阅我发布的Not able to retrieve element from Hazelcast's Map if using custom object as a key。我的密钥包含可以更改的时间变量。正如Desai先生提到的基于二进制数据的Hazelcast密钥搜索,这里是我用来从IMap中获取实际密钥并进一步使用以获取值对象的行。

HazelcastInstance instance = Hazelcast.newHazelcastInstance();
private static ConcurrentMap<HMapKey, String> testMap = instance.getMap("MY_HAZELCAST_MAP");
testMap.put(new HMapKey("code1",123), "This is Code 1");
....
....
....
// While retrieving
private static ConcurrentMap<HMapKey, String> testMap2 = instance.getMap("MY_HAZELCAST_MAP");
HMapKey key = new HMapKey("code1",0);
key = ArrayList<HMapKey>(testMap2.keySet()).get(new ArrayList<HMapKey>(testMap2.keySet()).indexOf(key));

String MapValue = testMap2.get(key);

如果我让你明白,请告诉我。我也把这个问题和我以前的问题联系起来。如果这不起作用,也会在此问题中添加该内容。

1 个答案:

答案 0 :(得分:0)

Not able to retrieve element from Hazelcast's Map if using custom object as a key上的关键班HMapKey是造成问题的原因。

对于密钥,所有字段都应该是相等测试的一部分。

此外,对于Hazelcast,按键进行了序列化比较。