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:com.microsoft.intellij.forms.CreateRedisCacheForm.java

private void onOK() {
    try {//from w  w w  .j a v  a  2s.c om
        Azure azure = azureManager.getAzure(currentSub.getSubscriptionId());
        setSubscription(currentSub);
        ProcessingStrategy processor = RedisCacheUtil.doGetProcessor(azure, skus, redisCacheNameValue,
                selectedLocationValue, selectedResGrpValue, selectedPriceTierValue, noSSLPort, newResGrp);
        ExecutorService executor = Executors.newSingleThreadExecutor();
        ListeningExecutorService executorService = MoreExecutors.listeningDecorator(executor);
        ListenableFuture<Void> futureTask = executorService.submit(new CreateRedisCallable(processor));
        final ProcessingStrategy processorInner = processor;
        Futures.addCallback(futureTask, new FutureCallback<Void>() {
            @Override
            public void onSuccess(Void arg0) {
                if (onCreate != null) {
                    onCreate.run();
                }
            }

            @Override
            public void onFailure(Throwable throwable) {
                JOptionPane.showMessageDialog(null, throwable.getMessage(),
                        "Error occurred when creating Redis Cache: " + redisCacheNameValue,
                        JOptionPane.ERROR_MESSAGE, null);
                try {
                    // notify the waitting thread the thread being waited incurred exception to clear blocking queue
                    processorInner.notifyCompletion();
                } catch (InterruptedException ex) {
                    String msg = String.format(CREATING_ERROR_INDICATOR, "notifyCompletion", ex.getMessage());
                    PluginUtil.displayErrorDialogAndLog(message("errTtl"), msg, ex);
                }
            }
        });
        close(DialogWrapper.OK_EXIT_CODE, true);
    } catch (Exception ex) {
        ex.printStackTrace();
    }
}

From source file:com.google.gerrit.server.change.MergeabilityChecker.java

@Inject
public MergeabilityChecker(ThreadLocalRequestContext tl, SchemaFactory<ReviewDb> schemaFactory,
        IdentifiedUser.GenericFactory identifiedUserFactory, ChangeControl.GenericFactory changeControlFactory,
        Provider<Mergeable> mergeable, ChangeIndexer indexer,
        @MergeabilityChecksExecutor(Priority.BACKGROUND) Executor backgroundExecutor,
        @MergeabilityChecksExecutor(Priority.INTERACTIVE) Executor interactiveExecutor,
        MergeabilityCheckQueue mergeabilityCheckQueue, MetaDataUpdate.Server metaDataUpdateFactory) {
    this.tl = tl;
    this.schemaFactory = schemaFactory;
    this.identifiedUserFactory = identifiedUserFactory;
    this.changeControlFactory = changeControlFactory;
    this.mergeable = mergeable;
    this.indexer = indexer;
    this.backgroundExecutor = MoreExecutors.listeningDecorator(backgroundExecutor);
    this.interactiveExecutor = MoreExecutors.listeningDecorator(interactiveExecutor);
    this.mergeabilityCheckQueue = mergeabilityCheckQueue;
    this.metaDataUpdateFactory = metaDataUpdateFactory;
}

From source file:org.bitcoinj.net.discovery.TorDiscovery.java

private synchronized void createThreadPool(int size) {
    threadPool = MoreExecutors.listeningDecorator(
            Executors.newFixedThreadPool(size, new ContextPropagatingThreadFactory("Tor DNS discovery")));
}

From source file:com.facebook.buck.parser.PerBuildStateFactoryWithConfigurableAttributes.java

@Override
protected PerBuildStateWithConfigurableAttributes create(ParsingContext parsingContext,
        DaemonicParserState daemonicParserState, ImmutableList<String> targetPlatforms,
        Optional<AtomicLong> parseProcessedBytes) {

    Cell rootCell = parsingContext.getCell();
    ListeningExecutorService executorService = parsingContext.getExecutor();
    SymlinkCache symlinkCache = new SymlinkCache(eventBus, daemonicParserState);
    CellManager cellManager = new CellManager(symlinkCache);

    TargetNodeListener<TargetNode<?>> symlinkCheckers = cellManager::registerInputsUnderSymlinks;
    ParserConfig parserConfig = rootCell.getBuckConfig().getView(ParserConfig.class);
    int numParsingThreads = parserConfig.getNumParsingThreads();
    DefaultProjectBuildFileParserFactory projectBuildFileParserFactory = new DefaultProjectBuildFileParserFactory(
            typeCoercerFactory, parserPythonInterpreterProvider, parsingContext.isProfilingEnabled(),
            parseProcessedBytes, knownRuleTypesProvider, manifestServiceSupplier, fileHashCache);
    ProjectBuildFileParserPool projectBuildFileParserPool = new ProjectBuildFileParserPool(numParsingThreads, // Max parsers to create per cell.
            projectBuildFileParserFactory, parsingContext.isProfilingEnabled());

    TargetNodeFactory targetNodeFactory = new TargetNodeFactory(typeCoercerFactory);

    BuildFileRawNodeParsePipeline buildFileRawNodeParsePipeline = new BuildFileRawNodeParsePipeline(
            new PipelineNodeCache<>(daemonicParserState.getRawNodeCache()), projectBuildFileParserPool,
            executorService, eventBus, watchman);

    BuildTargetRawNodeParsePipeline buildTargetRawNodeParsePipeline = new BuildTargetRawNodeParsePipeline(
            executorService, buildFileRawNodeParsePipeline);

    ListeningExecutorService pipelineExecutorService = parserConfig.getEnableParallelParsing() ? executorService
            : MoreExecutors.newDirectExecutorService();
    boolean enableSpeculativeParsing = parserConfig.getEnableParallelParsing()
            && parsingContext.getSpeculativeParsing() == SpeculativeParsing.ENABLED;
    RawTargetNodePipeline rawTargetNodePipeline = new RawTargetNodePipeline(pipelineExecutorService,
            daemonicParserState.getOrCreateNodeCache(RawTargetNode.class), eventBus,
            buildFileRawNodeParsePipeline, buildTargetRawNodeParsePipeline,
            new DefaultRawTargetNodeFactory(knownRuleTypesProvider, new BuiltTargetVerifier()));

    PackageBoundaryChecker packageBoundaryChecker = new ThrowingPackageBoundaryChecker(
            daemonicParserState.getBuildFileTrees());

    ParserTargetNodeFactory<RawTargetNode> nonResolvingRawTargetNodeToTargetNodeFactory = new NonResolvingRawTargetNodeToTargetNodeFactory(
            DefaultParserTargetNodeFactory.createForParser(knownRuleTypesProvider, marshaller,
                    daemonicParserState.getBuildFileTrees(), symlinkCheckers, targetNodeFactory));

    // This pipeline uses a direct executor instead of pipelineExecutorService to avoid
    // deadlocks happening when too many node are requested from targetNodeParsePipeline.
    // That pipeline does blocking calls to get nodes from nonResolvingTargetNodeParsePipeline
    // which can lead to deadlocks.
    ParsePipeline<TargetNode<?>> nonResolvingTargetNodeParsePipeline = new RawTargetNodeToTargetNodeParsePipeline(
            daemonicParserState.getOrCreateNodeCache(TargetNode.class),
            MoreExecutors.newDirectExecutorService(), rawTargetNodePipeline, eventBus,
            "nonresolving_raw_target_node_parse_pipeline", enableSpeculativeParsing,
            nonResolvingRawTargetNodeToTargetNodeFactory);

    ConfigurationRuleResolver configurationRuleResolver = new SameThreadConfigurationRuleResolver(
            cellManager::getCell, nonResolvingTargetNodeParsePipeline::getNode);

    SelectableResolver selectableResolver = new ConfigurationRuleSelectableResolver(configurationRuleResolver);

    SelectorListResolver selectorListResolver = new DefaultSelectorListResolver(selectableResolver);

    ConstraintResolver constraintResolver = new RuleBasedConstraintResolver(configurationRuleResolver);

    Supplier<Platform> targetPlatform = Suppliers.memoize(
            () -> getTargetPlatform(configurationRuleResolver, constraintResolver, rootCell, targetPlatforms));

    RawTargetNodeToTargetNodeFactory rawTargetNodeToTargetNodeFactory = new RawTargetNodeToTargetNodeFactory(
            knownRuleTypesProvider, marshaller, targetNodeFactory, packageBoundaryChecker, symlinkCheckers,
            selectorListResolver, constraintResolver, targetPlatform);

    ListeningExecutorService configuredPipelineExecutor = MoreExecutors
            .listeningDecorator(createExecutorService(rootCell.getBuckConfig(), "configured-pipeline"));

    ParsePipeline<TargetNode<?>> targetNodeParsePipeline = new RawTargetNodeToTargetNodeParsePipeline(
            daemonicParserState.getOrCreateNodeCache(TargetNode.class), configuredPipelineExecutor,
            rawTargetNodePipeline, eventBus, "configured_raw_target_node_parse_pipeline",
            enableSpeculativeParsing, rawTargetNodeToTargetNodeFactory) {
        @Override/*ww w .java2s  .c om*/
        public void close() {
            super.close();
            nonResolvingTargetNodeParsePipeline.close();
            rawTargetNodePipeline.close();
            try {
                MostExecutors.shutdown(configuredPipelineExecutor, 1, TimeUnit.MINUTES);
            } catch (InterruptedException e) {
            }
        }
    };

    cellManager.register(rootCell);

    return new PerBuildStateWithConfigurableAttributes(cellManager, buildFileRawNodeParsePipeline,
            targetNodeParsePipeline, parsingContext, constraintResolver, selectorListResolver, targetPlatform);
}

From source file:com.github.rinde.rinsim.central.rt.RtSolverModel.java

void initExecutor() {
    if (!executor.isPresent() && mode != Mode.UNKNOWN) {

        final ThreadFactory factory;
        final String newName = String.format("%s-%s", Thread.currentThread().getName(),
                getClass().getSimpleName());
        if (threadGroupingEnabled) {
            factory = new AffinityGroupThreadFactory(newName, manager);
        } else {/*  w ww .  j  a v  a 2  s  .com*/
            factory = new AffinityThreadFactory(newName, AffinityStrategies.ANY);
        }

        final int threads;
        if (threadPoolSize == 0) {
            if (mode == Mode.SINGLE_MODE) {
                threads = DEFAULT_NUM_THREADS_IN_SINGLE_MODE;
            } else {
                threads = Runtime.getRuntime().availableProcessors();
            }
        } else {
            threads = threadPoolSize;
        }
        LOGGER.trace("Create executor with {} threads and factory {}.", threads, factory);
        executor = Optional
                .of(MoreExecutors.listeningDecorator(Executors.newFixedThreadPool(threads, factory)));
    }
}

From source file:com.spotify.apollo.core.ServiceImpl.java

ListeningExecutorService createExecutorService(Closer closer) {
    final ListeningExecutorService executorService = MoreExecutors.listeningDecorator(Executors
            .newCachedThreadPool(new ThreadFactoryBuilder().setNameFormat(serviceName + "-worker-%d").build()));
    closer.register(asCloseable(executorService));
    return executorService;
}

From source file:org.apache.crunch.impl.spark.SparkRuntime.java

private void runCallables(Set<Target> unfinished) {
    Set<PipelineCallable<?>> oldCallables = activePipelineCallables;
    activePipelineCallables = Sets.newHashSet();
    List<PipelineCallable<?>> callablesToRun = Lists.newArrayList();
    List<PipelineCallable<?>> failedCallables = Lists.newArrayList();
    for (PipelineCallable<?> pipelineCallable : oldCallables) {
        if (Sets.intersection(allPipelineCallables.get(pipelineCallable), unfinished).isEmpty()) {
            if (pipelineCallable.runSingleThreaded()) {
                try {
                    if (pipelineCallable.call() != PipelineCallable.Status.SUCCESS) {
                        failedCallables.add(pipelineCallable);
                    }/* w w  w. ja v  a  2 s  . c om*/
                } catch (Throwable t) {
                    pipelineCallable.setMessage(t.getLocalizedMessage());
                    failedCallables.add(pipelineCallable);
                }
            } else {
                callablesToRun.add(pipelineCallable);
            }
        } else {
            // Still need to run this one
            activePipelineCallables.add(pipelineCallable);
        }
    }

    ListeningExecutorService es = MoreExecutors.listeningDecorator(Executors.newCachedThreadPool());
    try {
        List<Future<PipelineCallable.Status>> res = es.invokeAll(callablesToRun);
        for (int i = 0; i < res.size(); i++) {
            if (res.get(i).get() != PipelineCallable.Status.SUCCESS) {
                failedCallables.add((PipelineCallable) callablesToRun.get(i));
            }
        }
    } catch (Throwable t) {
        t.printStackTrace();
        failedCallables.addAll((List) callablesToRun);
    } finally {
        es.shutdownNow();
    }

    if (!failedCallables.isEmpty()) {
        LOG.error("{} callable failure(s) occurred:", failedCallables.size());
        for (PipelineCallable<?> c : failedCallables) {
            LOG.error("{} : {}", c.getName(), c.getMessage());
        }
        status.set(Status.FAILED);
        set(PipelineResult.EMPTY);
        doneSignal.countDown();
    }
}

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

BaseAppenderatorDriver(Appenderator appenderator, SegmentAllocator segmentAllocator,
        UsedSegmentChecker usedSegmentChecker, DataSegmentKiller dataSegmentKiller) {
    this.appenderator = Preconditions.checkNotNull(appenderator, "appenderator");
    this.segmentAllocator = Preconditions.checkNotNull(segmentAllocator, "segmentAllocator");
    this.usedSegmentChecker = Preconditions.checkNotNull(usedSegmentChecker, "usedSegmentChecker");
    this.dataSegmentKiller = Preconditions.checkNotNull(dataSegmentKiller, "dataSegmentKiller");
    this.executor = MoreExecutors.listeningDecorator(Execs.singleThreaded("publish-%d"));
}

From source file:org.attribyte.relay.Relay.java

/**
 * Creates a relay from command line arguments.
 * <p>//from w  ww .  ja  v a  2  s  . c  om
 *    Arguments that begin with '-' are added as configuration properties.
 *    Other arguments are expected to be configuration files.
 *    Files that start with 'log.' are used to configure loggers.
 * </p>
 * @param args The arguments.
 * @throws Exception on initialization error.
 */
public Relay(String[] args) throws Exception {

    this.registry = new MetricRegistry();

    CLI cli = new CLI("relay", args);
    this.logger = cli.logger != null ? cli.logger : new ConsoleLogger();

    InitUtil relayProps = new InitUtil("relay.", cli.props, false);

    String acceptCodes = relayProps.getProperty("acceptCodes", Integer.toString(Publisher.HTTP_ACCEPTED));
    Set<Integer> acceptCodeSet = Sets.newHashSet();
    for (String codeStr : Splitter.on(", ").omitEmptyStrings().trimResults().split(acceptCodes)) {
        Integer code = Ints.tryParse(codeStr);
        if (code != null) {
            acceptCodeSet.add(code);
        }
    }

    cli.logInfo("Creating async publisher...");

    this.publisher = new AsyncPublisher(relayProps.getIntProperty("concurrency", 2),
            relayProps.getIntProperty("maxQueueSize", 0),
            relayProps.getIntProperty("notificationTimeoutSeconds", 30), acceptCodeSet);

    registry.register("async-publisher", this.publisher);

    InitUtil targetProps = new InitUtil("target.", cli.props, false);

    this.targetURL = targetProps.getProperty("notificationURL", "").trim();
    if (this.targetURL.isEmpty()) {
        throw new Exception("A 'target.notificationURL' must be supplied");
    }

    List<Header> headers = Lists.newArrayListWithExpectedSize(4);

    if (targetProps.hasProperty("username") && targetProps.hasProperty("password")) {
        final String authHeaderValue = BasicAuthScheme.buildAuthHeaderValue(
                targetProps.getProperty("username", "").trim(), targetProps.getProperty("password", "").trim());
        headers.add(new Header(BasicAuthScheme.AUTH_HEADER, authHeaderValue));
    }

    if (targetProps.hasProperty("headers")) {
        Splitter.on(',').trimResults().omitEmptyStrings().withKeyValueSeparator('=')
                .split(targetProps.getProperty("headers")).entrySet().stream()
                .forEach(kv -> headers.add(new Header(kv.getKey(), kv.getValue())));
    }

    this.headers = ImmutableList.copyOf(headers);

    InitUtil supplierProps = new InitUtil("supplier.", cli.props, false);

    cli.logInfo("Creating supplier...");
    this.supplier = (Supplier) supplierProps.initClass("class", Supplier.class);

    cli.logInfo("Creating transformer...");
    InitUtil transformerProps = new InitUtil("transformer.", cli.props, false);
    if (transformerProps.getProperty("class", "").trim().isEmpty()) {
        this.transformer = Transformer.NOOP;
    } else {
        this.transformer = (Transformer) transformerProps.initClass("class", Transformer.class);
    }

    cli.logInfo("Initializing supplier...");

    final Optional<ByteString> savedState;
    String supplierStateFile = supplierProps.getProperty("state.file", "").trim();
    if (!supplierStateFile.isEmpty()) {
        cli.logInfo("Initializing saved supplier state...");
        this.supplierStateFile = new File(supplierStateFile);
        final Optional<ByteString> state;
        if (this.supplierStateFile.exists()) {
            state = Optional.of(ByteString.copyFrom(Files.toByteArray(this.supplierStateFile)));
        } else {
            state = Optional.absent();
        }
        savedState = state;
    } else {
        this.supplierStateFile = null;
        savedState = Optional.absent();
    }

    this.completeWithTransformedMessage = supplierProps.getProperty("completeWithTransformedMessage", "false")
            .equalsIgnoreCase("true");

    this.supplier.init(supplierProps.getProperties(), savedState, logger);
    registry.register("supplier", this.supplier);

    cli.logInfo("Initializing transformer...");
    this.transformer.init(this.supplier, transformerProps.getProperties(), logger);
    registry.register("transformer", this.transformer);

    int finishExecutorPoolSize = relayProps.getIntProperty("finishExecutorPoolSize", 2);
    this.finishExecutor = Executors.newFixedThreadPool(finishExecutorPoolSize,
            new ThreadFactoryBuilder().setNameFormat("relay-finish-%d").build());

    int retryExecutorPoolSize = relayProps.getIntProperty("retryExecutorPoolSize", 2);
    this.retryExecutor = MoreExecutors.listeningDecorator(new ScheduledThreadPoolExecutor(retryExecutorPoolSize,
            new ThreadFactoryBuilder().setNameFormat("relay-retry-%d").build(),
            new ThreadPoolExecutor.AbortPolicy()));

    this.maxRetryAttempts = relayProps.getIntProperty("maxRetryAttempts", 10);
    this.baseBackOffDelayMillis = relayProps.getIntProperty("baseBackOffDelayMillis", 50);
    this.maxShutdownWaitSeconds = relayProps.getIntProperty("maxShutdownWaitSeconds", 30);
    this.reporting = new Reporting("metrics-reporting.", cli.props, registry, null);
    this.reporting.start();
}

From source file:com.ibm.og.client.ApacheClient.java

private ApacheClient(final Builder builder) {
    this.connectTimeout = builder.connectTimeout;
    this.soTimeout = builder.soTimeout;
    this.soReuseAddress = builder.soReuseAddress;
    this.soLinger = builder.soLinger;
    this.soKeepAlive = builder.soKeepAlive;
    this.tcpNoDelay = builder.tcpNoDelay;
    this.soSndBuf = builder.soSndBuf;
    this.soRcvBuf = builder.soRcvBuf;
    this.persistentConnections = builder.persistentConnections;
    this.validateAfterInactivity = builder.validateAfterInactivity;
    this.maxIdleTime = builder.maxIdleTime;
    this.chunkedEncoding = builder.chunkedEncoding;
    this.expectContinue = builder.expectContinue;
    this.waitForContinue = builder.waitForContinue;
    this.retryCount = builder.retryCount;
    this.requestSentRetry = builder.requestSentRetry;

    // TODO validate protocol values
    final List<String> protocols = builder.protocols;
    if (protocols != null) {
        this.protocols = ImmutableList.copyOf(protocols);
    } else {//from w  ww.jav  a  2s  .c  om
        this.protocols = null;
    }

    final List<String> cipherSuites = builder.cipherSuites;
    if (cipherSuites != null) {
        this.cipherSuites = ImmutableList.copyOf(cipherSuites);
    } else {
        this.cipherSuites = null;
    }

    final String keyStore = builder.keyStore;
    if (keyStore != null) {
        this.keyStore = new File(keyStore);
        checkArgument(this.keyStore.exists(), "keyStore does not exist [%s]", this.keyStore);
    } else {
        this.keyStore = null;
    }
    this.keyStorePassword = builder.keyStorePassword;
    if (this.keyStorePassword != null) {
        checkArgument(this.keyStore != null, "if keyStorePassword is != null, keyStore must be != null");
    }
    this.keyPassword = builder.keyPassword;
    if (this.keyPassword != null) {
        checkArgument(this.keyStore != null, "if keyPassword is != null, keyStore must be != null");
    }

    final String trustStore = builder.trustStore;
    if (trustStore != null) {
        this.trustStore = new File(trustStore);
        checkArgument(this.trustStore.exists(), "trustStore does not exist [%s]", this.trustStore);
    } else {
        this.trustStore = null;
    }
    this.trustStorePassword = builder.trustStorePassword;
    if (this.trustStorePassword != null) {
        checkArgument(this.trustStore != null, "if trustStorePassword is != null, trustStore must be != null");
    }
    this.trustSelfSignedCertificates = builder.trustSelfSignedCertificates;
    this.dnsCacheTtl = builder.dnsCacheTtl;
    this.dnsCacheNegativeTtl = builder.dnsCacheNegativeTtl;
    this.authentication = checkNotNull(builder.authentication);
    this.userAgent = builder.userAgent;
    this.writeThroughput = builder.writeThroughput;
    this.readThroughput = builder.readThroughput;
    this.responseBodyConsumers = ImmutableMap.copyOf(builder.responseBodyConsumers);
    this.running = true;
    this.abortedRequestsAtShutdown = new AtomicInteger();
    final ThreadFactory fac = new ThreadFactoryBuilder().setNameFormat("client-%d").build();
    this.executorService = MoreExecutors.listeningDecorator(Executors.newCachedThreadPool(fac));
    this.gson = createGson();

    // perform checks on instance fields rather than builder fields
    checkArgument(this.connectTimeout >= 0, "connectTimeout must be >= 0 [%s]", this.connectTimeout);
    checkArgument(this.soTimeout >= 0, "soTimeout must be >= 0 [%s]", this.soTimeout);
    checkArgument(this.soLinger >= -1, "soLinger must be >= -1 [%s]", this.soLinger);
    checkArgument(this.soSndBuf >= 0, "soSndBuf must be >= 0 [%s]", this.soSndBuf);
    checkArgument(this.soRcvBuf >= 0, "soRcvBuf must be >= 0 [%s]", this.soRcvBuf);
    checkArgument(this.validateAfterInactivity > 0, "validateAfterInactivity must be > 0 [%s]",
            this.validateAfterInactivity);
    checkArgument(this.maxIdleTime > 0, "maxIdleTime must be > 0 [%s]", this.maxIdleTime);
    checkArgument(this.waitForContinue > 0, "waitForContinue must be > 0 [%s]", this.waitForContinue);
    checkArgument(this.retryCount >= 0, "retryCount must be >= 0 [%s]", this.retryCount);
    checkArgument(this.dnsCacheTtl >= -1, "dnsCacheTtl must be >= -1 [%s]", this.dnsCacheTtl);
    checkArgument(this.dnsCacheNegativeTtl >= -1, "dnsCacheNegativeTtl must be >= -1 [%s]",
            this.dnsCacheNegativeTtl);
    checkArgument(this.writeThroughput >= 0, "writeThroughput must be >= 0 [%s]", this.writeThroughput);
    checkArgument(this.readThroughput >= 0, "readThroughput must be >= 0 [%s]", this.readThroughput);

    Security.setProperty("networkaddress.cache.ttl", String.valueOf(this.dnsCacheTtl));
    Security.setProperty("networkaddress.cache.negative.ttl", String.valueOf(this.dnsCacheNegativeTtl));

    this.client = createClient();
}