IGNITE-45 - Examples
Project: http://git-wip-us.apache.org/repos/asf/incubator-ignite/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-ignite/commit/d48f5802 Tree: http://git-wip-us.apache.org/repos/asf/incubator-ignite/tree/d48f5802 Diff: http://git-wip-us.apache.org/repos/asf/incubator-ignite/diff/d48f5802 Branch: refs/heads/ignite-497-stick Commit: d48f580276a2bbc275340c3c8bb42c463d5c863a Parents: 75a8dcc Author: Valentin Kulichenko <vkuliche...@gridgain.com> Authored: Sun Mar 22 21:53:00 2015 -0700 Committer: Valentin Kulichenko <vkuliche...@gridgain.com> Committed: Sun Mar 22 21:53:00 2015 -0700 ---------------------------------------------------------------------- .../datagrid/store/CacheStoreExample.java | 94 -------------- .../CacheStoreExampleCacheConfigurator.java | 125 ------------------- .../store/CacheStoreLoadDataExample.java | 69 ---------- .../store/dummy/CacheDummyStoreExample.java | 112 +++++++++++++++++ .../hibernate/CacheHibernateStoreExample.java | 112 +++++++++++++++++ .../store/jdbc/CacheJdbcStoreExample.java | 112 +++++++++++++++++ 6 files changed, 336 insertions(+), 288 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/d48f5802/examples/src/main/java/org/apache/ignite/examples/datagrid/store/CacheStoreExample.java ---------------------------------------------------------------------- diff --git a/examples/src/main/java/org/apache/ignite/examples/datagrid/store/CacheStoreExample.java b/examples/src/main/java/org/apache/ignite/examples/datagrid/store/CacheStoreExample.java deleted file mode 100644 index 39d5452..0000000 --- a/examples/src/main/java/org/apache/ignite/examples/datagrid/store/CacheStoreExample.java +++ /dev/null @@ -1,94 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.apache.ignite.examples.datagrid.store; - -import org.apache.ignite.*; -import org.apache.ignite.configuration.*; -import org.apache.ignite.examples.*; -import org.apache.ignite.transactions.*; - -import java.util.*; - -import static org.apache.ignite.examples.datagrid.store.CacheStoreExampleCacheConfigurator.*; - -/** - * Demonstrates usage of cache with underlying persistent store configured. - * <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 CacheStoreExample { - /** Global person ID to use across entire example. */ - private static final Long id = Math.abs(UUID.randomUUID().getLeastSignificantBits()); - - /** - * Executes example. - * - * @param args Command line arguments, none required. - * @throws IgniteException If example execution failed. - */ - public static void main(String[] args) throws IgniteException { - // To start ignite with desired configuration uncomment the appropriate line. - try (Ignite ignite = Ignition.start("examples/config/example-ignite.xml")) { - System.out.println(); - System.out.println(">>> Cache store example started."); - System.out.println(">>> Store: " + STORE); - - CacheConfiguration<Long, Person> cacheCfg = CacheStoreExampleCacheConfigurator.cacheConfiguration(); - - try (IgniteCache<Long, Person> cache = ignite.createCache(cacheCfg)) { - try (Transaction tx = ignite.transactions().txStart()) { - Person val = cache.get(id); - - System.out.println("Read value: " + val); - - val = cache.getAndPut(id, new Person(id, "Isaac", "Newton")); - - System.out.println("Overwrote old value: " + val); - - val = cache.get(id); - - System.out.println("Read value: " + val); - - tx.commit(); - } - - System.out.println("Read value after commit: " + cache.get(id)); - - // If example run with CacheJdbcPojoStore. - // Example of CacheJdbcPojoStore special features. - if (STORE.equals(AUTO)) { - System.out.println(">>> Example of CacheJdbcPojoStore special feature: load from DB with custom SQL."); - - cache.clear(); - - System.out.println("Cache size: " + cache.size()); - - // Load values from DB into store with custom SQL. - cache.loadCache(null, "java.lang.Long", "select * from PERSON where id = 2"); - - System.out.println("Cache size: " + cache.size()); - System.out.println("Person: " + cache.get(2L)); - } - } - } - } -} http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/d48f5802/examples/src/main/java/org/apache/ignite/examples/datagrid/store/CacheStoreExampleCacheConfigurator.java ---------------------------------------------------------------------- diff --git a/examples/src/main/java/org/apache/ignite/examples/datagrid/store/CacheStoreExampleCacheConfigurator.java b/examples/src/main/java/org/apache/ignite/examples/datagrid/store/CacheStoreExampleCacheConfigurator.java deleted file mode 100644 index d44c2a1..0000000 --- a/examples/src/main/java/org/apache/ignite/examples/datagrid/store/CacheStoreExampleCacheConfigurator.java +++ /dev/null @@ -1,125 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.apache.ignite.examples.datagrid.store; - -import org.apache.ignite.*; -import org.apache.ignite.cache.*; -import org.apache.ignite.cache.store.*; -import org.apache.ignite.configuration.*; -import org.apache.ignite.examples.datagrid.store.dummy.*; -import org.apache.ignite.examples.datagrid.store.hibernate.*; -import org.apache.ignite.examples.datagrid.store.jdbc.*; -import org.apache.ignite.internal.util.typedef.*; - -import javax.cache.configuration.*; -import java.sql.*; -import java.util.*; - -import static org.apache.ignite.cache.CacheAtomicityMode.*; - -/** - * Starts up an empty node with example cache and store configuration. - */ -public class CacheStoreExampleCacheConfigurator { - /** Use org.apache.ignite.examples.datagrid.store.dummy.CacheDummyPersonStore to run example. */ - public static final String DUMMY = "DUMMY"; - - /** Use org.apache.ignite.examples.datagrid.store.jdbc.CacheJdbcPersonStore to run example. */ - public static final String SIMPLE_JDBC = "SIMPLE_JDBC"; - - /** Use org.apache.ignite.examples.datagrid.store.hibernate.CacheHibernatePersonStore to run example. */ - public static final String HIBERNATE = "HIBERNATE"; - - /** Use org.apache.ignite.examples.datagrid.store.jdbc.CacheJdbcPojoPersonStore to run example. */ - public static final String AUTO = "AUTO"; - - /** Store to use. */ - public static final String STORE = DUMMY; - - /** - * Configure ignite. - * - * @return Ignite configuration. - * @throws IgniteException If failed. - */ - public static CacheConfiguration<Long, Person> cacheConfiguration() throws IgniteException { - CacheConfiguration<Long, Person> cacheCfg = new CacheConfiguration<>(); - - // Set atomicity as transaction, since we are showing transactions in example. - cacheCfg.setAtomicityMode(TRANSACTIONAL); - - cacheCfg.setCacheStoreFactory(new Factory<CacheStore<? super Long, ? super Person>>() { - @Override public CacheStore<? super Long, ? super Person> create() { - CacheStore<Long, Person> store; - - switch (STORE) { - case DUMMY: - store = new CacheDummyPersonStore(); - break; - - case SIMPLE_JDBC: - store = new CacheJdbcPersonStore(); - break; - - case HIBERNATE: - store = new CacheHibernatePersonStore(); - break; - - case AUTO: - store = new CacheJdbcPojoPersonStore(); - break; - - default: - throw new IllegalStateException("Unexpected store configured: " + STORE); - } - - return store; - } - }); - - if (STORE.equals(AUTO)) - cacheCfg.setTypeMetadata(typeMetadata()); - - cacheCfg.setReadThrough(true); - cacheCfg.setWriteThrough(true); - - return cacheCfg; - } - - /** - * @return Type mapping description. - */ - private static Collection<CacheTypeMetadata> typeMetadata() { - CacheTypeMetadata tm = new CacheTypeMetadata(); - - tm.setDatabaseTable("PERSON"); - - tm.setKeyType("java.lang.Long"); - tm.setValueType("org.apache.ignite.examples.datagrid.store.model.Person"); - - tm.setKeyFields(F.asList(new CacheTypeFieldMetadata("ID", Types.BIGINT, "id", Long.class))); - - tm.setValueFields(F.asList( - new CacheTypeFieldMetadata("ID", Types.BIGINT, "id", long.class), - new CacheTypeFieldMetadata("FIRST_NAME", Types.VARCHAR, "firstName", String.class), - new CacheTypeFieldMetadata("LAST_NAME", Types.VARCHAR, "lastName", String.class) - )); - - return F.asList(tm); - } -} http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/d48f5802/examples/src/main/java/org/apache/ignite/examples/datagrid/store/CacheStoreLoadDataExample.java ---------------------------------------------------------------------- diff --git a/examples/src/main/java/org/apache/ignite/examples/datagrid/store/CacheStoreLoadDataExample.java b/examples/src/main/java/org/apache/ignite/examples/datagrid/store/CacheStoreLoadDataExample.java deleted file mode 100644 index d59e004..0000000 --- a/examples/src/main/java/org/apache/ignite/examples/datagrid/store/CacheStoreLoadDataExample.java +++ /dev/null @@ -1,69 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.apache.ignite.examples.datagrid.store; - -import org.apache.ignite.*; -import org.apache.ignite.configuration.*; -import org.apache.ignite.examples.*; -import org.apache.ignite.lang.*; - -/** - * Loads data on all cache nodes from persistent store at cache startup by calling - * {@link IgniteCache#loadCache(IgniteBiPredicate, Object...)} method. - * <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 CacheStoreLoadDataExample { - /** Heap size required to run this example. */ - public static final int MIN_MEMORY = 1024 * 1024 * 1024; - - /** Number of entries to load. */ - private static final int ENTRY_COUNT = 100_000; - - /** - * Executes example. - * - * @param args Command line arguments, none required. - * @throws 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-ignite.xml")) { - System.out.println(); - System.out.println(">>> Cache store load data example started."); - - CacheConfiguration<Long, Person> cacheCfg = CacheStoreExampleCacheConfigurator.cacheConfiguration(); - - try (IgniteCache<Long, Person> cache = ignite.createCache(cacheCfg)) { - long start = System.currentTimeMillis(); - - // Start loading cache from persistent store on all caching nodes. - cache.loadCache(null, ENTRY_COUNT); - - long end = System.currentTimeMillis(); - - System.out.println(">>> Loaded " + cache.size() + " keys with backups in " + (end - start) + "ms."); - } - } - } -} http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/d48f5802/examples/src/main/java/org/apache/ignite/examples/datagrid/store/dummy/CacheDummyStoreExample.java ---------------------------------------------------------------------- diff --git a/examples/src/main/java/org/apache/ignite/examples/datagrid/store/dummy/CacheDummyStoreExample.java b/examples/src/main/java/org/apache/ignite/examples/datagrid/store/dummy/CacheDummyStoreExample.java new file mode 100644 index 0000000..d3d6977 --- /dev/null +++ b/examples/src/main/java/org/apache/ignite/examples/datagrid/store/dummy/CacheDummyStoreExample.java @@ -0,0 +1,112 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.ignite.examples.datagrid.store.dummy; + +import org.apache.ignite.*; +import org.apache.ignite.cache.store.*; +import org.apache.ignite.configuration.*; +import org.apache.ignite.examples.*; +import org.apache.ignite.examples.datagrid.store.*; +import org.apache.ignite.transactions.*; + +import javax.cache.configuration.*; +import java.util.*; + +import static org.apache.ignite.cache.CacheAtomicityMode.*; + +/** + * Demonstrates usage of cache with underlying persistent store configured. + * <p> + * This example uses {@link CacheDummyPersonStore} as a persistent store. + * <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 CacheDummyStoreExample { + /** Heap size required to run this example. */ + public static final int MIN_MEMORY = 1024 * 1024 * 1024; + + /** Number of entries to load. */ + private static final int ENTRY_COUNT = 100_000; + + /** Global person ID to use across entire example. */ + private static final Long id = Math.abs(UUID.randomUUID().getLeastSignificantBits()); + + /** + * Executes example. + * + * @param args Command line arguments, none required. + * @throws IgniteException If example execution failed. + */ + public static void main(String[] args) throws IgniteException { + ExamplesUtils.checkMinMemory(MIN_MEMORY); + + // To start ignite with desired configuration uncomment the appropriate line. + try (Ignite ignite = Ignition.start("examples/config/example-ignite.xml")) { + System.out.println(); + System.out.println(">>> Cache store example started."); + + CacheConfiguration<Long, Person> cacheCfg = new CacheConfiguration<>(); + + // Set atomicity as transaction, since we are showing transactions in example. + cacheCfg.setAtomicityMode(TRANSACTIONAL); + + cacheCfg.setCacheStoreFactory(new Factory<CacheStore<? super Long, ? super Person>>() { + @Override public CacheStore<? super Long, ? super Person> create() { + return new CacheDummyPersonStore(); + } + }); + + cacheCfg.setReadThrough(true); + cacheCfg.setWriteThrough(true); + + try (IgniteCache<Long, Person> cache = ignite.createCache(cacheCfg)) { + long start = System.currentTimeMillis(); + + // Start loading cache from persistent store on all caching nodes. + cache.loadCache(null, ENTRY_COUNT); + + long end = System.currentTimeMillis(); + + System.out.println(">>> Loaded " + cache.size() + " keys with backups in " + (end - start) + "ms."); + + // Start transaction and make several operations with write/read-through. + try (Transaction tx = ignite.transactions().txStart()) { + Person val = cache.get(id); + + System.out.println("Read value: " + val); + + val = cache.getAndPut(id, new Person(id, "Isaac", "Newton")); + + System.out.println("Overwrote old value: " + val); + + val = cache.get(id); + + System.out.println("Read value: " + val); + + tx.commit(); + } + + System.out.println("Read value after commit: " + cache.get(id)); + } + } + } +} http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/d48f5802/examples/src/main/java/org/apache/ignite/examples/datagrid/store/hibernate/CacheHibernateStoreExample.java ---------------------------------------------------------------------- diff --git a/examples/src/main/java/org/apache/ignite/examples/datagrid/store/hibernate/CacheHibernateStoreExample.java b/examples/src/main/java/org/apache/ignite/examples/datagrid/store/hibernate/CacheHibernateStoreExample.java new file mode 100644 index 0000000..0eed3a9 --- /dev/null +++ b/examples/src/main/java/org/apache/ignite/examples/datagrid/store/hibernate/CacheHibernateStoreExample.java @@ -0,0 +1,112 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.ignite.examples.datagrid.store.hibernate; + +import org.apache.ignite.*; +import org.apache.ignite.cache.store.*; +import org.apache.ignite.configuration.*; +import org.apache.ignite.examples.*; +import org.apache.ignite.examples.datagrid.store.*; +import org.apache.ignite.transactions.*; + +import javax.cache.configuration.*; +import java.util.*; + +import static org.apache.ignite.cache.CacheAtomicityMode.*; + +/** + * Demonstrates usage of cache with underlying persistent store configured. + * <p> + * This example uses {@link CacheHibernatePersonStore} as a persistent store. + * <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 CacheHibernateStoreExample { + /** Heap size required to run this example. */ + public static final int MIN_MEMORY = 1024 * 1024 * 1024; + + /** Number of entries to load. */ + private static final int ENTRY_COUNT = 100_000; + + /** Global person ID to use across entire example. */ + private static final Long id = Math.abs(UUID.randomUUID().getLeastSignificantBits()); + + /** + * Executes example. + * + * @param args Command line arguments, none required. + * @throws IgniteException If example execution failed. + */ + public static void main(String[] args) throws IgniteException { + ExamplesUtils.checkMinMemory(MIN_MEMORY); + + // To start ignite with desired configuration uncomment the appropriate line. + try (Ignite ignite = Ignition.start("examples/config/example-ignite.xml")) { + System.out.println(); + System.out.println(">>> Cache store example started."); + + CacheConfiguration<Long, Person> cacheCfg = new CacheConfiguration<>(); + + // Set atomicity as transaction, since we are showing transactions in example. + cacheCfg.setAtomicityMode(TRANSACTIONAL); + + cacheCfg.setCacheStoreFactory(new Factory<CacheStore<? super Long, ? super Person>>() { + @Override public CacheStore<? super Long, ? super Person> create() { + return new CacheHibernatePersonStore(); + } + }); + + cacheCfg.setReadThrough(true); + cacheCfg.setWriteThrough(true); + + try (IgniteCache<Long, Person> cache = ignite.createCache(cacheCfg)) { + long start = System.currentTimeMillis(); + + // Start loading cache from persistent store on all caching nodes. + cache.loadCache(null, ENTRY_COUNT); + + long end = System.currentTimeMillis(); + + System.out.println(">>> Loaded " + cache.size() + " keys with backups in " + (end - start) + "ms."); + + // Start transaction and make several operations with write/read-through. + try (Transaction tx = ignite.transactions().txStart()) { + Person val = cache.get(id); + + System.out.println("Read value: " + val); + + val = cache.getAndPut(id, new Person(id, "Isaac", "Newton")); + + System.out.println("Overwrote old value: " + val); + + val = cache.get(id); + + System.out.println("Read value: " + val); + + tx.commit(); + } + + System.out.println("Read value after commit: " + cache.get(id)); + } + } + } +} http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/d48f5802/examples/src/main/java/org/apache/ignite/examples/datagrid/store/jdbc/CacheJdbcStoreExample.java ---------------------------------------------------------------------- diff --git a/examples/src/main/java/org/apache/ignite/examples/datagrid/store/jdbc/CacheJdbcStoreExample.java b/examples/src/main/java/org/apache/ignite/examples/datagrid/store/jdbc/CacheJdbcStoreExample.java new file mode 100644 index 0000000..7eef116 --- /dev/null +++ b/examples/src/main/java/org/apache/ignite/examples/datagrid/store/jdbc/CacheJdbcStoreExample.java @@ -0,0 +1,112 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.ignite.examples.datagrid.store.jdbc; + +import org.apache.ignite.*; +import org.apache.ignite.cache.store.*; +import org.apache.ignite.configuration.*; +import org.apache.ignite.examples.*; +import org.apache.ignite.examples.datagrid.store.*; +import org.apache.ignite.transactions.*; + +import javax.cache.configuration.*; +import java.util.*; + +import static org.apache.ignite.cache.CacheAtomicityMode.*; + +/** + * Demonstrates usage of cache with underlying persistent store configured. + * <p> + * This example uses {@link CacheJdbcPersonStore} as a persistent store. + * <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 CacheJdbcStoreExample { + /** Heap size required to run this example. */ + public static final int MIN_MEMORY = 1024 * 1024 * 1024; + + /** Number of entries to load. */ + private static final int ENTRY_COUNT = 100_000; + + /** Global person ID to use across entire example. */ + private static final Long id = Math.abs(UUID.randomUUID().getLeastSignificantBits()); + + /** + * Executes example. + * + * @param args Command line arguments, none required. + * @throws IgniteException If example execution failed. + */ + public static void main(String[] args) throws IgniteException { + ExamplesUtils.checkMinMemory(MIN_MEMORY); + + // To start ignite with desired configuration uncomment the appropriate line. + try (Ignite ignite = Ignition.start("examples/config/example-ignite.xml")) { + System.out.println(); + System.out.println(">>> Cache store example started."); + + CacheConfiguration<Long, Person> cacheCfg = new CacheConfiguration<>(); + + // Set atomicity as transaction, since we are showing transactions in example. + cacheCfg.setAtomicityMode(TRANSACTIONAL); + + cacheCfg.setCacheStoreFactory(new Factory<CacheStore<? super Long, ? super Person>>() { + @Override public CacheStore<? super Long, ? super Person> create() { + return new CacheJdbcPersonStore(); + } + }); + + cacheCfg.setReadThrough(true); + cacheCfg.setWriteThrough(true); + + try (IgniteCache<Long, Person> cache = ignite.createCache(cacheCfg)) { + long start = System.currentTimeMillis(); + + // Start loading cache from persistent store on all caching nodes. + cache.loadCache(null, ENTRY_COUNT); + + long end = System.currentTimeMillis(); + + System.out.println(">>> Loaded " + cache.size() + " keys with backups in " + (end - start) + "ms."); + + // Start transaction and make several operations with write/read-through. + try (Transaction tx = ignite.transactions().txStart()) { + Person val = cache.get(id); + + System.out.println("Read value: " + val); + + val = cache.getAndPut(id, new Person(id, "Isaac", "Newton")); + + System.out.println("Overwrote old value: " + val); + + val = cache.get(id); + + System.out.println("Read value: " + val); + + tx.commit(); + } + + System.out.println("Read value after commit: " + cache.get(id)); + } + } + } +}