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]);
}
});
}
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);
}
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);
}
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());
}
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));
}
Aggregations