简体   繁体   中英

Hazelcast Map listener not getting the updated object after updation in Map store

I am using Map store for a map in Hazelcast.I am modifying an object inside the Map store.My map store also implements PostProcessingMapStore interface.

I create a client which does a Map.put to the cache.This put triggers the store on MapStore.I have a listener to the Map on client side also.When I try to do a map.get, I get the modified object back(because of PostProcessingMapStore) , but the entry added method inside the listener shows the unmodified object.Am I missing some configuration?

I think this is an expected behaviour. Listener's entryAdded method will be triggered when you put element to map. It will not be affected or triggered by PostProcessingMapStore's updates because it is already invoked by put operation.

I tried to create your scenario. Here is the code:

public class Main  {        

public static void main(String[] args){
    Config config = new Config();
    MapConfig mapConfig = config.getMapConfig("map");
    MapStoreConfig mapStoreConfig = new MapStoreConfig();
    mapStoreConfig.setEnabled(true).setClassName(IncrementerPostProcessingMapStore.class.getName());
    mapConfig.setMapStoreConfig(mapStoreConfig);

    HazelcastInstance instance = Hazelcast.newHazelcastInstance(config);
    HazelcastInstance client = HazelcastClient.newHazelcastClient();

    IMap<Integer, SampleObject> map = client.getMap("map");
    map.addEntryListener(new EntryAdapter<Object, SampleObject>() {
        @Override
        public void entryAdded(EntryEvent<Object, SampleObject> event) {
            System.out.println("entry added:" + event.getValue().version);
        }
    }, true);

    map.put(1, new SampleObject(1));

    System.out.println("last:" + map.get(1).version);
}

public static class IncrementerPostProcessingMapStore implements MapStore<Integer, SampleObject>, PostProcessingMapStore {

    Map<Integer, SampleObject> map = new ConcurrentHashMap<Integer, SampleObject>();

    @Override
    public void store(Integer key, SampleObject value) {
        value.version++;
        map.put(key, value);
    }
    ...
}

public static class SampleObject implements Serializable {

    public int version;

    public SampleObject(int version) {
        this.version = version;
    }
}
}

Output:

entry added: 1
last: 2

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM