Advertisement
tpeierls

Extending Hazelcast MultiMap

Oct 17th, 2014
307
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.99 KB | None | 0 0
  1. package com.example.hazelcast.mmex;
  2.  
  3. import com.google.common.collect.*;
  4. import com.hazelcast.core.*;
  5. import java.util.*;
  6. import java.util.concurrent.*;
  7.  
  8.  
  9. /**
  10.  * Adds putAll function for MultiMap that batches all puts
  11.  * so that they occur on same node.
  12.  */
  13. public class MultiMapEx<K, V> {
  14.    
  15.     /**
  16.      * Distributed task to perform batch put.
  17.      */
  18.     static class Task<K, V> implements Callable<Void>,
  19.                                        java.io.Serializable,
  20.                                        HazelcastInstanceAware {
  21.         final K key;
  22.         final List<V> values;
  23.         final String mapName;
  24.         transient volatile HazelcastInstance hz;
  25.  
  26.        
  27.         Task(K key, List<V> values, String mapName) {
  28.             this.key = key;
  29.             this.values = values;
  30.             this.mapName = mapName;
  31.         }
  32.  
  33.        
  34.         @Override
  35.         public Void call() {
  36.             MultiMap<K, V> map = hz.getMultiMap(mapName);
  37.             for (V value : values) {
  38.                 map.put(key, value);
  39.             }
  40.             return null;
  41.         }
  42.        
  43.         @Override
  44.         public void setHazelcastInstance(HazelcastInstance hz) {
  45.             this.hz = hz;
  46.         }
  47.     }
  48.    
  49.     /** Name of common executor service for all MultiMapEx instances */
  50.     private static final String EXEC_NAME = "MultiMapEx.executorService";
  51.  
  52.  
  53.     final HazelcastInstance hz;
  54.     final String mapName;
  55.     final IExecutorService exec;
  56.    
  57.    
  58.     public MultiMapEx(HazelcastInstance hz, String mapName) {
  59.         this.hz = hz;
  60.         this.mapName = mapName;
  61.         this.exec = hz.getExecutorService(EXEC_NAME);
  62.     }
  63.  
  64.  
  65.     /**
  66.      * Access to underlying MultiMap.
  67.      */
  68.     public MultiMap<K, V> getMultiMap() {
  69.         return hz.getMultiMap(mapName);
  70.     }
  71.    
  72.     public void putAll(final K key, List<V> values)
  73.             throws InterruptedException, ExecutionException {
  74.                
  75.         Task<K, V> task = new Task<K, V>(key, values, mapName);
  76.         exec.submitToKeyOwner(task, key).get();
  77.     }
  78.  
  79.  
  80.     /**
  81.      * Test program, creates two HazelcastInstances,
  82.      * and runs a putAll followed by a get on each of them
  83.      * to make sure that we run it on an instance that is
  84.      * not the key owner.
  85.      */
  86.     public static void main(String... args) throws Exception {
  87.         HazelcastInstance hz1 = Hazelcast.newHazelcastInstance();
  88.         HazelcastInstance hz2 = Hazelcast.newHazelcastInstance();
  89.         try {
  90.             for (HazelcastInstance hz : Hazelcast.getAllHazelcastInstances()) {
  91.                 MultiMapEx<String, Integer> mme = new MultiMapEx<String, Integer>(hz, "testmap");
  92.                 mme.putAll("foo", ImmutableList.<Integer>of(1, 2, 3));
  93.                 ImmutableList<Integer> values = ImmutableList.copyOf(mme.getMultiMap().get("foo"));
  94.                 System.out.println("foo values: " + values);
  95.             }
  96.         } finally {
  97.             Hazelcast.shutdownAll();
  98.         }
  99.     }
  100. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement