Search in sources :

Example 16 with EntryAdapter

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

the class BasicMapTest method testMapQueryListener.

@Test
public void testMapQueryListener() {
    IMap<Object, Object> map = getInstance().getMap(randomMapName());
    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();
        }

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

        @Override
        public void mapEvicted(MapEvent event) {
        }

        @Override
        public void mapCleared(MapEvent event) {
        }
    };
    map.addEntryListener(listener, new StartsWithPredicate("a"), null, true);
    map.put("key1", "abc");
    map.put("key2", "bcd");
    map.put("key2", "axyz");
    map.remove("key1");
    assertTrueEventually(new AssertTask() {

        @Override
        public void run() {
            assertEquals("key1", addedKey[0]);
            assertEquals("abc", addedValue[0]);
            assertEquals("key2", updatedKey[0]);
            assertEquals("bcd", oldValue[0]);
            assertEquals("axyz", newValue[0]);
            assertEquals("key1", removedKey[0]);
            assertEquals("abc", removedValue[0]);
        }
    });
}
Also used : EntryAdapter(com.hazelcast.core.EntryAdapter) EntryEvent(com.hazelcast.core.EntryEvent) MapEvent(com.hazelcast.core.MapEvent) AssertTask(com.hazelcast.test.AssertTask) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test) ParallelTest(com.hazelcast.test.annotation.ParallelTest)

Example 17 with EntryAdapter

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

the class EvictionTest method testMapRecordEviction.

@Test
public void testMapRecordEviction() throws InterruptedException {
    final String mapName = randomMapName();
    final int size = 100;
    final AtomicInteger entryEvictedEventCount = new AtomicInteger(0);
    Config config = getConfig();
    MapConfig mapConfig = config.getMapConfig(mapName);
    mapConfig.setTimeToLiveSeconds(1);
    mapConfig.addEntryListenerConfig(new EntryListenerConfig().setImplementation(new EntryAdapter() {

        public void entryEvicted(EntryEvent event) {
            entryEvictedEventCount.incrementAndGet();
        }
    }).setLocal(true));
    HazelcastInstance instance = createHazelcastInstance(config);
    final IMap<Integer, Integer> map = instance.getMap(mapName);
    for (int i = 0; i < size; i++) {
        map.put(i, i);
    }
    //wait until eviction is complete
    assertSizeEventually(0, map, 300);
    assertTrueEventually(new AssertTask() {

        @Override
        public void run() throws Exception {
            assertEquals(size, entryEvictedEventCount.get());
        }
    }, 300);
}
Also used : MaxSizeConfig(com.hazelcast.config.MaxSizeConfig) MapConfig(com.hazelcast.config.MapConfig) EntryListenerConfig(com.hazelcast.config.EntryListenerConfig) Config(com.hazelcast.config.Config) NearCacheConfig(com.hazelcast.config.NearCacheConfig) EntryAdapter(com.hazelcast.core.EntryAdapter) EntryListenerConfig(com.hazelcast.config.EntryListenerConfig) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) HazelcastInstance(com.hazelcast.core.HazelcastInstance) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) EntryEvent(com.hazelcast.core.EntryEvent) AssertTask(com.hazelcast.test.AssertTask) MapConfig(com.hazelcast.config.MapConfig) QuickTest(com.hazelcast.test.annotation.QuickTest) NightlyTest(com.hazelcast.test.annotation.NightlyTest) Test(org.junit.Test) ParallelTest(com.hazelcast.test.annotation.ParallelTest)

Example 18 with EntryAdapter

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

the class EvictionTest method testMapRecordIdleEvictionOnMigration.

@Test
@Category(NightlyTest.class)
public void testMapRecordIdleEvictionOnMigration() {
    final String name = "testMapRecordIdleEvictionOnMigration";
    Config cfg = getConfig();
    cfg.setProperty(GroupProperty.PARTITION_COUNT.getName(), "1");
    MapConfig mapConfig = cfg.getMapConfig(name);
    int maxIdleSeconds = 30;
    int size = 100;
    final int nsize = size / 5;
    mapConfig.setMaxIdleSeconds(maxIdleSeconds);
    TestHazelcastInstanceFactory factory = createHazelcastInstanceFactory(3);
    HazelcastInstance instance1 = factory.newHazelcastInstance(cfg);
    final IMap<Integer, Integer> map = instance1.getMap(name);
    final CountDownLatch latch = new CountDownLatch(size - nsize);
    map.addEntryListener(new EntryAdapter() {

        public void entryEvicted(EntryEvent event) {
            latch.countDown();
        }
    }, false);
    // put sample data
    for (int i = 0; i < size; i++) {
        map.put(i, i);
    }
    // wait until some time that is close to eviction
    sleepSeconds(maxIdleSeconds - 5);
    // touch the ones you dont want to be evicted.
    for (int i = 0; i < nsize; i++) {
        map.get(i);
    }
    factory.newHazelcastInstance(cfg);
    factory.newHazelcastInstance(cfg);
    //wait until eviction is complete
    assertOpenEventually(latch, 240);
    assertSizeEventually(nsize, map);
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) HazelcastInstance(com.hazelcast.core.HazelcastInstance) MaxSizeConfig(com.hazelcast.config.MaxSizeConfig) MapConfig(com.hazelcast.config.MapConfig) EntryListenerConfig(com.hazelcast.config.EntryListenerConfig) Config(com.hazelcast.config.Config) NearCacheConfig(com.hazelcast.config.NearCacheConfig) EntryAdapter(com.hazelcast.core.EntryAdapter) EntryEvent(com.hazelcast.core.EntryEvent) MapConfig(com.hazelcast.config.MapConfig) CountDownLatch(java.util.concurrent.CountDownLatch) TestHazelcastInstanceFactory(com.hazelcast.test.TestHazelcastInstanceFactory) Category(org.junit.experimental.categories.Category) QuickTest(com.hazelcast.test.annotation.QuickTest) NightlyTest(com.hazelcast.test.annotation.NightlyTest) Test(org.junit.Test) ParallelTest(com.hazelcast.test.annotation.ParallelTest)

Example 19 with EntryAdapter

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

the class QueryAdvancedTest method testQueryWithTTL.

@Test
@SuppressWarnings("deprecation")
public void testQueryWithTTL() throws Exception {
    Config config = getConfig();
    String mapName = "default";
    config.getMapConfig(mapName).setTimeToLiveSeconds(5);
    HazelcastInstance instance = createHazelcastInstance(config);
    IMap<String, Employee> map = instance.getMap(mapName);
    map.addIndex("name", false);
    map.addIndex("age", false);
    map.addIndex("active", true);
    int passiveEmployees = 5;
    int activeEmployees = 5;
    int allEmployees = passiveEmployees + activeEmployees;
    final CountDownLatch latch = new CountDownLatch(allEmployees);
    map.addEntryListener(new EntryAdapter() {

        @Override
        public void entryEvicted(EntryEvent event) {
            latch.countDown();
        }
    }, false);
    for (int i = 0; i < activeEmployees; i++) {
        Employee employee = new Employee("activeEmployee" + i, 60, true, i);
        map.put("activeEmployee" + i, employee);
    }
    for (int i = 0; i < passiveEmployees; i++) {
        Employee employee = new Employee("passiveEmployee" + i, 60, false, i);
        map.put("passiveEmployee" + i, employee);
    }
    // check the query result before eviction
    Collection values = map.values(new SqlPredicate("active"));
    assertEquals(activeEmployees, values.size());
    // wait until eviction is completed
    assertOpenEventually(latch);
    // check the query result after eviction
    values = map.values(new SqlPredicate("active"));
    assertEquals(0, values.size());
}
Also used : HazelcastInstance(com.hazelcast.core.HazelcastInstance) Employee(com.hazelcast.query.SampleObjects.Employee) PortableEmployee(com.hazelcast.query.SampleObjects.PortableEmployee) Config(com.hazelcast.config.Config) MapIndexConfig(com.hazelcast.config.MapIndexConfig) MapStoreConfig(com.hazelcast.config.MapStoreConfig) EntryAdapter(com.hazelcast.core.EntryAdapter) EntryEvent(com.hazelcast.core.EntryEvent) Collection(java.util.Collection) SqlPredicate(com.hazelcast.query.SqlPredicate) CountDownLatch(java.util.concurrent.CountDownLatch) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test) ParallelTest(com.hazelcast.test.annotation.ParallelTest)

Example 20 with EntryAdapter

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

the class ListenerTest method testConfigListenerRegistration.

@Test
public void testConfigListenerRegistration() throws InterruptedException {
    final CountDownLatch latch = new CountDownLatch(1);
    String name = randomString();
    Config config = getConfig();
    MapConfig mapConfig = config.getMapConfig(name);
    EntryListenerConfig entryListenerConfig = new EntryListenerConfig();
    entryListenerConfig.setImplementation(new EntryAdapter() {

        public void entryAdded(EntryEvent event) {
            latch.countDown();
        }
    });
    mapConfig.addEntryListenerConfig(entryListenerConfig);
    HazelcastInstance instance = createHazelcastInstance(config);
    IMap<Object, Object> map = instance.getMap(name);
    map.put(1, 1);
    assertTrue(latch.await(10, TimeUnit.SECONDS));
}
Also used : HazelcastInstance(com.hazelcast.core.HazelcastInstance) MapConfig(com.hazelcast.config.MapConfig) EntryListenerConfig(com.hazelcast.config.EntryListenerConfig) Config(com.hazelcast.config.Config) MapPartitionLostListenerConfig(com.hazelcast.config.MapPartitionLostListenerConfig) EntryAdapter(com.hazelcast.core.EntryAdapter) EntryEvent(com.hazelcast.core.EntryEvent) MapConfig(com.hazelcast.config.MapConfig) CountDownLatch(java.util.concurrent.CountDownLatch) EntryListenerConfig(com.hazelcast.config.EntryListenerConfig) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test) ParallelTest(com.hazelcast.test.annotation.ParallelTest)

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