/chapters/chapter-persistence/persistence-store/src/main/java/com/blu/imdg/CacheStoreSample.java

https://github.com/srecon/ignite-book-code-samples · Java · 164 lines · 105 code · 33 blank · 26 comment · 12 complexity · 7bd22432abbb3928196296d76bd83f50 MD5 · raw file

  1. package com.blu.imdg;
  2. import com.blu.imdg.jdbc.PostgresDBStore;
  3. import com.blu.imdg.jdbc.model.Post;
  4. import com.blu.imdg.nosql.MongoDBStore;
  5. import com.blu.imdg.nosql.model.MongoPost;
  6. import java.time.LocalDate;
  7. import java.time.temporal.ChronoUnit;
  8. import javax.cache.configuration.FactoryBuilder;
  9. import org.apache.ignite.Ignite;
  10. import org.apache.ignite.IgniteCache;
  11. import org.apache.ignite.Ignition;
  12. import org.apache.ignite.cache.CacheAtomicityMode;
  13. import org.apache.ignite.configuration.CacheConfiguration;
  14. import org.apache.ignite.configuration.IgniteConfiguration;
  15. import org.apache.ignite.transactions.Transaction;
  16. import org.slf4j.Logger;
  17. import org.slf4j.LoggerFactory;
  18. import static org.apache.ignite.transactions.TransactionConcurrency.PESSIMISTIC;
  19. import static org.apache.ignite.transactions.TransactionIsolation.REPEATABLE_READ;
  20. /**
  21. * Created by isatimur on 8/9/16.
  22. */
  23. public class CacheStoreSample {
  24. /*
  25. Cache name to store posts.
  26. */
  27. private static final String POST_CACHE_NAME = CacheStoreSample.class.getSimpleName() + "-post";
  28. private static Logger LOGGER = LoggerFactory.getLogger(CacheStoreSample.class);
  29. private static final String POSTGRESQL = "postgresql";
  30. private static final String MONGODB = "mongodb";
  31. /**
  32. * This is an entry point of CacheStoreSample, the ignite configuration lies upon resources directory as
  33. * example-ignite.xml.
  34. *
  35. * @param args Command line arguments, none required.
  36. */
  37. public static void main(String[] args) throws Exception
  38. {
  39. if(args.length <= 0 ){
  40. LOGGER.error("Usages! java -jar .\\target\\cache-store-runnable.jar postgresql|mongodb");
  41. System.exit(0);
  42. }
  43. if(args[0].equalsIgnoreCase(POSTGRESQL)){
  44. jdbcStoreExample();
  45. } else if (args[0].equalsIgnoreCase(MONGODB)){
  46. nosqlStore();
  47. }
  48. }
  49. private static void jdbcStoreExample() throws Exception{
  50. //let's make a dynamic cache on the fly which is distributed across all running nodes.
  51. //the same configuration you would probably set in configuration xml format
  52. IgniteConfiguration cfg = new IgniteConfiguration();
  53. CacheConfiguration configuration = new CacheConfiguration();
  54. configuration.setName("dynamicCache");
  55. configuration.setAtomicityMode(CacheAtomicityMode.TRANSACTIONAL);
  56. configuration.setCacheStoreFactory(FactoryBuilder.factoryOf(PostgresDBStore.class));
  57. configuration.setReadThrough(true);
  58. configuration.setWriteThrough(true);
  59. configuration.setWriteBehindEnabled(true);
  60. log("Start. PersistenceStore example.");
  61. cfg.setCacheConfiguration(configuration);
  62. try (Ignite ignite = Ignition.start(cfg)) {
  63. //create cache if it doesn't exist
  64. int count = 10;
  65. try (IgniteCache<String, Post> igniteCache = ignite.getOrCreateCache(configuration)) {
  66. try (Transaction tx = ignite.transactions().txStart(PESSIMISTIC, REPEATABLE_READ)) {
  67. //let us clear
  68. for (int i = 1; i <= count; i++)
  69. igniteCache.put("_" + i, new Post("_" + i, "title-" + i, "description-" + i, LocalDate.now().plus(i, ChronoUnit.DAYS), "author-" + i));
  70. tx.commit();
  71. for (int i = 1; i < count; i += 2) {
  72. igniteCache.clear("_" + i);
  73. log("Clear every odd key: " + i);
  74. }
  75. for (long i = 1; i <= count; i++)
  76. log("Local peek at [key=_" + i + ", val=" + igniteCache.localPeek("_" + i) + ']');
  77. for (long i = 1; i <= count; i++)
  78. log("Got [key=_" + i + ", val=" + igniteCache.get("_" + i) + ']');
  79. tx.commit();
  80. }
  81. }
  82. log("PersistenceStore example finished.");
  83. //ignite.destroyCache("dynamicCache");
  84. Thread.sleep(Integer.MAX_VALUE);
  85. }
  86. }
  87. private static void nosqlStore() {
  88. //let's make a dynamic cache on the fly which is distributed across all running nodes.
  89. //the same configuration you would probably set in configuration xml format
  90. IgniteConfiguration cfg = new IgniteConfiguration();
  91. CacheConfiguration configuration = new CacheConfiguration();
  92. configuration.setName("mongoDynamicCache");
  93. configuration.setAtomicityMode(CacheAtomicityMode.TRANSACTIONAL);
  94. configuration.setCacheStoreFactory(FactoryBuilder.factoryOf(MongoDBStore.class));
  95. configuration.setReadThrough(true);
  96. configuration.setWriteThrough(true);
  97. configuration.setWriteBehindEnabled(true);
  98. log("Start. PersistenceStore example.");
  99. cfg.setCacheConfiguration(configuration);
  100. try (Ignite ignite = Ignition.start(cfg)) {
  101. //create cache if it doesn't exist
  102. int count = 10;
  103. try (IgniteCache<String, MongoPost> igniteCache = ignite.getOrCreateCache(configuration)) {
  104. try (Transaction tx = ignite.transactions().txStart(PESSIMISTIC, REPEATABLE_READ)) {
  105. //let us clear
  106. for (int i = 1; i <= count; i++)
  107. igniteCache.put("_" + i, new MongoPost("_" + i, "title-" + i, "description-" + i, LocalDate.now().plus(i, ChronoUnit.DAYS), "author-" + i));
  108. for (int i = 1; i < count; i += 2) {
  109. igniteCache.clear("_" + i);
  110. log("Clear every odd key: " + i);
  111. }
  112. for (long i = 1; i <= count; i++)
  113. log("Local peek at [key=_" + i + ", val=" + igniteCache.localPeek("_" + i) + ']');
  114. for (long i = 1; i <= count; i++)
  115. log("Got [key=_" + i + ", val=" + igniteCache.get("_" + i) + ']');
  116. tx.commit();
  117. }
  118. }
  119. log("PersistenceStore example finished.");
  120. ignite.destroyCache("mongoDynamicCache");
  121. }
  122. }
  123. /**
  124. * Prints message to logger.
  125. *
  126. * @param msg String.
  127. */
  128. private static void log(String msg) {
  129. LOGGER.info("\t" + msg);
  130. }
  131. }