http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/be0e755b/examples/src/main/java/org/apache/ignite/examples/java7/datastructures/IgniteAtomicLongExample.java ---------------------------------------------------------------------- diff --git a/examples/src/main/java/org/apache/ignite/examples/java7/datastructures/IgniteAtomicLongExample.java b/examples/src/main/java/org/apache/ignite/examples/java7/datastructures/IgniteAtomicLongExample.java new file mode 100644 index 0000000..5e99393 --- /dev/null +++ b/examples/src/main/java/org/apache/ignite/examples/java7/datastructures/IgniteAtomicLongExample.java @@ -0,0 +1,74 @@ +/* + * 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.java7.datastructures; + +import org.apache.ignite.*; +import org.apache.ignite.examples.java7.*; +import org.apache.ignite.lang.*; + +import java.util.*; + +/** + * Demonstrates a simple usage of distributed atomic long. + * <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 final class IgniteAtomicLongExample { + /** Number of retries */ + private static final int RETRIES = 20; + + /** + * 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-ignite.xml")) { + System.out.println(); + System.out.println(">>> Atomic long example started."); + + // Make name for atomic long (by which it will be known in the cluster). + String atomicName = UUID.randomUUID().toString(); + + // Initialize atomic long. + final IgniteAtomicLong atomicLong = ignite.atomicLong(atomicName, 0, true); + + System.out.println(); + System.out.println("Atomic long initial value : " + atomicLong.get() + '.'); + + // Try increment atomic long from all nodes. + // Note that this node is also part of the ignite cluster. + ignite.compute().broadcast(new IgniteCallable<Object>() { + @Override public Object call() { + for (int i = 0; i < RETRIES; i++) + System.out.println("AtomicLong value has been incremented: " + atomicLong.incrementAndGet()); + + return null; + } + }); + + System.out.println(); + System.out.println("Atomic long value after successful CAS: " + atomicLong.get()); + } + } +}
http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/be0e755b/examples/src/main/java/org/apache/ignite/examples/java7/datastructures/IgniteAtomicReferenceExample.java ---------------------------------------------------------------------- diff --git a/examples/src/main/java/org/apache/ignite/examples/java7/datastructures/IgniteAtomicReferenceExample.java b/examples/src/main/java/org/apache/ignite/examples/java7/datastructures/IgniteAtomicReferenceExample.java new file mode 100644 index 0000000..4632a45 --- /dev/null +++ b/examples/src/main/java/org/apache/ignite/examples/java7/datastructures/IgniteAtomicReferenceExample.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.examples.java7.datastructures; + +import org.apache.ignite.*; +import org.apache.ignite.examples.java7.*; +import org.apache.ignite.lang.*; + +import java.util.*; + +/** + * Demonstrates a simple usage of distributed atomic reference. + * <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 final class IgniteAtomicReferenceExample { + /** + * 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-ignite.xml")) { + System.out.println(); + System.out.println(">>> Atomic reference example started."); + + // Make name of atomic reference. + final String refName = UUID.randomUUID().toString(); + + // Make value of atomic reference. + String val = UUID.randomUUID().toString(); + + // Initialize atomic reference. + IgniteAtomicReference<String> ref = ignite.atomicReference(refName, val, true); + + System.out.println("Atomic reference initial value : " + ref.get() + '.'); + + // Make closure for checking atomic reference value on cluster. + IgniteRunnable c = new ReferenceClosure(refName); + + // Check atomic reference on all cluster nodes. + ignite.compute().run(c); + + // Make new value of atomic reference. + String newVal = UUID.randomUUID().toString(); + + System.out.println("Try to change value of atomic reference with wrong expected value."); + + ref.compareAndSet("WRONG EXPECTED VALUE", newVal); // Won't change. + + // Check atomic reference on all cluster nodes. + // Atomic reference value shouldn't be changed. + ignite.compute().run(c); + + System.out.println("Try to change value of atomic reference with correct expected value."); + + ref.compareAndSet(val, newVal); + + // Check atomic reference on all cluster nodes. + // Atomic reference value should be changed. + ignite.compute().run(c); + } + + System.out.println(); + System.out.println("Finished atomic reference example..."); + System.out.println("Check all nodes for output (this node is also part of the cluster)."); + } + + /** + * Obtains atomic reference. + */ + private static class ReferenceClosure implements IgniteRunnable { + /** Reference name. */ + private final String refName; + + /** + * @param refName Reference name. + */ + ReferenceClosure(String refName) { + this.refName = refName; + } + + /** {@inheritDoc} */ + @Override public void run() { + IgniteAtomicReference<String> ref = Ignition.ignite().atomicReference(refName, null, true); + + System.out.println("Atomic reference value is " + ref.get() + '.'); + } + } +} http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/be0e755b/examples/src/main/java/org/apache/ignite/examples/java7/datastructures/IgniteAtomicSequenceExample.java ---------------------------------------------------------------------- diff --git a/examples/src/main/java/org/apache/ignite/examples/java7/datastructures/IgniteAtomicSequenceExample.java b/examples/src/main/java/org/apache/ignite/examples/java7/datastructures/IgniteAtomicSequenceExample.java new file mode 100644 index 0000000..6507534 --- /dev/null +++ b/examples/src/main/java/org/apache/ignite/examples/java7/datastructures/IgniteAtomicSequenceExample.java @@ -0,0 +1,91 @@ +/* + * 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.java7.datastructures; + +import org.apache.ignite.*; +import org.apache.ignite.examples.java7.*; +import org.apache.ignite.lang.*; + +/** + * Demonstrates a simple usage of distributed atomic sequence. + * <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 final class IgniteAtomicSequenceExample { + /** Number of retries */ + private static final int RETRIES = 20; + + /** + * 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-ignite.xml")) { + System.out.println(); + System.out.println(">>> Cache atomic sequence example started."); + + // Try increment atomic sequence on all cluster nodes. Note that this node is also part of the cluster. + ignite.compute().broadcast(new SequenceClosure("example-sequence")); + + System.out.println(); + System.out.println("Finished atomic sequence example..."); + System.out.println("Check all nodes for output (this node is also part of the cluster)."); + System.out.println(); + } + } + + /** + * Obtains atomic sequence. + */ + private static class SequenceClosure implements IgniteRunnable { + /** Sequence name. */ + private final String seqName; + + /** + * @param seqName Sequence name. + */ + SequenceClosure(String seqName) { + this.seqName = seqName; + } + + /** {@inheritDoc} */ + @Override public void run() { + // Create sequence. Only one concurrent call will succeed in creation. + // Rest of the callers will get already created instance. + IgniteAtomicSequence seq = Ignition.ignite().atomicSequence(seqName, 0, true); + + // First value of atomic sequence on this node. + long firstVal = seq.get(); + + System.out.println("Sequence initial value on local node: " + firstVal); + + for (int i = 0; i < RETRIES; i++) + System.out.println("Sequence [currentValue=" + seq.get() + ", afterIncrement=" + + seq.incrementAndGet() + ']'); + + System.out.println("Sequence after incrementing [expected=" + (firstVal + RETRIES) + ", actual=" + + seq.get() + ']'); + } + } +} http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/be0e755b/examples/src/main/java/org/apache/ignite/examples/java7/datastructures/IgniteAtomicStampedExample.java ---------------------------------------------------------------------- diff --git a/examples/src/main/java/org/apache/ignite/examples/java7/datastructures/IgniteAtomicStampedExample.java b/examples/src/main/java/org/apache/ignite/examples/java7/datastructures/IgniteAtomicStampedExample.java new file mode 100644 index 0000000..fcd2bf2 --- /dev/null +++ b/examples/src/main/java/org/apache/ignite/examples/java7/datastructures/IgniteAtomicStampedExample.java @@ -0,0 +1,117 @@ +/* + * 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.java7.datastructures; + +import org.apache.ignite.*; +import org.apache.ignite.examples.java7.*; +import org.apache.ignite.lang.*; + +import java.util.*; + +/** + * Demonstrates a simple usage of distributed atomic stamped. + * <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 final class IgniteAtomicStampedExample { + /** + * 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-ignite.xml")) { + System.out.println(); + System.out.println(">>> Atomic stamped example started."); + + // Make name of atomic stamped. + String stampedName = UUID.randomUUID().toString(); + + // Make value of atomic stamped. + String val = UUID.randomUUID().toString(); + + // Make stamp of atomic stamped. + String stamp = UUID.randomUUID().toString(); + + // Initialize atomic stamped. + IgniteAtomicStamped<String, String> stamped = ignite.atomicStamped(stampedName, val, stamp, true); + + System.out.println("Atomic stamped initial [value=" + stamped.value() + ", stamp=" + stamped.stamp() + ']'); + + // Make closure for checking atomic stamped. + IgniteRunnable c = new StampedUpdateClosure(stampedName); + + // Check atomic stamped on all cluster nodes. + ignite.compute().broadcast(c); + + // Make new value of atomic stamped. + String newVal = UUID.randomUUID().toString(); + + // Make new stamp of atomic stamped. + String newStamp = UUID.randomUUID().toString(); + + System.out.println("Try to change value and stamp of atomic stamped with wrong expected value and stamp."); + + stamped.compareAndSet("WRONG EXPECTED VALUE", newVal, "WRONG EXPECTED STAMP", newStamp); + + // Check atomic stamped on all cluster nodes. + // Atomic stamped value and stamp shouldn't be changed. + ignite.compute().run(c); + + System.out.println("Try to change value and stamp of atomic stamped with correct value and stamp."); + + stamped.compareAndSet(val, newVal, stamp, newStamp); + + // Check atomic stamped on all cluster nodes. + // Atomic stamped value and stamp should be changed. + ignite.compute().run(c); + } + + System.out.println(); + System.out.println("Finished atomic stamped example..."); + System.out.println("Check all nodes for output (this node is also part of the cluster)."); + } + + /** + * Performs update of on an atomic stamped variable in cache. + */ + private static class StampedUpdateClosure implements IgniteRunnable { + /** Atomic stamped variable name. */ + private final String stampedName; + + /** + * @param stampedName Atomic stamped variable name. + */ + StampedUpdateClosure(String stampedName) { + this.stampedName = stampedName; + } + + /** {@inheritDoc} */ + @Override public void run() { + IgniteAtomicStamped<String, String> stamped = Ignition.ignite(). + atomicStamped(stampedName, null, null, true); + + System.out.println("Atomic stamped [value=" + stamped.value() + ", stamp=" + stamped.stamp() + ']'); + } + } +} http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/be0e755b/examples/src/main/java/org/apache/ignite/examples/java7/datastructures/IgniteCountDownLatchExample.java ---------------------------------------------------------------------- diff --git a/examples/src/main/java/org/apache/ignite/examples/java7/datastructures/IgniteCountDownLatchExample.java b/examples/src/main/java/org/apache/ignite/examples/java7/datastructures/IgniteCountDownLatchExample.java new file mode 100644 index 0000000..feff109 --- /dev/null +++ b/examples/src/main/java/org/apache/ignite/examples/java7/datastructures/IgniteCountDownLatchExample.java @@ -0,0 +1,95 @@ +/* + * 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.java7.datastructures; + +import org.apache.ignite.*; +import org.apache.ignite.examples.java7.*; +import org.apache.ignite.lang.*; + +import java.util.*; + +/** + * Demonstrates a simple usage of distributed count down latch. + * <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 IgniteCountDownLatchExample { + /** Number of latch initial count */ + private static final int INITIAL_COUNT = 10; + + /** + * Executes example. + * + * @param args Command line arguments, none required. + */ + public static void main(String[] args) { + try (Ignite ignite = Ignition.start("examples/config/example-ignite.xml")) { + System.out.println(); + System.out.println(">>> Cache atomic countdown latch example started."); + + // Make name of count down latch. + final String latchName = UUID.randomUUID().toString(); + + // Initialize count down latch. + IgniteCountDownLatch latch = ignite.countDownLatch(latchName, INITIAL_COUNT, false, true); + + System.out.println("Latch initial value: " + latch.count()); + + // Start waiting on the latch on all cluster nodes. + for (int i = 0; i < INITIAL_COUNT; i++) + ignite.compute().run(new LatchClosure(latchName)); + + // Wait for latch to go down which essentially means that all remote closures completed. + latch.await(); + + System.out.println("All latch closures have completed."); + } + + System.out.println(); + System.out.println("Finished count down latch example..."); + System.out.println("Check all nodes for output (this node is also part of the cluster)."); + } + + /** + * Closure which simply waits on the latch on all nodes. + */ + private static class LatchClosure implements IgniteRunnable { + /** Latch name. */ + private final String latchName; + + /** + * @param latchName Latch name. + */ + LatchClosure(String latchName) { + this.latchName = latchName; + } + + /** {@inheritDoc} */ + @Override public void run() { + IgniteCountDownLatch latch = Ignition.ignite().countDownLatch(latchName, 1, false, true); + + int newCnt = latch.countDown(); + + System.out.println("Counted down [newCnt=" + newCnt + ", nodeId=" + Ignition.ignite().cluster().localNode().id() + ']'); + } + } +} http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/be0e755b/examples/src/main/java/org/apache/ignite/examples/java7/datastructures/IgniteQueueExample.java ---------------------------------------------------------------------- diff --git a/examples/src/main/java/org/apache/ignite/examples/java7/datastructures/IgniteQueueExample.java b/examples/src/main/java/org/apache/ignite/examples/java7/datastructures/IgniteQueueExample.java new file mode 100644 index 0000000..c052ee2 --- /dev/null +++ b/examples/src/main/java/org/apache/ignite/examples/java7/datastructures/IgniteQueueExample.java @@ -0,0 +1,215 @@ +/* + * 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.java7.datastructures; + +import org.apache.ignite.*; +import org.apache.ignite.cache.*; +import org.apache.ignite.configuration.*; +import org.apache.ignite.examples.java7.*; +import org.apache.ignite.lang.*; + +import java.util.*; + +/** + * Ignite cache distributed queue example. This example demonstrates {@code FIFO} unbounded + * cache queue. + * <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 IgniteQueueExample { + /** Cache name. */ + private static final String CACHE_NAME = IgniteQueueExample.class.getSimpleName(); + + /** Number of retries */ + private static final int RETRIES = 20; + + /** Queue instance. */ + private static IgniteQueue<String> queue; + + /** + * 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-ignite.xml")) { + System.out.println(); + System.out.println(">>> Ignite queue example started."); + + CacheConfiguration<Object, Object> cfg = new CacheConfiguration<>(); + + cfg.setCacheMode(CacheMode.PARTITIONED); + cfg.setName(CACHE_NAME); + cfg.setAtomicWriteOrderMode(CacheAtomicWriteOrderMode.PRIMARY); + + try (IgniteCache<Object, Object> cache = ignite.createCache(cfg)) { + // Make queue name. + String queueName = UUID.randomUUID().toString(); + + queue = initializeQueue(ignite, queueName); + + readFromQueue(ignite); + + writeToQueue(ignite); + + clearAndRemoveQueue(); + } + } + + System.out.println("Cache queue example finished."); + } + + /** + * Initialize queue. + * + * @param ignite Ignite. + * @param queueName Name of queue. + * @return Queue. + * @throws IgniteException If execution failed. + */ + private static IgniteQueue<String> initializeQueue(Ignite ignite, String queueName) throws IgniteException { + CollectionConfiguration colCfg = new CollectionConfiguration(); + + colCfg.setCacheName(CACHE_NAME); + + // Initialize new FIFO queue. + IgniteQueue<String> queue = ignite.queue(queueName, 0, colCfg); + + // Initialize queue items. + // We will be use blocking operation and queue size must be appropriated. + for (int i = 0; i < ignite.cluster().nodes().size() * RETRIES * 2; i++) + queue.put(Integer.toString(i)); + + System.out.println("Queue size after initializing: " + queue.size()); + + return queue; + } + + /** + * Read items from head and tail of queue. + * + * @param ignite Ignite. + * @throws IgniteException If failed. + */ + private static void readFromQueue(Ignite ignite) throws IgniteException { + final String queueName = queue.name(); + + // Read queue items on each node. + ignite.compute().broadcast(new QueueClosure(queueName, false)); + + System.out.println("Queue size after reading [expected=0, actual=" + queue.size() + ']'); + } + + /** + * Write items into queue. + * + * @param ignite Ignite. + * @throws IgniteException If failed. + */ + private static void writeToQueue(Ignite ignite) throws IgniteException { + final String queueName = queue.name(); + + // Write queue items on each node. + ignite.compute().broadcast(new QueueClosure(queueName, true)); + + System.out.println("Queue size after writing [expected=" + ignite.cluster().nodes().size() * RETRIES + + ", actual=" + queue.size() + ']'); + + System.out.println("Iterate over queue."); + + // Iterate over queue. + for (String item : queue) + System.out.println("Queue item: " + item); + } + + /** + * Clear and remove queue. + * + * @throws IgniteException If execution failed. + */ + private static void clearAndRemoveQueue() throws IgniteException { + System.out.println("Queue size before clearing: " + queue.size()); + + // Clear queue. + queue.clear(); + + System.out.println("Queue size after clearing: " + queue.size()); + + // Remove queue. + queue.close(); + + // Try to work with removed queue. + try { + queue.poll(); + } + catch (IllegalStateException expected) { + System.out.println("Expected exception - " + expected.getMessage()); + } + } + + /** + * Closure to populate or poll the queue. + */ + private static class QueueClosure implements IgniteRunnable { + /** Queue name. */ + private final String queueName; + + /** Flag indicating whether to put or poll. */ + private final boolean put; + + /** + * @param queueName Queue name. + * @param put Flag indicating whether to put or poll. + */ + QueueClosure(String queueName, boolean put) { + this.queueName = queueName; + this.put = put; + } + + /** {@inheritDoc} */ + @Override public void run() { + IgniteQueue<String> queue = Ignition.ignite().queue(queueName, 0, null); + + if (put) { + UUID locId = Ignition.ignite().cluster().localNode().id(); + + for (int i = 0; i < RETRIES; i++) { + String item = locId + "_" + Integer.toString(i); + + queue.put(item); + + System.out.println("Queue item has been added: " + item); + } + } + else { + // Take items from queue head. + for (int i = 0; i < RETRIES; i++) + System.out.println("Queue item has been read from queue head: " + queue.take()); + + // Take items from queue head once again. + for (int i = 0; i < RETRIES; i++) + System.out.println("Queue item has been read from queue head: " + queue.poll()); + } + } + } +} http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/be0e755b/examples/src/main/java/org/apache/ignite/examples/java7/datastructures/IgniteSetExample.java ---------------------------------------------------------------------- diff --git a/examples/src/main/java/org/apache/ignite/examples/java7/datastructures/IgniteSetExample.java b/examples/src/main/java/org/apache/ignite/examples/java7/datastructures/IgniteSetExample.java new file mode 100644 index 0000000..9c78591 --- /dev/null +++ b/examples/src/main/java/org/apache/ignite/examples/java7/datastructures/IgniteSetExample.java @@ -0,0 +1,197 @@ +/* + * 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.java7.datastructures; + +import org.apache.ignite.*; +import org.apache.ignite.cache.*; +import org.apache.ignite.configuration.*; +import org.apache.ignite.examples.java7.*; +import org.apache.ignite.lang.*; + +import java.util.*; + +/** + * Ignite cache distributed set example. + * <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 IgniteSetExample { + /** Cache name. */ + private static final String CACHE_NAME = IgniteSetExample.class.getSimpleName(); + + /** Set instance. */ + private static IgniteSet<String> set; + + /** + * 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-ignite.xml")) { + System.out.println(); + System.out.println(">>> Ignite set example started."); + + CacheConfiguration<Integer, String> cfg = new CacheConfiguration<>(); + + cfg.setCacheMode(CacheMode.PARTITIONED); + cfg.setName(CACHE_NAME); + cfg.setAtomicityMode(CacheAtomicityMode.TRANSACTIONAL); + + NearCacheConfiguration<Integer, String> nearCacheCfg = new NearCacheConfiguration<>(); + + try (IgniteCache<Integer, String> cache = ignite.createCache(cfg, nearCacheCfg)) { + // Make set name. + String setName = UUID.randomUUID().toString(); + + set = initializeSet(ignite, setName); + + writeToSet(ignite); + + clearAndRemoveSet(); + } + } + + System.out.println("Ignite set example finished."); + } + + /** + * Initialize set. + * + * @param ignite Ignite. + * @param setName Name of set. + * @return Set. + * @throws IgniteException If execution failed. + */ + private static IgniteSet<String> initializeSet(Ignite ignite, String setName) throws IgniteException { + CollectionConfiguration setCfg = new CollectionConfiguration(); + + setCfg.setCacheName(CACHE_NAME); + + // Initialize new set. + IgniteSet<String> set = ignite.set(setName, setCfg); + + // Initialize set items. + for (int i = 0; i < 10; i++) + set.add(Integer.toString(i)); + + System.out.println("Set size after initializing: " + set.size()); + + return set; + } + + /** + * Write items into set. + * + * @param ignite Ignite. + * @throws IgniteException If failed. + */ + private static void writeToSet(Ignite ignite) throws IgniteException { + final String setName = set.name(); + + // Write set items on each node. + ignite.compute().broadcast(new SetClosure(setName)); + + System.out.println("Set size after writing [expected=" + (10 + ignite.cluster().nodes().size() * 5) + + ", actual=" + set.size() + ']'); + + System.out.println("Iterate over set."); + + // Iterate over set. + for (String item : set) + System.out.println("Set item: " + item); + + // Set API usage examples. + if (!set.contains("0")) + throw new RuntimeException("Set should contain '0' among its elements."); + + if (set.add("0")) + throw new RuntimeException("Set should not allow duplicates."); + + if (!set.remove("0")) + throw new RuntimeException("Set should correctly remove elements."); + + if (set.contains("0")) + throw new RuntimeException("Set should not contain '0' among its elements."); + + if (!set.add("0")) + throw new RuntimeException("Set should correctly add new elements."); + } + + /** + * Clear and remove set. + * + * @throws IgniteException If execution failed. + */ + private static void clearAndRemoveSet() throws IgniteException { + System.out.println("Set size before clearing: " + set.size()); + + // Clear set. + set.clear(); + + System.out.println("Set size after clearing: " + set.size()); + + // Remove set. + set.close(); + + System.out.println("Set was removed: " + set.removed()); + + // Try to work with removed set. + try { + set.contains("1"); + } + catch (IllegalStateException expected) { + System.out.println("Expected exception - " + expected.getMessage()); + } + } + + /** + * Closure to populate the set. + */ + private static class SetClosure implements IgniteRunnable { + /** Set name. */ + private final String setName; + + /** + * @param setName Set name. + */ + SetClosure(String setName) { + this.setName = setName; + } + + /** {@inheritDoc} */ + @Override public void run() { + IgniteSet<String> set = Ignition.ignite().set(setName, null); + + UUID locId = Ignition.ignite().cluster().localNode().id(); + + for (int i = 0; i < 5; i++) { + String item = locId + "_" + Integer.toString(i); + + set.add(item); + + System.out.println("Set item has been added: " + item); + } + } + } +} http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/be0e755b/examples/src/main/java/org/apache/ignite/examples/java7/datastructures/package-info.java ---------------------------------------------------------------------- diff --git a/examples/src/main/java/org/apache/ignite/examples/java7/datastructures/package-info.java b/examples/src/main/java/org/apache/ignite/examples/java7/datastructures/package-info.java new file mode 100644 index 0000000..041b0c2 --- /dev/null +++ b/examples/src/main/java/org/apache/ignite/examples/java7/datastructures/package-info.java @@ -0,0 +1,22 @@ +/* + * 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 description. --> + * Demonstrates using of blocking and non-blocking queues and atomic data structures. + */ +package org.apache.ignite.examples.java7.datastructures; http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/be0e755b/examples/src/main/java/org/apache/ignite/examples/java7/events/EventsExample.java ---------------------------------------------------------------------- diff --git a/examples/src/main/java/org/apache/ignite/examples/java7/events/EventsExample.java b/examples/src/main/java/org/apache/ignite/examples/java7/events/EventsExample.java new file mode 100644 index 0000000..612c445 --- /dev/null +++ b/examples/src/main/java/org/apache/ignite/examples/java7/events/EventsExample.java @@ -0,0 +1,144 @@ +/* + * 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.java7.events; + +import org.apache.ignite.*; +import org.apache.ignite.compute.*; +import org.apache.ignite.events.*; +import org.apache.ignite.examples.java7.*; +import org.apache.ignite.lang.*; +import org.apache.ignite.resources.*; + +import java.util.*; + +import static org.apache.ignite.events.EventType.*; + +/** + * Demonstrates event consume API that allows to register event listeners on remote nodes. + * Note that ignite events are disabled by default and must be specifically enabled, + * just like in {@code examples/config/example-ignite.xml} file. + * <p> + * Remote nodes should always be started with configuration: {@code 'ignite.sh 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 EventsExample { + /** + * 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-ignite.xml")) { + System.out.println(); + System.out.println(">>> Events API example started."); + + // Listen to events happening on local node. + localListen(); + + // Listen to events happening on all cluster nodes. + remoteListen(); + + // Wait for a while while callback is notified about remaining puts. + Thread.sleep(1000); + } + } + + /** + * Listen to events that happen only on local node. + * + * @throws IgniteException If failed. + */ + private static void localListen() throws IgniteException { + System.out.println(); + System.out.println(">>> Local event listener example."); + + Ignite ignite = Ignition.ignite(); + + IgnitePredicate<TaskEvent> lsnr = new IgnitePredicate<TaskEvent>() { + @Override public boolean apply(TaskEvent evt) { + System.out.println("Received task event [evt=" + evt.name() + ", taskName=" + evt.taskName() + ']'); + + return true; // Return true to continue listening. + } + }; + + // Register event listener for all local task execution events. + ignite.events().localListen(lsnr, EVTS_TASK_EXECUTION); + + // Generate task events. + ignite.compute().withName("example-event-task").run(new IgniteRunnable() { + @Override public void run() { + System.out.println("Executing sample job."); + } + }); + + // Unsubscribe local task event listener. + ignite.events().stopLocalListen(lsnr); + } + + /** + * Listen to events coming from all cluster nodes. + * + * @throws IgniteException If failed. + */ + private static void remoteListen() throws IgniteException { + System.out.println(); + System.out.println(">>> Remote event listener example."); + + // This optional local callback is called for each event notification + // that passed remote predicate listener. + IgniteBiPredicate<UUID, TaskEvent> locLsnr = new IgniteBiPredicate<UUID, TaskEvent>() { + @Override public boolean apply(UUID nodeId, TaskEvent evt) { + // Remote filter only accepts tasks whose name being with "good-task" prefix. + assert evt.taskName().startsWith("good-task"); + + System.out.println("Received task event [evt=" + evt.name() + ", taskName=" + evt.taskName()); + + return true; // Return true to continue listening. + } + }; + + // Remote filter which only accepts tasks whose name begins with "good-task" prefix. + IgnitePredicate<TaskEvent> rmtLsnr = new IgnitePredicate<TaskEvent>() { + @Override public boolean apply(TaskEvent evt) { + return evt.taskName().startsWith("good-task"); + } + }; + + Ignite ignite = Ignition.ignite(); + + // Register event listeners on all nodes to listen for task events. + ignite.events().remoteListen(locLsnr, rmtLsnr, EVTS_TASK_EXECUTION); + + // Generate task events. + for (int i = 0; i < 10; i++) { + ignite.compute().withName(i < 5 ? "good-task-" + i : "bad-task-" + i).run(new IgniteRunnable() { + // Auto-inject task session. + @TaskSessionResource + private ComputeTaskSession ses; + + @Override public void run() { + System.out.println("Executing sample job for task: " + ses.getTaskName()); + } + }); + } + } +} http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/be0e755b/examples/src/main/java/org/apache/ignite/examples/java7/events/package-info.java ---------------------------------------------------------------------- diff --git a/examples/src/main/java/org/apache/ignite/examples/java7/events/package-info.java b/examples/src/main/java/org/apache/ignite/examples/java7/events/package-info.java new file mode 100644 index 0000000..dbd9c27 --- /dev/null +++ b/examples/src/main/java/org/apache/ignite/examples/java7/events/package-info.java @@ -0,0 +1,22 @@ +/* + * 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 description. --> + * Demonstrates events management API. + */ +package org.apache.ignite.examples.java7.events; http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/be0e755b/examples/src/main/java/org/apache/ignite/examples/java7/igfs/IgfsExample.java ---------------------------------------------------------------------- diff --git a/examples/src/main/java/org/apache/ignite/examples/java7/igfs/IgfsExample.java b/examples/src/main/java/org/apache/ignite/examples/java7/igfs/IgfsExample.java new file mode 100644 index 0000000..97d0d83 --- /dev/null +++ b/examples/src/main/java/org/apache/ignite/examples/java7/igfs/IgfsExample.java @@ -0,0 +1,278 @@ +/* + * 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.java7.igfs; + +import org.apache.ignite.*; +import org.apache.ignite.igfs.*; +import org.jetbrains.annotations.*; + +import java.io.*; +import java.util.*; + +/** + * Example that shows usage of {@link org.apache.ignite.IgniteFileSystem} API. It starts a node with {@code IgniteFs} + * configured and performs several file system operations (create, write, append, read and delete + * files, create, list and delete directories). + * <p> + * Remote nodes should always be started with configuration file which includes + * IGFS: {@code 'ignite.sh examples/config/filesystem/example-igfs.xml'}. + * <p> + * Alternatively you can run {@link IgfsNodeStartup} in another JVM which will start + * node with {@code examples/config/filesystem/example-igfs.xml} configuration. + */ +public final class IgfsExample { + /** + * Executes example. + * + * @param args Command line arguments, none required. + * @throws Exception If example execution failed. + */ + public static void main(String[] args) throws Exception { + Ignite ignite = Ignition.start("examples/config/filesystem/example-igfs.xml"); + + System.out.println(); + System.out.println(">>> IGFS example started."); + + try { + // Get an instance of Ignite File System. + IgniteFileSystem fs = ignite.fileSystem("igfs"); + + // Working directory path. + IgfsPath workDir = new IgfsPath("/examples/fs"); + + // Cleanup working directory. + delete(fs, workDir); + + // Create empty working directory. + mkdirs(fs, workDir); + + // Print information for working directory. + printInfo(fs, workDir); + + // File path. + IgfsPath filePath = new IgfsPath(workDir, "file.txt"); + + // Create file. + create(fs, filePath, new byte[] {1, 2, 3}); + + // Print information for file. + printInfo(fs, filePath); + + // Append more data to previously created file. + append(fs, filePath, new byte[] {4, 5}); + + // Print information for file. + printInfo(fs, filePath); + + // Read data from file. + read(fs, filePath); + + // Delete file. + delete(fs, filePath); + + // Print information for file. + printInfo(fs, filePath); + + // Create several files. + for (int i = 0; i < 5; i++) + create(fs, new IgfsPath(workDir, "file-" + i + ".txt"), null); + + list(fs, workDir); + } + finally { + Ignition.stop(false); + } + } + + /** + * Deletes file or directory. If directory + * is not empty, it's deleted recursively. + * + * @param fs IGFS. + * @param path File or directory path. + * @throws IgniteException In case of error. + */ + private static void delete(IgniteFileSystem fs, IgfsPath path) throws IgniteException { + assert fs != null; + assert path != null; + + if (fs.exists(path)) { + boolean isFile = fs.info(path).isFile(); + + try { + fs.delete(path, true); + + System.out.println(); + System.out.println(">>> Deleted " + (isFile ? "file" : "directory") + ": " + path); + } + catch (IgfsException e) { + System.out.println(); + System.out.println(">>> Failed to delete " + (isFile ? "file" : "directory") + " [path=" + path + + ", msg=" + e.getMessage() + ']'); + } + } + else { + System.out.println(); + System.out.println(">>> Won't delete file or directory (doesn't exist): " + path); + } + } + + /** + * Creates directories. + * + * @param fs IGFS. + * @param path Directory path. + * @throws IgniteException In case of error. + */ + private static void mkdirs(IgniteFileSystem fs, IgfsPath path) throws IgniteException { + assert fs != null; + assert path != null; + + try { + fs.mkdirs(path); + + System.out.println(); + System.out.println(">>> Created directory: " + path); + } + catch (IgfsException e) { + System.out.println(); + System.out.println(">>> Failed to create a directory [path=" + path + ", msg=" + e.getMessage() + ']'); + } + + System.out.println(); + } + + /** + * Creates file and writes provided data to it. + * + * @param fs IGFS. + * @param path File path. + * @param data Data. + * @throws IgniteException If file can't be created. + * @throws IOException If data can't be written. + */ + private static void create(IgniteFileSystem fs, IgfsPath path, @Nullable byte[] data) + throws IgniteException, IOException { + assert fs != null; + assert path != null; + + try (OutputStream out = fs.create(path, true)) { + System.out.println(); + System.out.println(">>> Created file: " + path); + + if (data != null) { + out.write(data); + + System.out.println(); + System.out.println(">>> Wrote data to file: " + path); + } + } + + System.out.println(); + } + + /** + * Opens file and appends provided data to it. + * + * @param fs IGFS. + * @param path File path. + * @param data Data. + * @throws IgniteException If file can't be created. + * @throws IOException If data can't be written. + */ + private static void append(IgniteFileSystem fs, IgfsPath path, byte[] data) throws IgniteException, IOException { + assert fs != null; + assert path != null; + assert data != null; + assert fs.info(path).isFile(); + + try (OutputStream out = fs.append(path, true)) { + System.out.println(); + System.out.println(">>> Opened file: " + path); + + out.write(data); + } + + System.out.println(); + System.out.println(">>> Appended data to file: " + path); + } + + /** + * Opens file and reads it to byte array. + * + * @param fs IgniteFs. + * @param path File path. + * @throws IgniteException If file can't be opened. + * @throws IOException If data can't be read. + */ + private static void read(IgniteFileSystem fs, IgfsPath path) throws IgniteException, IOException { + assert fs != null; + assert path != null; + assert fs.info(path).isFile(); + + byte[] data = new byte[(int)fs.info(path).length()]; + + try (IgfsInputStream in = fs.open(path)) { + in.read(data); + } + + System.out.println(); + System.out.println(">>> Read data from " + path + ": " + Arrays.toString(data)); + } + + /** + * Lists files in directory. + * + * @param fs IGFS. + * @param path Directory path. + * @throws IgniteException In case of error. + */ + private static void list(IgniteFileSystem fs, IgfsPath path) throws IgniteException { + assert fs != null; + assert path != null; + assert fs.info(path).isDirectory(); + + Collection<IgfsPath> files = fs.listPaths(path); + + if (files.isEmpty()) { + System.out.println(); + System.out.println(">>> No files in directory: " + path); + } + else { + System.out.println(); + System.out.println(">>> List of files in directory: " + path); + + for (IgfsPath f : files) + System.out.println(">>> " + f.name()); + } + + System.out.println(); + } + + /** + * Prints information for file or directory. + * + * @param fs IGFS. + * @param path File or directory path. + * @throws IgniteException In case of error. + */ + private static void printInfo(IgniteFileSystem fs, IgfsPath path) throws IgniteException { + System.out.println(); + System.out.println("Information for " + path + ": " + fs.info(path)); + } +} http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/be0e755b/examples/src/main/java/org/apache/ignite/examples/java7/igfs/IgfsMapReduceExample.java ---------------------------------------------------------------------- diff --git a/examples/src/main/java/org/apache/ignite/examples/java7/igfs/IgfsMapReduceExample.java b/examples/src/main/java/org/apache/ignite/examples/java7/igfs/IgfsMapReduceExample.java new file mode 100644 index 0000000..757d2dd --- /dev/null +++ b/examples/src/main/java/org/apache/ignite/examples/java7/igfs/IgfsMapReduceExample.java @@ -0,0 +1,249 @@ +/* + * 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.java7.igfs; + +import org.apache.ignite.*; +import org.apache.ignite.compute.*; +import org.apache.ignite.igfs.*; +import org.apache.ignite.igfs.mapreduce.*; +import org.apache.ignite.igfs.mapreduce.records.*; + +import java.io.*; +import java.util.*; + +/** + * Example that shows how to use {@link org.apache.ignite.igfs.mapreduce.IgfsTask} to find lines matching particular pattern in the file in pretty + * the same way as {@code grep} command does. + * <p> + * Remote nodes should always be started with configuration file which includes + * IGFS: {@code 'ignite.sh examples/config/filesystem/example-igfs.xml'}. + * <p> + * Alternatively you can run {@link IgfsNodeStartup} in another JVM which will start + * node with {@code examples/config/filesystem/example-igfs.xml} configuration. + */ +public class IgfsMapReduceExample { + /** + * Executes example. + * + * @param args Command line arguments. First argument is file name, second argument is regex to look for. + * @throws Exception If failed. + */ + public static void main(String[] args) throws Exception { + if (args.length == 0) + System.out.println("Please provide file name and regular expression."); + else if (args.length == 1) + System.out.println("Please provide regular expression."); + else { + try (Ignite ignite = Ignition.start("examples/config/filesystem/example-igfs.xml")) { + System.out.println(); + System.out.println(">>> IGFS map reduce example started."); + + // Prepare arguments. + String fileName = args[0]; + + File file = new File(fileName); + + String regexStr = args[1]; + + // Get an instance of Ignite File System. + IgniteFileSystem fs = ignite.fileSystem("igfs"); + + // Working directory path. + IgfsPath workDir = new IgfsPath("/examples/fs"); + + // Write file to IGFS. + IgfsPath fsPath = new IgfsPath(workDir, file.getName()); + + writeFile(fs, fsPath, file); + + Collection<Line> lines = fs.execute(new GrepTask(), IgfsNewLineRecordResolver.NEW_LINE, + Collections.singleton(fsPath), regexStr); + + if (lines.isEmpty()) { + System.out.println(); + System.out.println("No lines were found."); + } + else { + for (Line line : lines) + print(line.fileLine()); + } + } + } + } + + /** + * Write file to the Ignite file system. + * + * @param fs Ignite file system. + * @param fsPath Ignite file system path. + * @param file File to write. + * @throws Exception In case of exception. + */ + private static void writeFile(IgniteFileSystem fs, IgfsPath fsPath, File file) throws Exception { + System.out.println(); + System.out.println("Copying file to IGFS: " + file); + + try ( + IgfsOutputStream os = fs.create(fsPath, true); + FileInputStream fis = new FileInputStream(file) + ) { + byte[] buf = new byte[2048]; + + int read = fis.read(buf); + + while (read != -1) { + os.write(buf, 0, read); + + read = fis.read(buf); + } + } + } + + /** + * Print particular string. + * + * @param str String. + */ + private static void print(String str) { + System.out.println(">>> " + str); + } + + /** + * Grep task. + */ + private static class GrepTask extends IgfsTask<String, Collection<Line>> { + /** {@inheritDoc} */ + @Override public IgfsJob createJob(IgfsPath path, IgfsFileRange range, + IgfsTaskArgs<String> args) { + return new GrepJob(args.userArgument()); + } + + /** {@inheritDoc} */ + @Override public Collection<Line> reduce(List<ComputeJobResult> results) { + Collection<Line> lines = new TreeSet<>(new Comparator<Line>() { + @Override public int compare(Line line1, Line line2) { + return line1.rangePosition() < line2.rangePosition() ? -1 : + line1.rangePosition() > line2.rangePosition() ? 1 : line1.lineIndex() - line2.lineIndex(); + } + }); + + for (ComputeJobResult res : results) { + if (res.getException() != null) + throw res.getException(); + + Collection<Line> line = res.getData(); + + if (line != null) + lines.addAll(line); + } + + return lines; + } + } + + /** + * Grep job. + */ + private static class GrepJob extends IgfsInputStreamJobAdapter { + /** Regex string. */ + private final String regex; + + /** + * Constructor. + * + * @param regex Regex string. + */ + private GrepJob(String regex) { + this.regex = regex; + } + + /** {@inheritDoc} */ + @Override public Object execute(IgniteFileSystem igfs, IgfsRangeInputStream in) throws IgniteException, IOException { + Collection<Line> res = null; + + long start = in.startOffset(); + + try (BufferedReader br = new BufferedReader(new InputStreamReader(in))) { + int ctr = 0; + + String line = br.readLine(); + + while (line != null) { + if (line.matches(".*" + regex + ".*")) { + if (res == null) + res = new HashSet<>(); + + res.add(new Line(start, ctr++, line)); + } + + line = br.readLine(); + } + } + + return res; + } + } + + /** + * Single file line with it's position. + */ + private static class Line { + /** Line start position in the file. */ + private long rangePos; + + /** Matching line index within the range. */ + private final int lineIdx; + + /** File line. */ + private String line; + + /** + * Constructor. + * + * @param rangePos Range position. + * @param lineIdx Matching line index within the range. + * @param line File line. + */ + private Line(long rangePos, int lineIdx, String line) { + this.rangePos = rangePos; + this.lineIdx = lineIdx; + this.line = line; + } + + /** + * @return Range position. + */ + public long rangePosition() { + return rangePos; + } + + /** + * @return Matching line index within the range. + */ + public int lineIndex() { + return lineIdx; + } + + /** + * @return File line. + */ + public String fileLine() { + return line; + } + } +} http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/be0e755b/examples/src/main/java/org/apache/ignite/examples/java7/igfs/IgfsNodeStartup.java ---------------------------------------------------------------------- diff --git a/examples/src/main/java/org/apache/ignite/examples/java7/igfs/IgfsNodeStartup.java b/examples/src/main/java/org/apache/ignite/examples/java7/igfs/IgfsNodeStartup.java new file mode 100644 index 0000000..d25b6ff --- /dev/null +++ b/examples/src/main/java/org/apache/ignite/examples/java7/igfs/IgfsNodeStartup.java @@ -0,0 +1,41 @@ +/* + * 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.java7.igfs; + +import org.apache.ignite.*; + +/** + * Starts up an empty node with IGFS configuration. + * You can also start a stand-alone Ignite instance by passing the path + * to configuration file to {@code 'ignite.{sh|bat}'} script, like so: + * {@code 'ignite.sh examples/config/filesystem/example-igfs.xml'}. + * <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 IgfsNodeStartup { + /** + * Start up an empty node with specified cache configuration. + * + * @param args Command line arguments, none required. + * @throws IgniteException If example execution failed. + */ + public static void main(String[] args) throws IgniteException { + Ignition.start("examples/config/filesystem/example-igfs.xml"); + } +} http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/be0e755b/examples/src/main/java/org/apache/ignite/examples/java7/igfs/package-info.java ---------------------------------------------------------------------- diff --git a/examples/src/main/java/org/apache/ignite/examples/java7/igfs/package-info.java b/examples/src/main/java/org/apache/ignite/examples/java7/igfs/package-info.java new file mode 100644 index 0000000..b514112 --- /dev/null +++ b/examples/src/main/java/org/apache/ignite/examples/java7/igfs/package-info.java @@ -0,0 +1,22 @@ +/* + * 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 description. --> + * Demonstrates usage of Ignite File System. + */ +package org.apache.ignite.examples.java7.igfs; http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/be0e755b/examples/src/main/java/org/apache/ignite/examples/java7/messaging/MessagingExample.java ---------------------------------------------------------------------- diff --git a/examples/src/main/java/org/apache/ignite/examples/java7/messaging/MessagingExample.java b/examples/src/main/java/org/apache/ignite/examples/java7/messaging/MessagingExample.java new file mode 100644 index 0000000..dee2807 --- /dev/null +++ b/examples/src/main/java/org/apache/ignite/examples/java7/messaging/MessagingExample.java @@ -0,0 +1,173 @@ +/* + * 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.java7.messaging; + +import org.apache.ignite.*; +import org.apache.ignite.cluster.*; +import org.apache.ignite.examples.java7.*; +import org.apache.ignite.lang.*; +import org.apache.ignite.resources.*; + +import java.util.*; +import java.util.concurrent.*; + +/** + * Example that demonstrates how to exchange messages between nodes. Use such + * functionality for cases when you need to communicate to other nodes outside + * of ignite task. + * <p> + * To run this example you must have at least one remote node started. + * <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 final class MessagingExample { + /** Number of messages. */ + private static final int MESSAGES_NUM = 10; + + /** Message topics. */ + private enum TOPIC { ORDERED, UNORDERED } + + /** + * 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-ignite.xml")) { + if (!ExamplesUtils.checkMinTopologySize(ignite.cluster(), 2)) + return; + + System.out.println(); + System.out.println(">>> Messaging example started."); + + // Group for remote nodes. + ClusterGroup rmts = ignite.cluster().forRemotes(); + + // Listen for messages from remote nodes to make sure that they received all the messages. + int msgCnt = rmts.nodes().size() * MESSAGES_NUM; + + CountDownLatch orderedLatch = new CountDownLatch(msgCnt); + CountDownLatch unorderedLatch = new CountDownLatch(msgCnt); + + localListen(ignite.message(ignite.cluster().forLocal()), orderedLatch, unorderedLatch); + + // Register listeners on all cluster nodes. + startListening(ignite.message(rmts)); + + // Send unordered messages to all remote nodes. + for (int i = 0; i < MESSAGES_NUM; i++) + ignite.message(rmts).send(TOPIC.UNORDERED, Integer.toString(i)); + + System.out.println(">>> Finished sending unordered messages."); + + // Send ordered messages to all remote nodes. + for (int i = 0; i < MESSAGES_NUM; i++) + ignite.message(rmts).sendOrdered(TOPIC.ORDERED, Integer.toString(i), 0); + + System.out.println(">>> Finished sending ordered messages."); + System.out.println(">>> Check output on all nodes for message printouts."); + System.out.println(">>> Will wait for messages acknowledgements from all remote nodes."); + + orderedLatch.await(); + unorderedLatch.await(); + + System.out.println(">>> Messaging example finished."); + } + } + + /** + * Start listening to messages on remote cluster nodes. + * + * @param msg Ignite messaging. + */ + private static void startListening(IgniteMessaging msg) { + // Add ordered message listener. + msg.remoteListen(TOPIC.ORDERED, new IgniteBiPredicate<UUID, String>() { + @IgniteInstanceResource + private Ignite ignite; + + @Override public boolean apply(UUID nodeId, String msg) { + System.out.println("Received ordered message [msg=" + msg + ", fromNodeId=" + nodeId + ']'); + + try { + ignite.message(ignite.cluster().forNodeId(nodeId)).send(TOPIC.ORDERED, msg); + } + catch (IgniteException e) { + e.printStackTrace(); + } + + return true; // Return true to continue listening. + } + }); + + // Add unordered message listener. + msg.remoteListen(TOPIC.UNORDERED, new IgniteBiPredicate<UUID, String>() { + @IgniteInstanceResource + private Ignite ignite; + + @Override public boolean apply(UUID nodeId, String msg) { + System.out.println("Received unordered message [msg=" + msg + ", fromNodeId=" + nodeId + ']'); + + try { + ignite.message(ignite.cluster().forNodeId(nodeId)).send(TOPIC.UNORDERED, msg); + } + catch (IgniteException e) { + e.printStackTrace(); + } + + return true; // Return true to continue listening. + } + }); + } + + /** + * Listen for messages from remote nodes. + * + * @param msg Ignite messaging. + * @param orderedLatch Latch for ordered messages acks. + * @param unorderedLatch Latch for unordered messages acks. + */ + private static void localListen( + IgniteMessaging msg, + final CountDownLatch orderedLatch, + final CountDownLatch unorderedLatch + ) { + msg.localListen(TOPIC.ORDERED, new IgniteBiPredicate<UUID, String>() { + @Override public boolean apply(UUID nodeId, String msg) { + orderedLatch.countDown(); + + // Return true to continue listening, false to stop. + return orderedLatch.getCount() > 0; + } + }); + + msg.localListen(TOPIC.UNORDERED, new IgniteBiPredicate<UUID, String>() { + @Override public boolean apply(UUID nodeId, String msg) { + unorderedLatch.countDown(); + + // Return true to continue listening, false to stop. + return unorderedLatch.getCount() > 0; + } + }); + } +} http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/be0e755b/examples/src/main/java/org/apache/ignite/examples/java7/messaging/MessagingPingPongExample.java ---------------------------------------------------------------------- diff --git a/examples/src/main/java/org/apache/ignite/examples/java7/messaging/MessagingPingPongExample.java b/examples/src/main/java/org/apache/ignite/examples/java7/messaging/MessagingPingPongExample.java new file mode 100644 index 0000000..cc2d114 --- /dev/null +++ b/examples/src/main/java/org/apache/ignite/examples/java7/messaging/MessagingPingPongExample.java @@ -0,0 +1,133 @@ +/* + * 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.java7.messaging; + +import org.apache.ignite.*; +import org.apache.ignite.cluster.*; +import org.apache.ignite.examples.java7.*; +import org.apache.ignite.internal.util.lang.*; +import org.apache.ignite.lang.*; +import org.apache.ignite.resources.*; + +import java.util.*; +import java.util.concurrent.*; + +/** + * Demonstrates simple message exchange between local and remote nodes. + * <p> + * To run this example you must have at least one remote node started. + * <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 MessagingPingPongExample { + /** + * Executes example. + * + * @param args Command line arguments, none required. + * @throws Exception If example execution failed. + */ + public static void main(String[] args) throws Exception { + // Game is played over the default ignite. + try (Ignite ignite = Ignition.start("examples/config/example-ignite.xml")) { + if (!ExamplesUtils.checkMinTopologySize(ignite.cluster(), 2)) + return; + + System.out.println(); + System.out.println(">>> Messaging ping-pong example started."); + + // Pick random remote node as a partner. + ClusterGroup nodeB = ignite.cluster().forRemotes().forRandom(); + + // Note that both nodeA and nodeB will always point to + // same nodes regardless of whether they were implicitly + // serialized and deserialized on another node as part of + // anonymous closure's state during its remote execution. + + // Set up remote player. + ignite.message(nodeB).remoteListen(null, new IgniteBiPredicate<UUID, String>() { + /** This will be injected on node listener comes to. */ + @IgniteInstanceResource + private Ignite ignite; + + @Override public boolean apply(UUID nodeId, String rcvMsg) { + System.out.println("Received message [msg=" + rcvMsg + ", sender=" + nodeId + ']'); + + try { + if ("PING".equals(rcvMsg)) { + ignite.message(ignite.cluster().forNodeId(nodeId)).send(null, "PONG"); + + return true; // Continue listening. + } + + return false; // Unsubscribe. + } + catch (IgniteException e) { + throw new GridClosureException(e); + } + } + }); + + int MAX_PLAYS = 10; + + final CountDownLatch cnt = new CountDownLatch(MAX_PLAYS); + + // Set up local player. + ignite.message().localListen(null, new IgniteBiPredicate<UUID, String>() { + @Override public boolean apply(UUID nodeId, String rcvMsg) { + System.out.println("Received message [msg=" + rcvMsg + ", sender=" + nodeId + ']'); + + try { + if (cnt.getCount() == 1) { + ignite.message(ignite.cluster().forNodeId(nodeId)).send(null, "STOP"); + + cnt.countDown(); + + return false; // Stop listening. + } + else if ("PONG".equals(rcvMsg)) + ignite.message(ignite.cluster().forNodeId(nodeId)).send(null, "PING"); + else + throw new RuntimeException("Received unexpected message: " + rcvMsg); + + cnt.countDown(); + + return true; // Continue listening. + } + catch (IgniteException e) { + throw new GridClosureException(e); + } + } + }); + + // Serve! + ignite.message(nodeB).send(null, "PING"); + + // Wait til the game is over. + try { + cnt.await(); + } + catch (InterruptedException e) { + System.err.println("Hm... let us finish the game!\n" + e); + } + } + } +} http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/be0e755b/examples/src/main/java/org/apache/ignite/examples/java7/messaging/MessagingPingPongListenActorExample.java ---------------------------------------------------------------------- diff --git a/examples/src/main/java/org/apache/ignite/examples/java7/messaging/MessagingPingPongListenActorExample.java b/examples/src/main/java/org/apache/ignite/examples/java7/messaging/MessagingPingPongListenActorExample.java new file mode 100644 index 0000000..f5c5514 --- /dev/null +++ b/examples/src/main/java/org/apache/ignite/examples/java7/messaging/MessagingPingPongListenActorExample.java @@ -0,0 +1,106 @@ +/* + * 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.java7.messaging; + +import org.apache.ignite.*; +import org.apache.ignite.cluster.*; +import org.apache.ignite.examples.java7.*; +import org.apache.ignite.messaging.*; + +import java.util.*; +import java.util.concurrent.*; + +/** + * Demonstrates messaging with {@link MessagingListenActor} convenience adapter. + * <p> + * To run this example you must have at least one remote node started. + * <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 MessagingPingPongListenActorExample { + /** + * Executes example. + * + * @param args Command line arguments, none required. + */ + public static void main(String[] args) { + // Game is played over the default ignite. + try (Ignite ignite = Ignition.start("examples/config/example-ignite.xml")) { + if (!ExamplesUtils.checkMinTopologySize(ignite.cluster(), 2)) + return; + + System.out.println(); + System.out.println(">>> Messaging ping-pong listen actor example started."); + + // Pick first remote node as a partner. + Collection<ClusterNode> rmtNodes = ignite.cluster().forRemotes().nodes(); + + ClusterGroup nodeB = ignite.cluster().forNode(rmtNodes.iterator().next()); + + // Note that both nodeA and nodeB will always point to + // same nodes regardless of whether they were implicitly + // serialized and deserialized on another node as part of + // anonymous closure's state during its remote execution. + + // Set up remote player. + ignite.message(nodeB).remoteListen(null, new MessagingListenActor<String>() { + @Override public void receive(UUID nodeId, String rcvMsg) { + System.out.println(rcvMsg); + + if ("PING".equals(rcvMsg)) + respond("PONG"); + else if ("STOP".equals(rcvMsg)) + stop(); + } + }); + + int MAX_PLAYS = 10; + + final CountDownLatch cnt = new CountDownLatch(MAX_PLAYS); + + // Set up local player. + ignite.message().localListen(null, new MessagingListenActor<String>() { + @Override protected void receive(UUID nodeId, String rcvMsg) throws IgniteException { + System.out.println(rcvMsg); + + if (cnt.getCount() == 1) + stop("STOP"); + else if ("PONG".equals(rcvMsg)) + respond("PING"); + + cnt.countDown(); + } + }); + + // Serve! + ignite.message(nodeB).send(null, "PING"); + + // Wait til the game is over. + try { + cnt.await(); + } + catch (InterruptedException e) { + System.err.println("Hm... let us finish the game!\n" + e); + } + } + } +} http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/be0e755b/examples/src/main/java/org/apache/ignite/examples/java7/messaging/package-info.java ---------------------------------------------------------------------- diff --git a/examples/src/main/java/org/apache/ignite/examples/java7/messaging/package-info.java b/examples/src/main/java/org/apache/ignite/examples/java7/messaging/package-info.java new file mode 100644 index 0000000..7947a3b --- /dev/null +++ b/examples/src/main/java/org/apache/ignite/examples/java7/messaging/package-info.java @@ -0,0 +1,22 @@ +/* + * 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 description. --> + * Demonstrates how to exchange messages between nodes. + */ +package org.apache.ignite.examples.java7.messaging;