Advertisement
tpeierls

MapStoreExample.java

Jan 17th, 2015
504
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 3.61 KB | None | 0 0
  1. package com.example.hazelcast.mapstore;
  2.  
  3. import com.hazelcast.core.*;
  4. import com.hazelcast.config.*;
  5.  
  6. import java.util.*;
  7. import java.util.concurrent.*;
  8. import static java.util.stream.Collectors.*;
  9.  
  10.  
  11. /**
  12.  * Minimal app to see if map loader/store in wildcard map config
  13.  * is properly set up in all nodes.
  14.  */
  15. public class MapStoreExample {
  16.    
  17.     /**
  18.      * Just enough to see whether the right load methods are called.
  19.      */
  20.     static class TinyMapLoader implements MapLoader<String, String> {
  21.        
  22.         final String name;
  23.        
  24.         TinyMapLoader(String name, Properties props) {
  25.             this.name = name;
  26.         }
  27.        
  28.         public String load(String key) {
  29.             return name + "-" + key;
  30.         }
  31.        
  32.         public Map<String, String> loadAll(Collection<String> keys) {
  33.             return keys.stream().collect(toMap(k -> k, k -> load(k)));
  34.         }
  35.        
  36.         public Set<String> loadAllKeys() {
  37.             return new HashSet<>(Arrays.asList("a", "b", "c"));
  38.         }
  39.     }
  40.    
  41.    
  42.     final String instanceName;
  43.     final int minNodes;
  44.     volatile HazelcastInstance hz = null;
  45.     volatile boolean initiator = false;
  46.    
  47.     MapStoreExample(String instanceName, int minNodes) {
  48.         this.instanceName = instanceName;
  49.         this.minNodes = minNodes;
  50.     }
  51.    
  52.    
  53.     public void whenEnoughNodes(Runnable task) throws InterruptedException {
  54.         Config config = new Config(instanceName);
  55.         config.getMapConfig("map-*").setMapStoreConfig(
  56.             new MapStoreConfig()
  57.                 .setFactoryImplementation((MapStoreFactory) TinyMapLoader::new)
  58.         );
  59.        
  60.         hz = Hazelcast.newHazelcastInstance(config);
  61.        
  62.         ITopic<Void> shutdownTopic = hz.getTopic("shutdown");
  63.         shutdownTopic.addMessageListener(m -> Hazelcast.shutdownAll());
  64.  
  65.         ITopic<Void> taskTopic = hz.getTopic("task");
  66.         taskTopic.addMessageListener(m -> {
  67.             task.run();
  68.             try {
  69.                 TimeUnit.SECONDS.sleep(1);
  70.             } catch (InterruptedException ex) {
  71.                 Thread.currentThread().interrupt();
  72.             }
  73.             if (initiator) {
  74.                 shutdownTopic.publish(null);
  75.             }
  76.         });
  77.        
  78.         hz.getCluster().addMembershipListener(new InitialMembershipListener() {
  79.             public void init(InitialMembershipEvent event) {
  80.                 if (event.getMembers().size() >= minNodes) {
  81.                     System.out.printf("****** %s is initiator ******%n", instanceName);
  82.                     initiator = true;
  83.                     taskTopic.publish(null);
  84.                 }
  85.             }
  86.             public void memberAdded(MembershipEvent event) {}
  87.             public void memberAttributeChanged(MemberAttributeEvent event) {}
  88.             public void memberRemoved(MembershipEvent event) {}
  89.         });
  90.     }
  91.    
  92.     public void run() {
  93.         dumpMap("map-rst");
  94.         dumpMap("map-uvw");
  95.         dumpMap("map-xyz");
  96.     }
  97.    
  98.     void dumpMap(String mapName) {
  99.         IMap<String, String> map = hz.getMap(mapName);
  100.         for (String key : map.keySet()) {
  101.             String value = map.get(key);
  102.             System.out.printf(
  103.                 "****** %s: %s.get(%s) -> %s ******%n",
  104.                 instanceName, mapName, key, value);
  105.         }
  106.     }
  107.    
  108.     public static void main(String... args) throws NumberFormatException, InterruptedException {
  109.         MapStoreExample mse = new MapStoreExample(args[0], Integer.parseInt(args[1]));
  110.         mse.whenEnoughNodes(() -> mse.run());
  111.     }
  112. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement