package org.infinispan.demo;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import org.infinispan.client.hotrod.RemoteCache;
import org.infinispan.client.hotrod.RemoteCacheManager;
import org.infinispan.client.hotrod.configuration.ConfigurationBuilder;
import org.infinispan.client.hotrod.impl.ConfigurationProperties;

public class Metadata {
    
    public static final String ISPN_IP = "127.0.0.1";
    public static final String KEY = "key";
    public static final String VALUE_PREXIF = "value";

    public static void main(String[] args) {
        ExecutorService es = Executors.newFixedThreadPool(10);
        Replacer r1 = new Replacer("default");
        Replacer r2 = new Replacer("default");
        es.execute(r1);
        es.execute(r2);
        es.shutdown();
    }
    
    private static class Replacer implements Runnable {
        
        private final String threadName = Thread.currentThread().getName();
        private final String cacheName;
        private final RemoteCacheManager cacheManager;
        
        public Replacer(String cacheName) {
            this.cacheName = cacheName;
            
            ConfigurationBuilder builder = new ConfigurationBuilder();
            builder.addServer().host(ISPN_IP).port(ConfigurationProperties.DEFAULT_HOTROD_PORT);
            cacheManager = new RemoteCacheManager(builder.build());
        }
        
        public void run() {
            RemoteCache<Object, Object> cache = cacheManager.getCache(cacheName);
            
            for (int i = 0; i < 10; i++) {
                boolean replaced = cache.replaceWithVersion(KEY, VALUE_PREXIF + i, i);
                if (replaced) {
                    System.out.println(String.format("[%s] %d: replacing with result %b", threadName, i, replaced));
                } else {
                    System.out.println(String.format("[%s] Failed to replace, justing just put", threadName));
                    cache.put(KEY, VALUE_PREXIF + i);
                    System.out.println(String.format("[%s] Stored with version %d", threadName, cache.getWithMetadata(KEY).getVersion()));
                }
            }
            
            cacheManager.stop();
        }
    }

}

@@ -376,20 +376,16 @@
 adata %7B%0A
-    
 %0A    pub
@@ -529,16 +529,71 @@
 %22value%22;
+%0A    public static final String CACHE_NAME = %22default%22;
 %0A%0A    pu
@@ -725,33 +725,34 @@
 ew Replacer(
-%22default%22
+CACHE_NAME
 );%0A        R
@@ -777,25 +777,26 @@
 eplacer(
-%22default%22
+CACHE_NAME
 );%0A     
@@ -867,20 +867,16 @@
 ;%0A    %7D%0A
-    
 %0A    pri
@@ -924,24 +924,16 @@
 nable %7B%0A
-        
 %0A       
@@ -1096,24 +1096,16 @@
 anager;%0A
-        
 %0A       
@@ -1181,28 +1181,16 @@
 heName;%0A
-            
 %0A       
@@ -1428,24 +1428,16 @@
       %7D%0A
-        
 %0A       
@@ -1539,28 +1539,16 @@
 eName);%0A
-            
 %0A       
@@ -1922,20 +1922,18 @@
 eplace, 
-j
 us
-t
 ing just
@@ -2102,16 +2102,44 @@
 eadName,
+%0A                           
  cache.g
@@ -2206,28 +2206,16 @@
       %7D%0A
-            
 %0A       

Fix typos, formating