Search in sources :

Example 6 with CacheEvent

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;
        }
    }
}
Also used : IgnitePredicate(org.apache.ignite.lang.IgnitePredicate) CacheEvent(org.apache.ignite.events.CacheEvent) IgniteException(org.apache.ignite.IgniteException) ConnectException(org.apache.kafka.connect.errors.ConnectException) ConnectException(org.apache.kafka.connect.errors.ConnectException)

Example 7 with CacheEvent

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;
}
Also used : IgniteException(org.apache.ignite.IgniteException) ArrayList(java.util.ArrayList) CacheEvent(org.apache.ignite.events.CacheEvent) SourceRecord(org.apache.kafka.connect.source.SourceRecord)

Example 8 with CacheEvent

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;
}
Also used : IgniteBiPredicate(org.apache.ignite.lang.IgniteBiPredicate) CacheEvent(org.apache.ignite.events.CacheEvent) Ignite(org.apache.ignite.Ignite) CountDownLatch(java.util.concurrent.CountDownLatch) UUID(java.util.UUID)

Example 9 with CacheEvent

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;
}
Also used : IgniteBiPredicate(org.apache.ignite.lang.IgniteBiPredicate) CacheEvent(org.apache.ignite.events.CacheEvent) Ignite(org.apache.ignite.Ignite) CountDownLatch(java.util.concurrent.CountDownLatch) UUID(java.util.UUID)

Example 10 with CacheEvent

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);
        }
    }
}
Also used : IgniteBiPredicate(org.apache.ignite.lang.IgniteBiPredicate) IgnitePredicate(org.apache.ignite.lang.IgnitePredicate) CacheEvent(org.apache.ignite.events.CacheEvent) Ignite(org.apache.ignite.Ignite) UUID(java.util.UUID)

Aggregations

CacheEvent (org.apache.ignite.events.CacheEvent)22 CountDownLatch (java.util.concurrent.CountDownLatch)11 UUID (java.util.UUID)10 Ignite (org.apache.ignite.Ignite)8 IgniteBiPredicate (org.apache.ignite.lang.IgniteBiPredicate)8 Event (org.apache.ignite.events.Event)5 IgnitePredicate (org.apache.ignite.lang.IgnitePredicate)5 HashMap (java.util.HashMap)3 IgniteException (org.apache.ignite.IgniteException)3 ArrayList (java.util.ArrayList)2 Map (java.util.Map)2 ClusterNode (org.apache.ignite.cluster.ClusterNode)2 CacheConfiguration (org.apache.ignite.configuration.CacheConfiguration)2 NearCacheConfiguration (org.apache.ignite.configuration.NearCacheConfiguration)2 CacheRebalancingEvent (org.apache.ignite.events.CacheRebalancingEvent)2 DiscoveryEvent (org.apache.ignite.events.DiscoveryEvent)2 IOException (java.io.IOException)1 LinkedList (java.util.LinkedList)1 Properties (java.util.Properties)1 TimeoutException (java.util.concurrent.TimeoutException)1