Exemplo n.º 1
0
 @Test
 public void testInvokeAllTimeoutCancelled() throws Exception {
   ExecutorService executor = createSingleNodeExecutorService("testInvokeAll");
   assertFalse(executor.isShutdown());
   // Only one task
   ArrayList<Callable<Boolean>> tasks = new ArrayList<Callable<Boolean>>();
   tasks.add(new CancellationAwareTask(0));
   List<Future<Boolean>> futures = executor.invokeAll(tasks, 5, TimeUnit.SECONDS);
   assertEquals(futures.size(), 1);
   assertEquals(futures.get(0).get(), Boolean.TRUE);
   // More tasks
   tasks.clear();
   for (int i = 0; i < COUNT; i++) {
     tasks.add(new CancellationAwareTask(i < 2 ? 0 : 20000));
   }
   futures = executor.invokeAll(tasks, 5, TimeUnit.SECONDS);
   assertEquals(futures.size(), COUNT);
   for (int i = 0; i < COUNT; i++) {
     if (i < 2) {
       assertEquals(futures.get(i).get(), Boolean.TRUE);
     } else {
       boolean excepted = false;
       try {
         futures.get(i).get();
       } catch (CancellationException e) {
         excepted = true;
       }
       assertTrue(excepted);
     }
   }
 }
Exemplo n.º 2
0
  public List<TravelQuote> getRankedTravelQuotes(
      TravelInfo travelInfo,
      Set<TravelCompany> companies,
      Comparator<TravelQuote> ranking,
      long time,
      TimeUnit unit)
      throws InterruptedException {
    List<QuoteTask> tasks = new ArrayList<QuoteTask>();
    for (TravelCompany company : companies) tasks.add(new QuoteTask(company, travelInfo));

    List<Future<TravelQuote>> futures = exec.invokeAll(tasks, time, unit);

    List<TravelQuote> quotes = new ArrayList<TravelQuote>(tasks.size());
    Iterator<QuoteTask> taskIter = tasks.iterator();
    for (Future<TravelQuote> f : futures) {
      QuoteTask task = taskIter.next();
      try {
        quotes.add(f.get());
      } catch (ExecutionException e) {
        quotes.add(task.getFailureQuote(e.getCause()));
      } catch (CancellationException e) {
        quotes.add(task.getTimeoutQuote(e));
      }
    }

    Collections.sort(quotes, ranking);
    return quotes;
  }
Exemplo n.º 3
0
  /**
   * Execute the {@link Callable} tasks in parallel (per the configured size of the {@link
   * WorkerPool}) and wait for them to complete.
   *
   * @param tasks a map of {@link Callable}s with keys by which you will be able to access each
   *     return value
   * @return the return values of each {@link Callable}s mapped by their input key
   */
  public <K, V> Map<K, V> invokeAll(Map<K, Callable<V>> tasks) {
    String caller =
        LOGGER.isDebugEnabled() ? Thread.currentThread().getStackTrace()[2].toString() : "n/a";
    LOGGER.debug("[%s] is invoking %d mapped tasks", caller, tasks.size());

    List<K> orderedKeys = new ArrayList<K>(tasks.size());
    List<Callable<V>> orderedTasks = new ArrayList<Callable<V>>(tasks.size());
    for (Map.Entry<K, Callable<V>> entry : tasks.entrySet()) {
      orderedKeys.add(entry.getKey());
      orderedTasks.add(entry.getValue());
    }

    try {
      long start = System.currentTimeMillis();
      List<Future<V>> executorResults = executorService.invokeAll(orderedTasks);
      long finish = System.currentTimeMillis();
      LOGGER.debug("[%s] invoked %d mapped tasks in %d ms", caller, tasks.size(), finish - start);

      Map<K, V> mappedResults = new LinkedHashMap<K, V>(tasks.size());
      for (int i = 0; i < tasks.size(); i++) {
        K key = orderedKeys.get(i);
        V result = executorResults.get(i).get();
        mappedResults.put(key, result);
      }
      return mappedResults;
    } catch (InterruptedException e) {
      throw new RuntimeException(e);
    } catch (ExecutionException e) {
      throw new RuntimeException(e);
    }
  }
Exemplo n.º 4
0
 @Test
 public void testInvokeAllTimeoutSuccess() throws Exception {
   ExecutorService executor = createSingleNodeExecutorService("testInvokeAll");
   assertFalse(executor.isShutdown());
   // Only one task
   ArrayList<Callable<String>> tasks = new ArrayList<Callable<String>>();
   tasks.add(new BasicTestTask());
   List<Future<String>> futures = executor.invokeAll(tasks, 5, TimeUnit.SECONDS);
   assertEquals(futures.size(), 1);
   assertEquals(futures.get(0).get(), BasicTestTask.RESULT);
   // More tasks
   tasks.clear();
   for (int i = 0; i < COUNT; i++) {
     tasks.add(new BasicTestTask());
   }
   futures = executor.invokeAll(tasks, 5, TimeUnit.SECONDS);
   assertEquals(futures.size(), COUNT);
   for (int i = 0; i < COUNT; i++) {
     assertEquals(futures.get(i).get(), BasicTestTask.RESULT);
   }
 }
Exemplo n.º 5
0
 /**
  * Execute the {@link Callable} tasks in parallel (per the configured size of the {@link
  * WorkerPool}) and wait for them to complete.
  *
  * @param tasks a list of {@link Callable}s
  * @return the ordered return values
  */
 public <T> List<T> invokeAll(List<Callable<T>> tasks) {
   try {
     List<Future<T>> executorResults = executorService.invokeAll(tasks);
     List<T> results = new ArrayList<T>(tasks.size());
     for (Future<T> future : executorResults) {
       results.add(future.get());
     }
     return results;
   } catch (InterruptedException e) {
     throw new RuntimeException(e);
   } catch (ExecutionException e) {
     throw new RuntimeException(e);
   }
 }
Exemplo n.º 6
0
  public List<SimplifiedLog> consume(String topicName, int topicPartitions, int expectedMsg)
      throws InterruptedException {
    ConsumerConnector consumer =
        KafkaUtils.createConsumer(zkServer.getConnectString(), "test_group", "1");
    List<KafkaStream<String, SimplifiedLog>> streams =
        KafkaUtils.getConsumerStreams(consumer, topicName, topicPartitions);

    List<Callable<List<SimplifiedLog>>> tasks = new ArrayList<>();
    streams.forEach(stream -> tasks.add(createConsumerThread(stream.iterator(), expectedMsg)));

    ExecutorService executor = Executors.newFixedThreadPool(streams.size());
    List<Future<List<SimplifiedLog>>> futures =
        executor.invokeAll(tasks, 5 * expectedMsg, TimeUnit.SECONDS);

    List<SimplifiedLog> received = getResultsFromFutures(futures);
    consumer.shutdown();
    return received;
  }
Exemplo n.º 7
0
  /**
   * Execute the {@link Callable} tasks in parallel (per the configured size of the {@link
   * WorkerPool}) and wait for them to complete.
   *
   * @param tasks a list of {@link Callable}s
   * @return the ordered return values
   */
  public <T> List<T> invokeAll(List<Callable<T>> tasks) {
    String caller =
        LOGGER.isDebugEnabled() ? Thread.currentThread().getStackTrace()[2].toString() : "n/a";
    LOGGER.debug("[%s] is invoking %d listed tasks", caller, tasks.size());

    try {
      long start = System.currentTimeMillis();
      List<Future<T>> executorResults = executorService.invokeAll(tasks);
      long finish = System.currentTimeMillis();
      LOGGER.debug("[%s] invoked %d listed tasks in %d ms", caller, tasks.size(), finish - start);

      List<T> results = new ArrayList<T>(tasks.size());
      for (Future<T> future : executorResults) {
        results.add(future.get());
      }
      return results;
    } catch (InterruptedException e) {
      throw new RuntimeException(e);
    } catch (ExecutionException e) {
      throw new RuntimeException(e);
    }
  }
Exemplo n.º 8
0
  /**
   * Execute the {@link Callable} tasks in parallel (per the configured size of the {@link
   * WorkerPool}) and wait for them to complete.
   *
   * @param tasks a map of {@link Callable}s with keys by which you will be able to access each
   *     return value
   * @return the return values of each {@link Callable}s mapped by their input key
   */
  public <K, V> Map<K, V> invokeAll(Map<K, Callable<V>> tasks) {
    List<K> orderedKeys = new ArrayList<K>(tasks.size());
    List<Callable<V>> orderedTasks = new ArrayList<Callable<V>>(tasks.size());
    for (Map.Entry<K, Callable<V>> entry : tasks.entrySet()) {
      orderedKeys.add(entry.getKey());
      orderedTasks.add(entry.getValue());
    }

    try {
      List<Future<V>> executorResults = executorService.invokeAll(orderedTasks);

      Map<K, V> mappedResults = new LinkedHashMap<K, V>(tasks.size());
      for (int i = 0; i < tasks.size(); i++) {
        K key = orderedKeys.get(i);
        V result = executorResults.get(i).get();
        mappedResults.put(key, result);
      }
      return mappedResults;
    } catch (InterruptedException e) {
      throw new RuntimeException(e);
    } catch (ExecutionException e) {
      throw new RuntimeException(e);
    }
  }
Exemplo n.º 9
0
 public <T> List<Future<T>> invokeAll(
     Collection<? extends Callable<T>> tasks, long timeout, TimeUnit unit)
     throws InterruptedException {
   return e.invokeAll(tasks, timeout, unit);
 }
Exemplo n.º 10
0
 public <T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks)
     throws InterruptedException {
   return e.invokeAll(tasks);
 }