1 Reply Latest reply on Apr 11, 2011 3:46 PM by Vladimir Blagojevic

    Does exectutor api work with EmbeddedCacheManager?

    Scott Stark Master

      Can the distributed execution env be used with an embedded cache? I keep getting an NPE on this modification of the example I pulled from the wiki page, filling in the cache initialization pieces with an EmbeddedCacheManager.

       

      http://community.jboss.org/wiki/InfinispanDistributedExecutionFramework#Examples

       

       

      package distributed;
      
      import org.infinispan.Cache;
      import org.infinispan.config.Configuration;
      import org.infinispan.config.GlobalConfiguration;
      import org.infinispan.distexec.DefaultExecutorService;
      import org.infinispan.distexec.DistributedExecutorService;
      import org.infinispan.manager.DefaultCacheManager;
      import org.infinispan.manager.EmbeddedCacheManager;
      
      import java.io.Serializable;
      import java.util.ArrayList;
      import java.util.Collections;
      import java.util.List;
      import java.util.concurrent.Callable;
      import java.util.concurrent.Future;
      
      public class PiAppx {
      
          public static void main(String[] arg) throws Exception {
              GlobalConfiguration gc = GlobalConfiguration.getNonClusteredDefault();
              Configuration c = new Configuration();
              DefaultCacheManager defaultCacheManager = new DefaultCacheManager(gc, c, true);
              EmbeddedCacheManager manager = new DefaultCacheManager(true);
              List<Cache<Object, Object>> caches = Collections.singletonList(manager.getCache());
              Cache cache = caches.get(0);
      
              int numPoints = 10000000;
              int numServers = caches.size();
              int numberPerWorker = numPoints / numServers;
      
              DistributedExecutorService des = new DefaultExecutorService(cache);
              long start = System.currentTimeMillis();
              CircleTest ct = new CircleTest(numberPerWorker);
              List<Future<Integer>> results = des.submitEverywhere(ct);
              int countCircle = 0;
              for (Future<Integer> f : results) {
                  countCircle += f.get();
              }
              double appxPi = 4.0 * countCircle / numPoints;
      
              System.out.println("Distributed PI appx is " + appxPi +
                      " completed in " + (System.currentTimeMillis() - start) + " ms");
          }
      
          private static class CircleTest implements Callable<Integer>, Serializable {
              /**
               * The serialVersionUID
               */
              private static final long serialVersionUID = 3496135215525904755L;
      
              private final int loopCount;
      
              public CircleTest(int loopCount) {
                  this.loopCount = loopCount;
              }
      
              @Override
              public Integer call() throws Exception {
                  int insideCircleCount = 0;
                  for (int i = 0; i < loopCount; i++) {
                      double x = Math.random();
                      double y = Math.random();
                      if (insideCircle(x, y))
                          insideCircleCount++;
                  }
                  return insideCircleCount;
              }
      
              private boolean insideCircle(double x, double y) {
                  return (Math.pow(x - 0.5, 2) + Math.pow(y - 0.5, 2))
                          <= Math.pow(0.5, 2);
              }
          }
      }
      
      

       

      Exception in thread "main" java.lang.NullPointerException
          at org.infinispan.distexec.DefaultExecutorService.submitEverywhere(DefaultExecutorService.java:306)
          at distributed.PiAppx.main(PiAppx.java:31)
      Disconnected from the target VM, address: '127.0.0.1:59869', transport: 'socket'
      
      
        • 1. Re: Does exectutor api work with EmbeddedCacheManager?
          Vladimir Blagojevic Master

          Hey Scott,

           

          You can not use non-clustered caches for distributed execution. One needs at least a few booted and clustered Infinispan nodes before invoking distributed executors on them. I will correct this NPE to be more informative IllegalStateException.

           

          I am currently making a demo that will enable users to try out distribtued executors without a lot of manual setup. However, you can do this yourself if you want. 

           

          Regards,

          Vladimir