use of javax.cache.event.EventType in project hazelcast by hazelcast.
the class CacheEventListenerAdaptor method createCacheEntryEvent.
private Iterable<CacheEntryEvent<? extends K, ? extends V>> createCacheEntryEvent(Collection<CacheEventData> keys) {
HashSet<CacheEntryEvent<? extends K, ? extends V>> evt = new HashSet<CacheEntryEvent<? extends K, ? extends V>>();
for (CacheEventData cacheEventData : keys) {
EventType eventType = CacheEventType.convertToEventType(cacheEventData.getCacheEventType());
K key = toObject(cacheEventData.getDataKey());
boolean hasNewValue = !(eventType == EventType.REMOVED || eventType == EventType.EXPIRED);
final V newValue;
final V oldValue;
if (isOldValueRequired) {
if (hasNewValue) {
newValue = toObject(cacheEventData.getDataValue());
oldValue = toObject(cacheEventData.getDataOldValue());
} else {
// according to contract of CacheEntryEvent#getValue
oldValue = toObject(cacheEventData.getDataValue());
newValue = oldValue;
}
} else {
if (hasNewValue) {
newValue = toObject(cacheEventData.getDataValue());
oldValue = null;
} else {
newValue = null;
oldValue = null;
}
}
final CacheEntryEventImpl<K, V> event = new CacheEntryEventImpl<K, V>(source, eventType, key, newValue, oldValue);
if (filter == null || filter.evaluate(event)) {
evt.add(event);
}
}
return evt;
}
use of javax.cache.event.EventType in project ignite by apache.
the class CacheContinuousQueryManager method onEntryUpdated.
/**
* @param lsnrCol Listeners to notify.
* @param key Key.
* @param newVal New value.
* @param oldVal Old value.
* @param internal Internal entry (internal key or not user cache),
* @param partId Partition.
* @param primary {@code True} if called on primary node.
* @param preload Whether update happened during preloading.
* @param updateCntr Update counter.
* @param topVer Topology version.
* @param fut Dht atomic future.
* @throws IgniteCheckedException In case of error.
*/
public void onEntryUpdated(Map<UUID, CacheContinuousQueryListener> lsnrCol, KeyCacheObject key, CacheObject newVal, CacheObject oldVal, boolean internal, int partId, boolean primary, boolean preload, long updateCntr, @Nullable GridDhtAtomicAbstractUpdateFuture fut, AffinityTopologyVersion topVer) throws IgniteCheckedException {
assert key != null;
assert lsnrCol != null;
boolean hasNewVal = newVal != null;
boolean hasOldVal = oldVal != null;
if (!hasNewVal && !hasOldVal) {
skipUpdateEvent(lsnrCol, key, partId, updateCntr, primary, topVer);
return;
}
EventType evtType = !hasNewVal ? REMOVED : !hasOldVal ? CREATED : UPDATED;
boolean initialized = false;
boolean recordIgniteEvt = primary && !internal && cctx.events().isRecordable(EVT_CACHE_QUERY_OBJECT_READ);
for (CacheContinuousQueryListener lsnr : lsnrCol.values()) {
if (preload && !lsnr.notifyExisting() || lsnr.isPrimaryOnly() && !primary)
continue;
if (!initialized) {
if (lsnr.oldValueRequired()) {
oldVal = (CacheObject) cctx.unwrapTemporary(oldVal);
if (oldVal != null)
oldVal.finishUnmarshal(cctx.cacheObjectContext(), cctx.deploy().globalLoader());
}
if (newVal != null)
newVal.finishUnmarshal(cctx.cacheObjectContext(), cctx.deploy().globalLoader());
initialized = true;
}
CacheContinuousQueryEntry e0 = new CacheContinuousQueryEntry(cctx.cacheId(), evtType, key, (!internal && evtType == REMOVED && lsnr.oldValueRequired()) ? oldVal : newVal, lsnr.oldValueRequired() ? oldVal : null, lsnr.keepBinary(), partId, updateCntr, topVer, (byte) 0);
IgniteCacheProxy jcache = cctx.kernalContext().cache().jcacheProxy(cctx.name(), true);
assert jcache != null : "Failed to get cache proxy [name=" + cctx.name() + ", locStart=" + cctx.startTopologyVersion() + ", locNode=" + cctx.localNode() + ", stopping=" + cctx.kernalContext().isStopping();
CacheContinuousQueryEvent evt = new CacheContinuousQueryEvent<>(jcache, cctx, e0);
lsnr.onEntryUpdated(evt, primary, recordIgniteEvt, fut);
}
}
use of javax.cache.event.EventType in project ignite by apache.
the class CacheEntryListenersTest method testListenersWithRemoteFilter.
/**
* Test continuous queries and JCache entry listeners with remote filters.
*/
@Test
@SuppressWarnings("deprecation")
public void testListenersWithRemoteFilter() throws Exception {
try (IgniteClient client = startClient(0, 1, 2)) {
ClientCache<Integer, Integer> cache = client.getOrCreateCache("testListenersWithRmtFilter");
CacheEntryEventSerializableFilter<Integer, Integer> rmtFilter = evt -> (evt.getKey() & 1) == 0;
ContinuousQueryListener<Integer, Integer> lsnr1 = new ContinuousQueryListener<>();
ContinuousQueryListener<Integer, Integer> lsnr2 = new ContinuousQueryListener<>();
cache.query(new ContinuousQuery<Integer, Integer>().setLocalListener(lsnr1).setRemoteFilterFactory(() -> rmtFilter));
cache.query(new ContinuousQuery<Integer, Integer>().setLocalListener(lsnr2).setRemoteFilter(rmtFilter));
JCacheEntryListener<Integer, Integer> lsnr3 = new JCacheEntryListener<>();
cache.registerCacheEntryListener(new MutableCacheEntryListenerConfiguration<>(() -> lsnr3, () -> rmtFilter, true, false));
for (int i = 0; i < 10; i++) cache.put(i, i);
Map<EventType, Map<Integer, Integer>> expRes = F.asMap(EventType.CREATED, IntStream.range(0, 5).boxed().collect(Collectors.toMap(i -> i * 2, i -> i * 2)));
assertEquals(expRes, aggregateListenerEvents(lsnr1, 5));
assertEquals(expRes, aggregateListenerEvents(lsnr2, 5));
assertEquals(expRes, aggregateListenerEvents(lsnr3, 5));
for (int i = 0; i < 10; i++) cache.put(i, -i);
expRes = F.asMap(EventType.UPDATED, IntStream.range(0, 5).boxed().collect(Collectors.toMap(i -> i * 2, i -> -i * 2)));
assertEquals(expRes, aggregateListenerEvents(lsnr1, 5));
assertEquals(expRes, aggregateListenerEvents(lsnr2, 5));
assertEquals(expRes, aggregateListenerEvents(lsnr3, 5));
for (int i = 0; i < 10; i++) cache.remove(i);
expRes = F.asMap(EventType.REMOVED, IntStream.range(0, 5).boxed().collect(Collectors.toMap(i -> i * 2, i -> -i * 2)));
assertEquals(expRes, aggregateListenerEvents(lsnr1, 5));
assertEquals(expRes, aggregateListenerEvents(lsnr2, 5));
assertEquals(expRes, aggregateListenerEvents(lsnr3, 5));
assertTrue(lsnr1.isQueueEmpty());
assertTrue(lsnr2.isQueueEmpty());
assertTrue(lsnr3.isQueueEmpty());
}
}
use of javax.cache.event.EventType in project ignite by apache.
the class ClientCacheEntryListenerHandler method acceptNotification.
/**
* {@inheritDoc}
*/
@Override
public void acceptNotification(ByteBuffer payload, Exception err) {
if (err == null && payload != null) {
BinaryInputStream in = BinaryByteBufferInputStream.create(payload);
int cnt = in.readInt();
List<CacheEntryEvent<? extends K, ? extends V>> evts = new ArrayList<>(cnt);
for (int i = 0; i < cnt; i++) {
K key = utils.readObject(in, keepBinary);
V oldVal = utils.readObject(in, keepBinary);
V val = utils.readObject(in, keepBinary);
byte evtTypeByte = in.readByte();
EventType evtType = eventType(evtTypeByte);
if (evtType == null)
onChannelClosed(new ClientException("Unknown event type: " + evtTypeByte));
evts.add(new CacheEntryEventImpl<>(jCacheAdapter, evtType, key, oldVal, val));
}
locLsnr.onUpdated(evts);
}
}
Aggregations