use of javax.cache.event.CacheEntryListener in project hazelcast by hazelcast.
the class AbstractCacheService method registerListenerInternal.
protected String registerListenerInternal(String name, CacheEventListener listener, EventFilter eventFilter, boolean isLocal) {
EventService eventService = getNodeEngine().getEventService();
EventRegistration reg;
if (isLocal) {
if (eventFilter == null) {
reg = eventService.registerLocalListener(AbstractCacheService.SERVICE_NAME, name, listener);
} else {
reg = eventService.registerLocalListener(AbstractCacheService.SERVICE_NAME, name, eventFilter, listener);
}
} else {
if (eventFilter == null) {
reg = eventService.registerListener(AbstractCacheService.SERVICE_NAME, name, listener);
} else {
reg = eventService.registerListener(AbstractCacheService.SERVICE_NAME, name, eventFilter, listener);
}
}
String id = reg.getId();
if (listener instanceof Closeable) {
closeableListeners.put(id, (Closeable) listener);
} else if (listener instanceof CacheEntryListenerProvider) {
CacheEntryListener cacheEntryListener = ((CacheEntryListenerProvider) listener).getCacheEntryListener();
if (cacheEntryListener instanceof Closeable) {
closeableListeners.put(id, (Closeable) cacheEntryListener);
}
}
return id;
}
use of javax.cache.event.CacheEntryListener in project ignite by apache.
the class IgniteCacheEntryListenerAbstractTest method testSynchronousEvents.
/**
* @throws Exception If failed.
*/
public void testSynchronousEvents() throws Exception {
final CacheEntryCreatedListener<Object, Object> lsnr = new CreateUpdateRemoveExpireListener() {
@Override
public void onRemoved(Iterable<CacheEntryEvent<?, ?>> evts) {
super.onRemoved(evts);
awaitLatch();
}
@Override
public void onCreated(Iterable<CacheEntryEvent<?, ?>> evts) {
super.onCreated(evts);
awaitLatch();
}
@Override
public void onUpdated(Iterable<CacheEntryEvent<?, ?>> evts) {
super.onUpdated(evts);
awaitLatch();
}
private void awaitLatch() {
try {
assertTrue(syncEvtLatch.await(5000, MILLISECONDS));
} catch (InterruptedException e) {
fail("Unexpected exception: " + e);
}
}
};
CacheEntryListenerConfiguration<Object, Object> lsnrCfg = new MutableCacheEntryListenerConfiguration<>(new Factory<CacheEntryListener<Object, Object>>() {
@Override
public CacheEntryListener<Object, Object> create() {
return lsnr;
}
}, null, true, true);
IgniteCache<Object, Object> cache = jcache();
cache.registerCacheEntryListener(lsnrCfg);
try {
for (Integer key : keys()) {
log.info("Check synchronous create event [key=" + key + ']');
syncEvent(key, 1, cache, 1);
checkEvent(evts.iterator(), key, CREATED, 1, null);
log.info("Check synchronous update event [key=" + key + ']');
syncEvent(key, 2, cache, 1);
checkEvent(evts.iterator(), key, UPDATED, 2, 1);
log.info("Check synchronous remove event [key=" + key + ']');
syncEvent(key, null, cache, 1);
checkEvent(evts.iterator(), key, REMOVED, null, 2);
log.info("Check synchronous expire event [key=" + key + ']');
syncEvent(key, 3, cache.withExpiryPolicy(new ModifiedExpiryPolicy(new Duration(MILLISECONDS, 1000))), eagerTtl() ? 2 : 1);
checkEvent(evts.iterator(), key, CREATED, 3, null);
if (!eagerTtl()) {
U.sleep(1100);
assertNull(primaryCache(key(key), cache.getName()).get(key(key)));
evtsLatch.await(5000, MILLISECONDS);
assertEquals(1, evts.size());
}
checkEvent(evts.iterator(), key, EXPIRED, null, 3);
assertEquals(0, evts.size());
}
} finally {
cache.deregisterCacheEntryListener(lsnrCfg);
}
}
use of javax.cache.event.CacheEntryListener in project ignite by apache.
the class IgniteCacheEntryListenerAbstractTest method testConcurrentRegisterDeregister.
/**
* @throws Exception If failed.
*/
public void testConcurrentRegisterDeregister() throws Exception {
final int THREADS = 10;
final CyclicBarrier barrier = new CyclicBarrier(THREADS);
final IgniteCache<Object, Object> cache = jcache(0);
GridTestUtils.runMultiThreadedAsync(new Callable<Void>() {
@Override
public Void call() throws Exception {
CacheEntryListenerConfiguration<Object, Object> cfg = new MutableCacheEntryListenerConfiguration<>(new Factory<CacheEntryListener<Object, Object>>() {
@Override
public CacheEntryListener<Object, Object> create() {
return new CreateUpdateRemoveExpireListener();
}
}, null, true, false);
barrier.await();
for (int i = 0; i < 100; i++) {
cache.registerCacheEntryListener(cfg);
cache.deregisterCacheEntryListener(cfg);
}
return null;
}
}, THREADS, "register-thread").get();
}
use of javax.cache.event.CacheEntryListener in project ignite by apache.
the class GridCacheReplicatedPreloadSelfTest method testExternalClassesAtConfiguration.
/**
* @throws Exception If test failed.
*/
public void testExternalClassesAtConfiguration() throws Exception {
try {
extClassloadingAtCfg = true;
useExtClassLoader = true;
Ignite g1 = startGrid(1);
// Checks deserialization at node join.
Ignite g2 = startGrid(2);
isClient = true;
Ignite g3 = startGrid(3);
IgniteCache<Integer, Object> cache1 = g1.cache(DEFAULT_CACHE_NAME);
IgniteCache<Integer, Object> cache2 = g2.cache(DEFAULT_CACHE_NAME);
IgniteCache<Integer, Object> cache3 = g3.cache(DEFAULT_CACHE_NAME);
final Class<CacheEntryListener> cls1 = (Class<CacheEntryListener>) getExternalClassLoader().loadClass("org.apache.ignite.tests.p2p.CacheDeploymentCacheEntryListener");
final Class<CacheEntryEventSerializableFilter> cls2 = (Class<CacheEntryEventSerializableFilter>) getExternalClassLoader().loadClass("org.apache.ignite.tests.p2p.CacheDeploymentCacheEntryEventSerializableFilter");
CacheEntryListenerConfiguration<Integer, Object> lsnrCfg = new MutableCacheEntryListenerConfiguration<>(new Factory<CacheEntryListener<Integer, Object>>() {
@Override
public CacheEntryListener<Integer, Object> create() {
try {
return cls1.newInstance();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}, new ClassFilterFactory(cls2), true, true);
cache1.registerCacheEntryListener(lsnrCfg);
cache1.put(1, 1);
assertEquals(1, cache2.get(1));
assertEquals(1, cache3.get(1));
} finally {
extClassloadingAtCfg = false;
isClient = false;
useExtClassLoader = false;
}
}
Aggregations