Example usage for com.google.common.util.concurrent MoreExecutors listeningDecorator

List of usage examples for com.google.common.util.concurrent MoreExecutors listeningDecorator

Introduction

In this page you can find the example usage for com.google.common.util.concurrent MoreExecutors listeningDecorator.

Prototype

@GwtIncompatible("TODO")
public static ListeningScheduledExecutorService listeningDecorator(ScheduledExecutorService delegate) 

Source Link

Document

Creates a ScheduledExecutorService whose submit and invokeAll methods submit ListenableFutureTask instances to the given delegate executor.

Usage

From source file:org.jclouds.openstack.swift.v1.blobstore.RegionScopedSwiftBlobStore.java

@Beta
@Override//from   ww w .  ja  va  2  s .c o  m
public InputStream streamBlob(final String container, final String name, final ExecutorService executor) {

    final ListeningExecutorService listeningExecutor = MoreExecutors.listeningDecorator(executor);
    // User will receive the Input end of the piped stream
    final PipedOutputStream output;
    final PipedInputStream input;
    try {
        output = new PipedOutputStream();
        input = new PipedInputStream(output,
                getMinimumMultipartPartSize() * 5 > Integer.MAX_VALUE ? Integer.MAX_VALUE
                        : (int) getMinimumMultipartPartSize() * 5);
    } catch (IOException e) {
        throw new RuntimeException(e);
    }

    // The total length of the file to download is needed to determine ranges
    // It has to be obtainable without downloading the whole file
    final long contentLength = api.getObjectApi(regionId, container).getWithoutBody(name).getPayload()
            .getContentMetadata().getContentLength();

    // Determine download buffer size, smaller means less memory usage; larger is faster as long as threads are saturated
    final long partSize = getMinimumMultipartPartSize();

    // Used to communicate between the producer and consumer threads
    final LinkedBlockingQueue<ListenableFuture<byte[]>> results = new LinkedBlockingQueue<ListenableFuture<byte[]>>();

    listeningExecutor.submit(new Runnable() {
        @Override
        public void run() {
            ListenableFuture<byte[]> result;
            long from;
            try {
                for (from = 0; from < contentLength; from = from + partSize) {
                    logger.debug(Thread.currentThread() + " writing to output");
                    result = results.take();
                    if (result == null) {
                        output.close();
                        input.close();
                        throw new RuntimeException("Error downloading file part to stream");
                    }
                    output.write(result.get());
                }
            } catch (Exception e) {
                logger.debug(e.toString());
                // Close pipe so client is notified of an exception
                Closeables2.closeQuietly(input);
                throw new RuntimeException(e);
            } finally {
                // Finished writing results to stream
                Closeables2.closeQuietly(output);
            }
        }
    });

    listeningExecutor.submit(new Runnable() {
        @Override
        public void run() {
            long from;
            long to;
            // Loop through ranges within the file
            for (from = 0; from < contentLength; from = from + partSize) {
                to = (from + partSize >= contentLength) ? contentLength - 1 : from + partSize - 1;
                BlobStreamDownloader b = new BlobStreamDownloader(container, name, from, to);
                results.add(listeningExecutor.submit(b));
            }
        }
    });
    return input;
}

From source file:com.b2international.snowowl.snomed.api.impl.SnomedMergeReviewServiceImpl.java

public SnomedMergeReviewServiceImpl() {
    executorService = MoreExecutors.listeningDecorator(Executors.newFixedThreadPool(3));
}

From source file:org.apache.druid.segment.realtime.appenderator.AppenderatorImpl.java

private void initializeExecutors() {
    final int maxPendingPersists = tuningConfig.getMaxPendingPersists();

    if (persistExecutor == null) {
        // use a blocking single threaded executor to throttle the firehose when write to disk is slow
        persistExecutor = MoreExecutors.listeningDecorator(
                Execs.newBlockingSingleThreaded("appenderator_persist_%d", maxPendingPersists));
    }/*from  ww w.j a  v a  2  s .  c o  m*/
    if (pushExecutor == null) {
        // use a blocking single threaded executor to throttle the firehose when write to disk is slow
        pushExecutor = MoreExecutors
                .listeningDecorator(Execs.newBlockingSingleThreaded("appenderator_merge_%d", 1));
    }
    if (intermediateTempExecutor == null) {
        // use single threaded executor with SynchronousQueue so that all abandon operations occur sequentially
        intermediateTempExecutor = MoreExecutors
                .listeningDecorator(Execs.newBlockingSingleThreaded("appenderator_abandon_%d", 0));
    }
}

From source file:com.liaison.shachi.HBaseControl.java

public HBaseControl(final HBaseContext context, final HBaseResourceManager resMgr) {
    Util.ensureNotNull(context, this, "context", HBaseContext.class);
    this.context = context;
    Util.ensureNotNull(resMgr, this, "resMgr", HBaseResourceManager.class);
    this.resMgr = resMgr;
    this.delegate = new HBaseDelegate();
    if (context.getAsyncConfig().isAsyncEnabled()) {
        this.execPool = MoreExecutors
                .listeningDecorator(new ThreadPoolExecutor(context.getAsyncConfig().getMinSizeForThreadPool(),
                        context.getAsyncConfig().getMaxSizeForThreadPool(), DEFAULT_THREADPOOL_IDLEEXPIRE,
                        DEFAULT_THREADPOOL_IDLEEXPIRE_UNIT, new SynchronousQueue<Runnable>()));
    } else {//from   w w w  .  j  a v  a 2s .c  om
        this.execPool = null;
    }
}

From source file:com.google.devtools.build.android.AndroidResourceProcessor.java

/**
 * Merges all secondary resources with the primary resources.
 *///from  w ww  . ja va 2 s.c o m
private MergedAndroidData mergeData(final ParsedAndroidData primary, final Path primaryManifest,
        final List<? extends SerializedAndroidData> direct,
        final List<? extends SerializedAndroidData> transitive, final Path resourcesOut, final Path assetsOut,
        @Nullable final PngCruncher cruncher, final VariantType type, @Nullable final Path symbolsOut,
        @Nullable AndroidResourceClassWriter rclassWriter) throws MergingException {
    Stopwatch timer = Stopwatch.createStarted();
    final ListeningExecutorService executorService = MoreExecutors
            .listeningDecorator(Executors.newFixedThreadPool(15));
    try (Closeable closeable = ExecutorServiceCloser.createWith(executorService)) {
        AndroidDataMerger merger = AndroidDataMerger.createWithPathDeduplictor(executorService);
        UnwrittenMergedAndroidData merged = merger.loadAndMerge(transitive, direct, primary, primaryManifest,
                type != VariantType.LIBRARY);
        logger.fine(String.format("merge finished in %sms", timer.elapsed(TimeUnit.MILLISECONDS)));
        timer.reset().start();
        if (symbolsOut != null) {
            AndroidDataSerializer serializer = AndroidDataSerializer.create();
            merged.serializeTo(serializer);
            serializer.flushTo(symbolsOut);
            logger.fine(
                    String.format("serialize merge finished in %sms", timer.elapsed(TimeUnit.MILLISECONDS)));
            timer.reset().start();
        }
        if (rclassWriter != null) {
            merged.writeResourceClass(rclassWriter);
            logger.fine(String.format("write classes finished in %sms", timer.elapsed(TimeUnit.MILLISECONDS)));
            timer.reset().start();
        }
        AndroidDataWriter writer = AndroidDataWriter.createWith(resourcesOut.getParent(), resourcesOut,
                assetsOut, cruncher, executorService);
        return merged.write(writer);
    } catch (IOException e) {
        throw MergingException.wrapException(e).build();
    } finally {
        logger.fine(String.format("write merge finished in %sms", timer.elapsed(TimeUnit.MILLISECONDS)));
    }
}

From source file:com.google.devtools.build.android.AndroidResourceProcessor.java

/** Deserializes a list of serialized resource paths to a {@link ParsedAndroidData}. */
public ParsedAndroidData deserializeSymbolsToData(List<Path> symbolPaths) throws IOException, MergingException {
    AndroidDataSerializer serializer = AndroidDataSerializer.create();
    final ListeningExecutorService executorService = MoreExecutors
            .listeningDecorator(Executors.newFixedThreadPool(15));
    final Builder deserializedDataBuilder = ParsedAndroidData.Builder.newBuilder();
    try (Closeable closeable = ExecutorServiceCloser.createWith(executorService)) {
        List<ListenableFuture<Boolean>> deserializing = new ArrayList<>();
        for (final Path symbolPath : symbolPaths) {
            deserializing.add(/*from  w  w  w  .  ja  v  a2  s  . com*/
                    executorService.submit(new Deserialize(serializer, symbolPath, deserializedDataBuilder)));
        }
        FailedFutureAggregator<MergingException> aggregator = FailedFutureAggregator
                .createForMergingExceptionWithMessage("Failure(s) during dependency parsing");
        aggregator.aggregateAndMaybeThrow(deserializing);
    }
    return deserializedDataBuilder.build();
}

From source file:org.dswarm.graph.resources.GDMResource.java

private void shutDownDeltaDBs(final GraphDatabaseService existingResourceDB,
        final GraphDatabaseService newResourceDB) {

    GDMResource.LOG.debug("start shutting down working graph data model DBs for resources");

    // should probably be delegated to a background worker thread, since it looks like that shutting down the working graph
    // DBs take some time (for whatever reason)
    final ListeningExecutorService service = MoreExecutors.listeningDecorator(Executors.newFixedThreadPool(10));
    service.submit((Callable<Void>) () -> {

        newResourceDB.shutdown();/*  w w  w . j  a v  a 2 s. c  o  m*/
        existingResourceDB.shutdown();

        return null;
    });

    GDMResource.LOG.debug("finished shutting down working graph data model DBs for resources");
}

From source file:org.dswarm.graph.resources.GDMResource.java

private void shutDownDeltaDB(final GraphDatabaseService resourceDB) {

    GDMResource.LOG.debug("start shutting down working graph data model DB for resource");

    // should probably be delegated to a background worker thread, since it looks like that shutting down the working graph
    // DBs take some time (for whatever reason)
    final ListeningExecutorService service = MoreExecutors.listeningDecorator(Executors.newFixedThreadPool(10));
    service.submit((Callable<Void>) () -> {

        resourceDB.shutdown();/*  ww w . j  av a  2 s  .c  o  m*/

        return null;
    });

    GDMResource.LOG.debug("finished shutting down working graph data model DB for resource");
}