Package com.google.common.util.concurrent

Examples of com.google.common.util.concurrent.ListeningExecutorService


    return toListOfResults(futures);
  }
  protected List<RemoteCommandResult> initalizeHosts()
      throws Exception {
    List<ListenableFuture<List<RemoteCommandResult>>> futures = Lists.newArrayList();
    ListeningExecutorService executor = MoreExecutors.
        listeningDecorator(Executors.newFixedThreadPool(hostExecutors.size()));
    try {
      for(final HostExecutor hostExecutor : hostExecutors) {
        futures.add(executor.submit(new Callable<List<RemoteCommandResult>>() {
          @Override
          public List<RemoteCommandResult> call() throws Exception {
            return initalizeHost(hostExecutor);
          }
        }));
      }
      List<RemoteCommandResult> results = Lists.newArrayList();
      for(ListenableFuture<List<RemoteCommandResult>> future : futures) {
        List<RemoteCommandResult> result = future.get();
        if(result != null) {
          results.addAll(result);
        }
      }
      executor.shutdown();
      return results;
    } finally {
      if(executor.isShutdown()) {
        executor.shutdownNow();
      }
    }
  }
View Full Code Here


            for (int k = 0; k < 50; k++) {
                testRoot.addNode("n" + k);
            }
            session.save();

            ListeningExecutorService executorService = MoreExecutors.listeningDecorator(
                    Executors.newCachedThreadPool());

            List<ListenableFuture<?>> futures = Lists.newArrayList();
            for (int k = 0; k < 20; k ++) {
                futures.add(executorService.submit(new Callable<Void>() {
                    @Override
                    public Void call() throws Exception {
                        for (int k = 0; k < 100000; k++) {
                            session.refresh(false);
                            NodeIterator children = testRoot.getNodes();
                            children.hasNext();
                        }
                        return null;
                    }
                }));
            }

            // Throws ExecutionException if any of the submitted task failed
            Futures.allAsList(futures).get();
            executorService.shutdown();
            executorService.awaitTermination(1, TimeUnit.DAYS);
        } finally {
            session.logout();
        }
    }
View Full Code Here

            for (int k = 0; k < 50; k++) {
                testRoot.setProperty("p" + k, k);
            }
            session.save();

            ListeningExecutorService executorService = MoreExecutors.listeningDecorator(
                    Executors.newCachedThreadPool());

            List<ListenableFuture<?>> futures = Lists.newArrayList();
            for (int k = 0; k < 20; k ++) {
                futures.add(executorService.submit(new Callable<Void>() {
                    @Override
                    public Void call() throws Exception {
                        for (int k = 0; k < 100000; k++) {
                            session.refresh(false);
                            PropertyIterator properties = testRoot.getProperties();
                            properties.hasNext();
                        }
                        return null;
                    }
                }));
            }

            // Throws ExecutionException if any of the submitted task failed
            Futures.allAsList(futures).get();
            executorService.shutdown();
            executorService.awaitTermination(1, TimeUnit.DAYS);
        } finally {
            session.logout();
        }
    }
View Full Code Here

    public TableNameCompleter(QueryRunner queryRunner)
    {
        this.queryRunner = checkNotNull(queryRunner, "queryRunner session was null!");

        ListeningExecutorService listeningExecutor = MoreExecutors.listeningDecorator(executor);
        tableCache = CacheBuilder.newBuilder()
                .refreshAfterWrite(RELOAD_TIME_MINUTES, TimeUnit.MINUTES)
                .build(new BackgroundCacheLoader<String, List<String>>(listeningExecutor)
                {
                    @Override
View Full Code Here

   /**
    * Upload the files in parallel.
    */
   private void uploadFiles(String container, List<BlobDetail> blobDetails)
         throws InterruptedException, ExecutionException {
      ListeningExecutorService executor = MoreExecutors.listeningDecorator(newFixedThreadPool(THREADS));
      List<ListenableFuture<BlobDetail>> blobUploaderFutures = Lists.newArrayList();
      BlobUploaderCallback blobUploaderCallback = new BlobUploaderCallback();

      try {

         for (BlobDetail blobDetail: blobDetails) {
            BlobUploader blobUploader = new BlobUploader(container, blobDetail);
            ListenableFuture<BlobDetail> blobDetailFuture = executor.submit(blobUploader);
            blobUploaderFutures.add(blobDetailFuture);

            Futures.addCallback(blobDetailFuture, blobUploaderCallback);
         }

         ListenableFuture<List<BlobDetail>> future = Futures.successfulAsList(blobUploaderFutures);
         List<BlobDetail> uploadedBlobDetails = future.get(); // begin the upload

         System.out.format("%n");

         for (int i = 0; i < uploadedBlobDetails.size(); i++) {
            if (uploadedBlobDetails.get(i) != null) {
               BlobDetail blobDetail = uploadedBlobDetails.get(i);
               System.out.format("  %s (eTag: %s)%n", blobDetail.getRemoteBlobName(), blobDetail.getETag());
            }
            else {
               System.out.format(" %s (ERROR)%n", blobDetails.get(i).getLocalFile().getAbsolutePath());
            }
         }
      }
      finally {
         executor.shutdown();
      }
   }
View Full Code Here

    public void execute() throws InterruptedException, IOException {
        synchronized (this) {
            queue = Collections.unmodifiableList(queue);
        }

        ListeningExecutorService executor = MoreExecutors.listeningDecorator(
                Executors.newFixedThreadPool(threadCount));

        try {
            List<ListenableFuture<?>> futures = new ArrayList<ListenableFuture<?>>();

            synchronized (this) {
                for (HttpDownloadJob job : queue) {
                    futures.add(executor.submit(job));
                }
            }

            try {
                Futures.allAsList(futures).get();
            } catch (ExecutionException e) {
                throw new IOException("Something went wrong", e);
            }

            synchronized (this) {
                if (failed.size() > 0) {
                    throw new IOException(failed.size() + " file(s) could not be downloaded");
                }
            }
        } finally {
            executor.shutdownNow();
        }
    }
View Full Code Here

    @BeforeMethod
    public void setUp()
            throws Exception
    {
        mockSession = new MockCassandraSession(CONNECTOR_ID);
        ListeningExecutorService executor = listeningDecorator(newCachedThreadPool(new ThreadFactoryBuilder().setDaemon(true).build()));
        schemaProvider = new CachingCassandraSchemaProvider(
                mockSession,
                executor,
                new Duration(5, TimeUnit.MINUTES),
                new Duration(1, TimeUnit.MINUTES));
View Full Code Here

    public void setUp()
            throws Exception
    {
        mockClient = new MockHiveMetastoreClient();
        MockHiveCluster mockHiveCluster = new MockHiveCluster(mockClient);
        ListeningExecutorService executor = listeningDecorator(newCachedThreadPool(daemonThreadsNamed("test-%s")));
        metastore = new CachingHiveMetastore(mockHiveCluster, executor, new Duration(5, TimeUnit.MINUTES), new Duration(1, TimeUnit.MINUTES));
    }
View Full Code Here

   }

   public void testAwaitCompletionTimeout() throws Exception {
      final long timeoutMs = 1000;
      ListeningExecutorService userExecutor = MoreExecutors.listeningDecorator(Executors.newSingleThreadExecutor());
      Map<Void, ListenableFuture<?>> responses = newHashMap();
      try {
         responses.put(null, userExecutor.submit(new Runnable() {
            @Override
            public void run() {
               try {
                  Thread.sleep(2 * timeoutMs);
               } catch (InterruptedException ie) {
                  // triggered during shutdown
               }
            }
         }));
         Map<Void, Exception> errors = FutureIterables.awaitCompletion(responses, userExecutor, timeoutMs, Logger.NULL,
         /* prefix= */"");
         if (!errors.isEmpty()) {
            throw errors.values().iterator().next();
         }
         fail("Did not throw TimeoutException");
      } catch (TimeoutException te) {
         // expected
      } finally {
         userExecutor.shutdownNow();
      }
   }
View Full Code Here

      assertNull(module.userExecutorFromConstructor);
   }

   @AfterClass
   private void close() throws IOException {
      ListeningExecutorService user = injector.getInstance(Key.get(ListeningExecutorService.class,
            named(PROPERTY_USER_THREADS)));
      injector.getInstance(Closer.class).close();
      assertTrue(user.isShutdown());
   }
View Full Code Here

TOP

Related Classes of com.google.common.util.concurrent.ListeningExecutorService

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.