Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.hazelcast.util;
- import com.hazelcast.config.*;
- import com.hazelcast.core.*;
- import java.util.*;
- import java.util.concurrent.*;
- /**
- * IMap.replace does not result in a call to MapStore.store, even though
- * IMap.put does. Hazelcast 1.9.3.1.
- */
- public class IMapReplaceExample {
- public static void main(String... args) {
- Config config = new Config();
- config
- .getMapConfig("myMap")
- .setMapStoreConfig(new MapStoreConfig()
- //.setWriteDelaySeconds(1)
- .setImplementation(myMapStore));
- HazelcastInstance hc = Hazelcast.newHazelcastInstance(config);
- try {
- IMap<String, Long> myMap = hc.getMap("myMap");
- myMap.put("one", 1L);
- System.err.printf("main: Replacing 1 with 111.%n");
- myMap.replace("one", 1L, 111L);
- System.err.printf("main: Flushing map%n");
- myMap.flush();
- System.err.printf("main: myMap.get(one) returns %d%n", myMap.get("one"));
- System.err.printf("main: Unconditional replace of value.%n");
- myMap.replace("one", 1L);
- System.err.printf("main: myMap.get(one) returns %d%n", myMap.get("one"));
- } finally {
- Hazelcast.shutdownAll();
- }
- }
- static final ConcurrentMap<String, Long> store = new ConcurrentHashMap<String, Long>();
- static final MapStore<String, Long> myMapStore = new SimpleMapStore<String, Long>(store);
- static class SimpleMapStore<K, V> implements MapStore<K, V> {
- SimpleMapStore(ConcurrentMap<K, V> store) {
- this.store = store;
- }
- @Override public V load(K key) {
- V value = store.get(key);
- System.err.printf("SimpleMapStore.load(%s) = %s%n", key, value);
- return value;
- }
- @Override public Map<K, V> loadAll(Collection<K> keys) {
- Map<K, V> result = new HashMap<K, V>();
- for (K key : keys) {
- V value = store.get(key);
- if (value != null) {
- result.put(key, value);
- }
- }
- System.err.printf("SimpleMapStore.loadAll(%s) = %s%n", keys, result);
- return result;
- }
- @Override public Set<K> loadAllKeys() {
- Set<K> keys = store.keySet();
- System.err.printf("SimpleMapStore.loadAllKeys() = %s%n", keys);
- return keys;
- }
- //
- // MapStore methods
- //
- @Override public void store(K key, V value) {
- System.err.printf("SimpleMapStore.store(%s, %s)%n", key, value);
- store.put(key, value);
- }
- @Override public void delete(K key) {
- System.err.printf("SimpleMapStore.delete(%s)%n", key);
- store.remove(key);
- }
- @Override public void storeAll(Map<K, V> map) {
- System.err.printf("SimpleMapStore.storeAll(%s)%n", map);
- store.putAll(map);
- }
- @Override public void deleteAll(Collection<K> keys) {
- System.err.printf("SimpleMapStore.deleteAll(%s)%n", keys);
- for (K key : keys) {
- store.remove(key);
- }
- }
- private final ConcurrentMap<K, V> store;
- };
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement