Search in sources :

Example 41 with IgnitePredicate

use of org.apache.ignite.lang.IgnitePredicate in project ignite by apache.

the class GridCacheDhtPreloadSelfTest method startGrids.

/**
     * @param cnt Number of grids.
     * @param startIdx Start node index.
     * @param list List of started grids.
     * @throws Exception If failed.
     */
private void startGrids(int cnt, int startIdx, Collection<Ignite> list) throws Exception {
    for (int i = 0; i < cnt; i++) {
        final Ignite g = startGrid(startIdx++);
        if (DEBUG)
            g.events().localListen(new IgnitePredicate<Event>() {

                @Override
                public boolean apply(Event evt) {
                    info("\n>>> Preload event [igniteInstanceName=" + g.name() + ", evt=" + evt + ']');
                    return true;
                }
            }, EVTS_CACHE_REBALANCE);
        list.add(g);
    }
}
Also used : IgnitePredicate(org.apache.ignite.lang.IgnitePredicate) Event(org.apache.ignite.events.Event) CacheRebalancingEvent(org.apache.ignite.events.CacheRebalancingEvent) Ignite(org.apache.ignite.Ignite)

Example 42 with IgnitePredicate

use of org.apache.ignite.lang.IgnitePredicate in project ignite by apache.

the class ConfigVariationsTestSuiteBuilderTest method testIgniteConfigFilter.

/**
     * @throws Exception If failed.
     */
@SuppressWarnings("serial")
public void testIgniteConfigFilter() throws Exception {
    TestSuite dfltSuite = new ConfigVariationsTestSuiteBuilder("testSuite", NoopTest.class).build();
    final AtomicInteger cnt = new AtomicInteger();
    TestSuite filteredSuite = new ConfigVariationsTestSuiteBuilder("testSuite", NoopTest.class).withIgniteConfigFilters(new IgnitePredicate<IgniteConfiguration>() {

        @Override
        public boolean apply(IgniteConfiguration configuration) {
            return cnt.getAndIncrement() % 2 == 0;
        }
    }).build();
    assertEquals(dfltSuite.countTestCases() / 2, filteredSuite.countTestCases());
}
Also used : ConfigVariationsTestSuiteBuilder(org.apache.ignite.testframework.configvariations.ConfigVariationsTestSuiteBuilder) TestSuite(junit.framework.TestSuite) IgniteConfiguration(org.apache.ignite.configuration.IgniteConfiguration) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) IgnitePredicate(org.apache.ignite.lang.IgnitePredicate)

Example 43 with IgnitePredicate

use of org.apache.ignite.lang.IgnitePredicate in project ignite by apache.

the class ConfigVariationsTestSuiteBuilderTest method testCacheConfigFilter.

/**
     * @throws Exception If failed.
     */
@SuppressWarnings("serial")
public void testCacheConfigFilter() throws Exception {
    TestSuite dfltSuite = new ConfigVariationsTestSuiteBuilder("testSuite", NoopTest.class).withBasicCacheParams().build();
    final AtomicInteger cnt = new AtomicInteger();
    TestSuite filteredSuite = new ConfigVariationsTestSuiteBuilder("testSuite", NoopTest.class).withBasicCacheParams().withCacheConfigFilters(new IgnitePredicate<CacheConfiguration>() {

        @Override
        public boolean apply(CacheConfiguration configuration) {
            return cnt.getAndIncrement() % 2 == 0;
        }
    }).build();
    assertEquals(dfltSuite.countTestCases() / 2, filteredSuite.countTestCases());
}
Also used : ConfigVariationsTestSuiteBuilder(org.apache.ignite.testframework.configvariations.ConfigVariationsTestSuiteBuilder) TestSuite(junit.framework.TestSuite) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) IgnitePredicate(org.apache.ignite.lang.IgnitePredicate) CacheConfiguration(org.apache.ignite.configuration.CacheConfiguration)

Example 44 with IgnitePredicate

use of org.apache.ignite.lang.IgnitePredicate 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)

Example 45 with IgnitePredicate

use of org.apache.ignite.lang.IgnitePredicate in project ignite by apache.

the class ClusterGroupExample 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 {
    try (Ignite ignite = Ignition.start("examples/config/example-ignite.xml")) {
        if (!ExamplesUtils.checkMinTopologySize(ignite.cluster(), 2))
            return;
        System.out.println();
        System.out.println("Compute example started.");
        IgniteCluster cluster = ignite.cluster();
        // Say hello to all nodes in the cluster, including local node.
        sayHello(ignite, cluster);
        // Say hello to all remote nodes.
        sayHello(ignite, cluster.forRemotes());
        // Pick random node out of remote nodes.
        ClusterGroup randomNode = cluster.forRemotes().forRandom();
        // Say hello to a random node.
        sayHello(ignite, randomNode);
        // Say hello to all nodes residing on the same host with random node.
        sayHello(ignite, cluster.forHost(randomNode.node()));
        // Say hello to all nodes that have current CPU load less than 50%.
        sayHello(ignite, cluster.forPredicate(new IgnitePredicate<ClusterNode>() {

            @Override
            public boolean apply(ClusterNode n) {
                return n.metrics().getCurrentCpuLoad() < 0.5;
            }
        }));
    }
}
Also used : ClusterNode(org.apache.ignite.cluster.ClusterNode) IgniteCluster(org.apache.ignite.IgniteCluster) IgnitePredicate(org.apache.ignite.lang.IgnitePredicate) ClusterGroup(org.apache.ignite.cluster.ClusterGroup) Ignite(org.apache.ignite.Ignite)

Aggregations

IgnitePredicate (org.apache.ignite.lang.IgnitePredicate)75 Event (org.apache.ignite.events.Event)43 CountDownLatch (java.util.concurrent.CountDownLatch)33 Ignite (org.apache.ignite.Ignite)27 ArrayList (java.util.ArrayList)15 UUID (java.util.UUID)15 ClusterNode (org.apache.ignite.cluster.ClusterNode)14 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)9 IgfsEvent (org.apache.ignite.events.IgfsEvent)9 HashMap (java.util.HashMap)8 CacheEvent (org.apache.ignite.events.CacheEvent)7 CacheQueryExecutedEvent (org.apache.ignite.events.CacheQueryExecutedEvent)7 IgniteRunnable (org.apache.ignite.lang.IgniteRunnable)7 Map (java.util.Map)6 IgniteException (org.apache.ignite.IgniteException)6 ClusterGroup (org.apache.ignite.cluster.ClusterGroup)6 IgniteConfiguration (org.apache.ignite.configuration.IgniteConfiguration)6 CacheQueryReadEvent (org.apache.ignite.events.CacheQueryReadEvent)6 IgniteBiPredicate (org.apache.ignite.lang.IgniteBiPredicate)6 Ignition (org.apache.ignite.Ignition)5