Advertisement
Guest User

Untitled

a guest
Apr 24th, 2015
334
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.48 KB | None | 0 0
  1. // Node.java
  2.  
  3. package org.ignite.myexamples.serverclient;
  4.  
  5. import org.apache.ignite.*;
  6. import org.apache.ignite.cache.CacheMode;
  7. import org.apache.ignite.configuration.CacheConfiguration;
  8. import org.apache.ignite.configuration.ConnectorConfiguration;
  9. import org.apache.ignite.configuration.IgniteConfiguration;
  10. import org.apache.ignite.spi.discovery.tcp.TcpDiscoverySpi;
  11. import org.apache.ignite.spi.discovery.tcp.ipfinder.vm.TcpDiscoveryVmIpFinder;
  12. import static org.apache.ignite.cache.CacheRebalanceMode.SYNC;
  13. import static org.apache.ignite.cache.CacheWriteSynchronizationMode.FULL_SYNC;
  14. import java.util.*;
  15.  
  16. public class Node {
  17.  
  18.     private static final String CACHE_NAME = "mycache";
  19.  
  20.  
  21.     /**
  22.      * Create Ignite configuration with cache
  23.      *
  24.      */
  25.  
  26.     public static String getCacheNameName() {
  27.         return CACHE_NAME;
  28.     }
  29.  
  30.     public static CacheConfiguration cacheConf()  throws IgniteException {
  31.         CacheConfiguration cacheCfg = new CacheConfiguration();
  32.  
  33.         cacheCfg.setName(CACHE_NAME);
  34.         cacheCfg.setCacheMode(CacheMode.REPLICATED);
  35.         cacheCfg.setRebalanceMode(SYNC);
  36.         cacheCfg.setWriteSynchronizationMode(FULL_SYNC);
  37.  
  38.         return  cacheCfg;
  39.     }
  40.  
  41.     public static IgniteConfiguration configuration(String ip, boolean mode) throws IgniteException {
  42.         IgniteConfiguration cfg = new IgniteConfiguration();
  43.  
  44.         cfg.setLocalHost(ip);
  45.         cfg.setClientMode(mode);
  46.  
  47.         cfg.setCacheConfiguration(cacheConf());
  48.  
  49.         TcpDiscoverySpi discoSpi = new TcpDiscoverySpi();
  50.  
  51.         TcpDiscoveryVmIpFinder ipFinder = new TcpDiscoveryVmIpFinder();
  52.  
  53.         ipFinder.setAddresses(Collections.singletonList(ip + ":47500..47509"));
  54.  
  55.         discoSpi.setIpFinder(ipFinder);
  56.  
  57.         cfg.setDiscoverySpi(discoSpi);
  58.  
  59.         return cfg;
  60.     }
  61.  
  62. }
  63.  
  64.  
  65. // Server.java
  66.  
  67. package org.ignite.myexamples.serverclient;
  68.  
  69. import org.apache.ignite.Ignite;
  70. import org.apache.ignite.IgniteCache;
  71. import org.apache.ignite.IgniteException;
  72. import org.apache.ignite.Ignition;
  73. import org.apache.ignite.cache.CachePeekMode;
  74.  
  75. import java.text.DateFormat;
  76. import java.text.SimpleDateFormat;
  77. import java.util.Date;
  78.  
  79. /**
  80.  * Server node
  81.  */
  82. public class Server extends Node {
  83.     public static void main(String[] args) throws IgniteException, InterruptedException {
  84.         if (args.length == 0) {
  85.             System.out.println("IP address in command-line not found");
  86.             System.exit(0);
  87.         }
  88.         // get ip address from 1st command-line argument
  89.         String ip = args[0];
  90.  
  91.         // Start server node
  92.         try (Ignite ignite = Ignition.start(configuration(ip, false))) {
  93.             // Get cache
  94.             try (IgniteCache<Integer, String> cache = ignite.getOrCreateCache(cacheConf())) {
  95.                 // server nodes print out total size of cache every 5sec
  96.                 DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
  97.  
  98.                 while (true) {
  99.                     Date date = new Date();
  100.                     System.out.println(dateFormat.format(date) + " " + getCacheNameName() +
  101.                             " size: " + Integer.toString(cache.size(CachePeekMode.PRIMARY))
  102.                     );
  103.                     Thread.sleep(5000);
  104.                 }
  105.             }
  106.         }
  107.     }
  108. }
  109.  
  110.  
  111. // Client.java
  112.  
  113. package org.ignite.myexamples.serverclient;
  114.  
  115. import org.apache.ignite.Ignite;
  116. import org.apache.ignite.IgniteCache;
  117. import org.apache.ignite.IgniteException;
  118. import org.apache.ignite.Ignition;
  119.  
  120. /**
  121.  * Client node
  122.  */
  123. public class Client extends Node {
  124.     public static void main(String[] args) throws IgniteException {
  125.         if (args.length == 0) {
  126.             System.out.println("IP address in command-line not found");
  127.             System.exit(0);
  128.         }
  129.         // get ip address from 1st command-line argument
  130.         String ip = args[0];
  131.  
  132.         // Start client node
  133.         try (Ignite ignite = Ignition.start(configuration(ip, true))) {
  134.             // Get cache created by server node
  135.             IgniteCache<Integer, String> cache = ignite.getOrCreateCache(cacheConf());
  136.  
  137.             // Put keys into cache
  138.             final int keyCnt = 20;
  139.  
  140.             // Store keys in cache.
  141.             for (int i = 0; i < keyCnt; i++) {
  142.                 String val = Integer.toString(i);
  143.                 cache.put(i, val);
  144.                 System.out.println("put [key=" + i + ", val=" + val + ']');
  145.             }
  146.         }
  147.     }
  148. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement