use of javax.cache.configuration.MutableCacheEntryListenerConfiguration in project ignite by apache.
the class CacheContinuousQueryFactoryFilterRandomOperationTest method doTestContinuousQuery.
/**
* {@inheritDoc}
*/
@Override
protected void doTestContinuousQuery(CacheConfiguration<Object, Object> ccfg, ContinuousDeploy deploy) throws Exception {
ignite(0).createCache(ccfg);
try {
long seed = System.currentTimeMillis();
Random rnd = new Random(seed);
log.info("Random seed: " + seed);
List<BlockingQueue<CacheEntryEvent<?, ?>>> evtsQueues = new ArrayList<>();
Collection<QueryCursor<?>> curs = new ArrayList<>();
Collection<T2<Integer, MutableCacheEntryListenerConfiguration>> lsnrCfgs = new ArrayList<>();
if (deploy == CLIENT)
evtsQueues.add(registerListener(ccfg.getName(), NODES - 1, curs, lsnrCfgs, rnd.nextBoolean()));
else if (deploy == SERVER)
evtsQueues.add(registerListener(ccfg.getName(), rnd.nextInt(NODES - 1), curs, lsnrCfgs, rnd.nextBoolean()));
else {
boolean isSync = rnd.nextBoolean();
for (int i = 0; i < NODES - 1; i++) evtsQueues.add(registerListener(ccfg.getName(), i, curs, lsnrCfgs, isSync));
}
ConcurrentMap<Object, Object> expData = new ConcurrentHashMap<>();
Map<Integer, Long> partCntr = new ConcurrentHashMap<>();
try {
for (int i = 0; i < ITERATION_CNT; i++) {
if (i % 10 == 0)
log.info("Iteration: " + i);
for (int idx = 0; idx < NODES; idx++) randomUpdate(rnd, evtsQueues, expData, partCntr, grid(idx).cache(ccfg.getName()));
}
} finally {
for (QueryCursor<?> cur : curs) cur.close();
for (T2<Integer, MutableCacheEntryListenerConfiguration> e : lsnrCfgs) grid(e.get1()).cache(ccfg.getName()).deregisterCacheEntryListener(e.get2());
}
} finally {
ignite(0).destroyCache(ccfg.getName());
}
}
use of javax.cache.configuration.MutableCacheEntryListenerConfiguration in project ignite by apache.
the class CacheContinuousQueryOperationP2PTest method testContinuousQuery.
/**
* @param ccfg Cache configuration.
* @param isClient Client.
* @throws Exception If failed.
*/
protected void testContinuousQuery(CacheConfiguration<Object, Object> ccfg, boolean isClient) throws Exception {
ignite(0).createCache(ccfg);
ThreadLocalRandom rnd = ThreadLocalRandom.current();
QueryCursor<?> cur = null;
final Class<Factory<CacheEntryEventFilter>> evtFilterFactory = (Class<Factory<CacheEntryEventFilter>>) getExternalClassLoader().loadClass("org.apache.ignite.tests.p2p.CacheDeploymentEntryEventFilterFactory");
final CountDownLatch latch = new CountDownLatch(10);
ContinuousQuery<Integer, Integer> qry = new ContinuousQuery<>();
TestLocalListener localLsnr = new TestLocalListener() {
@Override
public void onEvent(Iterable<CacheEntryEvent<? extends Integer, ? extends Integer>> evts) throws CacheEntryListenerException {
for (CacheEntryEvent<? extends Integer, ? extends Integer> evt : evts) {
latch.countDown();
log.info("Received event: " + evt);
}
}
};
MutableCacheEntryListenerConfiguration<Integer, Integer> lsnrCfg = new MutableCacheEntryListenerConfiguration<>(new FactoryBuilder.SingletonFactory<>(localLsnr), (Factory<? extends CacheEntryEventFilter<? super Integer, ? super Integer>>) (Object) evtFilterFactory.newInstance(), true, true);
qry.setLocalListener(localLsnr);
qry.setRemoteFilterFactory((Factory<? extends CacheEntryEventFilter<Integer, Integer>>) (Object) evtFilterFactory.newInstance());
IgniteCache<Integer, Integer> cache = null;
try {
if (isClient)
cache = grid(NODES - 1).cache(ccfg.getName());
else
cache = grid(rnd.nextInt(NODES - 1)).cache(ccfg.getName());
cur = cache.query(qry);
cache.registerCacheEntryListener(lsnrCfg);
for (int i = 0; i < 10; i++) cache.put(i, i);
assertTrue(latch.await(3, TimeUnit.SECONDS));
} finally {
if (cur != null)
cur.close();
if (cache != null)
cache.deregisterCacheEntryListener(lsnrCfg);
}
}
use of javax.cache.configuration.MutableCacheEntryListenerConfiguration 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.configuration.MutableCacheEntryListenerConfiguration 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.configuration.MutableCacheEntryListenerConfiguration in project hazelcast by hazelcast.
the class CacheBasicAbstractTest method testRemovingSameEntryTwiceShouldTriggerEntryListenerOnlyOnce.
@Test
public void testRemovingSameEntryTwiceShouldTriggerEntryListenerOnlyOnce() {
String cacheName = randomString();
CacheConfig<Integer, String> config = createCacheConfig();
final CacheFromDifferentNodesTest.SimpleEntryListener<Integer, String> listener = new CacheFromDifferentNodesTest.SimpleEntryListener<Integer, String>();
MutableCacheEntryListenerConfiguration<Integer, String> listenerConfiguration = new MutableCacheEntryListenerConfiguration<Integer, String>(FactoryBuilder.factoryOf(listener), null, true, true);
config.addCacheEntryListenerConfiguration(listenerConfiguration);
Cache<Integer, String> cache = cacheManager.createCache(cacheName, config);
assertNotNull(cache);
Integer key = 1;
String value = "value";
cache.put(key, value);
assertTrueEventually(new AssertTask() {
@Override
public void run() throws Exception {
assertEquals(1, listener.created.get());
}
});
cache.removeAll();
cache.removeAll();
assertTrueEventually(new AssertTask() {
@Override
public void run() throws Exception {
assertEquals(1, listener.removed.get());
}
});
}
Aggregations