IGNITE-45 - Examples

Project: http://git-wip-us.apache.org/repos/asf/incubator-ignite/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-ignite/commit/d086ae05
Tree: http://git-wip-us.apache.org/repos/asf/incubator-ignite/tree/d086ae05
Diff: http://git-wip-us.apache.org/repos/asf/incubator-ignite/diff/d086ae05

Branch: refs/heads/ignite-45
Commit: d086ae05040e5c6f28bccce0747d95e6d15aea4f
Parents: 02c4ca5
Author: Valentin Kulichenko <vkuliche...@gridgain.com>
Authored: Sun Mar 22 18:35:18 2015 -0700
Committer: Valentin Kulichenko <vkuliche...@gridgain.com>
Committed: Sun Mar 22 18:35:18 2015 -0700

----------------------------------------------------------------------
 .../computegrid/ComputeAsyncExample.java        | 73 +++++++++++++++++
 .../computegrid/ComputeRunnableExample.java     | 15 +---
 .../examples/datagrid/CacheApiExample.java      | 25 +-----
 .../examples/datagrid/CacheAsyncApiExample.java | 86 ++++++++++++++++++++
 .../java8/computegrid/ComputeAsyncExample.java  | 71 ++++++++++++++++
 .../computegrid/ComputeRunnableExample.java     | 12 +--
 .../java8/datagrid/CacheApiExample.java         | 13 ---
 .../java8/datagrid/CacheAsyncApiExample.java    | 82 +++++++++++++++++++
 8 files changed, 317 insertions(+), 60 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/d086ae05/examples/src/main/java/org/apache/ignite/examples/computegrid/ComputeAsyncExample.java
----------------------------------------------------------------------
diff --git 
a/examples/src/main/java/org/apache/ignite/examples/computegrid/ComputeAsyncExample.java
 
b/examples/src/main/java/org/apache/ignite/examples/computegrid/ComputeAsyncExample.java
new file mode 100644
index 0000000..22a5847
--- /dev/null
+++ 
b/examples/src/main/java/org/apache/ignite/examples/computegrid/ComputeAsyncExample.java
@@ -0,0 +1,73 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.ignite.examples.computegrid;
+
+import org.apache.ignite.*;
+import org.apache.ignite.examples.*;
+import org.apache.ignite.lang.*;
+
+import java.util.*;
+
+/**
+ * Demonstrates a simple use of {@link IgniteRunnable}.
+ * <p>
+ * Remote nodes should always be started with special configuration file which
+ * enables P2P class loading: {@code 'ignite.{sh|bat} 
examples/config/example-ignite.xml'}.
+ * <p>
+ * Alternatively you can run {@link ExampleNodeStartup} in another JVM which 
will start node
+ * with {@code examples/config/example-ignite.xml} configuration.
+ */
+public class ComputeAsyncExample {
+    /**
+     * 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")) {
+            System.out.println();
+            System.out.println("Compute asynchronous example started.");
+
+            // Enable asynchronous mode.
+            IgniteCompute compute = ignite.compute().withAsync();
+
+            Collection<IgniteFuture<?>> futs = new ArrayList<>();
+
+            // Iterate through all words in the sentence and create runnable 
jobs.
+            for (final String word : "Print words using runnable".split(" ")) {
+                // Execute runnable on some node.
+                compute.run(new IgniteRunnable() {
+                    @Override public void run() {
+                        System.out.println();
+                        System.out.println(">>> Printing '" + word + "' on 
this node from ignite job.");
+                    }
+                });
+
+                futs.add(compute.future());
+            }
+
+            // Wait for completion of all futures.
+            futs.forEach(IgniteFuture::get);
+
+            System.out.println();
+            System.out.println(">>> Finished printing words using runnable 
execution.");
+            System.out.println(">>> Check all nodes for output (this node is 
also part of the cluster).");
+        }
+    }
+}

http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/d086ae05/examples/src/main/java/org/apache/ignite/examples/computegrid/ComputeRunnableExample.java
----------------------------------------------------------------------
diff --git 
a/examples/src/main/java/org/apache/ignite/examples/computegrid/ComputeRunnableExample.java
 
b/examples/src/main/java/org/apache/ignite/examples/computegrid/ComputeRunnableExample.java
index 1d907e3..4763bba 100644
--- 
a/examples/src/main/java/org/apache/ignite/examples/computegrid/ComputeRunnableExample.java
+++ 
b/examples/src/main/java/org/apache/ignite/examples/computegrid/ComputeRunnableExample.java
@@ -21,8 +21,6 @@ import org.apache.ignite.*;
 import org.apache.ignite.examples.*;
 import org.apache.ignite.lang.*;
 
-import java.util.*;
-
 /**
  * Demonstrates a simple use of {@link IgniteRunnable}.
  * <p>
@@ -44,12 +42,9 @@ public class ComputeRunnableExample {
             System.out.println();
             System.out.println("Compute runnable example started.");
 
-            Collection<IgniteFuture> futs = new ArrayList<>();
-
-            // Enable asynchronous mode.
-            IgniteCompute compute = ignite.compute().withAsync();
+            IgniteCompute compute = ignite.compute();
 
-            // Iterate through all words in the sentence and create callable 
jobs.
+            // Iterate through all words in the sentence and create runnable 
jobs.
             for (final String word : "Print words using runnable".split(" ")) {
                 // Execute runnable on some node.
                 compute.run(new IgniteRunnable() {
@@ -58,14 +53,8 @@ public class ComputeRunnableExample {
                         System.out.println(">>> Printing '" + word + "' on 
this node from ignite job.");
                     }
                 });
-
-                futs.add(compute.future());
             }
 
-            // Wait for all futures to complete.
-            for (IgniteFuture<?> f : futs)
-                f.get();
-
             System.out.println();
             System.out.println(">>> Finished printing words using runnable 
execution.");
             System.out.println(">>> Check all nodes for output (this node is 
also part of the cluster).");

http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/d086ae05/examples/src/main/java/org/apache/ignite/examples/datagrid/CacheApiExample.java
----------------------------------------------------------------------
diff --git 
a/examples/src/main/java/org/apache/ignite/examples/datagrid/CacheApiExample.java
 
b/examples/src/main/java/org/apache/ignite/examples/datagrid/CacheApiExample.java
index c912d47..35b7ac6 100644
--- 
a/examples/src/main/java/org/apache/ignite/examples/datagrid/CacheApiExample.java
+++ 
b/examples/src/main/java/org/apache/ignite/examples/datagrid/CacheApiExample.java
@@ -21,7 +21,6 @@ import org.apache.ignite.*;
 import org.apache.ignite.cache.*;
 import org.apache.ignite.configuration.*;
 import org.apache.ignite.examples.*;
-import org.apache.ignite.lang.*;
 
 import javax.cache.processor.*;
 import java.util.concurrent.*;
@@ -76,32 +75,10 @@ public class CacheApiExample {
         String v = cache.getAndPut(1, "1");
         assert v == null;
 
-        // Put and do not return previous value (all methods ending with 'x' 
return boolean).
+        // Put and do not return previous value.
         // Performs better when previous value is not needed.
         cache.put(2, "2");
 
-        // Put asynchronously.
-        final IgniteCache<Integer, String> asyncCache = cache.withAsync();
-
-        asyncCache.put(3, "3");
-
-        asyncCache.get(3);
-
-        IgniteFuture<String> fut = asyncCache.future();
-
-        //Asynchronously wait for result.
-        fut.listen(new IgniteInClosure<IgniteFuture<String>>() {
-            @Override
-            public void apply(IgniteFuture<String> fut) {
-                try {
-                    System.out.println("Put operation completed 
[previous-value=" + fut.get() + ']');
-                }
-                catch (IgniteException e) {
-                    e.printStackTrace();
-                }
-            }
-        });
-
         // Put-if-absent.
         boolean b1 = cache.putIfAbsent(4, "4");
         boolean b2 = cache.putIfAbsent(4, "44");

http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/d086ae05/examples/src/main/java/org/apache/ignite/examples/datagrid/CacheAsyncApiExample.java
----------------------------------------------------------------------
diff --git 
a/examples/src/main/java/org/apache/ignite/examples/datagrid/CacheAsyncApiExample.java
 
b/examples/src/main/java/org/apache/ignite/examples/datagrid/CacheAsyncApiExample.java
new file mode 100644
index 0000000..e7daa09
--- /dev/null
+++ 
b/examples/src/main/java/org/apache/ignite/examples/datagrid/CacheAsyncApiExample.java
@@ -0,0 +1,86 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.ignite.examples.datagrid;
+
+import org.apache.ignite.*;
+import org.apache.ignite.cache.*;
+import org.apache.ignite.configuration.*;
+import org.apache.ignite.examples.*;
+import org.apache.ignite.lang.*;
+
+import java.util.*;
+
+/**
+ * This example demonstrates some of the cache rich API capabilities.
+ * <p>
+ * Remote nodes should always be started with special configuration file which
+ * enables P2P class loading: {@code 'ignite.{sh|bat} 
examples/config/example-ignite.xml'}.
+ * <p>
+ * Alternatively you can run {@link ExampleNodeStartup} in another JVM which 
will
+ * start node with {@code examples/config/example-ignite.xml} configuration.
+ */
+public class CacheAsyncApiExample {
+    /** Cache name. */
+    private static final String CACHE_NAME = 
CacheAsyncApiExample.class.getSimpleName();
+
+    /**
+     * 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")) {
+            System.out.println();
+            System.out.println(">>> Cache asynchronous API example started.");
+
+            CacheConfiguration<Integer, String> cfg = new 
CacheConfiguration<>();
+
+            cfg.setCacheMode(CacheMode.PARTITIONED);
+            cfg.setName(CACHE_NAME);
+
+            try (IgniteCache<Integer, String> cache = ignite.createCache(cfg)) 
{
+                // Enable asynchronous mode.
+                IgniteCache<Integer, String> asyncCache = cache.withAsync();
+
+                Collection<IgniteFuture<?>> futs = new ArrayList<>();
+
+                // Execute several puts asynchronously.
+                for (int i = 0; i < 10; i++) {
+                    asyncCache.put(i, String.valueOf(i));
+
+                    futs.add(asyncCache.future());
+                }
+
+                // Wait for completion of all futures.
+                for (IgniteFuture<?> fut : futs)
+                    fut.get();
+
+                // Execute get operation asynchronously.
+                asyncCache.get(1);
+
+                // Asynchronously wait for result.
+                asyncCache.<String>future().listen(new 
IgniteInClosure<IgniteFuture<String>>() {
+                    @Override public void apply(IgniteFuture<String> fut) {
+                        System.out.println("Get operation completed [value=" + 
fut.get() + ']');
+                    }
+                });
+            }
+        }
+    }
+}

http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/d086ae05/examples/src/main/java8/org/apache/ignite/examples/java8/computegrid/ComputeAsyncExample.java
----------------------------------------------------------------------
diff --git 
a/examples/src/main/java8/org/apache/ignite/examples/java8/computegrid/ComputeAsyncExample.java
 
b/examples/src/main/java8/org/apache/ignite/examples/java8/computegrid/ComputeAsyncExample.java
new file mode 100644
index 0000000..b6922b8
--- /dev/null
+++ 
b/examples/src/main/java8/org/apache/ignite/examples/java8/computegrid/ComputeAsyncExample.java
@@ -0,0 +1,71 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.ignite.examples.java8.computegrid;
+
+import org.apache.ignite.*;
+import org.apache.ignite.examples.*;
+import org.apache.ignite.lang.*;
+
+import java.util.*;
+
+/**
+ * Demonstrates a simple use of {@link IgniteRunnable}.
+ * <p>
+ * Remote nodes should always be started with special configuration file which
+ * enables P2P class loading: {@code 'ignite.{sh|bat} 
examples/config/example-ignite.xml'}.
+ * <p>
+ * Alternatively you can run {@link ExampleNodeStartup} in another JVM which 
will start node
+ * with {@code examples/config/example-ignite.xml} configuration.
+ */
+public class ComputeAsyncExample {
+    /**
+     * 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")) {
+            System.out.println();
+            System.out.println("Compute asynchronous example started.");
+
+            // Enable asynchronous mode.
+            IgniteCompute compute = ignite.compute().withAsync();
+
+            Collection<IgniteFuture<?>> futs = new ArrayList<>();
+
+            // Iterate through all words in the sentence and create runnable 
jobs.
+            for (final String word : "Print words using runnable".split(" ")) {
+                // Execute runnable on some node.
+                compute.run(() -> {
+                    System.out.println();
+                    System.out.println(">>> Printing '" + word + "' on this 
node from ignite job.");
+                });
+
+                futs.add(compute.future());
+            }
+
+            // Wait for completion of all futures.
+            futs.forEach(IgniteFuture::get);
+
+            System.out.println();
+            System.out.println(">>> Finished printing words using runnable 
execution.");
+            System.out.println(">>> Check all nodes for output (this node is 
also part of the cluster).");
+        }
+    }
+}

http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/d086ae05/examples/src/main/java8/org/apache/ignite/examples/java8/computegrid/ComputeRunnableExample.java
----------------------------------------------------------------------
diff --git 
a/examples/src/main/java8/org/apache/ignite/examples/java8/computegrid/ComputeRunnableExample.java
 
b/examples/src/main/java8/org/apache/ignite/examples/java8/computegrid/ComputeRunnableExample.java
index df390b2..474ae20 100644
--- 
a/examples/src/main/java8/org/apache/ignite/examples/java8/computegrid/ComputeRunnableExample.java
+++ 
b/examples/src/main/java8/org/apache/ignite/examples/java8/computegrid/ComputeRunnableExample.java
@@ -44,25 +44,17 @@ public class ComputeRunnableExample {
             System.out.println();
             System.out.println("Compute runnable example started.");
 
-            Collection<IgniteFuture> futs = new ArrayList<>();
+            IgniteCompute compute = ignite.compute();
 
-            // Enable asynchronous mode.
-            IgniteCompute compute = ignite.compute().withAsync();
-
-            // Iterate through all words in the sentence and create callable 
jobs.
+            // Iterate through all words in the sentence and create runnable 
jobs.
             for (final String word : "Print words using runnable".split(" ")) {
                 // Execute runnable on some node.
                 compute.run(() -> {
                     System.out.println();
                     System.out.println(">>> Printing '" + word + "' on this 
node from ignite job.");
                 });
-
-                futs.add(compute.future());
             }
 
-            // Wait for all futures to complete.
-            futs.forEach(IgniteFuture::get);
-
             System.out.println();
             System.out.println(">>> Finished printing words using runnable 
execution.");
             System.out.println(">>> Check all nodes for output (this node is 
also part of the cluster).");

http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/d086ae05/examples/src/main/java8/org/apache/ignite/examples/java8/datagrid/CacheApiExample.java
----------------------------------------------------------------------
diff --git 
a/examples/src/main/java8/org/apache/ignite/examples/java8/datagrid/CacheApiExample.java
 
b/examples/src/main/java8/org/apache/ignite/examples/java8/datagrid/CacheApiExample.java
index efe045f..f0cb2ac 100644
--- 
a/examples/src/main/java8/org/apache/ignite/examples/java8/datagrid/CacheApiExample.java
+++ 
b/examples/src/main/java8/org/apache/ignite/examples/java8/datagrid/CacheApiExample.java
@@ -21,7 +21,6 @@ import org.apache.ignite.*;
 import org.apache.ignite.cache.*;
 import org.apache.ignite.configuration.*;
 import org.apache.ignite.examples.*;
-import org.apache.ignite.lang.*;
 
 import java.util.concurrent.*;
 
@@ -79,18 +78,6 @@ public class CacheApiExample {
         // Performs better when previous value is not needed.
         cache.put(2, "2");
 
-        // Put asynchronously.
-        final IgniteCache<Integer, String> asyncCache = cache.withAsync();
-
-        asyncCache.put(3, "3");
-
-        asyncCache.get(3);
-
-        IgniteFuture<String> fut = asyncCache.future();
-
-        //Asynchronously wait for result.
-        fut.listen(f -> System.out.println("Put operation completed 
[previous-value=" + f.get() + ']'));
-
         // Put-if-absent.
         boolean b1 = cache.putIfAbsent(4, "4");
         boolean b2 = cache.putIfAbsent(4, "44");

http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/d086ae05/examples/src/main/java8/org/apache/ignite/examples/java8/datagrid/CacheAsyncApiExample.java
----------------------------------------------------------------------
diff --git 
a/examples/src/main/java8/org/apache/ignite/examples/java8/datagrid/CacheAsyncApiExample.java
 
b/examples/src/main/java8/org/apache/ignite/examples/java8/datagrid/CacheAsyncApiExample.java
new file mode 100644
index 0000000..0a95169
--- /dev/null
+++ 
b/examples/src/main/java8/org/apache/ignite/examples/java8/datagrid/CacheAsyncApiExample.java
@@ -0,0 +1,82 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.ignite.examples.java8.datagrid;
+
+import org.apache.ignite.*;
+import org.apache.ignite.cache.*;
+import org.apache.ignite.configuration.*;
+import org.apache.ignite.examples.*;
+import org.apache.ignite.lang.*;
+
+import java.util.*;
+
+/**
+ * This example demonstrates some of the cache rich API capabilities.
+ * <p>
+ * Remote nodes should always be started with special configuration file which
+ * enables P2P class loading: {@code 'ignite.{sh|bat} 
examples/config/example-ignite.xml'}.
+ * <p>
+ * Alternatively you can run {@link ExampleNodeStartup} in another JVM which 
will
+ * start node with {@code examples/config/example-ignite.xml} configuration.
+ */
+public class CacheAsyncApiExample {
+    /** Cache name. */
+    private static final String CACHE_NAME = 
CacheAsyncApiExample.class.getSimpleName();
+
+    /**
+     * 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")) {
+            System.out.println();
+            System.out.println(">>> Cache asynchronous API example started.");
+
+            CacheConfiguration<Integer, String> cfg = new 
CacheConfiguration<>();
+
+            cfg.setCacheMode(CacheMode.PARTITIONED);
+            cfg.setName(CACHE_NAME);
+
+            try (IgniteCache<Integer, String> cache = ignite.createCache(cfg)) 
{
+                // Enable asynchronous mode.
+                IgniteCache<Integer, String> asyncCache = cache.withAsync();
+
+                Collection<IgniteFuture<?>> futs = new ArrayList<>();
+
+                // Execute several puts asynchronously.
+                for (int i = 0; i < 10; i++) {
+                    asyncCache.put(i, String.valueOf(i));
+
+                    futs.add(asyncCache.future());
+                }
+
+                // Wait for completion of all futures.
+                futs.forEach(IgniteFuture::get);
+
+                // Execute get operation asynchronously.
+                asyncCache.get(1);
+
+                // Asynchronously wait for result.
+                asyncCache.<String>future().listen(fut ->
+                    System.out.println("Get operation completed [value=" + 
fut.get() + ']'));
+            }
+        }
+    }
+}

Reply via email to