Java Code Examples for com.google.common.util.concurrent.ListeningExecutorService#submit()

The following examples show how to use com.google.common.util.concurrent.ListeningExecutorService#submit() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MoreFuturesTest.java    From buck with Apache License 2.0 6 votes vote down vote up
@Test
public void combineFuturesFailWhenOneFails() throws InterruptedException {
  SettableFuture<String> firstFuture = SettableFuture.create();
  SettableFuture<Integer> secondFuture = SettableFuture.create();

  ListeningExecutorService executor = MoreExecutors.newDirectExecutorService();

  ListenableFuture<Pair<String, Integer>> combinedFuture =
      MoreFutures.combinedFutures(firstFuture, secondFuture, executor);

  assertFalse(combinedFuture.isDone());

  executor.submit(() -> firstFuture.setException(new Exception()));

  assertTrue(combinedFuture.isDone());
  assertFalse(MoreFutures.isSuccess(combinedFuture));
}
 
Example 2
Source File: ActiveTraceRepositoryTest.java    From pinpoint with Apache License 2.0 6 votes vote down vote up
private ListenableFuture<TraceThreadTuple> executeSampledContinuedTrace(ListeningExecutorService executorService, final CountDownLatch awaitLatch, final CountDownLatch executeLatch, final long id) {
    return executorService.submit(new Callable<TraceThreadTuple>() {
        @Override
        public TraceThreadTuple call() throws Exception {
            try {
                TraceId agentId1 = new DefaultTraceId("agentId", 0L, id);
                Trace agentId = traceContext.continueTraceObject(agentId1);
                return new TraceThreadTuple(agentId, Thread.currentThread().getId());
            } finally {
                executeLatch.countDown();
                awaitLatch.await();
                traceContext.removeTraceObject();
            }
        }
    });
}
 
Example 3
Source File: RollupService.java    From glowroot with Apache License 2.0 6 votes vote down vote up
private ListenableFuture<?> rollupGauges(AgentRollup agentRollup,
        ListeningExecutorService workerExecutor) {
    List<AgentRollup> childAgentRollups = agentRollup.children();
    if (childAgentRollups.isEmpty()) {
        // optimization of common case
        return workerExecutor.submit(new RollupGauges(agentRollup.id()));
    }
    // need to roll up children first, since gauge values initial roll up from children is
    // done on the 1-min aggregates of the children
    List<ListenableFuture<?>> futures = new ArrayList<>();
    for (AgentRollup childAgentRollup : shuffle(childAgentRollups)) {
        futures.add(rollupGauges(childAgentRollup, workerExecutor));
    }
    // using _whenAllSucceed_ because need to _not_ roll up parent if exception occurs while
    // rolling up a child, since gauge values initial roll up from children is done on the 1-min
    // aggregates of the children
    return Futures.whenAllSucceed(futures)
            .run(new RollupGauges(agentRollup.id()), workerExecutor);
}
 
Example 4
Source File: GuavaListenableFutureIT.java    From glowroot with Apache License 2.0 6 votes vote down vote up
@Override
public void transactionMarker() throws Exception {
    ListeningExecutorService executor =
            MoreExecutors.listeningDecorator(Executors.newCachedThreadPool());
    ListenableFuture<Void> future1 = executor.submit(new Callable<Void>() {
        @Override
        public Void call() {
            return null;
        }
    });
    MILLISECONDS.sleep(100);
    future1.addListener(new Runnable() {
        @Override
        public void run() {
            new CreateTraceEntry().traceEntryMarker();
        }
    }, executor);
    MILLISECONDS.sleep(100);
    executor.shutdown();
    executor.awaitTermination(10, SECONDS);
}
 
Example 5
Source File: TestContainerExecution.java    From tez with Apache License 2.0 6 votes vote down vote up
@Test(timeout = 5000)
public void testGetTaskShouldDie() throws InterruptedException, ExecutionException {
  ListeningExecutorService executor = null;
  try {
    ExecutorService rawExecutor = Executors.newFixedThreadPool(1);
    executor = MoreExecutors.listeningDecorator(rawExecutor);
    ApplicationId appId = ApplicationId.newInstance(10000, 1);
    ApplicationAttemptId appAttemptId = ApplicationAttemptId.newInstance(appId, 1);
    @SuppressWarnings("deprecation")
    ContainerId containerId = ContainerId.newInstance(appAttemptId, 1);

    TaskExecutionTestHelpers.TezTaskUmbilicalForTest
        umbilical = new TaskExecutionTestHelpers.TezTaskUmbilicalForTest();
    ContainerContext containerContext = new ContainerContext(containerId.toString());

    ContainerReporter containerReporter = new ContainerReporter(umbilical, containerContext, 100);
    ListenableFuture<ContainerTask> getTaskFuture = executor.submit(containerReporter);

    getTaskFuture.get();
    assertEquals(1, umbilical.getTaskInvocations);

  } finally {
    executor.shutdownNow();
  }
}
 
Example 6
Source File: RingBufferInputStreamTest.java    From bazel-buildfarm with Apache License 2.0 6 votes vote down vote up
@Test
public void writesUnblockReads() throws ExecutionException, InterruptedException {
  ListeningExecutorService service = listeningDecorator(newSingleThreadExecutor());
  AtomicInteger counter = new AtomicInteger();

  RingBufferInputStream buffer = new RingBufferInputStream(1);
  ListenableFuture<Integer> readFuture =
      service.submit(
          () -> {
            counter.getAndIncrement();
            return buffer.read();
          });
  byte[] content = new byte[1];
  content[0] = 42;
  while (counter.get() != 1) {
    MICROSECONDS.sleep(10);
  }
  assertThat(readFuture.isDone()).isFalse();
  buffer.write(content);
  assertThat(readFuture.get()).isEqualTo(content[0]);
  service.shutdown();
  service.awaitTermination(10, MICROSECONDS);
}
 
Example 7
Source File: RingBufferInputStreamTest.java    From bazel-buildfarm with Apache License 2.0 6 votes vote down vote up
@Test
public void readUnblocksWrite() throws ExecutionException, IOException, InterruptedException {
  ListeningExecutorService service = listeningDecorator(newSingleThreadExecutor());
  AtomicInteger counter = new AtomicInteger();

  RingBufferInputStream buffer = new RingBufferInputStream(1);
  byte[] content = new byte[1];
  content[0] = 42;
  buffer.write(content); // buffer is now full
  ListenableFuture<Void> writeFuture =
      service.submit(
          () -> {
            counter.getAndIncrement();
            buffer.write(content);
            return null;
          });
  while (counter.get() != 1) {
    MICROSECONDS.sleep(10);
  }
  assertThat(writeFuture.isDone()).isFalse();
  buffer.read();
  assertThat(writeFuture.get()).isEqualTo(null);
  service.shutdown();
  service.awaitTermination(10, MICROSECONDS);
}
 
Example 8
Source File: TestRunning.java    From buck with Apache License 2.0 6 votes vote down vote up
private static ListenableFuture<TestResults> runStepsAndYieldResult(
    ExecutionContext context,
    List<Step> steps,
    Callable<TestResults> interpretResults,
    BuildTarget buildTarget,
    BuckEventBus eventBus,
    ListeningExecutorService listeningExecutorService) {
  Preconditions.checkState(!listeningExecutorService.isShutdown());
  Callable<TestResults> callable =
      () -> {
        LOG.debug("Test steps will run for %s", buildTarget);
        eventBus.post(TestRuleEvent.started(buildTarget));
        for (Step step : steps) {
          StepRunner.runStep(context, step, Optional.of(buildTarget));
        }
        LOG.debug("Test steps did run for %s", buildTarget);
        eventBus.post(TestRuleEvent.finished(buildTarget));

        return interpretResults.call();
      };

  return listeningExecutorService.submit(callable);
}
 
Example 9
Source File: ABIProviderTest.java    From eosio-java with MIT License 5 votes vote down vote up
@Test
public void testGetAbiAsync() {
    final CountDownLatch testLock = new CountDownLatch(1);
    final String[] retrievedEosioAbiJsonStrings = {new String()};

    ListeningExecutorService service = MoreExecutors.listeningDecorator(Executors.newFixedThreadPool(10));
    ListenableFuture<String> getEosioAbiFuture = service.submit(new Callable<String>() {
        @Override
        public String call() throws Exception {
            // Its not thread safe to use the global mocks like this but we're not trying to
            // run concurrent calls.
            IABIProvider abiProvider = new ABIProviderImpl(mockRpcProvider, mockSerializationProvider);
            return abiProvider.getAbi(chainId, new EOSIOName("eosio"));
        }
    });
    Futures.addCallback(getEosioAbiFuture, new FutureCallback<String>() {
        @Override
        public void onSuccess(@NullableDecl String result) {
            retrievedEosioAbiJsonStrings[0] = result;
            testLock.countDown();
        }

        @Override
        public void onFailure(Throwable t) {
            testLock.countDown();
        }
    }, MoreExecutors.directExecutor());

    try {
        testLock.await(2000, TimeUnit.MILLISECONDS);
        assertNotNull(retrievedEosioAbiJsonStrings[0]);
        assertFalse(retrievedEosioAbiJsonStrings[0].isEmpty());
        assertEquals(eosioAbiJsonString, retrievedEosioAbiJsonStrings[0]);
    } catch (InterruptedException interruptedException) {
        fail("Interrupted waiting for getAbi() to complete: " +
                interruptedException.getLocalizedMessage());
    }
}
 
Example 10
Source File: DebugClientTransport.java    From intellij with Apache License 2.0 5 votes vote down vote up
private ListenableFuture<?> processEvents(InputStream eventStream) {
  ListeningExecutorService executor =
      MoreExecutors.listeningDecorator(PooledThreadExecutor.INSTANCE);
  return executor.submit(
      () -> {
        try {
          listenForEvents(eventStream);
        } catch (IOException e) {
          if (!ignoreErrors()) {
            logger.error("Malformed event proto", e);
          }
          close();
        }
      });
}
 
Example 11
Source File: JavaAsync.java    From tutorials with MIT License 5 votes vote down vote up
/**
 * Finds factorial of a number using Guava's ListeningExecutorService.submit()
 * @param number
 * @return
 */
@Loggable
public static ListenableFuture<Long> factorialUsingGuavaServiceSubmit(int number) {
    ListeningExecutorService service = MoreExecutors.listeningDecorator(threadpool);
    ListenableFuture<Long> factorialFuture = (ListenableFuture<Long>) service.submit(()-> factorial(number));
    return factorialFuture;
}
 
Example 12
Source File: PerformanceMeasurer.java    From brooklyn-server with Apache License 2.0 5 votes vote down vote up
protected static void runConcurrentAndBlock(ListeningExecutorService executor, Runnable job, int numConcurrentJobs) throws InterruptedException, ExecutionException {
    List<ListenableFuture<?>> futures = new ArrayList<ListenableFuture<?>>(numConcurrentJobs);
    for (int i = 0; i < numConcurrentJobs; i++) {
        ListenableFuture<?> future = executor.submit(job);
        futures.add(future);
        Futures.allAsList(futures).get();
    }
}
 
Example 13
Source File: ListLandmarksGraph.java    From curiostack with MIT License 5 votes vote down vote up
@Produces
static ListenableFuture<List<Landmark>> writeNewLandmarksToDb(
    PlacesSearchResponse searchResponse,
    List<List<Landmark>> dbLandmarks,
    DSLContext cafemapdb,
    @ForDatabase ListeningExecutorService dbExecutor) {
  if (searchResponse.results.length == 0) {
    return immediateFuture(ImmutableList.of());
  }

  Set<String> dbPlaceIds =
      dbLandmarks.stream()
          .flatMap(List::stream)
          .map(Landmark::getGooglePlaceId)
          .collect(toImmutableSet());

  List<LandmarkRecord> newLandmarks =
      Arrays.stream(searchResponse.results)
          .filter(result -> !dbPlaceIds.contains(result.placeId))
          .map(
              result ->
                  new LandmarkRecord()
                      .setGooglePlaceId(result.placeId)
                      .setType("park")
                      .setS2Cell(
                          ULong.valueOf(
                              S2CellId.fromLatLng(
                                      S2Util.convertFromLatLng(result.geometry.location))
                                  .id())))
          .collect(toImmutableList());

  return dbExecutor.submit(
      () -> {
        int[] numInserted = cafemapdb.batchStore(newLandmarks).execute();
        return newLandmarks.stream().map(Landmark::new).collect(toImmutableList());
      });
}
 
Example 14
Source File: ActiveTraceRepositoryTest.java    From pinpoint with Apache License 2.0 5 votes vote down vote up
private ListenableFuture<TraceThreadTuple> executeUnsampledContinuedTrace(ListeningExecutorService executorService, final CountDownLatch awaitLatch, final CountDownLatch executeLatch) {
    return executorService.submit(new Callable<TraceThreadTuple>() {
        @Override
        public TraceThreadTuple call() throws Exception {
            try {
                long id = Thread.currentThread().getId();
                return new TraceThreadTuple(traceContext.disableSampling(), id);
            } finally {
                executeLatch.countDown();
                awaitLatch.await();
                traceContext.removeTraceObject();
            }
        }
    });
}
 
Example 15
Source File: MoreFuturesTest.java    From buck with Apache License 2.0 4 votes vote down vote up
@Test
public void combineFuturesSucceed() throws InterruptedException, ExecutionException {
  SettableFuture<String> firstFuture = SettableFuture.create();
  SettableFuture<Integer> secondFuture = SettableFuture.create();

  ListeningExecutorService executor = MoreExecutors.newDirectExecutorService();

  ListenableFuture<Pair<String, Integer>> combinedFuture =
      MoreFutures.combinedFutures(firstFuture, secondFuture, executor);

  assertFalse(combinedFuture.isDone());

  executor.submit(() -> firstFuture.set("test"));

  assertFalse(combinedFuture.isDone());

  executor.submit(() -> secondFuture.set(42));

  assertTrue(combinedFuture.isDone());

  combinedFuture.get().getFirst().equals("test");
  combinedFuture.get().getSecond().equals(42);
}
 
Example 16
Source File: ResourceSymbols.java    From bazel with Apache License 2.0 4 votes vote down vote up
/** Read the symbols from the provided symbol file. */
public static ListenableFuture<ResourceSymbols> load(
    Path primaryRTxt, ListeningExecutorService executorService) {
  return executorService.submit(new SymbolLoadingTask(primaryRTxt));
}
 
Example 17
Source File: HttpTestUtils.java    From brooklyn-server with Apache License 2.0 4 votes vote down vote up
/**
 * Schedules (with the given executor) a poller that repeatedly accesses the given url, to confirm it always gives
 * back the expected status code.
 * 
 * Expected usage is to query the future, such as:
 * 
 * <pre>
 * {@code
 * Future<?> future = assertAsyncHttpStatusCodeContinuallyEquals(executor, url, 200);
 * // do other stuff...
 * if (future.isDone()) future.get(); // get exception if it's failed
 * }
 * </pre>
 * 
 * For stopping it, you can either do future.cancel(true), or you can just do executor.shutdownNow().
 * 
 * TODO Look at difference between this and WebAppMonitor, to decide if this should be kept.
 */
public static ListenableFuture<?> assertAsyncHttpStatusCodeContinuallyEquals(ListeningExecutorService executor, final String url, final int expectedStatusCode) {
    return executor.submit(new Runnable() {
        @Override public void run() {
            // TODO Need to drop logging; remove sleep when that's done.
            while (!Thread.currentThread().isInterrupted()) {
                assertHttpStatusCodeEquals(url, expectedStatusCode);
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    return; // graceful return
                }
            }
        }
    });
}
 
Example 18
Source File: TestMinWorkerRequirement.java    From presto with Apache License 2.0 4 votes vote down vote up
@Test
public void testMultipleRequiredWorkerNodesSessionOverride()
        throws Exception
{
    ListeningExecutorService service = MoreExecutors.listeningDecorator(newFixedThreadPool(3));
    try (DistributedQueryRunner queryRunner = TpchQueryRunnerBuilder.builder().setNodeCount(1).build()) {
        Session session1 = testSessionBuilder()
                .setSystemProperty(REQUIRED_WORKERS_COUNT, "2")
                .setCatalog("tpch")
                .setSchema("tiny")
                .build();
        ListenableFuture<ResultWithQueryId<MaterializedResult>> queryFuture1 = service.submit(() -> queryRunner.executeWithQueryId(session1, "SELECT COUNT(*) from lineitem"));

        Session session2 = Session.builder(session1)
                .setSystemProperty(REQUIRED_WORKERS_COUNT, "3")
                .build();
        ListenableFuture<ResultWithQueryId<MaterializedResult>> queryFuture2 = service.submit(() -> queryRunner.executeWithQueryId(session2, "SELECT COUNT(*) from lineitem"));

        Session session3 = Session.builder(session1)
                .setSystemProperty(REQUIRED_WORKERS_COUNT, "4")
                .build();
        ListenableFuture<ResultWithQueryId<MaterializedResult>> queryFuture3 = service.submit(() -> queryRunner.executeWithQueryId(session3, "SELECT COUNT(*) from lineitem"));

        MILLISECONDS.sleep(1000);
        // None of the queries should run
        assertFalse(queryFuture1.isDone());
        assertFalse(queryFuture2.isDone());
        assertFalse(queryFuture3.isDone());

        queryRunner.addServers(1);
        assertEquals(queryRunner.getCoordinator().refreshNodes().getActiveNodes().size(), 2);
        // After adding 1 node, only 1st query should run
        MILLISECONDS.sleep(1000);
        assertTrue(queryFuture1.get().getResult().getRowCount() > 0);
        QueryManager queryManager = queryRunner.getCoordinator().getQueryManager();
        QueryInfo completedQueryInfo = queryManager.getFullQueryInfo(queryFuture1.get().getQueryId());
        assertTrue(completedQueryInfo.getQueryStats().getResourceWaitingTime().roundTo(SECONDS) >= 1);

        assertFalse(queryFuture2.isDone());
        assertFalse(queryFuture3.isDone());

        // After adding 2 nodes, 2nd and 3rd query should also run
        queryRunner.addServers(2);
        assertEquals(queryRunner.getCoordinator().refreshNodes().getActiveNodes().size(), 4);
        assertTrue(queryFuture2.get().getResult().getRowCount() > 0);
        completedQueryInfo = queryManager.getFullQueryInfo(queryFuture2.get().getQueryId());
        assertTrue(completedQueryInfo.getQueryStats().getResourceWaitingTime().roundTo(SECONDS) >= 2);

        assertTrue(queryFuture3.get().getResult().getRowCount() > 0);
        completedQueryInfo = queryManager.getFullQueryInfo(queryFuture3.get().getQueryId());
        assertTrue(completedQueryInfo.getQueryStats().getResourceWaitingTime().roundTo(SECONDS) >= 2);
    }
    finally {
        service.shutdown();
    }
}
 
Example 19
Source File: HttpAsserts.java    From brooklyn-server with Apache License 2.0 4 votes vote down vote up
/**
 * Schedules (with the given executor) a poller that repeatedly accesses the given url, to confirm it always gives
 * back the expected status code.
 * 
 * Expected usage is to query the future, such as:
 * 
 * <pre>
 * {@code
 * Future<?> future = assertAsyncHttpStatusCodeContinuallyEquals(executor, url, 200);
 * // do other stuff...
 * if (future.isDone()) future.get(); // get exception if its Asserts.failed
 * }
 * </pre>
 *
 * NOTE that the exception thrown by future.get() is a java.util.concurrent.ExecutionException,
 * not an AssertionError.
 * 
 * For stopping it, you can either do future.cancel(true), or you can just do executor.shutdownNow().
 * 
 * TODO Look at difference between this and WebAppMonitor, to decide if this should be kept.
 */
public static ListenableFuture<?> assertAsyncHttpStatusCodeContinuallyEquals(ListeningExecutorService executor, final String url, final int expectedStatusCode) {
    return executor.submit(new Runnable() {
        @Override public void run() {
            // TODO Need to drop logging; remove sleep when that's done.
            while (!Thread.currentThread().isInterrupted()) {
                assertHttpStatusCodeEquals(url, expectedStatusCode);
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    return; // graceful return
                }
            }
        }
    });
}
 
Example 20
Source File: KafkaHelper.java    From kafka-junit with Apache License 2.0 2 votes vote down vote up
/**
 * Attempt to consume the specified number of messages
 *
 * @param topic                Topic to consume
 * @param consumer             Consumer to use
 * @param numMessagesToConsume Number of messages to consume
 * @param <K>                  Type of Key
 * @param <V>                  Type of Value
 * @return ListenableFuture
 */
public <K, V> ListenableFuture<List<ConsumerRecord<K, V>>> consume(String topic, KafkaConsumer<K, V> consumer, int numMessagesToConsume) {
    consumer.subscribe(Lists.newArrayList(topic));
    ListeningExecutorService executor = MoreExecutors.listeningDecorator(Executors.newSingleThreadExecutor());
    return executor.submit(new RecordConsumer<>(numMessagesToConsume, consumer));
}