List of usage examples for com.google.common.util.concurrent ListeningExecutorService submit
@Override ListenableFuture<?> submit(Runnable task);
From source file:ddf.security.samlp.MetadataConfigurationParser.java
private void buildEntityDescriptor(String entityDescription) throws IOException { EntityDescriptor entityDescriptor = null; entityDescription = entityDescription.trim(); if (entityDescription.startsWith(HTTPS) || entityDescription.startsWith(HTTP)) { if (entityDescription.startsWith(HTTP)) { LOGGER.warn(/* w ww . j ava 2 s .com*/ "Retrieving metadata via HTTP instead of HTTPS. The metadata configuration is unsafe!!!"); } HttpTransport httpTransport = new ApacheHttpTransport(); HttpRequest httpRequest = httpTransport.createRequestFactory() .buildGetRequest(new GenericUrl(entityDescription)); httpRequest.setUnsuccessfulResponseHandler( new HttpBackOffUnsuccessfulResponseHandler(new ExponentialBackOff()) .setBackOffRequired(HttpBackOffUnsuccessfulResponseHandler.BackOffRequired.ALWAYS)); ListeningExecutorService service = MoreExecutors .listeningDecorator(Executors.newSingleThreadExecutor()); ListenableFuture<HttpResponse> httpResponseFuture = service.submit(httpRequest::execute); Futures.addCallback(httpResponseFuture, new FutureCallback<HttpResponse>() { @Override public void onSuccess(HttpResponse httpResponse) { if (httpResponse != null) { try { String parsedResponse = httpResponse.parseAsString(); buildEntityDescriptor(parsedResponse); } catch (IOException e) { LOGGER.error("Unable to parse metadata from: {}", httpResponse.getRequest().getUrl().toString(), e); } } } @Override public void onFailure(Throwable throwable) { LOGGER.error("Unable to retrieve metadata.", throwable); } }); service.shutdown(); } else if (entityDescription.startsWith(FILE + System.getProperty("ddf.home"))) { String pathStr = StringUtils.substringAfter(entityDescription, FILE); Path path = Paths.get(pathStr); if (Files.isReadable(path)) { try (InputStream fileInputStream = Files.newInputStream(path)) { entityDescriptor = readEntityDescriptor(new InputStreamReader(fileInputStream, "UTF-8")); } } } else if (entityDescription.startsWith("<") && entityDescription.endsWith(">")) { entityDescriptor = readEntityDescriptor(new StringReader(entityDescription)); } else { LOGGER.warn("Skipping unknown metadata configuration value: " + entityDescription); } if (entityDescriptor != null) { entityDescriptorMap.put(entityDescriptor.getEntityID(), entityDescriptor); if (updateCallback != null) { updateCallback.accept(entityDescriptor); } } }
From source file:org.springframework.integration.samples.helloworld.HelloService.java
public void doGuavaDownloadWithChainedThreads(final URL url) throws IOException { ListeningExecutorService service = MoreExecutors.listeningDecorator(Executors.newFixedThreadPool(10)); List<ListenableFuture<String>> calls = new ArrayList<ListenableFuture<String>>(); while (calls.size() < 5) { com.google.common.util.concurrent.ListenableFuture<String> call = service .submit(new Callable<String>() { @Override/*from ww w.j a v a 2 s . c o m*/ public String call() throws Exception { try (InputStream input = url.openStream()) { return IOUtils.toString(input, StandardCharsets.UTF_8); } } }); calls.add(call); } ListenableFuture<List<String>> goodCalls = Futures.successfulAsList(calls); Futures.addCallback(goodCalls, new FutureCallback<List<String>>() { @Override public void onSuccess(List<String> strings) { System.out.print("Successful call"); } @Override public void onFailure(Throwable throwable) { System.err.println("Problems"); } }); }
From source file:org.jclouds.chef.strategy.internal.DeleteAllNodesInListImpl.java
@Override public void execute(final ListeningExecutorService executor, Iterable<String> names) { ListenableFuture<List<Node>> futures = allAsList( transform(names, new Function<String, ListenableFuture<Node>>() { @Override//w ww.j ava 2 s . c o m public ListenableFuture<Node> apply(final String input) { return executor.submit(new Callable<Node>() { @Override public Node call() throws Exception { return api.deleteNode(input); } }); } })); logger.trace(String.format("deleting nodes: %s", Joiner.on(',').join(names))); getUnchecked(futures); }
From source file:com.google.javascript.jscomp.PrebuildAst.java
void prebuild(Iterable<CompilerInput> allInputs) { ThreadFactory threadFactory = new ThreadFactory() { @Override/*from w w w.jav a2 s . c o m*/ public Thread newThread(Runnable r) { Thread t = new Thread(null, r, "jscompiler-PrebuildAst", CompilerExecutor.COMPILER_STACK_SIZE); t.setDaemon(true); // Do not prevent the JVM from exiting. return t; } }; ThreadPoolExecutor poolExecutor = new ThreadPoolExecutor(numParallelThreads, numParallelThreads, Integer.MAX_VALUE, TimeUnit.SECONDS, new LinkedBlockingQueue<Runnable>(), threadFactory); ListeningExecutorService executorService = MoreExecutors.listeningDecorator(poolExecutor); List<ListenableFuture<?>> futureList = new ArrayList<>(Iterables.size(allInputs)); // TODO(moz): Support canceling all parsing on the first halting error for (final CompilerInput input : allInputs) { futureList.add(executorService.submit(new Runnable() { @Override public void run() { input.getAstRoot(compiler); } })); } poolExecutor.shutdown(); try { Futures.allAsList(futureList).get(); } catch (InterruptedException | ExecutionException e) { throw new RuntimeException(e); } }
From source file:com.google.javascript.jscomp.PrebuildDependencyInfo.java
void prebuild(Iterable<CompilerInput> allInputs) { ThreadFactory threadFactory = new ThreadFactory() { @Override/*from w w w . j av a 2 s .co m*/ public Thread newThread(Runnable r) { Thread t = new Thread(null, r, "jscompiler-PrebuildDependencyInfo", CompilerExecutor.COMPILER_STACK_SIZE); t.setDaemon(true); // Do not prevent the JVM from exiting. return t; } }; ThreadPoolExecutor poolExecutor = new ThreadPoolExecutor(numParallelThreads, numParallelThreads, Integer.MAX_VALUE, TimeUnit.SECONDS, new LinkedBlockingQueue<Runnable>(), threadFactory); ListeningExecutorService executorService = MoreExecutors.listeningDecorator(poolExecutor); List<ListenableFuture<?>> futureList = new ArrayList<>(Iterables.size(allInputs)); // TODO(moz): Support canceling all parsing on the first halting error for (final CompilerInput input : allInputs) { futureList.add(executorService.submit(new Runnable() { @Override public void run() { input.getDependencyInfo(); } })); } poolExecutor.shutdown(); try { Futures.allAsList(futureList).get(); } catch (InterruptedException | ExecutionException e) { throw new RuntimeException(e); } }
From source file:com.github.charithe.kafka.KafkaHelper.java
/** * 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/*from w w w . j a v a 2 s .c o m*/ */ 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)); }
From source file:org.jclouds.chef.strategy.internal.BaseListNodesImpl.java
protected Iterable<? extends Node> executeConcurrently(final ListeningExecutorService executor, Iterable<String> toGet) { ListenableFuture<List<Node>> futures = allAsList( transform(toGet, new Function<String, ListenableFuture<Node>>() { @Override/* w ww . j av a 2s.c o m*/ public ListenableFuture<Node> apply(final String input) { return executor.submit(new Callable<Node>() { @Override public Node call() throws Exception { return api.getNode(input); } }); } })); logger.trace(String.format("getting nodes: %s", Joiner.on(',').join(toGet))); return getUnchecked(futures); }
From source file:com.google.idea.blaze.android.sync.importer.problems.GeneratedResourceClassifier.java
GeneratedResourceClassifier(Collection<ArtifactLocation> generatedResourceLocations, ArtifactLocationDecoder artifactLocationDecoder, ListeningExecutorService executorService) { FileAttributeProvider fileAttributeProvider = FileAttributeProvider.getInstance(); List<ListenableFuture<GenResourceClassification>> jobs = generatedResourceLocations.stream() .map(location -> executorService .submit(() -> classifyLocation(location, artifactLocationDecoder, fileAttributeProvider))) .collect(Collectors.toList()); ImmutableSortedMap.Builder<ArtifactLocation, Integer> interesting = ImmutableSortedMap.naturalOrder(); try {//from w w w . ja va 2s . co m for (GenResourceClassification classification : Futures.allAsList(jobs).get()) { if (classification.isInteresting) { interesting.put(classification.artifactLocation, classification.numSubDirs); } } } catch (InterruptedException e) { Thread.currentThread().interrupt(); interesting = considerAllInteresting(generatedResourceLocations); } catch (ExecutionException e) { logger.error(e); interesting = considerAllInteresting(generatedResourceLocations); } finally { interestingDirectories = interesting.build(); } }
From source file:org.jclouds.chef.strategy.internal.DeleteAllClientsInListImpl.java
@Override public void execute(final ListeningExecutorService executor, Iterable<String> names) { ListenableFuture<List<Client>> futures = allAsList( transform(names, new Function<String, ListenableFuture<Client>>() { @Override//w ww.j a v a2 s . c o m public ListenableFuture<Client> apply(final String input) { return executor.submit(new Callable<Client>() { @Override public Client call() throws Exception { return api.deleteClient(input); } }); } })); logger.trace(String.format("deleting clients: %s", Joiner.on(',').join(names))); getUnchecked(futures); }
From source file:org.apache.hive.ptest.execution.Phase.java
protected List<RemoteCommandResult> initalizeHosts() throws Exception { List<ListenableFuture<List<RemoteCommandResult>>> futures = Lists.newArrayList(); ListeningExecutorService executor = MoreExecutors .listeningDecorator(Executors.newFixedThreadPool(hostExecutors.size())); try {//from w w w. j a v a 2s .c om 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(); } } }