Search in sources :

Example 6 with EntryAdapter

use of com.hazelcast.core.EntryAdapter in project hazelcast by hazelcast.

the class AbstractMultiMapAddEntryListenerMessageTask method call.

@Override
protected Object call() throws Exception {
    final ClientEndpoint endpoint = getEndpoint();
    final MultiMapService service = getService(MultiMapService.SERVICE_NAME);
    EntryAdapter listener = new MultiMapListener();
    final String name = getDistributedObjectName();
    Data key = getKey();
    boolean includeValue = shouldIncludeValue();
    String registrationId = service.addListener(name, listener, key, includeValue, isLocalOnly());
    endpoint.addListenerDestroyAction(MultiMapService.SERVICE_NAME, name, registrationId);
    return registrationId;
}
Also used : MultiMapService(com.hazelcast.multimap.impl.MultiMapService) EntryAdapter(com.hazelcast.core.EntryAdapter) Data(com.hazelcast.nio.serialization.Data) ClientEndpoint(com.hazelcast.client.ClientEndpoint)

Example 7 with EntryAdapter

use of com.hazelcast.core.EntryAdapter in project hazelcast by hazelcast.

the class AllTest method loadMapOperations.

private List<Runnable> loadMapOperations() {
    ArrayList<Runnable> operations = new ArrayList<Runnable>();
    addOperation(operations, new Runnable() {

        public void run() {
            IMap map = hazelcast.getMap("myMap");
            map.evict(random.nextInt(SIZE));
        }
    }, 5);
    addOperation(operations, new Runnable() {

        public void run() {
            IMap map = hazelcast.getMap("myMap");
            try {
                map.getAsync(random.nextInt(SIZE)).get();
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            } catch (ExecutionException e) {
                throw new RuntimeException(e);
            }
        }
    }, 1);
    addOperation(operations, new Runnable() {

        public void run() {
            IMap map = hazelcast.getMap("myMap");
            map.containsKey(random.nextInt(SIZE));
        }
    }, 2);
    addOperation(operations, new Runnable() {

        public void run() {
            IMap map = hazelcast.getMap("myMap");
            map.containsValue(new Customer(random.nextInt(100), String.valueOf(random.nextInt(100000))));
        }
    }, 2);
    addOperation(operations, new Runnable() {

        public void run() {
            IMap map = hazelcast.getMap("myMap");
            int key = random.nextInt(SIZE);
            map.lock(key);
            try {
                Thread.sleep(1);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            } finally {
                map.unlock(key);
            }
        }
    }, 1);
    //        addOperation(operations, new Runnable() {
    //            public void run() {
    //                IMap map = hazelcast.getMap("myMap");
    //                int key = random.nextInt(SIZE);
    //                map.lockMap(10, TimeUnit.MILLISECONDS);
    //                try {
    //                    Thread.sleep(1);
    //                } catch (InterruptedException e) {
    //                } finally {
    //                    map.unlockMap();
    //                }
    //            }
    //        }, 1);
    addOperation(operations, new Runnable() {

        public void run() {
            IMap map = hazelcast.getMap("myMap");
            int key = random.nextInt(SIZE);
            boolean locked = map.tryLock(key);
            if (locked) {
                try {
                    Thread.sleep(1);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                } finally {
                    map.unlock(key);
                }
            }
        }
    }, 1);
    addOperation(operations, new Runnable() {

        public void run() {
            IMap map = hazelcast.getMap("myMap");
            int key = random.nextInt(SIZE);
            boolean locked = false;
            try {
                locked = map.tryLock(key, 10, TimeUnit.MILLISECONDS);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            if (locked) {
                try {
                    Thread.sleep(1);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                } finally {
                    map.unlock(key);
                }
            }
        }
    }, 1);
    addOperation(operations, new Runnable() {

        public void run() {
            IMap map = hazelcast.getMap("myMap");
            Iterator it = map.entrySet().iterator();
            for (int i = 0; i < 10 && it.hasNext(); i++) {
                it.next();
            }
        }
    }, 1);
    addOperation(operations, new Runnable() {

        public void run() {
            IMap map = hazelcast.getMap("myMap");
            map.getEntryView(random.nextInt(SIZE));
        }
    }, 2);
    addOperation(operations, new Runnable() {

        public void run() {
            IMap map = hazelcast.getMap("myMap");
            map.isEmpty();
        }
    }, 3);
    addOperation(operations, new Runnable() {

        public void run() {
            IMap map = hazelcast.getMap("myMap");
            map.put(random.nextInt(SIZE), new Customer(random.nextInt(100), String.valueOf(random.nextInt(10000))));
        }
    }, 50);
    addOperation(operations, new Runnable() {

        public void run() {
            IMap map = hazelcast.getMap("myMap");
            map.tryPut(random.nextInt(SIZE), new Customer(random.nextInt(100), String.valueOf(random.nextInt(10000))), 10, TimeUnit.MILLISECONDS);
        }
    }, 5);
    addOperation(operations, new Runnable() {

        public void run() {
            IMap map = hazelcast.getMap("myMap");
            try {
                map.putAsync(random.nextInt(SIZE), new Customer(random.nextInt(100), String.valueOf(random.nextInt(10000)))).get();
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            } catch (ExecutionException e) {
                throw new RuntimeException(e);
            }
        }
    }, 5);
    addOperation(operations, new Runnable() {

        public void run() {
            IMap map = hazelcast.getMap("myMap");
            map.put(random.nextInt(SIZE), new Customer(random.nextInt(100), String.valueOf(random.nextInt(10000))), 10, TimeUnit.MILLISECONDS);
        }
    }, 5);
    addOperation(operations, new Runnable() {

        public void run() {
            IMap map = hazelcast.getMap("myMap");
            map.putIfAbsent(random.nextInt(SIZE), new Customer(random.nextInt(100), String.valueOf(random.nextInt(10000))), 10, TimeUnit.MILLISECONDS);
        }
    }, 5);
    addOperation(operations, new Runnable() {

        public void run() {
            IMap map = hazelcast.getMap("myMap");
            map.putIfAbsent(random.nextInt(SIZE), new Customer(random.nextInt(100), String.valueOf(random.nextInt(10000))));
        }
    }, 5);
    addOperation(operations, new Runnable() {

        public void run() {
            IMap map = hazelcast.getMap("myMap");
            Map localMap = new HashMap();
            for (int i = 0; i < 10; i++) {
                localMap.put(random.nextInt(SIZE), new Customer(random.nextInt(100), String.valueOf(random.nextInt(10000))));
            }
            map.putAll(localMap);
        }
    }, 1);
    addOperation(operations, new Runnable() {

        public void run() {
            IMap map = hazelcast.getMap("myMap");
            map.get(random.nextInt(SIZE));
        }
    }, 100);
    addOperation(operations, new Runnable() {

        public void run() {
            IMap map = hazelcast.getMap("myMap");
            map.remove(random.nextInt(SIZE));
        }
    }, 10);
    addOperation(operations, new Runnable() {

        public void run() {
            IMap map = hazelcast.getMap("myMap");
            map.tryRemove(random.nextInt(SIZE), 10, TimeUnit.MILLISECONDS);
        }
    }, 10);
    addOperation(operations, new Runnable() {

        public void run() {
            IMap map = hazelcast.getMap("myMap");
            map.removeAsync(random.nextInt(SIZE));
        }
    }, 10);
    addOperation(operations, new Runnable() {

        public void run() {
            IMap map = hazelcast.getMap("myMap");
            map.remove(random.nextInt(SIZE), new Customer(random.nextInt(100), String.valueOf(random.nextInt(10000))));
        }
    }, 10);
    addOperation(operations, new Runnable() {

        public void run() {
            IMap map = hazelcast.getMap("myMap");
            map.replace(random.nextInt(SIZE), new Customer(random.nextInt(100), String.valueOf(random.nextInt(10000))));
        }
    }, 4);
    addOperation(operations, new Runnable() {

        public void run() {
            IMap map = hazelcast.getMap("myMap");
            map.replace(random.nextInt(SIZE), new Customer(random.nextInt(100), String.valueOf(random.nextInt(10000))), new Customer(random.nextInt(100), String.valueOf(random.nextInt(10000))));
        }
    }, 5);
    addOperation(operations, new Runnable() {

        public void run() {
            IMap map = hazelcast.getMap("myMap");
            map.size();
        }
    }, 4);
    addOperation(operations, new Runnable() {

        public void run() {
            IMap map = hazelcast.getMap("myMap");
            Iterator it = map.entrySet(new SqlPredicate("year=" + random.nextInt(100))).iterator();
            while (it.hasNext()) {
                it.next();
            }
        }
    }, 1);
    addOperation(operations, new Runnable() {

        public void run() {
            IMap map = hazelcast.getMap("myMap");
            Iterator it = map.entrySet(new SqlPredicate("name=" + random.nextInt(10000))).iterator();
            while (it.hasNext()) {
                it.next();
            }
        }
    }, 10);
    addOperation(operations, new Runnable() {

        public void run() {
            IMap map = hazelcast.getMap("myMap");
            Iterator it = map.keySet(new SqlPredicate("name=" + random.nextInt(10000))).iterator();
            while (it.hasNext()) {
                it.next();
            }
        }
    }, 10);
    addOperation(operations, new Runnable() {

        public void run() {
            IMap map = hazelcast.getMap("myMap");
            Iterator it = map.localKeySet().iterator();
            while (it.hasNext()) {
                it.next();
            }
        }
    }, 10);
    addOperation(operations, new Runnable() {

        public void run() {
            IMap map = hazelcast.getMap("myMap");
            Iterator it = map.localKeySet(new SqlPredicate("name=" + random.nextInt(10000))).iterator();
            while (it.hasNext()) {
                it.next();
            }
        }
    }, 10);
    addOperation(operations, new Runnable() {

        public void run() {
            IMap map = hazelcast.getMap("myMap");
            final CountDownLatch latch = new CountDownLatch(1);
            EntryListener listener = new EntryAdapter() {

                @Override
                public void onEntryEvent(EntryEvent event) {
                    latch.countDown();
                }
            };
            String id = map.addEntryListener(listener, true);
            try {
                latch.await();
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            map.removeEntryListener(id);
        }
    }, 1);
    addOperation(operations, new Runnable() {

        public void run() {
            IMap map = hazelcast.getMap("myMap");
            map.addIndex("year", true);
        }
    }, 1);
    addOperation(operations, new Runnable() {

        public void run() {
            IMap map = hazelcast.getMap("myMap");
            final CountDownLatch latch = new CountDownLatch(1);
            EntryListener listener = new EntryAdapter() {

                @Override
                public void onEntryEvent(EntryEvent event) {
                    latch.countDown();
                }
            };
            String id = map.addLocalEntryListener(listener);
            try {
                latch.await();
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            map.removeEntryListener(id);
        }
    }, 1);
    return operations;
}
Also used : HashMap(java.util.HashMap) EntryAdapter(com.hazelcast.core.EntryAdapter) ArrayList(java.util.ArrayList) SqlPredicate(com.hazelcast.query.SqlPredicate) CountDownLatch(java.util.concurrent.CountDownLatch) EntryListener(com.hazelcast.core.EntryListener) IMap(com.hazelcast.core.IMap) EntryEvent(com.hazelcast.core.EntryEvent) Iterator(java.util.Iterator) ExecutionException(java.util.concurrent.ExecutionException) HashMap(java.util.HashMap) Map(java.util.Map) IMap(com.hazelcast.core.IMap)

Example 8 with EntryAdapter

use of com.hazelcast.core.EntryAdapter in project hazelcast by hazelcast.

the class IssuesTest method testIssue321_3.

// verify an event including values and an event excluding values are received, in any order
@Test
public void testIssue321_3() throws Exception {
    int n = 1;
    TestHazelcastInstanceFactory factory = createHazelcastInstanceFactory(n);
    final IMap<Integer, Integer> imap = factory.newHazelcastInstance(getConfig()).getMap("testIssue321_3");
    final List<EntryEvent<Integer, Integer>> eventsWithValues = new ArrayList<EntryEvent<Integer, Integer>>();
    final List<EntryEvent<Integer, Integer>> eventsWithoutValues = new ArrayList<EntryEvent<Integer, Integer>>();
    final EntryAdapter<Integer, Integer> listener = new EntryAdapter<Integer, Integer>() {

        @Override
        public void entryAdded(com.hazelcast.core.EntryEvent<Integer, Integer> event) {
            if (event.getValue() == null) {
                eventsWithoutValues.add(event);
            } else {
                eventsWithValues.add(event);
            }
        }
    };
    imap.addEntryListener(listener, true);
    Thread.sleep(50L);
    imap.addEntryListener(listener, false);
    imap.put(1, 1);
    assertEqualsEventually(new Callable<Integer>() {

        @Override
        public Integer call() throws Exception {
            return eventsWithValues.size();
        }
    }, 1);
    assertEqualsEventually(new Callable<Integer>() {

        @Override
        public Integer call() throws Exception {
            return eventsWithoutValues.size();
        }
    }, 1);
}
Also used : EntryAdapter(com.hazelcast.core.EntryAdapter) ArrayList(java.util.ArrayList) IOException(java.io.IOException) EntryEvent(com.hazelcast.core.EntryEvent) TestHazelcastInstanceFactory(com.hazelcast.test.TestHazelcastInstanceFactory) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test) ParallelTest(com.hazelcast.test.annotation.ParallelTest)

Example 9 with EntryAdapter

use of com.hazelcast.core.EntryAdapter in project hazelcast by hazelcast.

the class MultiMapListenerTest method testListeners.

@Test
public void testListeners() throws Exception {
    int count = 4;
    String name = randomMapName();
    Config config = new Config();
    config.getMultiMapConfig(name).setValueCollectionType(MultiMapConfig.ValueCollectionType.LIST);
    TestHazelcastInstanceFactory factory = createHazelcastInstanceFactory(count);
    HazelcastInstance[] instances = factory.newInstances(config);
    final Set<String> keys = Collections.newSetFromMap(new ConcurrentHashMap<String, Boolean>());
    EntryListener<String, String> listener = new EntryAdapter<String, String>() {

        public void entryAdded(EntryEvent<String, String> event) {
            keys.add(event.getKey());
        }

        public void entryRemoved(EntryEvent<String, String> event) {
            keys.remove(event.getKey());
        }

        @Override
        public void mapCleared(MapEvent event) {
            keys.clear();
        }
    };
    final MultiMap<String, String> multiMap = instances[0].getMultiMap(name);
    final String id = multiMap.addLocalEntryListener(listener);
    multiMap.put("key1", "val1");
    multiMap.put("key2", "val2");
    multiMap.put("key3", "val3");
    multiMap.put("key4", "val4");
    multiMap.put("key5", "val5");
    multiMap.put("key6", "val6");
    multiMap.put("key7", "val7");
    multiMap.put("key8", "val8");
    assertTrueEventually(new AssertTask() {

        @Override
        public void run() throws Exception {
            assertContainsAll(multiMap.localKeySet(), keys);
        }
    });
    if (keys.size() != 0) {
        multiMap.remove(keys.iterator().next());
    }
    assertTrueEventually(new AssertTask() {

        @Override
        public void run() throws Exception {
            assertContainsAll(multiMap.localKeySet(), keys);
        }
    });
    multiMap.removeEntryListener(id);
    getMultiMap(instances, name).clear();
    keys.clear();
    final String id2 = multiMap.addEntryListener(listener, true);
    getMultiMap(instances, name).put("key3", "val3");
    getMultiMap(instances, name).put("key3", "val33");
    getMultiMap(instances, name).put("key4", "val4");
    getMultiMap(instances, name).remove("key3", "val33");
    assertSizeEventually(1, keys);
    getMultiMap(instances, name).clear();
    assertSizeEventually(0, keys);
    multiMap.removeEntryListener(id2);
    multiMap.addEntryListener(listener, "key7", true);
    getMultiMap(instances, name).put("key2", "val2");
    getMultiMap(instances, name).put("key3", "val3");
    getMultiMap(instances, name).put("key7", "val7");
    assertSizeEventually(1, keys);
}
Also used : MultiMapConfig(com.hazelcast.config.MultiMapConfig) EntryListenerConfig(com.hazelcast.config.EntryListenerConfig) Config(com.hazelcast.config.Config) EntryAdapter(com.hazelcast.core.EntryAdapter) MapEvent(com.hazelcast.core.MapEvent) HazelcastInstance(com.hazelcast.core.HazelcastInstance) EntryEvent(com.hazelcast.core.EntryEvent) AssertTask(com.hazelcast.test.AssertTask) TestHazelcastInstanceFactory(com.hazelcast.test.TestHazelcastInstanceFactory) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test) ParallelTest(com.hazelcast.test.annotation.ParallelTest)

Example 10 with EntryAdapter

use of com.hazelcast.core.EntryAdapter in project hazelcast by hazelcast.

the class RestTest method testMapTtl.

// issue #1783
@Test
public void testMapTtl() throws Exception {
    final CountDownLatch latch = new CountDownLatch(1);
    EntryListenerConfig listenerConfig = new EntryListenerConfig().setImplementation(new EntryAdapter() {

        @Override
        public void entryEvicted(EntryEvent event) {
            latch.countDown();
        }
    });
    String name = randomMapName();
    config.getMapConfig(name).setTimeToLiveSeconds(3).addEntryListenerConfig(listenerConfig);
    String key = "key";
    communicator.mapPut(name, key, "value");
    assertOpenEventually(latch);
    String value = communicator.mapGet(name, key);
    assertTrue(value.isEmpty());
}
Also used : EntryAdapter(com.hazelcast.core.EntryAdapter) EntryEvent(com.hazelcast.core.EntryEvent) CountDownLatch(java.util.concurrent.CountDownLatch) EntryListenerConfig(com.hazelcast.config.EntryListenerConfig) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Aggregations

EntryAdapter (com.hazelcast.core.EntryAdapter)26 Test (org.junit.Test)23 EntryEvent (com.hazelcast.core.EntryEvent)21 QuickTest (com.hazelcast.test.annotation.QuickTest)21 ParallelTest (com.hazelcast.test.annotation.ParallelTest)20 HazelcastInstance (com.hazelcast.core.HazelcastInstance)14 CountDownLatch (java.util.concurrent.CountDownLatch)13 Config (com.hazelcast.config.Config)12 AssertTask (com.hazelcast.test.AssertTask)8 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)8 EntryListenerConfig (com.hazelcast.config.EntryListenerConfig)6 TestHazelcastInstanceFactory (com.hazelcast.test.TestHazelcastInstanceFactory)6 NightlyTest (com.hazelcast.test.annotation.NightlyTest)6 MapConfig (com.hazelcast.config.MapConfig)5 IMap (com.hazelcast.core.IMap)4 MapEvent (com.hazelcast.core.MapEvent)4 SqlPredicate (com.hazelcast.query.SqlPredicate)4 MaxSizeConfig (com.hazelcast.config.MaxSizeConfig)3 ArrayList (java.util.ArrayList)3 MultiMapConfig (com.hazelcast.config.MultiMapConfig)2