Search in sources :

Example 1 with EventType

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;
}
Also used : EventType(javax.cache.event.EventType) CacheEventType(com.hazelcast.cache.CacheEventType) CacheEntryEvent(javax.cache.event.CacheEntryEvent) HashSet(java.util.HashSet)

Example 2 with EventType

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);
    }
}
Also used : EventType(javax.cache.event.EventType) IgniteCacheProxy(org.apache.ignite.internal.processors.cache.IgniteCacheProxy)

Example 3 with EventType

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());
    }
}
Also used : IntStream(java.util.stream.IntStream) ClientDisconnectListener(org.apache.ignite.client.ClientDisconnectListener) Person(org.apache.ignite.client.Person) GridTestUtils.waitForCondition(org.apache.ignite.testframework.GridTestUtils.waitForCondition) CacheEntryEventSerializableFilter(org.apache.ignite.cache.CacheEntryEventSerializableFilter) U(org.apache.ignite.internal.util.typedef.internal.U) HashMap(java.util.HashMap) CacheEntryExpiredListener(javax.cache.event.CacheEntryExpiredListener) ClientCache(org.apache.ignite.client.ClientCache) HashSet(java.util.HashSet) Future(java.util.concurrent.Future) CacheEntryListenerConfiguration(javax.cache.configuration.CacheEntryListenerConfiguration) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) IgniteClient(org.apache.ignite.client.IgniteClient) Map(java.util.Map) Cache(javax.cache.Cache) CacheEntryCreatedListener(javax.cache.event.CacheEntryCreatedListener) CacheEntryUpdatedListener(javax.cache.event.CacheEntryUpdatedListener) Duration(javax.cache.expiry.Duration) MutableCacheEntryListenerConfiguration(javax.cache.configuration.MutableCacheEntryListenerConfiguration) F(org.apache.ignite.internal.util.typedef.F) EventType(javax.cache.event.EventType) CreatedExpiryPolicy(javax.cache.expiry.CreatedExpiryPolicy) GridTestUtils.assertThrowsWithCause(org.apache.ignite.testframework.GridTestUtils.assertThrowsWithCause) EnumMap(java.util.EnumMap) FactoryBuilder(javax.cache.configuration.FactoryBuilder) Set(java.util.Set) BlockingQueue(java.util.concurrent.BlockingQueue) Test(org.junit.Test) UUID(java.util.UUID) ContinuousQuery(org.apache.ignite.cache.query.ContinuousQuery) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) Collectors(java.util.stream.Collectors) IgniteCache(org.apache.ignite.IgniteCache) T2(org.apache.ignite.internal.util.typedef.T2) GridTestUtils(org.apache.ignite.testframework.GridTestUtils) TimeUnit(java.util.concurrent.TimeUnit) IgniteConfiguration(org.apache.ignite.configuration.IgniteConfiguration) CacheEntryEvent(javax.cache.event.CacheEntryEvent) CacheEntryRemovedListener(javax.cache.event.CacheEntryRemovedListener) ThinClientConfiguration(org.apache.ignite.configuration.ThinClientConfiguration) QueryCursor(org.apache.ignite.cache.query.QueryCursor) ScanQuery(org.apache.ignite.cache.query.ScanQuery) ClientConnectorConfiguration(org.apache.ignite.configuration.ClientConnectorConfiguration) EventType(javax.cache.event.EventType) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) IgniteClient(org.apache.ignite.client.IgniteClient) HashMap(java.util.HashMap) Map(java.util.Map) EnumMap(java.util.EnumMap) Test(org.junit.Test)

Example 4 with EventType

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);
    }
}
Also used : BinaryInputStream(org.apache.ignite.internal.binary.streams.BinaryInputStream) EventType(javax.cache.event.EventType) ArrayList(java.util.ArrayList) ClientException(org.apache.ignite.client.ClientException) CacheEntryEvent(javax.cache.event.CacheEntryEvent)

Aggregations

EventType (javax.cache.event.EventType)4 CacheEntryEvent (javax.cache.event.CacheEntryEvent)3 HashSet (java.util.HashSet)2 CacheEventType (com.hazelcast.cache.CacheEventType)1 ArrayList (java.util.ArrayList)1 EnumMap (java.util.EnumMap)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 Set (java.util.Set)1 UUID (java.util.UUID)1 BlockingQueue (java.util.concurrent.BlockingQueue)1 Future (java.util.concurrent.Future)1 LinkedBlockingQueue (java.util.concurrent.LinkedBlockingQueue)1 TimeUnit (java.util.concurrent.TimeUnit)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 Collectors (java.util.stream.Collectors)1 IntStream (java.util.stream.IntStream)1 Cache (javax.cache.Cache)1 CacheEntryListenerConfiguration (javax.cache.configuration.CacheEntryListenerConfiguration)1 FactoryBuilder (javax.cache.configuration.FactoryBuilder)1