use of org.apache.ignite.events.CacheEvent in project ignite by apache.
the class IgniteSourceTask method start.
/**
* Filtering is done remotely. Local listener buffers data for injection into Kafka.
*
* @param props Task properties.
*/
@Override
public void start(Map<String, String> props) {
synchronized (lock) {
// Nothing to do if the task has been already started.
if (!stopped)
return;
cacheName = props.get(IgniteSourceConstants.CACHE_NAME);
igniteCfgFile = props.get(IgniteSourceConstants.CACHE_CFG_PATH);
topics = props.get(IgniteSourceConstants.TOPIC_NAMES).split("\\s*,\\s*");
if (props.containsKey(IgniteSourceConstants.INTL_BUF_SIZE))
evtBufSize = Integer.parseInt(props.get(IgniteSourceConstants.INTL_BUF_SIZE));
if (props.containsKey(IgniteSourceConstants.INTL_BATCH_SIZE))
evtBatchSize = Integer.parseInt(props.get(IgniteSourceConstants.INTL_BATCH_SIZE));
if (props.containsKey(IgniteSourceConstants.CACHE_FILTER_CLASS)) {
String filterCls = props.get(IgniteSourceConstants.CACHE_FILTER_CLASS);
if (filterCls != null && !filterCls.isEmpty()) {
try {
Class<? extends IgnitePredicate<CacheEvent>> clazz = (Class<? extends IgnitePredicate<CacheEvent>>) Class.forName(filterCls);
filter = clazz.newInstance();
} catch (Exception e) {
log.error("Failed to instantiate the provided filter! " + "User-enabled filtering is ignored!", e);
}
}
}
TaskRemoteFilter rmtLsnr = new TaskRemoteFilter(cacheName);
try {
int[] evts = cacheEvents(props.get(IgniteSourceConstants.CACHE_EVENTS));
rmtLsnrId = IgniteGrid.getIgnite().events(IgniteGrid.getIgnite().cluster().forCacheNodes(cacheName)).remoteListen(locLsnr, rmtLsnr, evts);
} catch (Exception e) {
log.error("Failed to register event listener!", e);
throw new ConnectException(e);
} finally {
stopped = false;
}
}
}
use of org.apache.ignite.events.CacheEvent in project ignite by apache.
the class IgniteSourceTask method poll.
/** {@inheritDoc} */
@Override
public List<SourceRecord> poll() throws InterruptedException {
ArrayList<SourceRecord> records = new ArrayList<>(evtBatchSize);
ArrayList<CacheEvent> evts = new ArrayList<>(evtBatchSize);
if (stopped)
return records;
try {
if (evtBuf.drainTo(evts, evtBatchSize) > 0) {
for (CacheEvent evt : evts) {
// schema and keys are ignored.
for (String topic : topics) records.add(new SourceRecord(srcPartition, offset, topic, null, evt));
}
return records;
}
} catch (IgniteException e) {
log.error("Error when polling event queue!", e);
}
// for shutdown.
return null;
}
use of org.apache.ignite.events.CacheEvent in project ignite by apache.
the class IgniteMqttStreamerTest method subscribeToPutEvents.
/**
* @param expect Expected count.
* @return Latch to be counted down in listener.
*/
private CountDownLatch subscribeToPutEvents(int expect) {
Ignite ignite = grid();
// Listen to cache PUT events and expect as many as messages as test data items
final CountDownLatch latch = new CountDownLatch(expect);
IgniteBiPredicate<UUID, CacheEvent> cb = new IgniteBiPredicate<UUID, CacheEvent>() {
@Override
public boolean apply(UUID uuid, CacheEvent evt) {
latch.countDown();
return true;
}
};
remoteLsnr = ignite.events(ignite.cluster().forCacheNodes(DEFAULT_CACHE_NAME)).remoteListen(cb, null, EVT_CACHE_OBJECT_PUT);
return latch;
}
use of org.apache.ignite.events.CacheEvent in project ignite by apache.
the class IgniteCamelStreamerTest method subscribeToPutEvents.
/**
* Subscribe to cache put events.
*/
private CountDownLatch subscribeToPutEvents(int expect) {
Ignite ignite = grid();
// Listen to cache PUT events and expect as many as messages as test data items
final CountDownLatch latch = new CountDownLatch(expect);
@SuppressWarnings("serial") IgniteBiPredicate<UUID, CacheEvent> callback = new IgniteBiPredicate<UUID, CacheEvent>() {
@Override
public boolean apply(UUID uuid, CacheEvent evt) {
latch.countDown();
return true;
}
};
remoteLsnr = ignite.events(ignite.cluster().forCacheNodes(DEFAULT_CACHE_NAME)).remoteListen(callback, null, EVT_CACHE_OBJECT_PUT);
return latch;
}
use of org.apache.ignite.events.CacheEvent in project ignite by apache.
the class CacheEventsExample method main.
/**
* Executes example.
*
* @param args Command line arguments, none required.
* @throws IgniteException If example execution failed.
*/
public static void main(String[] args) throws IgniteException, InterruptedException {
try (Ignite ignite = Ignition.start("examples/config/example-ignite.xml")) {
System.out.println();
System.out.println(">>> Cache events example started.");
// Auto-close cache at the end of the example.
try (IgniteCache<Integer, String> cache = ignite.getOrCreateCache(CACHE_NAME)) {
// This optional local callback is called for each event notification
// that passed remote predicate listener.
IgniteBiPredicate<UUID, CacheEvent> locLsnr = new IgniteBiPredicate<UUID, CacheEvent>() {
@Override
public boolean apply(UUID uuid, CacheEvent evt) {
System.out.println("Received event [evt=" + evt.name() + ", key=" + evt.key() + ", oldVal=" + evt.oldValue() + ", newVal=" + evt.newValue());
// Continue listening.
return true;
}
};
// Remote listener which only accepts events for keys that are
// greater or equal than 10 and if event node is primary for this key.
IgnitePredicate<CacheEvent> rmtLsnr = new IgnitePredicate<CacheEvent>() {
@Override
public boolean apply(CacheEvent evt) {
System.out.println("Cache event [name=" + evt.name() + ", key=" + evt.key() + ']');
int key = evt.key();
return key >= 10 && ignite.affinity(CACHE_NAME).isPrimary(ignite.cluster().localNode(), key);
}
};
// Subscribe to specified cache events on all nodes that have cache running.
// Cache events are explicitly enabled in examples/config/example-ignite.xml file.
ignite.events(ignite.cluster().forCacheNodes(CACHE_NAME)).remoteListen(locLsnr, rmtLsnr, EVT_CACHE_OBJECT_PUT, EVT_CACHE_OBJECT_READ, EVT_CACHE_OBJECT_REMOVED);
// Generate cache events.
for (int i = 0; i < 20; i++) cache.put(i, Integer.toString(i));
// Wait for a while while callback is notified about remaining puts.
Thread.sleep(2000);
} finally {
// Distributed cache could be removed from cluster only by #destroyCache() call.
ignite.destroyCache(CACHE_NAME);
}
}
}
Aggregations