use of com.hazelcast.core.EntryEvent in project Openfire by igniterealtime.
the class ClusterListener method simulateCacheInserts.
private void simulateCacheInserts(Cache cache) {
EntryListener EntryListener = EntryListeners.get(cache);
if (EntryListener != null) {
if (cache instanceof CacheWrapper) {
Cache wrapped = ((CacheWrapper) cache).getWrappedCache();
if (wrapped instanceof ClusteredCache) {
ClusteredCache clusteredCache = (ClusteredCache) wrapped;
for (Map.Entry entry : (Set<Map.Entry>) cache.entrySet()) {
EntryEvent event = new EntryEvent(clusteredCache.map.getName(), cluster.getLocalMember(), EntryEventType.ADDED.getType(), entry.getKey(), null, entry.getValue());
EntryListener.entryAdded(event);
}
}
}
}
}
use of com.hazelcast.core.EntryEvent 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);
}
use of com.hazelcast.core.EntryEvent in project hazelcast by hazelcast.
the class ClientEvictionTest method testMaxSizeEvictionWorks.
@Test
public void testMaxSizeEvictionWorks() throws Exception {
final int maxSize = 1000;
final int populationCount = 5000;
String mapName = randomString();
String cacheName = randomString();
QueryCacheConfig cacheConfig = new QueryCacheConfig(cacheName);
cacheConfig.getEvictionConfig().setSize(maxSize).setEvictionPolicy(EvictionPolicy.LFU).setMaximumSizePolicy(EvictionConfig.MaxSizePolicy.ENTRY_COUNT);
ClientConfig clientConfig = new ClientConfig();
clientConfig.addQueryCacheConfig(mapName, cacheConfig);
HazelcastInstance client = factory.newHazelcastClient(clientConfig);
IMap<Integer, Integer> map = client.getMap(mapName);
// expecting at least populationCount - maxSize + 10 evicted entries according to max size.
// 10 states an error margin since eviction does not sweep precise number of entries.
int margin = 10;
final CountDownLatch evictedCount = new CountDownLatch(populationCount - maxSize - margin);
final QueryCache<Integer, Integer> cache = map.getQueryCache(cacheName, TruePredicate.INSTANCE, true);
String listener = cache.addEntryListener(new EntryEvictedListener() {
@Override
public void entryEvicted(EntryEvent event) {
evictedCount.countDown();
}
}, false);
for (int i = 0; i < populationCount; i++) {
map.put(i, i);
}
assertOpenEventually(evictedCount);
assertQueryCacheEvicted(maxSize, margin, cache);
assertTrue(cache.removeEntryListener(listener));
}
use of com.hazelcast.core.EntryEvent in project hazelcast by hazelcast.
the class ExpirationManagerTest method stops_running_when_clusterState_turns_passive.
@Test
public void stops_running_when_clusterState_turns_passive() throws Exception {
Config config = new Config();
config.setProperty(SYS_PROP_EXPIRATION_TASK_PERIOD_SECONDS, "1");
HazelcastInstance node = createHazelcastInstance(config);
final AtomicInteger expirationCounter = new AtomicInteger();
IMap map = node.getMap("test");
map.addEntryListener(new EntryExpiredListener() {
@Override
public void entryExpired(EntryEvent event) {
expirationCounter.incrementAndGet();
}
}, true);
map.put(1, 1, 3, TimeUnit.SECONDS);
node.getCluster().changeClusterState(PASSIVE);
// wait a little to see if any expiration is occurring
sleepSeconds(3);
int expirationCount = expirationCounter.get();
assertEquals(format("Expecting no expiration but found:%d", expirationCount), 0, expirationCount);
}
use of com.hazelcast.core.EntryEvent in project hazelcast by hazelcast.
the class ExpirationManagerTest method starts_running_when_clusterState_turns_active.
@Test
public void starts_running_when_clusterState_turns_active() throws Exception {
Config config = new Config();
config.setProperty(SYS_PROP_EXPIRATION_TASK_PERIOD_SECONDS, "1");
HazelcastInstance node = createHazelcastInstance(config);
final AtomicInteger expirationCounter = new AtomicInteger();
IMap map = node.getMap("test");
map.addEntryListener(new EntryExpiredListener() {
@Override
public void entryExpired(EntryEvent event) {
expirationCounter.incrementAndGet();
}
}, true);
map.put(1, 1, 3, SECONDS);
node.getCluster().changeClusterState(PASSIVE);
node.getCluster().changeClusterState(ACTIVE);
assertTrueEventually(new AssertTask() {
@Override
public void run() throws Exception {
int expirationCount = expirationCounter.get();
assertEquals(format("Expecting 1 expiration but found:%d", expirationCount), 1, expirationCount);
}
});
}
Aggregations