ignite-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From sboi...@apache.org
Subject [07/32] incubator-ignite git commit: ignite-454 predicates becomes lambda
Date Fri, 20 Mar 2015 16:54:32 GMT
http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/28720c44/examples/src/main/java8/org/apache/ignite/examples8/computegrid/failover/ComputeFailoverNodeStartup.java
----------------------------------------------------------------------
diff --git a/examples/src/main/java8/org/apache/ignite/examples8/computegrid/failover/ComputeFailoverNodeStartup.java b/examples/src/main/java8/org/apache/ignite/examples8/computegrid/failover/ComputeFailoverNodeStartup.java
new file mode 100644
index 0000000..4462c8e
--- /dev/null
+++ b/examples/src/main/java8/org/apache/ignite/examples8/computegrid/failover/ComputeFailoverNodeStartup.java
@@ -0,0 +1,77 @@
+/*
+ * 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.examples8.computegrid.failover;
+
+import org.apache.ignite.*;
+import org.apache.ignite.configuration.*;
+import org.apache.ignite.spi.checkpoint.sharedfs.*;
+import org.apache.ignite.spi.discovery.tcp.*;
+import org.apache.ignite.spi.discovery.tcp.ipfinder.vm.*;
+
+import java.util.*;
+
+/**
+ * Starts up an empty node with checkpoint-enabled configuration.
+ * <p>
+ * The difference is that running this class from IDE adds all example classes to classpath
+ * but running from command line doesn't.
+ */
+public class ComputeFailoverNodeStartup {
+    /**
+     * Start up an empty node with specified configuration.
+     *
+     * @param args Command line arguments, none required.
+     * @throws org.apache.ignite.IgniteException If example execution failed.
+     */
+    public static void main(String[] args) throws IgniteException {
+        Ignition.start(configuration());
+    }
+
+    /**
+     * Create Ignite configuration with configured checkpoints.
+     *
+     * @return Ignite configuration.
+     * @throws org.apache.ignite.IgniteException If configuration creation failed.
+     */
+    public static IgniteConfiguration configuration() throws IgniteException {
+        IgniteConfiguration cfg = new IgniteConfiguration();
+
+        cfg.setLocalHost("127.0.0.1");
+        cfg.setPeerClassLoadingEnabled(true);
+
+        // Configure checkpoint SPI.
+        SharedFsCheckpointSpi checkpointSpi = new SharedFsCheckpointSpi();
+
+        checkpointSpi.setDirectoryPaths(Collections.singletonList("work/checkpoint/sharedfs"));
+
+        cfg.setCheckpointSpi(checkpointSpi);
+
+        // Configure discovery SPI.
+        TcpDiscoverySpi discoSpi = new TcpDiscoverySpi();
+
+        TcpDiscoveryVmIpFinder ipFinder = new TcpDiscoveryVmIpFinder();
+
+        ipFinder.setAddresses(Arrays.asList("127.0.0.1:47500..47509"));
+
+        discoSpi.setIpFinder(ipFinder);
+
+        cfg.setDiscoverySpi(discoSpi);
+
+        return cfg;
+    }
+}

http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/28720c44/examples/src/main/java8/org/apache/ignite/examples8/computegrid/failover/package.html
----------------------------------------------------------------------
diff --git a/examples/src/main/java8/org/apache/ignite/examples8/computegrid/failover/package.html b/examples/src/main/java8/org/apache/ignite/examples8/computegrid/failover/package.html
new file mode 100644
index 0000000..55b2ad4
--- /dev/null
+++ b/examples/src/main/java8/org/apache/ignite/examples8/computegrid/failover/package.html
@@ -0,0 +1,24 @@
+<!--
+  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.
+-->
+
+<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
+<html>
+<body>
+    <!-- Package description. -->
+    Compute failover example.
+</body>
+</html>

http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/28720c44/examples/src/main/java8/org/apache/ignite/examples8/computegrid/montecarlo/Credit.java
----------------------------------------------------------------------
diff --git a/examples/src/main/java8/org/apache/ignite/examples8/computegrid/montecarlo/Credit.java b/examples/src/main/java8/org/apache/ignite/examples8/computegrid/montecarlo/Credit.java
new file mode 100644
index 0000000..77c0dfb
--- /dev/null
+++ b/examples/src/main/java8/org/apache/ignite/examples8/computegrid/montecarlo/Credit.java
@@ -0,0 +1,110 @@
+/*
+ * 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.examples8.computegrid.montecarlo;
+
+import java.io.*;
+
+/**
+ * This class provides a simple model for a credit contract (or a loan). It is basically
+ * defines as remaining crediting amount to date, credit remaining term, APR and annual
+ * probability on default. Although this model is simplified for the purpose
+ * of this example, it is close enough to emulate the real-life credit
+ * risk assessment application.
+ */
+public class Credit implements Serializable {
+    /** Remaining crediting amount. */
+    private final double remAmnt;
+
+    /** Remaining crediting remTerm. */
+    private final int remTerm;
+
+    /** Annual percentage rate (APR). */
+    private final double apr;
+
+    /** Expected annual probability of default (EaDF). */
+    private final double edf;
+
+    /**
+     * Creates new credit instance with given information.
+     *
+     * @param remAmnt Remained crediting amount.
+     * @param remTerm Remained crediting remTerm.
+     * @param apr Annual percentage rate (APR).
+     * @param edf Expected annual probability of default (EaDF).
+     */
+    public Credit(double remAmnt, int remTerm, double apr, double edf) {
+        this.remAmnt = remAmnt;
+        this.remTerm = remTerm;
+        this.apr = apr;
+        this.edf = edf;
+    }
+
+    /**
+     * Gets remained crediting amount.
+     *
+     * @return Remained amount of credit.
+     */
+    double getRemainingAmount() {
+        return remAmnt;
+    }
+
+    /**
+     * Gets remained crediting remTerm.
+     *
+     * @return Remained crediting remTerm in days.
+     */
+    int getRemainingTerm() {
+        return remTerm;
+    }
+
+    /**
+     * Gets annual percentage rate.
+     *
+     * @return Annual percentage rate in relative percents (percentage / 100).
+     */
+    double getAnnualRate() {
+        return apr;
+    }
+
+    /**
+     * Gets either credit probability of default for the given period of time
+     * if remaining term is less than crediting time or probability of default
+     * for whole remained crediting time.
+     *
+     * @param term Default term.
+     * @return Credit probability of default in relative percents
+     *     (percentage / 100).
+     */
+    double getDefaultProbability(int term) {
+        return 1 - Math.exp(Math.log(1 - edf) * Math.min(remTerm, term) / 365.0);
+    }
+
+    /** {@inheritDoc} */
+    @Override public String toString() {
+        StringBuilder buf = new StringBuilder();
+
+        buf.append(getClass().getName());
+        buf.append(" [remAmnt=").append(remAmnt);
+        buf.append(", remTerm=").append(remTerm);
+        buf.append(", apr=").append(apr);
+        buf.append(", edf=").append(edf);
+        buf.append(']');
+
+        return buf.toString();
+    }
+}

http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/28720c44/examples/src/main/java8/org/apache/ignite/examples8/computegrid/montecarlo/CreditRiskExample.java
----------------------------------------------------------------------
diff --git a/examples/src/main/java8/org/apache/ignite/examples8/computegrid/montecarlo/CreditRiskExample.java b/examples/src/main/java8/org/apache/ignite/examples8/computegrid/montecarlo/CreditRiskExample.java
new file mode 100644
index 0000000..d67d873
--- /dev/null
+++ b/examples/src/main/java8/org/apache/ignite/examples8/computegrid/montecarlo/CreditRiskExample.java
@@ -0,0 +1,147 @@
+/*
+ * 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.examples8.computegrid.montecarlo;
+
+import org.apache.ignite.*;
+import org.apache.ignite.lang.*;
+
+import java.util.*;
+
+/**
+ * Monte-Carlo example. Demonstrates distributed credit risk calculation.
+ * <p>
+ * Remote nodes should always be started with special configuration file which
+ * enables P2P class loading: {@code 'ignite.{sh|bat} examples/config/example-compute.xml'}.
+ * <p>
+ * Alternatively you can run {@link org.apache.ignite.examples8.ComputeNodeStartup} in another JVM which will start node
+ * with {@code examples/config/example-compute.xml} configuration.
+ */
+public final class CreditRiskExample {
+    /**
+     * Executes example.
+     *
+     * @param args Command line arguments, none required.
+     * @throws org.apache.ignite.IgniteException If example execution failed.
+     */
+    public static void main(String[] args) throws IgniteException {
+        try (Ignite ignite = Ignition.start("examples/config/example-compute.xml")) {
+            System.out.println();
+            System.out.println("Credit risk example started.");
+
+            // Create portfolio.
+            Credit[] portfolio = new Credit[5000];
+
+            Random rnd = new Random();
+
+            // Generate some test portfolio items.
+            for (int i = 0; i < portfolio.length; i++) {
+                portfolio[i] = new Credit(
+                    50000 * rnd.nextDouble(), // Credit amount.
+                    rnd.nextInt(1000), // Credit term in days.
+                    rnd.nextDouble() / 10, // APR.
+                    rnd.nextDouble() / 20 + 0.02 // EDF.
+                );
+            }
+
+            // Forecast horizon in days.
+            int horizon = 365;
+
+            // Number of Monte-Carlo iterations.
+            int iter = 10000;
+
+            // Percentile.
+            double percentile = 0.95;
+
+            // Mark the stopwatch.
+            long start = System.currentTimeMillis();
+
+            // Calculate credit risk and print it out.
+            // As you can see the ignite enabling is completely hidden from the caller
+            // and it is fully transparent to him. In fact, the caller is never directly
+            // aware if method was executed just locally or on the 100s of cluster nodes.
+            // Credit risk crdRisk is the minimal amount that creditor has to have
+            // available to cover possible defaults.
+
+            double crdRisk = ignite.compute().call(jobs(ignite.cluster().nodes().size(), portfolio, horizon, iter, percentile),
+                new IgniteReducer<Double, Double>() {
+                    /** Collected values sum. */
+                    private double sum;
+
+                    /** Collected values count. */
+                    private int cnt;
+
+                    /** {@inheritDoc} */
+                    @Override public synchronized boolean collect(Double e) {
+                        sum += e;
+                        cnt++;
+
+                        return true;
+                    }
+
+                    /** {@inheritDoc} */
+                    @Override public synchronized Double reduce() {
+                        return sum / cnt;
+                    }
+                });
+
+            System.out.println();
+            System.out.println("Credit risk [crdRisk=" + crdRisk + ", duration=" +
+                (System.currentTimeMillis() - start) + "ms]");
+        }
+        // We specifically don't do any error handling here to
+        // simplify the example. Real application may want to
+        // add error handling and application specific recovery.
+    }
+
+    /**
+     * Creates closures for calculating credit risks.
+     *
+     * @param clusterSize Size of the cluster.
+     * @param portfolio Portfolio.
+     * @param horizon Forecast horizon in days.
+     * @param iter Number of Monte-Carlo iterations.
+     * @param percentile Percentile.
+     * @return Collection of closures.
+     */
+    private static Collection<IgniteCallable<Double>> jobs(int clusterSize, final Credit[] portfolio,
+        final int horizon, int iter, final double percentile) {
+        // Number of iterations should be done by each node.
+        int iterPerNode = Math.round(iter / (float)clusterSize);
+
+        // Number of iterations for the last/the only node.
+        int lastNodeIter = iter - (clusterSize - 1) * iterPerNode;
+
+        Collection<IgniteCallable<Double>> clos = new ArrayList<>(clusterSize);
+
+        // Note that for the purpose of this example we perform a simple homogeneous
+        // (non weighted) split assuming that all computing resources in this split
+        // will be identical. In real life scenarios when heterogeneous environment
+        // is used a split that is weighted by, for example, CPU benchmarks of each
+        // node in the split will be more efficient. It is fairly easy addition and
+        // Ignite comes with convenient Spring-compatible benchmark that can be
+        // used for weighted splits.
+        for (int i = 0; i < clusterSize; i++) {
+            final int nodeIter = i == clusterSize - 1 ? lastNodeIter : iterPerNode;
+
+            clos.add(() -> new CreditRiskManager().calculateCreditRiskMonteCarlo(
+                portfolio, horizon, nodeIter, percentile));
+        }
+
+        return clos;
+    }
+}

http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/28720c44/examples/src/main/java8/org/apache/ignite/examples8/computegrid/montecarlo/CreditRiskManager.java
----------------------------------------------------------------------
diff --git a/examples/src/main/java8/org/apache/ignite/examples8/computegrid/montecarlo/CreditRiskManager.java b/examples/src/main/java8/org/apache/ignite/examples8/computegrid/montecarlo/CreditRiskManager.java
new file mode 100644
index 0000000..36fa07a
--- /dev/null
+++ b/examples/src/main/java8/org/apache/ignite/examples8/computegrid/montecarlo/CreditRiskManager.java
@@ -0,0 +1,143 @@
+/*
+ * 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.examples8.computegrid.montecarlo;
+
+import java.util.*;
+
+/**
+ * This class abstracts out the calculation of risk for a credit portfolio.
+ */
+@SuppressWarnings({"FloatingPointEquality"})
+public class CreditRiskManager {
+    /**
+     * Default randomizer with normal distribution.
+     * Note that since every JVM on the cluster will have its own random
+     * generator (independently initialized) the Monte-Carlo simulation
+     * will be slightly skewed when performed on the ignite cluster due to skewed
+     * normal distribution of the sub-jobs comparing to execution on the
+     * local node only with single random generator. Real-life applications
+     * may want to provide its own implementation of distributed random
+     * generator.
+     */
+    private static Random rndGen = new Random();
+
+    /**
+     * Calculates credit risk for a given credit portfolio. This calculation uses
+     * Monte-Carlo Simulation to produce risk value.
+     *
+     * @param portfolio Credit portfolio.
+     * @param horizon Forecast horizon (in days).
+     * @param num Number of Monte-Carlo iterations.
+     * @param percentile Cutoff level.
+     * @return Credit risk value, i.e. the minimal amount that creditor has to
+     *      have available to cover possible defaults.
+     */
+    public double calculateCreditRiskMonteCarlo(Credit[] portfolio, int horizon, int num, double percentile) {
+        System.out.println(">>> Calculating credit risk for portfolio [size=" + portfolio.length + ", horizon=" +
+            horizon + ", percentile=" + percentile + ", iterations=" + num + "] <<<");
+
+        long start = System.currentTimeMillis();
+
+        double[] losses = calculateLosses(portfolio, horizon, num);
+
+        Arrays.sort(losses);
+
+        double[] lossProbs = new double[losses.length];
+
+        // Count variational numbers.
+        // Every next one either has the same value or previous one plus probability of loss.
+        for (int i = 0; i < losses.length; i++)
+            if (i == 0)
+                // First time it's just a probability of first value.
+                lossProbs[i] = getLossProbability(losses, 0);
+            else if (losses[i] != losses[i - 1])
+                // Probability of this loss plus previous one.
+                lossProbs[i] = getLossProbability(losses, i) + lossProbs[i - 1];
+            else
+                // The same loss the same probability.
+                lossProbs[i] = lossProbs[i - 1];
+
+        // Count percentile.
+        double crdRisk = 0;
+
+        for (int i = 0; i < lossProbs.length; i++)
+            if (lossProbs[i] > percentile) {
+                crdRisk = losses[i - 1];
+
+                break;
+            }
+
+        System.out.println(">>> Finished calculating portfolio risk [risk=" + crdRisk +
+            ", time=" + (System.currentTimeMillis() - start) + "ms]");
+
+        return crdRisk;
+    }
+
+    /**
+     * Calculates losses for the given credit portfolio using Monte-Carlo Simulation.
+     * Simulates probability of default only.
+     *
+     * @param portfolio Credit portfolio.
+     * @param horizon Forecast horizon.
+     * @param num Number of Monte-Carlo iterations.
+     * @return Losses array simulated by Monte Carlo method.
+     */
+    private double[] calculateLosses(Credit[] portfolio, int horizon, int num) {
+        double[] losses = new double[num];
+
+        // Count losses using Monte-Carlo method. We generate random probability of default,
+        // if it exceeds certain credit default value we count losses - otherwise count income.
+        for (int i = 0; i < num; i++)
+            for (Credit crd : portfolio) {
+                int remDays = Math.min(crd.getRemainingTerm(), horizon);
+
+                if (rndGen.nextDouble() >= 1 - crd.getDefaultProbability(remDays))
+                    // (1 + 'r' * min(H, W) / 365) * S.
+                    // Where W is a horizon, H is a remaining crediting term, 'r' is an annual credit rate,
+                    // S is a remaining credit amount.
+                    losses[i] += (1 + crd.getAnnualRate() * Math.min(horizon, crd.getRemainingTerm()) / 365)
+                        * crd.getRemainingAmount();
+                else
+                    // - 'r' * min(H,W) / 365 * S
+                    // Where W is a horizon, H is a remaining crediting term, 'r' is a annual credit rate,
+                    // S is a remaining credit amount.
+                    losses[i] -= crd.getAnnualRate() * Math.min(horizon, crd.getRemainingTerm()) / 365 *
+                        crd.getRemainingAmount();
+            }
+
+        return losses;
+    }
+
+    /**
+     * Calculates probability of certain loss in array of losses.
+     *
+     * @param losses Array of losses.
+     * @param i Index of certain loss in array.
+     * @return Probability of loss with given index.
+     */
+    private double getLossProbability(double[] losses, int i) {
+        double cnt = 0;
+        double loss = losses[i];
+
+        for (double tmp : losses)
+            if (loss == tmp)
+                cnt++;
+
+        return cnt / losses.length;
+    }
+}

http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/28720c44/examples/src/main/java8/org/apache/ignite/examples8/computegrid/montecarlo/package.html
----------------------------------------------------------------------
diff --git a/examples/src/main/java8/org/apache/ignite/examples8/computegrid/montecarlo/package.html b/examples/src/main/java8/org/apache/ignite/examples8/computegrid/montecarlo/package.html
new file mode 100644
index 0000000..4ec64f2
--- /dev/null
+++ b/examples/src/main/java8/org/apache/ignite/examples8/computegrid/montecarlo/package.html
@@ -0,0 +1,24 @@
+<!--
+  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.
+-->
+
+<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
+<html>
+<body>
+    <!-- Package description. -->
+    Monte-Carlo simulation example.
+</body>
+</html>

http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/28720c44/examples/src/main/java8/org/apache/ignite/examples8/computegrid/package.html
----------------------------------------------------------------------
diff --git a/examples/src/main/java8/org/apache/ignite/examples8/computegrid/package.html b/examples/src/main/java8/org/apache/ignite/examples8/computegrid/package.html
new file mode 100644
index 0000000..c17cd4e
--- /dev/null
+++ b/examples/src/main/java8/org/apache/ignite/examples8/computegrid/package.html
@@ -0,0 +1,24 @@
+<!--
+  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.
+-->
+
+<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
+<html>
+<body>
+    <!-- Package description. -->
+    Basic examples for computational ignite functionality.
+</body>
+</html>

http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/28720c44/examples/src/main/java8/org/apache/ignite/examples8/datagrid/CacheAffinityExample.java
----------------------------------------------------------------------
diff --git a/examples/src/main/java8/org/apache/ignite/examples8/datagrid/CacheAffinityExample.java b/examples/src/main/java8/org/apache/ignite/examples8/datagrid/CacheAffinityExample.java
new file mode 100644
index 0000000..b7e55e2
--- /dev/null
+++ b/examples/src/main/java8/org/apache/ignite/examples8/datagrid/CacheAffinityExample.java
@@ -0,0 +1,129 @@
+/*
+ * 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.examples8.datagrid;
+
+import org.apache.ignite.*;
+import org.apache.ignite.cluster.*;
+import org.apache.ignite.lang.*;
+
+import java.util.*;
+
+/**
+ * This example demonstrates the simplest code that populates the distributed cache
+ * and co-locates simple closure execution with each key. The goal of this particular
+ * example is to provide the simplest code example of this logic.
+ * <p>
+ * Remote nodes should always be started with special configuration file which
+ * enables P2P class loading: {@code 'ignite.{sh|bat} examples/config/example-cache.xml'}.
+ * <p>
+ * Alternatively you can run {@link org.apache.ignite.examples8.datagrid.CacheNodeStartup} in another JVM which will
+ * start node with {@code examples/config/example-cache.xml} configuration.
+ */
+public final class CacheAffinityExample {
+    /** Cache name. */
+    private static final String CACHE_NAME = "partitioned";
+
+    /** Number of keys. */
+    private static final int KEY_CNT = 20;
+
+    /**
+     * Executes example.
+     *
+     * @param args Command line arguments, none required.
+     * @throws org.apache.ignite.IgniteException If example execution failed.
+     */
+    public static void main(String[] args) throws IgniteException {
+        try (Ignite ignite = Ignition.start("examples/config/example-cache.xml")) {
+            System.out.println();
+            System.out.println(">>> Cache affinity example started.");
+
+            IgniteCache<Integer, String> cache = ignite.jcache(CACHE_NAME);
+
+            // Clean up caches on all nodes before run.
+            cache.clear();
+
+            for (int i = 0; i < KEY_CNT; i++)
+                cache.put(i, Integer.toString(i));
+
+            // Co-locates jobs with data using IgniteCompute.affinityRun(...) method.
+            visitUsingAffinityRun();
+
+            // Co-locates jobs with data using IgniteCluster.mapKeysToNodes(...) method.
+            visitUsingMapKeysToNodes();
+        }
+    }
+
+    /**
+     * Collocates jobs with keys they need to work on using
+     * {@link org.apache.ignite.IgniteCompute#affinityRun(String, Object, org.apache.ignite.lang.IgniteRunnable)} method.
+     */
+    private static void visitUsingAffinityRun() {
+        Ignite ignite = Ignition.ignite();
+
+        final IgniteCache<Integer, String> cache = ignite.jcache(CACHE_NAME);
+
+        for (int i = 0; i < KEY_CNT; i++) {
+            final int key = i;
+
+            // This runnable will execute on the remote node where
+            // data with the given key is located. Since it will be co-located
+            // we can use local 'peek' operation safely.
+            ignite.compute().affinityRun(CACHE_NAME, key, () -> {
+                // Peek is a local memory lookup, however, value should never be 'null'
+                // as we are co-located with node that has a given key.
+                System.out.println("Co-located using affinityRun [key= " + key + ", value=" + cache.localPeek(key) + ']');
+            });
+        }
+    }
+
+    /**
+     * Collocates jobs with keys they need to work on using {@link org.apache.ignite.IgniteCluster#mapKeysToNodes(String, java.util.Collection)}
+     * method. The difference from {@code affinityRun(...)} method is that here we process multiple keys
+     * in a single job.
+     */
+    private static void visitUsingMapKeysToNodes() {
+        final Ignite ignite = Ignition.ignite();
+
+        Collection<Integer> keys = new ArrayList<>(KEY_CNT);
+
+        for (int i = 0; i < KEY_CNT; i++)
+            keys.add(i);
+
+        // Map all keys to nodes.
+        Map<ClusterNode, Collection<Integer>> mappings = ignite.cluster().mapKeysToNodes(CACHE_NAME, keys);
+
+        for (Map.Entry<ClusterNode, Collection<Integer>> mapping : mappings.entrySet()) {
+            ClusterNode node = mapping.getKey();
+
+            final Collection<Integer> mappedKeys = mapping.getValue();
+
+            if (node != null) {
+                // Bring computations to the nodes where the data resides (i.e. collocation).
+                ignite.compute(ignite.cluster().forNode(node)).run(() -> {
+                    IgniteCache<Integer, String> cache = ignite.jcache(CACHE_NAME);
+
+                    // Peek is a local memory lookup, however, value should never be 'null'
+                    // as we are co-located with node that has a given key.
+                    for (Integer key : mappedKeys)
+                        System.out.println("Co-located using mapKeysToNodes [key= " + key +
+                            ", value=" + cache.localPeek(key) + ']');
+                });
+            }
+        }
+    }
+}

http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/28720c44/examples/src/main/java8/org/apache/ignite/examples8/datagrid/CacheApiExample.java
----------------------------------------------------------------------
diff --git a/examples/src/main/java8/org/apache/ignite/examples8/datagrid/CacheApiExample.java b/examples/src/main/java8/org/apache/ignite/examples8/datagrid/CacheApiExample.java
new file mode 100644
index 0000000..9f4fb2a
--- /dev/null
+++ b/examples/src/main/java8/org/apache/ignite/examples8/datagrid/CacheApiExample.java
@@ -0,0 +1,121 @@
+/*
+ * 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.examples8.datagrid;
+
+import org.apache.ignite.*;
+import org.apache.ignite.lang.*;
+
+import javax.cache.processor.*;
+
+/**
+ * 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-cache.xml'}.
+ * <p>
+ * Alternatively you can run {@link org.apache.ignite.examples8.datagrid.CacheNodeStartup} in another JVM which will
+ * start node with {@code examples/config/example-cache.xml} configuration.
+ */
+public class CacheApiExample {
+    /** Cache name. */
+    private static final String CACHE_NAME = "partitioned";
+
+    /**
+     * Executes example.
+     *
+     * @param args Command line arguments, none required.
+     * @throws org.apache.ignite.IgniteException If example execution failed.
+     */
+    public static void main(String[] args) throws IgniteException {
+        try (Ignite ignite = Ignition.start("examples/config/example-cache.xml")) {
+            System.out.println();
+            System.out.println(">>> Cache API example started.");
+
+            // Clean up caches on all nodes before run.
+            ignite.jcache(CACHE_NAME).clear();
+
+            // Demonstrate atomic map operations.
+            atomicMapOperations();
+        }
+    }
+
+    /**
+     * Demonstrates cache operations similar to {@link java.util.concurrent.ConcurrentMap} API. Note that
+     * cache API is a lot richer than the JDK {@link java.util.concurrent.ConcurrentMap}.
+     *
+     * @throws org.apache.ignite.IgniteException If failed.
+     */
+    private static void atomicMapOperations() throws IgniteException {
+        System.out.println();
+        System.out.println(">>> Cache atomic map operation examples.");
+
+        final IgniteCache<Integer, String> cache = Ignition.ignite().jcache(CACHE_NAME);
+
+        // Put and return previous value.
+        String v = cache.getAndPut(1, "1");
+        assert v == null;
+
+        // Put and do not return previous value (all methods ending with 'x' return boolean).
+        // 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");
+        assert b1 && !b2;
+
+        // Invoke - assign new value based on previous value.
+        cache.put(6, "6");
+        cache.invoke(6, (entry, args) -> {
+            String v1 = entry.getValue();
+
+            entry.setValue(v1 + "6"); // Set new value based on previous value.
+
+            return null;
+        });
+
+        // Replace.
+        cache.put(7, "7");
+        b1 = cache.replace(7, "7", "77");
+        b2 = cache.replace(7, "7", "777");
+        assert b1 & !b2;
+    }
+}

http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/28720c44/examples/src/main/java8/org/apache/ignite/examples8/datagrid/CacheContinuousQueryExample.java
----------------------------------------------------------------------
diff --git a/examples/src/main/java8/org/apache/ignite/examples8/datagrid/CacheContinuousQueryExample.java b/examples/src/main/java8/org/apache/ignite/examples8/datagrid/CacheContinuousQueryExample.java
new file mode 100644
index 0000000..2221060
--- /dev/null
+++ b/examples/src/main/java8/org/apache/ignite/examples8/datagrid/CacheContinuousQueryExample.java
@@ -0,0 +1,92 @@
+/*
+ * 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.examples8.datagrid;
+
+import org.apache.ignite.*;
+import org.apache.ignite.cache.query.*;
+import org.apache.ignite.lang.*;
+
+import javax.cache.*;
+import javax.cache.event.*;
+
+/**
+ * This examples demonstrates continuous query API.
+ * <p>
+ * Remote nodes should always be started with special configuration file which
+ * enables P2P class loading: {@code 'ignite.{sh|bat} examples/config/example-cache.xml'}.
+ * <p>
+ * Alternatively you can run {@link org.apache.ignite.examples8.datagrid.CacheNodeStartup} in another JVM which will
+ * start node with {@code examples/config/example-cache.xml} configuration.
+ */
+public class CacheContinuousQueryExample {
+    /** Cache name. */
+    private static final String CACHE_NAME = "partitioned";
+
+    /**
+     * Executes example.
+     *
+     * @param args Command line arguments, none required.
+     * @throws Exception If example execution failed.
+     */
+    public static void main(String[] args) throws Exception {
+        try (Ignite ignite = Ignition.start("examples/config/example-cache.xml")) {
+            System.out.println();
+            System.out.println(">>> Cache continuous query example started.");
+
+            IgniteCache<Integer, String> cache = ignite.jcache(CACHE_NAME);
+
+            // Clean up caches on all nodes before run.
+            cache.clear();
+
+            int keyCnt = 20;
+
+            // These entries will be queried by initial predicate.
+            for (int i = 0; i < keyCnt; i++)
+                cache.put(i, Integer.toString(i));
+
+            // Create new continuous query.
+            ContinuousQuery<Integer, String> qry = new ContinuousQuery<>();
+
+            qry.setInitialQuery(new ScanQuery<Integer, String>((key, val) -> key > 10));
+
+            // Callback that is called locally when update notifications are received.
+            qry.setLocalListener(evts -> {
+                for (CacheEntryEvent<? extends Integer, ? extends String> e : evts)
+                    System.out.println("Updated entry [key=" + e.getKey() + ", val=" + e.getValue() + ']');
+            });
+
+            // This filter will be evaluated remotely on all nodes.
+            // Entry that pass this filter will be sent to the caller.
+            qry.setRemoteFilter(e -> e.getKey() > 10);
+
+            // Execute query.
+            try (QueryCursor<Cache.Entry<Integer, String>> cur = cache.query(qry)) {
+                // Iterate through existing data.
+                for (Cache.Entry<Integer, String> e : cur)
+                    System.out.println("Queried existing entry [key=" + e.getKey() + ", val=" + e.getValue() + ']');
+
+                // Add a few more keys and watch more query notifications.
+                for (int i = keyCnt; i < keyCnt + 10; i++)
+                    cache.put(i, Integer.toString(i));
+
+                // Wait for a while while callback is notified about remaining puts.
+                Thread.sleep(2000);
+            }
+        }
+    }
+}

http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/28720c44/examples/src/main/java8/org/apache/ignite/examples8/datagrid/CacheDataStreamerExample.java
----------------------------------------------------------------------
diff --git a/examples/src/main/java8/org/apache/ignite/examples8/datagrid/CacheDataStreamerExample.java b/examples/src/main/java8/org/apache/ignite/examples8/datagrid/CacheDataStreamerExample.java
new file mode 100644
index 0000000..4907c82
--- /dev/null
+++ b/examples/src/main/java8/org/apache/ignite/examples8/datagrid/CacheDataStreamerExample.java
@@ -0,0 +1,85 @@
+/*
+ * 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.examples8.datagrid;
+
+import org.apache.ignite.*;
+import org.apache.ignite.examples8.*;
+
+/**
+ * Demonstrates how cache can be populated with data utilizing {@link org.apache.ignite.IgniteDataStreamer} API.
+ * {@link org.apache.ignite.IgniteDataStreamer} is a lot more efficient to use than standard
+ * {@code put(...)} operation as it properly buffers cache requests
+ * together and properly manages load on remote nodes.
+ * <p>
+ * Remote nodes should always be started with special configuration file which
+ * enables P2P class loading: {@code 'ignite.{sh|bat} examples/config/example-cache.xml'}.
+ * <p>
+ * Alternatively you can run {@link org.apache.ignite.examples8.datagrid.CacheNodeStartup} in another JVM which will
+ * start node with {@code examples/config/example-cache.xml} configuration.
+ */
+public class CacheDataStreamerExample {
+    /** Cache name. */
+    private static final String CACHE_NAME = "partitioned";
+
+    /** Number of entries to load. */
+    private static final int ENTRY_COUNT = 500000;
+
+    /** Heap size required to run this example. */
+    public static final int MIN_MEMORY = 512 * 1024 * 1024;
+
+    /**
+     * Executes example.
+     *
+     * @param args Command line arguments, none required.
+     * @throws org.apache.ignite.IgniteException If example execution failed.
+     */
+    public static void main(String[] args) throws IgniteException {
+        ExamplesUtils.checkMinMemory(MIN_MEMORY);
+
+        try (Ignite ignite = Ignition.start("examples/config/example-cache.xml")) {
+            System.out.println();
+            System.out.println(">>> Cache data streamer example started.");
+
+            // Clean up caches on all nodes before run.
+            ignite.jcache(CACHE_NAME).clear();
+
+            System.out.println();
+            System.out.println(">>> Cache clear finished.");
+
+            long start = System.currentTimeMillis();
+
+            try (IgniteDataStreamer<Integer, String> stmr = ignite.dataStreamer(CACHE_NAME)) {
+                // Configure loader.
+                stmr.perNodeBufferSize(1024);
+                stmr.perNodeParallelOperations(8);
+
+                for (int i = 0; i < ENTRY_COUNT; i++) {
+                    stmr.addData(i, Integer.toString(i));
+
+                    // Print out progress while loading cache.
+                    if (i > 0 && i % 10000 == 0)
+                        System.out.println("Loaded " + i + " keys.");
+                }
+            }
+
+            long end = System.currentTimeMillis();
+
+            System.out.println(">>> Loaded " + ENTRY_COUNT + " keys in " + (end - start) + "ms.");
+        }
+    }
+}

http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/28720c44/examples/src/main/java8/org/apache/ignite/examples8/datagrid/CacheEventsExample.java
----------------------------------------------------------------------
diff --git a/examples/src/main/java8/org/apache/ignite/examples8/datagrid/CacheEventsExample.java b/examples/src/main/java8/org/apache/ignite/examples8/datagrid/CacheEventsExample.java
new file mode 100644
index 0000000..3218b33
--- /dev/null
+++ b/examples/src/main/java8/org/apache/ignite/examples8/datagrid/CacheEventsExample.java
@@ -0,0 +1,90 @@
+/*
+ * 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.examples8.datagrid;
+
+import org.apache.ignite.*;
+import org.apache.ignite.events.*;
+import org.apache.ignite.lang.*;
+
+import java.util.*;
+
+import static org.apache.ignite.events.EventType.*;
+
+/**
+ * This examples demonstrates events API. Note that ignite events are disabled by default and
+ * must be specifically enabled, just like in {@code examples/config/example-cache.xml} file.
+ * <p>
+ * Remote nodes should always be started with special configuration file which
+ * enables P2P class loading: {@code 'ignite.{sh|bat} examples/config/example-cache.xml'}.
+ * <p>
+ * Alternatively you can run {@link org.apache.ignite.examples8.datagrid.CacheNodeStartup} in another JVM which will
+ * start node with {@code examples/config/example-cache.xml} configuration.
+ */
+public class CacheEventsExample {
+    /** Cache name. */
+    private static final String CACHE_NAME = "partitioned";
+
+    /**
+     * Executes example.
+     *
+     * @param args Command line arguments, none required.
+     * @throws org.apache.ignite.IgniteException If example execution failed.
+     */
+    public static void main(String[] args) throws IgniteException, InterruptedException {
+        try (Ignite ignite = Ignition.start("examples/config/example-cache.xml")) {
+            System.out.println();
+            System.out.println(">>> Cache events example started.");
+
+            final IgniteCache<Integer, String> cache = ignite.jcache(CACHE_NAME);
+
+            // Clean up caches on all nodes before run.
+            cache.clear();
+
+            // This optional local callback is called for each event notification
+            // that passed remote predicate listener.
+            IgniteBiPredicate<UUID, CacheEvent> locLsnr = (uuid, evt) -> {
+                System.out.println("Received event [evt=" + evt.name() + ", key=" + evt.key() +
+                    ", oldVal=" + evt.oldValue() + ", newVal=" + evt.newValue());
+
+                return true; // Continue listening.
+            };
+
+            // 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 = 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-cache.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);
+        }
+    }
+}

http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/28720c44/examples/src/main/java8/org/apache/ignite/examples8/datagrid/CacheNodeStartup.java
----------------------------------------------------------------------
diff --git a/examples/src/main/java8/org/apache/ignite/examples8/datagrid/CacheNodeStartup.java b/examples/src/main/java8/org/apache/ignite/examples8/datagrid/CacheNodeStartup.java
new file mode 100644
index 0000000..bb9e638
--- /dev/null
+++ b/examples/src/main/java8/org/apache/ignite/examples8/datagrid/CacheNodeStartup.java
@@ -0,0 +1,35 @@
+/*
+ * 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.examples8.datagrid;
+
+import org.apache.ignite.*;
+
+/**
+ * Starts up an empty node with example cache configuration.
+ */
+public class CacheNodeStartup {
+    /**
+     * Start up an empty node with specified cache configuration.
+     *
+     * @param args Command line arguments, none required.
+     * @throws org.apache.ignite.IgniteException If example execution failed.
+     */
+    public static void main(String[] args) throws IgniteException {
+        Ignition.start("examples/config/example-cache.xml");
+    }
+}

http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/28720c44/examples/src/main/java8/org/apache/ignite/examples8/datagrid/CachePopularNumbersExample.java
----------------------------------------------------------------------
diff --git a/examples/src/main/java8/org/apache/ignite/examples8/datagrid/CachePopularNumbersExample.java b/examples/src/main/java8/org/apache/ignite/examples8/datagrid/CachePopularNumbersExample.java
new file mode 100644
index 0000000..6c37c57
--- /dev/null
+++ b/examples/src/main/java8/org/apache/ignite/examples8/datagrid/CachePopularNumbersExample.java
@@ -0,0 +1,159 @@
+/*
+ * 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.examples8.datagrid;
+
+import org.apache.ignite.*;
+import org.apache.ignite.cluster.*;
+
+import javax.cache.processor.*;
+import java.util.*;
+
+import static org.apache.ignite.cache.query.Query.*;
+
+/**
+ * Real time popular numbers counter.
+ * <p>
+ * Remote nodes should always be started with special configuration file which
+ * enables P2P class loading: {@code 'ignite.{sh|bat} examples/config/example-cache.xml'}.
+ * <p>
+ * Alternatively you can run {@link org.apache.ignite.examples8.datagrid.CacheNodeStartup} in another JVM which will
+ * start node with {@code examples/config/example-cache.xml} configuration.
+ */
+public class CachePopularNumbersExample {
+    /** Cache name. */
+    private static final String CACHE_NAME = "partitioned";
+
+    /** Count of most popular numbers to retrieve from cluster. */
+    private static final int POPULAR_NUMBERS_CNT = 10;
+
+    /** Random number generator. */
+    private static final Random RAND = new Random();
+
+    /** Range within which to generate numbers. */
+    private static final int RANGE = 1000;
+
+    /** Count of total numbers to generate. */
+    private static final int CNT = 1000000;
+
+    /**
+     * Executes example.
+     *
+     * @param args Command line arguments, none required.
+     * @throws org.apache.ignite.IgniteException If example execution failed.
+     */
+    public static void main(String[] args) throws IgniteException {
+        Timer popularNumbersQryTimer = new Timer("numbers-query-worker");
+
+        try (Ignite ignite = Ignition.start("examples/config/example-cache.xml")) {
+            System.out.println();
+            System.out.println(">>> Cache popular numbers example started.");
+
+            // Clean up caches on all nodes before run.
+            ignite.jcache(CACHE_NAME).clear();
+
+            ClusterGroup prj = ignite.cluster().forCacheNodes(CACHE_NAME);
+
+            if (prj.nodes().isEmpty()) {
+                System.out.println("Ignite does not have cache configured: " + CACHE_NAME);
+
+                return;
+            }
+
+            TimerTask task = scheduleQuery(ignite, popularNumbersQryTimer, POPULAR_NUMBERS_CNT);
+
+            streamData(ignite);
+
+            // Force one more run to get final counts.
+            task.run();
+
+            popularNumbersQryTimer.cancel();
+        }
+    }
+
+    /**
+     * Populates cache in real time with numbers and keeps count for every number.
+     *
+     * @param ignite Ignite.
+     * @throws org.apache.ignite.IgniteException If failed.
+     */
+    private static void streamData(final Ignite ignite) throws IgniteException {
+        try (IgniteDataStreamer<Integer, Long> stmr = ignite.dataStreamer(CACHE_NAME)) {
+            // Set larger per-node buffer size since our state is relatively small.
+            stmr.perNodeBufferSize(2048);
+
+            stmr.updater(new IncrementingUpdater());
+
+            for (int i = 0; i < CNT; i++)
+                stmr.addData(RAND.nextInt(RANGE), 1L);
+        }
+    }
+
+    /**
+     * Schedules our popular numbers query to run every 3 seconds.
+     *
+     * @param ignite Ignite.
+     * @param timer Timer.
+     * @param cnt Number of popular numbers to return.
+     * @return Scheduled task.
+     */
+    private static TimerTask scheduleQuery(final Ignite ignite, Timer timer, final int cnt) {
+        TimerTask task = new TimerTask() {
+            @Override public void run() {
+                // Get reference to cache.
+                IgniteCache<Integer, Long> cache = ignite.jcache(CACHE_NAME);
+
+                try {
+                    List<List<?>> results = new ArrayList<>(cache.queryFields(
+                        sql("select _key, _val from Long order by _val desc, _key limit ?").setArgs(cnt)).getAll());
+
+                    for (List<?> res : results)
+                        System.out.println(res.get(0) + "=" + res.get(1));
+
+                    System.out.println("----------------");
+                }
+                catch (Exception e) {
+                    e.printStackTrace();
+                }
+            }
+        };
+
+        timer.schedule(task, 3000, 3000);
+
+        return task;
+    }
+
+    /**
+     * Increments value for key.
+     */
+    private static class IncrementingUpdater implements IgniteDataStreamer.Updater<Integer, Long> {
+        /** Process entries to increase value by entry key. */
+        private static final EntryProcessor<Integer, Long, Void> INC = (e, args) -> {
+            Long val = e.getValue();
+
+            e.setValue(val == null ? 1L : val + 1);
+
+            return null;
+        };
+
+        /** {@inheritDoc} */
+        @Override public void update(IgniteCache<Integer, Long> cache, Collection<Map.Entry<Integer, Long>> entries) {
+            for (Map.Entry<Integer, Long> entry : entries)
+                cache.invoke(entry.getKey(), INC);
+        }
+    }
+}

http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/28720c44/examples/src/main/java8/org/apache/ignite/examples8/datagrid/CachePutGetExample.java
----------------------------------------------------------------------
diff --git a/examples/src/main/java8/org/apache/ignite/examples8/datagrid/CachePutGetExample.java b/examples/src/main/java8/org/apache/ignite/examples8/datagrid/CachePutGetExample.java
new file mode 100644
index 0000000..115dcfc
--- /dev/null
+++ b/examples/src/main/java8/org/apache/ignite/examples8/datagrid/CachePutGetExample.java
@@ -0,0 +1,113 @@
+/*
+ * 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.examples8.datagrid;
+
+import org.apache.ignite.*;
+
+import java.util.*;
+
+/**
+ * This example demonstrates very basic operations on cache, such as 'put' and 'get'.
+ * <p>
+ * Remote nodes should always be started with special configuration file which
+ * enables P2P class loading: {@code 'ignite.{sh|bat} examples/config/example-cache.xml'}.
+ * <p>
+ * Alternatively you can run {@link org.apache.ignite.examples8.datagrid.CacheNodeStartup} in another JVM which will
+ * start node with {@code examples/config/example-cache.xml} configuration.
+ */
+public class CachePutGetExample {
+    /** Cache name. */
+    private static final String CACHE_NAME = "partitioned";
+
+    /**
+     * Executes example.
+     *
+     * @param args Command line arguments, none required.
+     * @throws org.apache.ignite.IgniteException If example execution failed.
+     */
+    public static void main(String[] args) throws IgniteException {
+        try (Ignite ignite = Ignition.start("examples/config/example-cache.xml")) {
+            // Clean up caches on all nodes before run.
+            ignite.jcache(CACHE_NAME).clear();
+
+            // Individual puts and gets.
+            putGet();
+
+            // Bulk puts and gets.
+            putAllGetAll();
+        }
+    }
+
+    /**
+     * Execute individual puts and gets.
+     *
+     * @throws org.apache.ignite.IgniteException If failed.
+     */
+    private static void putGet() throws IgniteException {
+        System.out.println();
+        System.out.println(">>> Cache put-get example started.");
+
+        Ignite ignite = Ignition.ignite();
+
+        final IgniteCache<Integer, String> cache = ignite.jcache(CACHE_NAME);
+
+        final int keyCnt = 20;
+
+        // Store keys in cache.
+        for (int i = 0; i < keyCnt; i++)
+            cache.put(i, Integer.toString(i));
+
+        System.out.println(">>> Stored values in cache.");
+
+        for (int i = 0; i < keyCnt; i++)
+            System.out.println("Got [key=" + i + ", val=" + cache.get(i) + ']');
+    }
+
+    /**
+     * Execute bulk {@code putAll(...)} and {@code getAll(...)} operations.
+     *
+     * @throws org.apache.ignite.IgniteException If failed.
+     */
+    private static void putAllGetAll() throws IgniteException {
+        System.out.println();
+        System.out.println(">>> Starting putAll-getAll example.");
+
+        Ignite ignite = Ignition.ignite();
+
+        final IgniteCache<Integer, String> cache = ignite.jcache(CACHE_NAME);
+
+        final int keyCnt = 20;
+
+        // Create batch.
+        Map<Integer, String> batch = new HashMap<>();
+
+        for (int i = 0; i < keyCnt; i++)
+            batch.put(i, "bulk-" + Integer.toString(i));
+
+        // Bulk-store entries in cache.
+        cache.putAll(batch);
+
+        System.out.println(">>> Bulk-stored values in cache.");
+
+        // Bulk-get values from cache.
+        Map<Integer, String> vals = cache.getAll(batch.keySet());
+
+        for (Map.Entry<Integer, String> e : vals.entrySet())
+            System.out.println("Got entry [key=" + e.getKey() + ", val=" + e.getValue() + ']');
+    }
+}

http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/28720c44/examples/src/main/java8/org/apache/ignite/examples8/datagrid/CacheQueryExample.java
----------------------------------------------------------------------
diff --git a/examples/src/main/java8/org/apache/ignite/examples8/datagrid/CacheQueryExample.java b/examples/src/main/java8/org/apache/ignite/examples8/datagrid/CacheQueryExample.java
new file mode 100644
index 0000000..6c4145a
--- /dev/null
+++ b/examples/src/main/java8/org/apache/ignite/examples8/datagrid/CacheQueryExample.java
@@ -0,0 +1,372 @@
+/*
+ * 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.examples8.datagrid;
+
+import org.apache.ignite.*;
+import org.apache.ignite.cache.affinity.*;
+import org.apache.ignite.cache.query.*;
+import org.apache.ignite.cache.query.annotations.*;
+
+import javax.cache.*;
+import java.io.*;
+import java.util.*;
+
+import static org.apache.ignite.cache.query.Query.*;
+
+/**
+ * Cache queries example. This example demonstrates SQL, TEXT, and FULL SCAN
+ * queries over cache.
+ * <p>
+ * Example also demonstrates usage of fields queries that return only required
+ * fields instead of whole key-value pairs. When fields queries are distributed
+ * across several nodes, they may not work as expected. Keep in mind following
+ * limitations (not applied if data is queried from one node only):
+ * <ul>
+ *     <li>
+ *         {@code Group by} and {@code sort by} statements are applied separately
+ *         on each node, so result set will likely be incorrectly grouped or sorted
+ *         after results from multiple remote nodes are grouped together.
+ *     </li>
+ *     <li>
+ *         Aggregation functions like {@code sum}, {@code max}, {@code avg}, etc.
+ *         are also applied on each node. Therefore you will get several results
+ *         containing aggregated values, one for each node.
+ *     </li>
+ *     <li>
+ *         Joins will work correctly only if joined objects are stored in
+ *         collocated mode. Refer to {@link org.apache.ignite.cache.affinity.CacheAffinityKey} javadoc for more details.
+ *     </li>
+ *     <li>
+ *         Note that if you created query on to replicated cache, all data will
+ *         be queried only on one node, not depending on what caches participate in
+ *         the query (some data from partitioned cache can be lost). And visa versa,
+ *         if you created it on partitioned cache, data from replicated caches
+ *         will be duplicated.
+ *     </li>
+ * </ul>
+ * <p>
+ * Remote nodes should always be started with special configuration file which
+ * enables P2P class loading: {@code 'ignite.{sh|bat} examples/config/example-cache.xml'}.
+ * <p>
+ * Alternatively you can run {@link org.apache.ignite.examples8.datagrid.CacheNodeStartup} in another JVM which will
+ * start node with {@code examples/config/example-cache.xml} configuration.
+ */
+public class CacheQueryExample {
+    /** Cache name. */
+    private static final String CACHE_NAME = "partitioned";
+
+    /**
+     * Executes example.
+     *
+     * @param args Command line arguments, none required.
+     * @throws Exception If example execution failed.
+     */
+    public static void main(String[] args) throws Exception {
+        try (Ignite ignite = Ignition.start("examples/config/example-cache.xml")) {
+            System.out.println();
+            System.out.println(">>> Cache query example started.");
+
+            // Clean up caches on all nodes before run.
+            ignite.jcache(CACHE_NAME).removeAll();
+
+            // Populate cache.
+            initialize();
+
+            // Example for SQL-based querying employees based on salary ranges.
+            sqlQuery();
+
+            // Example for SQL-based querying employees for a given organization (includes SQL join).
+            sqlQueryWithJoin();
+
+            // Example for TEXT-based querying for a given string in peoples resumes.
+            textQuery();
+
+            // Example for SQL-based fields queries that return only required
+            // fields instead of whole key-value pairs.
+            sqlFieldsQuery();
+
+            // Example for SQL-based fields queries that uses joins.
+            sqlFieldsQueryWithJoin();
+
+            print("Cache query example finished.");
+        }
+    }
+
+    /**
+     * Example for SQL queries based on salary ranges.
+     */
+    private static void sqlQuery() {
+        IgniteCache<CacheAffinityKey<UUID>, Person> cache = Ignition.ignite().jcache(CACHE_NAME);
+
+        // SQL clause which selects salaries based on range.
+        String sql = "salary > ? and salary <= ?";
+
+        // Execute queries for salary ranges.
+        print("People with salaries between 0 and 1000: ",
+            cache.query(sql(Person.class, sql).setArgs(0, 1000)).getAll());
+
+        print("People with salaries between 1000 and 2000: ",
+            cache.query(sql(Person.class, sql).setArgs(1000, 2000)).getAll());
+
+        print("People with salaries greater than 2000: ",
+            cache.query(sql(Person.class, sql).setArgs(2000, Integer.MAX_VALUE)).getAll());
+    }
+
+    /**
+     * Example for SQL queries based on all employees working for a specific organization.
+     */
+    private static void sqlQueryWithJoin() {
+        IgniteCache<CacheAffinityKey<UUID>, Person> cache = Ignition.ignite().jcache(CACHE_NAME);
+
+        // SQL clause query which joins on 2 types to select people for a specific organization.
+        String joinSql =
+            "from Person, Organization "
+            + "where Person.orgId = Organization.id "
+            + "and lower(Organization.name) = lower(?)";
+
+        // Execute queries for find employees for different organizations.
+        print("Following people are 'Ignite' employees: ",
+            cache.query(sql(Person.class, joinSql).setArgs("Ignite")).getAll());
+        print("Following people are 'Other' employees: ",
+            cache.query(sql(Person.class, joinSql).setArgs("Other")).getAll());
+    }
+
+    /**
+     * Example for TEXT queries using LUCENE-based indexing of people's resumes.
+     */
+    private static void textQuery() {
+        IgniteCache<CacheAffinityKey<UUID>, Person> cache = Ignition.ignite().jcache(CACHE_NAME);
+
+        //  Query for all people with "Master Degree" in their resumes.
+        QueryCursor<Cache.Entry<CacheAffinityKey<UUID>, Person>> masters =
+            cache.query(text(Person.class, "Master"));
+
+        // Query for all people with "Bachelor Degree" in their resumes.
+        QueryCursor<Cache.Entry<CacheAffinityKey<UUID>, Person>> bachelors =
+            cache.query(text(Person.class, "Bachelor"));
+
+        print("Following people have 'Master Degree' in their resumes: ", masters.getAll());
+        print("Following people have 'Bachelor Degree' in their resumes: ", bachelors.getAll());
+    }
+
+    /**
+     * Example for SQL-based fields queries that return only required
+     * fields instead of whole key-value pairs.
+     */
+    private static void sqlFieldsQuery() {
+        IgniteCache<?, ?> cache = Ignition.ignite().jcache(CACHE_NAME);
+
+        // Create query to get names of all employees.
+        QueryCursor<List<?>> cursor = cache.queryFields(
+                sql("select concat(firstName, ' ', lastName) from Person"));
+
+        // Execute query to get collection of rows. In this particular
+        // case each row will have one element with full name of an employees.
+        List<List<?>> res = cursor.getAll();
+
+        // Print names.
+        print("Names of all employees:", res);
+    }
+
+    /**
+     * Example for SQL-based fields queries that return only required
+     * fields instead of whole key-value pairs.
+     */
+    private static void sqlFieldsQueryWithJoin() {
+        IgniteCache<?, ?> cache = Ignition.ignite().jcache(CACHE_NAME);
+
+        // Execute query to get names of all employees.
+        QueryCursor<List<?>> cursor = cache.queryFields(sql("select concat(firstName, ' ', lastName), "
+                + "Organization.name from Person, Organization where "
+                + "Person.orgId = Organization.id"));
+
+        // In this particular case each row will have one element with full name of an employees.
+        List<List<?>> res = cursor.getAll();
+
+        // Print persons' names and organizations' names.
+        print("Names of all employees and organizations they belong to:", res);
+    }
+
+    /**
+     * Populate cache with test data.
+     */
+    private static void initialize() {
+        IgniteCache cache = Ignition.ignite().jcache(CACHE_NAME);
+
+        // Organizations.
+        Organization org1 = new Organization("Ignite");
+        Organization org2 = new Organization("Other");
+
+        // People.
+        Person p1 = new Person(org1, "John", "Doe", 2000, "John Doe has Master Degree.");
+        Person p2 = new Person(org1, "Jane", "Doe", 1000, "Jane Doe has Bachelor Degree.");
+        Person p3 = new Person(org2, "John", "Smith", 1000, "John Smith has Bachelor Degree.");
+        Person p4 = new Person(org2, "Jane", "Smith", 2000, "Jane Smith has Master Degree.");
+
+        cache.put(org1.id, org1);
+        cache.put(org2.id, org2);
+
+        // Note that in this example we use custom affinity key for Person objects
+        // to ensure that all persons are collocated with their organizations.
+        cache.put(p1.key(), p1);
+        cache.put(p2.key(), p2);
+        cache.put(p3.key(), p3);
+        cache.put(p4.key(), p4);
+    }
+
+    /**
+     * Prints collection of objects to standard out.
+     *
+     * @param msg Message to print before all objects are printed.
+     * @param col Query results.
+     */
+    private static void print(String msg, Iterable<?> col) {
+        if (msg != null)
+            System.out.println(">>> " + msg);
+
+        print(col);
+    }
+
+    /**
+     * Prints collection items.
+     *
+     * @param col Collection.
+     */
+    private static void print(Iterable<?> col) {
+        for (Object next : col) {
+            if (next instanceof Iterable)
+                print((Iterable<?>)next);
+            else
+                System.out.println(">>>     " + next);
+        }
+    }
+
+    /**
+     * Prints out given object to standard out.
+     *
+     * @param o Object to print.
+     */
+    private static void print(Object o) {
+        System.out.println(">>> " + o);
+    }
+
+    /**
+     * Person class.
+     */
+    private static class Person implements Serializable {
+        /** Person ID (indexed). */
+        @QuerySqlField(index = true)
+        private UUID id;
+
+        /** Organization ID (indexed). */
+        @QuerySqlField(index = true)
+        private UUID orgId;
+
+        /** First name (not-indexed). */
+        @QuerySqlField
+        private String firstName;
+
+        /** Last name (not indexed). */
+        @QuerySqlField
+        private String lastName;
+
+        /** Resume text (create LUCENE-based TEXT index for this field). */
+        @QueryTextField
+        private String resume;
+
+        /** Salary (indexed). */
+        @QuerySqlField(index = true)
+        private double salary;
+
+        /** Custom cache key to guarantee that person is always collocated with its organization. */
+        private transient CacheAffinityKey<UUID> key;
+
+        /**
+         * Constructs person record.
+         *
+         * @param org Organization.
+         * @param firstName First name.
+         * @param lastName Last name.
+         * @param salary Salary.
+         * @param resume Resume text.
+         */
+        Person(Organization org, String firstName, String lastName, double salary, String resume) {
+            // Generate unique ID for this person.
+            id = UUID.randomUUID();
+
+            orgId = org.id;
+
+            this.firstName = firstName;
+            this.lastName = lastName;
+            this.resume = resume;
+            this.salary = salary;
+        }
+
+        /**
+         * Gets cache affinity key. Since in some examples person needs to be collocated with organization, we create
+         * custom affinity key to guarantee this collocation.
+         *
+         * @return Custom affinity key to guarantee that person is always collocated with organization.
+         */
+        public CacheAffinityKey<UUID> key() {
+            if (key == null)
+                key = new CacheAffinityKey<>(id, orgId);
+
+            return key;
+        }
+
+        /** {@inheritDoc} */
+        @Override public String toString() {
+            return "Person [firstName=" + firstName +
+                ", lastName=" + lastName +
+                ", id=" + id +
+                ", orgId=" + orgId +
+                ", resume=" + resume +
+                ", salary=" + salary + ']';
+        }
+    }
+
+    /**
+     * Organization class.
+     */
+    private static class Organization implements Serializable {
+        /** Organization ID (indexed). */
+        @QuerySqlField(index = true)
+        private UUID id;
+
+        /** Organization name (indexed). */
+        @QuerySqlField(index = true)
+        private String name;
+
+        /**
+         * Create organization.
+         *
+         * @param name Organization name.
+         */
+        Organization(String name) {
+            id = UUID.randomUUID();
+
+            this.name = name;
+        }
+
+        /** {@inheritDoc} */
+        @Override public String toString() {
+            return "Organization [id=" + id + ", name=" + name + ']';
+        }
+    }
+}

http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/28720c44/examples/src/main/java8/org/apache/ignite/examples8/datagrid/CacheTransactionExample.java
----------------------------------------------------------------------
diff --git a/examples/src/main/java8/org/apache/ignite/examples8/datagrid/CacheTransactionExample.java b/examples/src/main/java8/org/apache/ignite/examples8/datagrid/CacheTransactionExample.java
new file mode 100644
index 0000000..e996ff4
--- /dev/null
+++ b/examples/src/main/java8/org/apache/ignite/examples8/datagrid/CacheTransactionExample.java
@@ -0,0 +1,143 @@
+/*
+ * 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.examples8.datagrid;
+
+import org.apache.ignite.*;
+import org.apache.ignite.transactions.*;
+
+import java.io.*;
+
+import static org.apache.ignite.transactions.TransactionConcurrency.*;
+import static org.apache.ignite.transactions.TransactionIsolation.*;
+
+/**
+ * Demonstrates how to use cache transactions.
+ * <p>
+ * Remote nodes should always be started with special configuration file which
+ * enables P2P class loading: {@code 'ignite.{sh|bat} examples/config/example-cache.xml'}.
+ * <p>
+ * Alternatively you can run {@link org.apache.ignite.examples8.datagrid.CacheNodeStartup} in another JVM which will
+ * start node with {@code examples/config/example-cache.xml} configuration.
+ */
+public class CacheTransactionExample {
+    /** Cache name. */
+    private static final String CACHE_NAME = "partitioned_tx";
+
+    /**
+     * Executes example.
+     *
+     * @param args Command line arguments, none required.
+     * @throws org.apache.ignite.IgniteException If example execution failed.
+     */
+    public static void main(String[] args) throws IgniteException {
+        try (Ignite ignite = Ignition.start("examples/config/example-cache.xml")) {
+            System.out.println();
+            System.out.println(">>> Cache transaction example started.");
+
+            // Clean up caches on all nodes before run.
+            ignite.jcache(CACHE_NAME).clear();
+
+            IgniteCache<Integer, Account> cache = ignite.jcache(CACHE_NAME);
+
+            // Initialize.
+            cache.put(1, new Account(1, 100));
+            cache.put(2, new Account(1, 200));
+
+            System.out.println();
+            System.out.println(">>> Accounts before deposit: ");
+            System.out.println(">>> " + cache.get(1));
+            System.out.println(">>> " + cache.get(2));
+
+            // Make transactional deposits.
+            deposit(1, 100);
+            deposit(2, 200);
+
+            System.out.println();
+            System.out.println(">>> Accounts after transfer: ");
+            System.out.println(">>> " + cache.get(1));
+            System.out.println(">>> " + cache.get(2));
+
+            System.out.println(">>> Cache transaction example finished.");
+        }
+    }
+
+    /**
+     * Make deposit into specified account.
+     *
+     * @param acctId Account ID.
+     * @param amount Amount to deposit.
+     * @throws org.apache.ignite.IgniteException If failed.
+     */
+    private static void deposit(int acctId, double amount) throws IgniteException {
+        // Clone every object we get from cache, so we can freely update it.
+        IgniteCache<Integer, Account> cache = Ignition.ignite().jcache(CACHE_NAME);
+
+        try (Transaction tx = Ignition.ignite().transactions().txStart(PESSIMISTIC, REPEATABLE_READ)) {
+            Account acct0 = cache.get(acctId);
+
+            assert acct0 != null;
+
+            Account acct = new Account(acct0.id, acct0.balance);
+
+            // Deposit into account.
+            acct.update(amount);
+
+            // Store updated account in cache.
+            cache.put(acctId, acct);
+
+            tx.commit();
+        }
+
+        System.out.println();
+        System.out.println(">>> Transferred amount: $" + amount);
+    }
+
+    /**
+     * Account.
+     */
+    private static class Account implements Serializable {
+        /** Account ID. */
+        private int id;
+
+        /** Account balance. */
+        private double balance;
+
+        /**
+         * @param id Account ID.
+         * @param balance Balance.
+         */
+        Account(int id, double balance) {
+            this.id = id;
+            this.balance = balance;
+        }
+
+        /**
+         * Change balance by specified amount.
+         *
+         * @param amount Amount to add to balance (may be negative).
+         */
+        void update(double amount) {
+            balance += amount;
+        }
+
+        /** {@inheritDoc} */
+        @Override public String toString() {
+            return "Account [id=" + id + ", balance=$" + balance + ']';
+        }
+    }
+}


Mime
View raw message