List of usage examples for com.google.common.util.concurrent ListeningExecutorService submit
@Override ListenableFuture<?> submit(Runnable task);
From source file:me.taylorkelly.mywarp.bukkit.commands.ImportCommands.java
/** * Starts the import from the given {@code WarpSource}. * * @param initiator the {@code Actor} who initated the import * @param warpSource the {@code WarpSource} to import from * @param importDataSource the {@code DataSource} used to import *//* w w w. ja v a 2s. com*/ private void start(final Actor initiator, final WarpSource warpSource, final SingleConnectionDataSource importDataSource) { initiator.sendMessage(ChatColor.AQUA + MESSAGES.getString("import.started")); final ListeningExecutorService executorService = MoreExecutors .listeningDecorator(Executors.newSingleThreadExecutor()); ListenableFuture<List<Warp>> futureWarps = executorService.submit(new Callable<List<Warp>>() { @Override public List<Warp> call() throws Exception { return warpSource.getWarps(); } }); Futures.addCallback(futureWarps, new FutureCallback<List<Warp>>() { @Override public void onFailure(final Throwable throwable) { initiator.sendError(MESSAGES.getString("import.no-connection", throwable.getMessage())); close(executorService, importDataSource); } @Override public void onSuccess(final List<Warp> warps) { Set<Warp> notImportedWarps = new HashSet<Warp>(); WarpManager warpManager = myWarp.getWarpManager(); for (Warp warp : warps) { if (warpManager.contains(warp.getName())) { // skip the warp notImportedWarps.add(warp); continue; } warpManager.add(warp); } if (notImportedWarps.isEmpty()) { initiator.sendMessage( ChatColor.AQUA + MESSAGES.getString("import.import-successful", warps.size())); } else { int successfullyImported = warps.size() - notImportedWarps.size(); initiator.sendError(MESSAGES.getString("import.import-with-skips", successfullyImported, notImportedWarps.size())); initiator.sendError(CommandUtils.joinWarps(notImportedWarps)); } close(executorService, importDataSource); } }, myWarp.getGame().getExecutor()); }
From source file:com.facebook.buck.distributed.DistBuildService.java
private ListenableFuture<Void> uploadMissingFilesFromList(final List<FileInfo> fileList, ListeningExecutorService executorService) { return executorService.submit(new Callable<Void>() { @Override//from w w w. j av a 2 s . c o m public Void call() throws IOException { Map<String, FileInfo> sha1ToFileInfo = new HashMap<>(); for (FileInfo file : fileList) { sha1ToFileInfo.put(file.getContentHash(), file); } List<String> contentHashes = ImmutableList.copyOf(sha1ToFileInfo.keySet()); CASContainsRequest containsReq = new CASContainsRequest(); containsReq.setContentSha1s(contentHashes); FrontendRequest request = new FrontendRequest(); request.setType(FrontendRequestType.CAS_CONTAINS); request.setCasContainsRequest(containsReq); FrontendResponse response = makeRequestChecked(request); Preconditions.checkState(response.getCasContainsResponse().exists.size() == contentHashes.size()); List<Boolean> isPresent = response.getCasContainsResponse().exists; List<FileInfo> filesToBeUploaded = new LinkedList<>(); for (int i = 0; i < isPresent.size(); ++i) { if (isPresent.get(i)) { continue; } filesToBeUploaded.add(sha1ToFileInfo.get(contentHashes.get(i))); } LOG.info("%d out of %d files already exist in the cache. Uploading %d files..", sha1ToFileInfo.size() - filesToBeUploaded.size(), sha1ToFileInfo.size(), filesToBeUploaded.size()); request = new FrontendRequest(); StoreLocalChangesRequest storeReq = new StoreLocalChangesRequest(); storeReq.setFiles(filesToBeUploaded); request.setType(FrontendRequestType.STORE_LOCAL_CHANGES); request.setStoreLocalChangesRequest(storeReq); makeRequestChecked(request); // No response expected. return null; } }); }
From source file:org.jclouds.chef.strategy.internal.ListClientsImpl.java
private Iterable<? extends Client> executeConcurrently(final ListeningExecutorService executor, Iterable<String> toGet) { ListenableFuture<List<Client>> futures = allAsList( transform(toGet, new Function<String, ListenableFuture<Client>>() { @Override/*w w w . j a v a 2 s .c om*/ public ListenableFuture<Client> apply(final String input) { return executor.submit(new Callable<Client>() { @Override public Client call() throws Exception { return api.getClient(input); } }); } })); logger.trace(String.format("getting clients: %s", Joiner.on(',').join(toGet))); return getUnchecked(futures); }
From source file:com.yahoo.yqlplus.engine.internal.java.runtime.TimeoutHandler.java
public <T> ListenableFuture<T> withTimeoutAsync(final Callable<ListenableFuture<T>> callable, final Timeout tracker, ListeningExecutorService executor) { final SettableFuture<T> result = SettableFuture.create(); final long remaining = tracker.remainingTicks(); final TimeUnit units = tracker.getTickUnits(); final ListenableFuture<ListenableFuture<T>> source = executor.submit(callable); final ScheduledFuture<?> scheduledFuture = timers.schedule(new IntermediateTask<>(result, remaining, units), remaining, units);/*from www. j a v a 2s . com*/ Futures.addCallback(source, scoper.continueScope(new FutureCallback<ListenableFuture<T>>() { @Override public void onSuccess(ListenableFuture<T> next) { scheduledFuture.cancel(false); try { long remaining = tracker.verify(); final ScheduledFuture<?> remainingFuture = timers .schedule(new TimeoutTask<>(next, result, remaining, units), remaining, units); Futures.addCallback(next, scoper.continueScope(new FutureCallback<T>() { @Override public void onSuccess(T out) { remainingFuture.cancel(false); result.set(out); } @Override public void onFailure(Throwable t) { remainingFuture.cancel(false); result.setException(t); } })); } catch (TimeoutException e) { next.cancel(true); result.setException(e); } } @Override public void onFailure(Throwable t) { scheduledFuture.cancel(false); result.setException(t); } })); return scoper.scopeCallbacks(result); }
From source file:org.jclouds.chef.strategy.internal.ListEnvironmentsImpl.java
private Iterable<? extends Environment> execute(final ListeningExecutorService executor, Iterable<String> toGet) { ListenableFuture<List<Environment>> futures = allAsList( transform(toGet, new Function<String, ListenableFuture<Environment>>() { @Override//from w ww .ja va2 s. co m public ListenableFuture<Environment> apply(final String input) { return executor.submit(new Callable<Environment>() { @Override public Environment call() throws Exception { return api.getEnvironment(input); } }); } })); logger.trace(String.format("deleting environments: %s", Joiner.on(',').join(toGet))); return getUnchecked(futures); }
From source file:de.softwareforge.kafka.LoadCommand.java
@Override public void execute() throws Exception { Logging logging = Logging.initialize(); logging.configure(new LoggingConfiguration()); new LoggingMBean().setLevel("kafka", "ERROR"); String tableNames = loaderOptions.tables; final Map<String, TpchTable<?>> allTables = ImmutableMap .copyOf(Maps.uniqueIndex(TpchTable.getTables(), new Function<TpchTable<?>, String>() { @Override//from w w w. j a v a2 s .c om public String apply(@Nonnull TpchTable<?> input) { return input.getTableName(); } })); List<String> tables; if (tableNames == null) { tables = ImmutableList.copyOf(allTables.keySet()); } else { ImmutableList.Builder<String> builder = ImmutableList.builder(); for (String tableName : Splitter.on(",").omitEmptyStrings().trimResults().split(tableNames)) { checkState(allTables.keySet().contains(tableName), "Table %s is unknown", tableName); builder.add(tableName); } tables = builder.build(); } LOG.info("Processing tables: %s", tables); Properties props = new Properties(); props.put("metadata.broker.list", loaderOptions.brokers); props.put("serializer.class", StringEncoder.class.getName()); props.put("key.serializer.class", LongEncoder.class.getName()); props.put("partitioner.class", LongPartitioner.class.getName()); props.put("serializer.encoding", "UTF8"); props.put("request.required.acks", "1"); ProducerConfig producerConfig = new ProducerConfig(props); final ObjectMapper mapper = objectMapperProvider.get(); mapper.enable(MapperFeature.AUTO_DETECT_GETTERS); final Producer<Long, String> producer = new Producer<>(producerConfig); ListeningExecutorService executor = MoreExecutors.listeningDecorator(Executors.newCachedThreadPool()); ImmutableList.Builder<ListenableFuture<Long>> futureBuilder = ImmutableList.builder(); for (final String table : tables) { ListenableFuture<Long> future = executor.submit(new Callable<Long>() { @Override public Long call() throws Exception { TpchTable<?> tpchTable = allTables.get(table); LOG.info("Loading table '%s' into topic '%s%s'...", table, loaderOptions.prefix, table); long count = 0; for (List<? extends TpchEntity> partition : Iterables.partition( tpchTable.createGenerator(loaderOptions.tpchType.getScaleFactor(), 1, 1), 100)) { ImmutableList.Builder<KeyedMessage<Long, String>> builder = ImmutableList.builder(); for (TpchEntity o : partition) { builder.add(new KeyedMessage<>(loaderOptions.prefix + table, count++, mapper.writeValueAsString(o))); } producer.send(builder.build()); } LOG.info("Generated %d rows for table '%s'.", count, table); return count; } }); futureBuilder.add(future); } Futures.allAsList(futureBuilder.build()).get(); executor.shutdown(); executor.awaitTermination(1, TimeUnit.DAYS); producer.close(); }
From source file:com.google.devtools.build.android.ideinfo.PackageParser.java
@Nonnull @VisibleForTesting/*from ww w. j a v a 2 s . c om*/ public Map<ArtifactLocation, String> parsePackageStrings(@Nonnull List<ArtifactLocation> sources) throws Exception { ListeningExecutorService executorService = MoreExecutors .listeningDecorator(Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors())); Map<ArtifactLocation, ListenableFuture<String>> futures = Maps.newHashMap(); for (final ArtifactLocation source : sources) { futures.put(source, executorService.submit(new Callable<String>() { @Override public String call() throws Exception { return getDeclaredPackageOfJavaFile(source); } })); } Map<ArtifactLocation, String> map = Maps.newHashMap(); for (Entry<ArtifactLocation, ListenableFuture<String>> entry : futures.entrySet()) { String value = entry.getValue().get(); if (value != null) { map.put(entry.getKey(), value); } } return map; }
From source file:de.cuseb.bilderbuch.images.ImageSearchService.java
public ImageResponse searchImages(final String query) { final ImageResponse response = new ImageResponse(); final ListeningExecutorService executor = MoreExecutors .listeningDecorator(Executors.newFixedThreadPool(searches.size())); for (final ImageSearch search : searches.values()) { if (!search.isEnabled()) { continue; }//from w w w . j a va 2 s. co m ListenableFuture<List<Image>> searchResult = executor.submit(new Callable<List<Image>>() { @Override public List<Image> call() throws Exception { log.debug("starting enabled search " + search.getClass().getSimpleName()); return search.searchImages(query); } }); Futures.addCallback(searchResult, new FutureCallback<List<Image>>() { @Override public void onSuccess(List<Image> result) { log.debug(search.getClass().getSimpleName() + " result size: " + result.size()); response.addImages(result); } @Override public void onFailure(Throwable t) { log.error(search.getClass().getSimpleName(), t); } }); } try { executor.shutdown(); executor.awaitTermination(timeout, TimeUnit.SECONDS); } catch (InterruptedException e) { log.error("awaitTermination interrupted", e); } if (shuffle) { log.debug("shuffling result"); response.shuffle(); } return response; }
From source file:org.jclouds.abiquo.strategy.infrastructure.ListMachines.java
private Iterable<MachineDto> listConcurrentMachines(final ListeningExecutorService executor, final Iterable<RackDto> racks) { ListenableFuture<List<MachinesDto>> futures = allAsList( transform(racks, new Function<RackDto, ListenableFuture<MachinesDto>>() { @Override//from w w w. j a va2 s . c o m public ListenableFuture<MachinesDto> apply(final RackDto input) { return executor.submit(new Callable<MachinesDto>() { @Override public MachinesDto call() throws Exception { return context.getApi().getInfrastructureApi().listMachines(input); } }); } })); logger.trace("getting machines"); return DomainWrapper.join(getUnchecked(futures)); }
From source file:org.jclouds.abiquo.strategy.enterprise.ListAllowedTiers.java
private Iterable<Tier> listConcurrentTiers(final ListeningExecutorService executor, List<RESTLink> tierLinks, final Datacenter datacenter) { ListenableFuture<List<TierDto>> futures = allAsList( transform(tierLinks, new Function<RESTLink, ListenableFuture<TierDto>>() { @Override//w ww .ja va 2s . c o m public ListenableFuture<TierDto> apply(final RESTLink input) { return executor.submit(new Callable<TierDto>() { @Override public TierDto call() throws Exception { return context.getApi().getInfrastructureApi().getTier(datacenter.unwrap(), input.getId()); } }); } })); logger.trace("getting allowed tiers"); return wrap(context, Tier.class, filter(getUnchecked(futures), notNull())); }