Search in sources :

Example 16 with EntryEvent

use of com.hazelcast.core.EntryEvent in project wildfly-camel by wildfly-extras.

the class HazelcastMapConsumerIntegrationTest method testRemove.

@Test
@SuppressWarnings("unchecked")
public void testRemove() throws Exception {
    CamelContext camelctx = createCamelContext();
    camelctx.start();
    try {
        MockEndpoint mock = camelctx.getEndpoint("mock:removed", MockEndpoint.class);
        mock.expectedMessageCount(1);
        EntryEvent<Object, Object> event = new EntryEvent<Object, Object>("foo", null, EntryEventType.REMOVED.getType(), "4711", "my-foo");
        argument.getValue().entryRemoved(event);
        mock.assertIsSatisfied(3000);
        checkHeaders(mock.getExchanges().get(0).getIn().getHeaders(), HazelcastConstants.REMOVED);
    } finally {
        camelctx.stop();
    }
}
Also used : DefaultCamelContext(org.apache.camel.impl.DefaultCamelContext) CamelContext(org.apache.camel.CamelContext) MockEndpoint(org.apache.camel.component.mock.MockEndpoint) EntryEvent(com.hazelcast.core.EntryEvent) Test(org.junit.Test)

Example 17 with EntryEvent

use of com.hazelcast.core.EntryEvent 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)).toCompletableFuture().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)))).toCompletableFuture().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, () -> {
        MapProxyImpl<Object, Object> map = (MapProxyImpl<Object, Object>) hazelcast.getMap("myMap");
        map.putIfAbsentAsync(random.nextInt(SIZE), new Customer(random.nextInt(100), String.valueOf(random.nextInt(10000))), 10, TimeUnit.MILLISECONDS);
    }, 5);
    addOperation(operations, () -> {
        MapProxyImpl<Object, Object> map = (MapProxyImpl<Object, Object>) hazelcast.getMap("myMap");
        map.putIfAbsentAsync(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(Predicates.sql("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(Predicates.sql("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(Predicates.sql("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(Predicates.sql("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();
                }
            };
            UUID 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(IndexType.SORTED, "year");
        }
    }, 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();
                }
            };
            UUID 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) CountDownLatch(java.util.concurrent.CountDownLatch) EntryListener(com.hazelcast.core.EntryListener) IMap(com.hazelcast.map.IMap) MapProxyImpl(com.hazelcast.map.impl.proxy.MapProxyImpl) EntryEvent(com.hazelcast.core.EntryEvent) Iterator(java.util.Iterator) ExecutionException(java.util.concurrent.ExecutionException) UUID(java.util.UUID) HashMap(java.util.HashMap) Map(java.util.Map) IMap(com.hazelcast.map.IMap)

Example 18 with EntryEvent

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

the class BasicMapTest method testMapListenersWithValue.

@Test
public void testMapListenersWithValue() {
    IMap<Object, Object> map = getInstance().getMap("testMapListenersWithValue");
    final Object[] addedKey = new Object[1];
    final Object[] addedValue = new Object[1];
    final Object[] updatedKey = new Object[1];
    final Object[] oldValue = new Object[1];
    final Object[] newValue = new Object[1];
    final Object[] removedKey = new Object[1];
    final Object[] removedValue = new Object[1];
    EntryListener<Object, Object> listener = new EntryAdapter<Object, Object>() {

        @Override
        public void entryAdded(EntryEvent<Object, Object> event) {
            addedKey[0] = event.getKey();
            addedValue[0] = event.getValue();
        }

        @Override
        public void entryRemoved(EntryEvent<Object, Object> event) {
            removedKey[0] = event.getKey();
            removedValue[0] = event.getOldValue();
        }

        @Override
        public void entryUpdated(EntryEvent<Object, Object> event) {
            updatedKey[0] = event.getKey();
            oldValue[0] = event.getOldValue();
            newValue[0] = event.getValue();
        }

        public void entryEvicted(EntryEvent<Object, Object> event) {
        }

        @Override
        public void mapEvicted(MapEvent event) {
        }

        @Override
        public void mapCleared(MapEvent event) {
        }
    };
    map.addEntryListener(listener, true);
    map.put("key", "value");
    map.put("key", "value2");
    map.remove("key");
    assertTrueEventually(new AssertTask() {

        @Override
        public void run() {
            assertEquals(addedKey[0], "key");
            assertEquals(addedValue[0], "value");
            assertEquals(updatedKey[0], "key");
            assertEquals(oldValue[0], "value");
            assertEquals(newValue[0], "value2");
            assertEquals(removedKey[0], "key");
            assertEquals(removedValue[0], "value2");
        }
    });
}
Also used : EntryAdapter(com.hazelcast.core.EntryAdapter) EntryEvent(com.hazelcast.core.EntryEvent) AssertTask(com.hazelcast.test.AssertTask) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test) SlowTest(com.hazelcast.test.annotation.SlowTest)

Example 19 with EntryEvent

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

the class EntryLoadedListenerTest method load_listener_notified_when_getAll_loads_from_map_loader.

@Test
public void load_listener_notified_when_getAll_loads_from_map_loader() {
    final Queue<EntryEvent> entryEvents = new ConcurrentLinkedQueue<EntryEvent>();
    IMap<Integer, Integer> map = node.getMap("noInitialLoading_test_getAll");
    map.addEntryListener(new EntryLoadedListener<Integer, Integer>() {

        @Override
        public void entryLoaded(EntryEvent<Integer, Integer> event) {
            entryEvents.add(event);
        }
    }, true);
    final List<Integer> keyList = Arrays.asList(1, 2, 3, 4, 5);
    map.getAll(new HashSet<Integer>(keyList));
    assertTrueEventually(new AssertTask() {

        @Override
        public void run() {
            assertEquals(keyList.size(), entryEvents.size());
            for (EntryEvent entryEvent : entryEvents) {
                assertEquals(LOADED, entryEvent.getEventType());
            }
        }
    });
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) EntryEvent(com.hazelcast.core.EntryEvent) AssertTask(com.hazelcast.test.AssertTask) ConcurrentLinkedQueue(java.util.concurrent.ConcurrentLinkedQueue) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Example 20 with EntryEvent

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

the class ServerQueryCacheRecreationTest method listeners_still_works_after_query_cache_recreation.

@Test
public void listeners_still_works_after_query_cache_recreation() {
    IMap<Object, Object> map = serverWithQueryCache.getMap(mapName);
    QueryCache<Object, Object> queryCache = map.getQueryCache(queryCacheName, Predicates.alwaysTrue(), true);
    final AtomicInteger entryAddedCounter = new AtomicInteger();
    queryCache.addEntryListener(new EntryAdapter() {

        @Override
        public void entryAdded(EntryEvent event) {
            entryAddedCounter.incrementAndGet();
        }
    }, Predicates.sql("__key >= 10"), true);
    // Restart server
    server.shutdown();
    server = factory.newHazelcastInstance(getConfig());
    // Recreate query cache on same reference
    // Recreation empties query cache.
    ((InternalQueryCache) queryCache).recreate();
    for (int i = 0; i < 100; i++) {
        map.put(i, i);
    }
    AssertTask assertTask = () -> assertEquals(90, entryAddedCounter.get());
    assertTrueEventually(assertTask);
    assertTrueAllTheTime(assertTask, 3);
}
Also used : InternalQueryCache(com.hazelcast.map.impl.querycache.subscriber.InternalQueryCache) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) EntryAdapter(com.hazelcast.core.EntryAdapter) EntryEvent(com.hazelcast.core.EntryEvent) AssertTask(com.hazelcast.test.AssertTask) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Aggregations

EntryEvent (com.hazelcast.core.EntryEvent)71 Test (org.junit.Test)62 QuickTest (com.hazelcast.test.annotation.QuickTest)47 ParallelJVMTest (com.hazelcast.test.annotation.ParallelJVMTest)39 EntryAdapter (com.hazelcast.core.EntryAdapter)22 HazelcastInstance (com.hazelcast.core.HazelcastInstance)22 Config (com.hazelcast.config.Config)19 CountDownLatch (java.util.concurrent.CountDownLatch)19 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)18 AssertTask (com.hazelcast.test.AssertTask)15 MockEndpoint (org.apache.camel.component.mock.MockEndpoint)15 MapListener (com.hazelcast.map.listener.MapListener)11 MapListenerAdapter (com.hazelcast.map.impl.MapListenerAdapter)10 MapConfig (com.hazelcast.config.MapConfig)9 EntryListenerConfig (com.hazelcast.config.EntryListenerConfig)8 Predicate (com.hazelcast.query.Predicate)8 TestHazelcastInstanceFactory (com.hazelcast.test.TestHazelcastInstanceFactory)8 ParallelTest (com.hazelcast.test.annotation.ParallelTest)8 EntryAddedListener (com.hazelcast.map.listener.EntryAddedListener)7 QueryCacheConfig (com.hazelcast.config.QueryCacheConfig)6