Java Code Examples for javax.cache.event.CacheEntryEvent#getKey()

The following examples show how to use javax.cache.event.CacheEntryEvent#getKey() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: IgniteCacheSinkTaskCQ.java    From spring-boot-akka-event-sourcing-starter with Apache License 2.0 6 votes vote down vote up
/**
 * Buffers records.
 *
 * @param records Records to inject into grid.
 */
@SuppressWarnings("unchecked")
@Override
public void put(Collection<CacheEntryEvent> records) {
	if (log.isDebugEnabled()) {
		log.debug("Sink cache put : {}", records);
	}
	if (null != records && !records.isEmpty()) {
		for (CacheEntryEvent record : records) {
			// Data is flushed asynchronously when CACHE_PER_NODE_DATA_SIZE is reached.
			if (streamerContext.getExtractor() != null) {
				Map.Entry<Object, Object> entry = streamerContext.getExtractor().extract(record);
				if (null != entry) {
					streamerContext.getStreamer().addData(entry.getKey(), entry.getValue());
				}
			} else {
				if (record.getKey() != null) {
					streamerContext.getStreamer().addData(record.getKey(), record.getValue());
				} else {
					log.error("Failed to stream a record with null key!");
				}
			}
		}
	}

}
 
Example 2
Source File: HadoopJobTracker.java    From ignite with Apache License 2.0 5 votes vote down vote up
/**
 * @param updated Updated cache entries.
 * @throws IgniteCheckedException If failed.
 */
private void processJobMetadataUpdates(
    Iterable<CacheEntryEvent<? extends HadoopJobId, ? extends HadoopJobMetadata>> updated)
    throws IgniteCheckedException {
    UUID locNodeId = ctx.localNodeId();

    for (CacheEntryEvent<? extends HadoopJobId, ? extends HadoopJobMetadata> entry : updated) {
        HadoopJobId jobId = entry.getKey();
        HadoopJobMetadata meta = entry.getValue();

        if (meta == null || !ctx.isParticipating(meta))
            continue;

        if (log.isDebugEnabled())
            log.debug("Processing job metadata update callback [locNodeId=" + locNodeId +
                ", meta=" + meta + ']');

        try {
            ctx.taskExecutor().onJobStateChanged(meta);
        }
        catch (IgniteCheckedException e) {
            U.error(log, "Failed to process job state changed callback (will fail the job) " +
                "[locNodeId=" + locNodeId + ", jobId=" + jobId + ", meta=" + meta + ']', e);

            transform(jobId, new CancelJobProcessor(null, e));

            continue;
        }

        processJobMetaUpdate(jobId, meta, locNodeId);
    }
}
 
Example 3
Source File: GridServiceProcessor.java    From ignite with Apache License 2.0 5 votes vote down vote up
/**
 * @param evts Update events.
 */
private void onSystemCacheUpdated(final Iterable<CacheEntryEvent<?, ?>> evts) {
    for (CacheEntryEvent<?, ?> e : evts) {
        if (e.getKey() instanceof GridServiceDeploymentKey)
            processDeployment((CacheEntryEvent)e);
        else if (e.getKey() instanceof GridServiceAssignmentsKey)
            processAssignment((CacheEntryEvent)e);
    }
}
 
Example 4
Source File: CacheContinuousQueryVariationsTest.java    From ignite with Apache License 2.0 5 votes vote down vote up
/**
 * @param evtsQueues Event queue.
 * @param key Key.
 * @param val Value.
 * @param oldVal Old value.
 * @param keepBinary Keep binary.
 * @param withFilter With filter.
 * @throws Exception If failed.
 */
private void waitAndCheckEvent(List<BlockingQueue<CacheEntryEvent<?, ?>>> evtsQueues,
    Object key,
    Object val,
    Object oldVal,
    boolean keepBinary, boolean withFilter)
    throws Exception {
    if (val == null && oldVal == null || (withFilter && val != null && !isAccepted(val, false, dataMode))) {
        checkNoEvent(evtsQueues);

        return;
    }

    for (BlockingQueue<CacheEntryEvent<?, ?>> evtsQueue : evtsQueues) {
        CacheEntryEvent<?, ?> evt = evtsQueue.poll(5, SECONDS);

        assertNotNull("Failed to wait for event [key=" + key + ", val=" + val + ", oldVal=" + oldVal + ']', evt);

        Object actKey = evt.getKey();
        Object actVal = evt.getValue();
        Object actOldVal = evt.getOldValue();

        if (keepBinary) {
            actKey = checkAndGetObject(actKey);
            actVal = checkAndGetObject(actVal);
            actOldVal = checkAndGetObject(actOldVal);
        }

        assertEquals(key, actKey);
        assertEquals(val, actVal);
        assertEquals(oldVal, actOldVal);
    }
}
 
Example 5
Source File: CacheContinuousQueryFailoverAbstractSelfTest.java    From ignite with Apache License 2.0 5 votes vote down vote up
/** {@inheritDoc} */
@Override public synchronized void onUpdated(Iterable<CacheEntryEvent<?, ?>> evts)
    throws CacheEntryListenerException {
    try {
        for (CacheEntryEvent<?, ?> evt : evts) {
            Integer key = (Integer)evt.getKey();
            Integer val = (Integer)evt.getValue();

            assertNotNull(key);
            assertNotNull(val);

            Integer prevVal = vals.get(key);

            boolean dup = false;

            if (prevVal != null && prevVal.equals(val))
                dup = true;

            if (!dup) {
                vals.put(key, val);

                List<CacheEntryEvent<?, ?>> keyEvts = this.evts.get(key);

                if (keyEvts == null) {
                    keyEvts = Collections.synchronizedList(new ArrayList<CacheEntryEvent<?, ?>>());

                    this.evts.put(key, keyEvts);
                }

                keyEvts.add(evt);
            }
        }
    }
    catch (Throwable e) {
        err = true;

        log.error("Unexpected error", e);
    }
}
 
Example 6
Source File: CacheContinuousAsyncQueryExample.java    From ignite with Apache License 2.0 5 votes vote down vote up
/** {@inheritDoc} */
@Override public boolean evaluate(CacheEntryEvent<? extends Integer, ? extends String> e)
    throws CacheEntryListenerException {
    // This cache operation is safe because filter has Ignite AsyncCallback annotation.
    if (e.getKey() < 10 && String.valueOf(e.getKey()).equals(e.getValue()))
        ignite.cache(CACHE_NAME).put(e.getKey(), e.getValue() + "_less_than_10");

    return e.getKey() > 10;
}
 
Example 7
Source File: CacheListenersTest.java    From blazingcache with Apache License 2.0 5 votes vote down vote up
@Override
public void onCreated(Iterable<CacheEntryEvent<? extends K, ? extends V>> events) throws CacheEntryListenerException {
    for (CacheEntryEvent<? extends K, ? extends V> event : events) {
        assertEquals(CREATED.toString(), event.getEventType().toString());
        created.incrementAndGet();

        // added for code coverage.
        event.getKey();
        event.getValue();
        event.getSource();
    }
}
 
Example 8
Source File: CacheListenersTest.java    From blazingcache with Apache License 2.0 5 votes vote down vote up
@Override
public void onRemoved(Iterable<CacheEntryEvent<? extends K, ? extends V>> events) throws CacheEntryListenerException {            
    for (CacheEntryEvent<? extends K, ? extends V> event : events) {
        
        assertEquals(REMOVED.toString(), event.getEventType().toString());
        removed.incrementAndGet();
        event.getKey();
        if (event.isOldValueAvailable()) {
            event.getOldValue();
        }
    }
}
 
Example 9
Source File: CacheListenersTest.java    From blazingcache with Apache License 2.0 5 votes vote down vote up
@Override
public void onUpdated(Iterable<CacheEntryEvent<? extends K, ? extends V>> events) throws CacheEntryListenerException {
    for (CacheEntryEvent<? extends K, ? extends V> event : events) {
        assertEquals(UPDATED.toString(), event.getEventType().toString());
        updated.incrementAndGet();
        event.getKey();
        if (event.isOldValueAvailable()) {
            event.getOldValue();
        }
    }
}
 
Example 10
Source File: CacheTestSupport.java    From cache2k with Apache License 2.0 5 votes vote down vote up
@Override
public void onCreated(Iterable<CacheEntryEvent<? extends K, ? extends V>> events) throws CacheEntryListenerException {
  for (CacheEntryEvent<? extends K, ? extends V> event : events) {
    assertEquals(CREATED, event.getEventType());
    created.incrementAndGet();

    // added for code coverage.
    event.getKey();
    event.getValue();
    event.getSource();
  }
}
 
Example 11
Source File: CacheTestSupport.java    From cache2k with Apache License 2.0 5 votes vote down vote up
@Override
public void onRemoved(Iterable<CacheEntryEvent<? extends K, ? extends V>> events) throws CacheEntryListenerException {
  for (CacheEntryEvent<? extends K, ? extends V> event : events) {
    assertEquals(REMOVED, event.getEventType());
    assertTrue("isOldValueAvailable() for onRemoved", event.isOldValueAvailable());
    assertNotNull("old value non-null for onRemoved", event.getOldValue());
    assertEquals("old value identical to value at onRemoved", event.getOldValue(), event.getValue());
    removed.incrementAndGet();
    event.getKey();
    if (event.isOldValueAvailable()) {
      event.getOldValue();
    }
  }
}
 
Example 12
Source File: CacheTestSupport.java    From cache2k with Apache License 2.0 5 votes vote down vote up
@Override
public void onUpdated(Iterable<CacheEntryEvent<? extends K, ? extends V>> events) throws CacheEntryListenerException {
  for (CacheEntryEvent<? extends K, ? extends V> event : events) {
    assertEquals(UPDATED, event.getEventType());
    assertTrue("isOldValueAvailable() for onUpdated", event.isOldValueAvailable());
    assertNotNull("old value non-null for onUpdated", event.getOldValue());
    updated.incrementAndGet();
    event.getKey();
    if (event.isOldValueAvailable()) {
      event.getOldValue();
    }
  }
}
 
Example 13
Source File: CacheDataStructuresManager.java    From ignite with Apache License 2.0 4 votes vote down vote up
/** {@inheritDoc} */
@Override public boolean evaluate(CacheEntryEvent<? extends K, ? extends V> e) {
    return e.getKey() instanceof GridCacheQueueHeaderKey;
}
 
Example 14
Source File: CacheContinuousWithTransformerReplicatedSelfTest.java    From ignite with Apache License 2.0 4 votes vote down vote up
/** {@inheritDoc} */
@Override public boolean evaluate(
    CacheEntryEvent<? extends Integer, ?> event) throws CacheEntryListenerException {
    return event.getKey() % 2 == 0;
}
 
Example 15
Source File: CacheContinuousQueryOperationP2PTest.java    From ignite with Apache License 2.0 4 votes vote down vote up
/**
 * @param ccfg Cache configuration.
 * @param isClient Client.
 * @param joinNode If a node should be added to topology after a query is started.
 * @param evtFilterFactoryCls Remote filter factory class.
 * @throws Exception If failed.
 */
private void testContinuousQuery(CacheConfiguration<Object, Object> ccfg,
    boolean isClient, boolean joinNode,
    Class<Factory<CacheEntryEventFilter>> evtFilterFactoryCls) throws Exception {

    ThreadLocalRandom rnd = ThreadLocalRandom.current();

    final CountDownLatch latch = new CountDownLatch(UPDATES);

    ContinuousQuery<Integer, Integer> qry = new ContinuousQuery<>();

    AtomicReference<String> err = new AtomicReference<>();

    TestLocalListener locLsnr = new TestLocalListener() {
        @Override protected void onEvent(Iterable<CacheEntryEvent<? extends Integer, ? extends Integer>> evts) {
            for (CacheEntryEvent<? extends Integer, ? extends Integer> evt : evts) {
                latch.countDown();

                log.info("Received event: " + evt);

                int key = evt.getKey();

                if (key % 2 == 0)
                    err.set("Event received on entry, that doesn't pass a filter: " + key);
            }
        }
    };

    qry.setLocalListener(locLsnr);

    qry.setRemoteFilterFactory(
        (Factory<? extends CacheEntryEventFilter<Integer, Integer>>)(Object)evtFilterFactoryCls.newInstance());

    MutableCacheEntryListenerConfiguration<Integer, Integer> lsnrCfg =
        new MutableCacheEntryListenerConfiguration<>(
            new FactoryBuilder.SingletonFactory<>(locLsnr),
            (Factory<? extends CacheEntryEventFilter<? super Integer, ? super Integer>>)
                (Object)evtFilterFactoryCls.newInstance(),
            true,
            true
        );

    IgniteCache<Integer, Integer> cache;

    cache = isClient
        ? grid(NODES - 1).cache(ccfg.getName())
        : grid(rnd.nextInt(NODES - 1)).cache(ccfg.getName());

    try (QueryCursor<?> cur = cache.query(qry)) {
        cache.registerCacheEntryListener(lsnrCfg);

        if (joinNode) {
            startGrid(NODES);
            awaitPartitionMapExchange();
        }

        for (int i = 0; i < UPDATES; i++)
            cache.put(i, i);

        assertTrue("Failed to wait for local listener invocations: " + latch.getCount(),
            latch.await(3, TimeUnit.SECONDS));

        assertNull(err.get(), err.get());
    }
}