Package com.google.common.util.concurrent

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


      final boolean includeCurrentServer
  )
  {
    Pair<Double, ServerHolder> bestServer = Pair.of(Double.POSITIVE_INFINITY, null);

    ListeningExecutorService service = MoreExecutors.listeningDecorator(Executors.newFixedThreadPool(threadCount));
    List<ListenableFuture<Pair<Double, ServerHolder>>> futures = Lists.newArrayList();

    for (final ServerHolder server : serverHolders) {
      futures.add(
          service.submit(
              new Callable<Pair<Double, ServerHolder>>()
              {
                @Override
                public Pair<Double, ServerHolder> call() throws Exception
                {
                  return Pair.of(computeCost(proposalSegment, server, includeCurrentServer), server);
                }
              }
          )
      );
    }

    final ListenableFuture<List<Pair<Double, ServerHolder>>> resultsFuture = Futures.allAsList(futures);

    try {
      for (Pair<Double, ServerHolder> server : resultsFuture.get()) {
        if (server.lhs < bestServer.lhs) {
          bestServer = server;
        }
      }
    }
    catch (Exception e) {
      log.makeAlert(e, "Cost Balancer Multithread strategy wasn't able to complete cost computation.").emit();
    }
    service.shutdown();
    return bestServer;
  }
View Full Code Here


  @Override
  public QueryRunner<SegmentAnalysis> mergeRunners(
      ExecutorService exec, Iterable<QueryRunner<SegmentAnalysis>> queryRunners
  )
  {
    final ListeningExecutorService queryExecutor = MoreExecutors.listeningDecorator(exec);
    return new ConcatQueryRunner<SegmentAnalysis>(
        Sequences.map(
            Sequences.simple(queryRunners),
            new Function<QueryRunner<SegmentAnalysis>, QueryRunner<SegmentAnalysis>>()
            {
              @Override
              public QueryRunner<SegmentAnalysis> apply(final QueryRunner<SegmentAnalysis> input)
              {
                return new QueryRunner<SegmentAnalysis>()
                {
                  @Override
                  public Sequence<SegmentAnalysis> run(
                      final Query<SegmentAnalysis> query,
                      final Map<String, Object> context
                  )
                  {
                    final int priority = query.getContextPriority(0);
                    ListenableFuture<Sequence<SegmentAnalysis>> future = queryExecutor.submit(
                        new AbstractPrioritizedCallable<Sequence<SegmentAnalysis>>(priority)
                        {
                          @Override
                          public Sequence<SegmentAnalysis> call() throws Exception
                          {
View Full Code Here

  @Override
  public QueryRunner<Row> mergeRunners(final ExecutorService exec, Iterable<QueryRunner<Row>> queryRunners)
  {
    // mergeRunners should take ListeningExecutorService at some point
    final ListeningExecutorService queryExecutor = MoreExecutors.listeningDecorator(exec);

    if (config.get().isSingleThreaded()) {
      return new ConcatQueryRunner<>(
          Sequences.map(
              Sequences.simple(queryRunners),
              new Function<QueryRunner<Row>, QueryRunner<Row>>()
              {
                @Override
                public QueryRunner<Row> apply(final QueryRunner<Row> input)
                {
                  return new QueryRunner<Row>()
                  {
                    @Override
                    public Sequence<Row> run(final Query<Row> query, final Map<String, Object> context)
                    {
                      final GroupByQuery queryParam = (GroupByQuery) query;
                      final Pair<IncrementalIndex, Accumulator<IncrementalIndex, Row>> indexAccumulatorPair = GroupByQueryHelper
                          .createIndexAccumulatorPair(
                              queryParam,
                              config.get(),
                              computationBufferPool
                          );
                      final Pair<List, Accumulator<List, Row>> bySegmentAccumulatorPair = GroupByQueryHelper.createBySegmentAccumulatorPair();
                      final int priority = query.getContextPriority(0);
                      final boolean bySegment = query.getContextBySegment(false);

                      final ListenableFuture<Void> future = queryExecutor.submit(
                          new AbstractPrioritizedCallable<Void>(priority)
                          {
                            @Override
                            public Void call() throws Exception
                            {
View Full Code Here

                // Read current situation of record and assure it is still actual
                final IndexerDefinition indexer = indexerModel.getFreshIndexer(indexerName);
                IndexerDefinitionBuilder updatedIndexer = new IndexerDefinitionBuilder().startFrom(indexer);
                final String[] batchArguments = createBatchArguments(indexer);
                if (needsBatchBuildStart(indexer)) {
                    final ListeningExecutorService executor =
                            MoreExecutors.listeningDecorator(Executors.newSingleThreadExecutor());
                    executor.submit(new Callable<Integer>() {
                        @Override
                        public Integer call() throws Exception {
                            HBaseMapReduceIndexerTool tool = new HBaseMapReduceIndexerTool();
                            tool.setConf(hbaseConf);
                            return tool.run(batchArguments, new IndexerDefinitionUpdaterJobProgressCallback(indexerName));
View Full Code Here

    if (!servingHostNames.isEmpty()) {
      specifications = Iterables.concat(specifications, ImmutableList.of(
        createWebappSpec(ProgramType.WEBAPP.toString().toLowerCase())));
    }

    ListeningExecutorService executorService = MoreExecutors.listeningDecorator(
      Executors.newFixedThreadPool(10, Threads.createDaemonThreadFactory("program-gen-%d"))
    );
    try {
      List<ListenableFuture<Location>> futures = Lists.newArrayList();
      for (final ProgramSpecification spec: specifications) {
        ListenableFuture<Location> future = executorService.submit(
          new Callable<Location>() {
          @Override
          public Location call() throws Exception {
            ProgramType type = ProgramTypes.fromSpecification(spec);
            String name = String.format(Locale.ENGLISH, "%s/%s", type, applicationName);
            Location programDir = newOutputDir.append(name);
            if (!programDir.exists()) {
              programDir.mkdirs();
            }
            Location output = programDir.append(String.format("%s.jar", spec.getName()));
            return ProgramBundle.create(o.getApplicationId(), bundler, output, spec.getName(),
                                          spec.getClassName(), type, appSpec);
            }
        });
        futures.add(future);
      }

      for (Location jarLocation : Futures.allAsList(futures).get()) {
        programs.add(Programs.create(jarLocation, null));
      }
    } finally {
      executorService.shutdown();
    }

    // Emits the received specification with programs.
    emit(new ApplicationWithPrograms(o, programs.build()));
  }
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.ioExecutorFromConstructor);
   }

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

   @Test
   public void testShutdownOnClose() throws IOException {
      Injector i = Guice.createInjector();

      Closer closer = i.getInstance(Closer.class);
      ListeningExecutorService executor = createMock(ListeningExecutorService.class);
      ExecutorServiceModule.shutdownOnClose(executor, closer);

      expect(executor.shutdownNow()).andReturn(ImmutableList.<Runnable> of()).atLeastOnce();

      replay(executor);
      closer.close();

      verify(executor);
View Full Code Here

      verify(executor);
   }

   @Test(timeOut = 5000)
   public void testExceptionInSubmitRunnableIncludesSubmissionTrace() throws Exception {
      ListeningExecutorService user = injector.getInstance(Key.get(ListeningExecutorService.class,
            named(PROPERTY_USER_THREADS)));
      ListeningExecutorService io = injector.getInstance(Key.get(ListeningExecutorService.class,
            named(PROPERTY_IO_WORKER_THREADS)));

      for (ListeningExecutorService exec : ImmutableList.of(user, io)) {
         String submission = null;
         try {
View Full Code Here

   // The error is "JSchException: channel is not opened".
   // With the thread-pool size at 100, you get failures a lot more often.
   @Test
   public void testExecHostnameConcurrentlyWithSameSessions() throws Exception {
      final SshClient client = setupClient();
      ListeningExecutorService userExecutor = MoreExecutors.listeningDecorator(Executors.newFixedThreadPool(10));
      List<ListenableFuture<ExecResponse>> futures = Lists.newArrayList();
      try {
         for (int i = 0; i < 100; i++) {
            futures.add(userExecutor.submit(new Callable<ExecResponse>() {
               @Override
               public ExecResponse call() {
                  ExecResponse response = client.exec("hostname");
                  //System.out.println("completed (concurrently) "+count.incrementAndGet());
                  return response;
                 
               }
            }));
         }
         List<ExecResponse> responses = Futures.allAsList(futures).get(3000, TimeUnit.SECONDS);
         for (ExecResponse response : responses) {
            assertEquals(response.getError(), "");
            assertEquals(response.getOutput().trim(), "localhost".equals(sshHost) ? InetAddress.getLocalHost().getHostName()
                     : sshHost);
         }
      } finally {
         userExecutor.shutdownNow();
         client.disconnect();
      }
   }
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.