package org.infinispan.demo;

import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

import org.infinispan.client.hotrod.RemoteCacheManager;
import org.infinispan.client.hotrod.RemoteCounterManagerFactory;
import org.infinispan.client.hotrod.configuration.ConfigurationBuilder;
import org.infinispan.client.hotrod.impl.ConfigurationProperties;
import org.infinispan.counter.api.CounterManager;
import org.infinispan.counter.api.StrongCounter;

public class HotRodCounters {
    
    private static final String ISPN_IP = "127.0.0.1";
    private static final String DEFAULT_COUNTER_NAME = "test-counter";

    public static void main(String[] args) {
        String counterName = args.length > 0 ? args[0] : DEFAULT_COUNTER_NAME;
        ConfigurationBuilder builder = new ConfigurationBuilder();
        builder.addServer().host(ISPN_IP).port(ConfigurationProperties.DEFAULT_HOTROD_PORT);
        RemoteCacheManager cacheManager = new RemoteCacheManager(builder.build());
        CounterManager rcm = RemoteCounterManagerFactory.asCounterManager(cacheManager);
        StrongCounter cnt = rcm.getStrongCounter(counterName);
        
        try {
            System.out.printf("[Start] Counter value: %d\n", cnt.getValue().get(5, TimeUnit.SECONDS));
        } catch (TimeoutException | ExecutionException | InterruptedException e) {
            e.printStackTrace();
        }
        
        for (long i = 0; i < 1000; i++) {
            try {
                System.out.printf("[Loop] Counter value: %d\n", cnt.incrementAndGet().get(5, TimeUnit.SECONDS));
            } catch (TimeoutException | ExecutionException | InterruptedException e) {
                e.printStackTrace();
            }
        }
        
        try {
            System.out.printf("[Stop] Counter value: %d\n", cnt.getValue().get(5, TimeUnit.SECONDS));
        } catch (TimeoutException | ExecutionException | InterruptedException e) {
            e.printStackTrace();
        }
        
        cacheManager.stop();
    }

}

@@ -23,16 +23,63 @@
 .demo;%0A%0A
+import java.util.concurrent.CompletableFuture;%0A
 import j
@@ -1256,34 +1256,48 @@
    %0A        
-try %7B%0A    
+futureGet(cnt.reset());%0A
         Syst
@@ -1333,32 +1333,42 @@
 r value: %25d%5Cn%22, 
+futureGet(
 cnt.getValue().g
@@ -1369,170 +1369,11 @@
 ue()
-.get(5, TimeUnit.SECONDS));%0A        %7D catch (TimeoutException %7C ExecutionException %7C InterruptedException e) %7B%0A            e.printStackTrace();%0A        %7D%0A        
+));
 %0A   
@@ -1401,17 +1401,16 @@
  i %3C 100
-0
 ; i++) %7B
@@ -1414,30 +1414,8 @@
 ) %7B%0A
-            try %7B%0A    
     
@@ -1466,24 +1466,34 @@
 lue: %25d%5Cn%22, 
+futureGet(
 cnt.incremen
@@ -1505,32 +1505,8 @@
 et()
-.get(5, TimeUnit.SECONDS
 ));%0A
@@ -1517,86 +1517,93 @@
     
+%7D%0A
     
-%7D catch (TimeoutException %7C ExecutionException %7C InterruptedException e) %7B
+    System.out.printf(%22%5BStop%5D Counter value: %25d%5Cn%22, futureGet(cnt.getValue()));
 %0A   
@@ -1603,32 +1603,33 @@
 ));%0A        
+%0A
         
 e.printStack
@@ -1620,33 +1620,33 @@
     
-e.printStackTrace
+cacheManager.stop
 ();%0A    
     
@@ -1645,36 +1645,105 @@
     
+%7D%0A
     
+%0A
     
-%7D%0A     
+public static Object futureGet(CompletableFuture%3C?%3E f) %7B%0A
    
-%7D%0A
      
-   
+Object result = null;
 %0A   
@@ -1769,70 +1769,18 @@
     
-System.out.printf(%22%5BStop%5D Counter value: %25d%5Cn%22, cnt.getValue()
+result = f
 .get
@@ -1800,17 +1800,16 @@
 SECONDS)
-)
 ;%0A      
@@ -1940,43 +1940,28 @@
     
-%0A        cacheManager.stop()
+return result
 ;%0A    %7D%0A
 %0A%7D%0A
@@ -1957,11 +1957,10 @@
 ;%0A    %7D%0A
-%0A
 %7D%0A

Simplify counter example