Example usage for com.google.common.util.concurrent ListeningExecutorService submit

List of usage examples for com.google.common.util.concurrent ListeningExecutorService submit

Introduction

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

Prototype

@Override
ListenableFuture<?> submit(Runnable task);

Source Link

Usage

From source file:com.datastax.driver.core.SessionManager.java

ListenableFuture<Boolean> forceRenewPool(final Host host, ListeningExecutorService executor) {
    final HostDistance distance = cluster.manager.loadBalancingPolicy().distance(host);
    if (distance == HostDistance.IGNORED)
        return Futures.immediateFuture(true);

    // Creating a pool is somewhat long since it has to create the connection, so do it asynchronously.
    return executor.submit(new Callable<Boolean>() {
        public Boolean call() {
            while (true) {
                try {
                    if (isClosing)
                        return true;

                    HostConnectionPool newPool = new HostConnectionPool(host, distance, SessionManager.this);
                    HostConnectionPool previous = pools.put(host, newPool);
                    if (previous == null) {
                        logger.debug("Added connection pool for {}", host);
                    } else {
                        logger.debug("Renewed connection pool for {}", host);
                        previous.closeAsync();
                    }/*w  w w  . ja  va  2  s  . c  o m*/

                    // If we raced with a session shutdown, ensure that the pool will be closed.
                    if (isClosing)
                        newPool.closeAsync();

                    return true;
                } catch (Exception e) {
                    logger.error("Error creating pool to " + host, e);
                    return false;
                }
            }
        }
    });
}

From source file:org.nmdp.service.epitope.guice.CachingFunction.java

/**
 * construct a CachingResolver with the specified resolver and cache parameters
 * @param resolver the resolver to delegate to to populate the cache
 * @param duration duration of time to cache for before expiring
 * @param period period of time between cache refreshes
 * @param cacheCapacity max capacity of the cache
 *///w  w  w .  jav a2 s .  c  o  m
@Inject
public CachingFunction(final Function<K, V> delegate, final @CacheDuration long duration,
        final @CachePeriod long period, final @CacheCapacity long cacheCapacity) {
    final ListeningExecutorService executor = MoreExecutors
            .listeningDecorator(Executors.newFixedThreadPool(10));
    cache = CacheBuilder.newBuilder().refreshAfterWrite(period, TimeUnit.MILLISECONDS)
            .expireAfterAccess(duration, TimeUnit.MILLISECONDS).initialCapacity(3).maximumSize(cacheCapacity)
            .build(new CacheLoader<K, Optional<V>>() {
                public Optional<V> load(K key) {
                    return Optional.fromNullable(delegate.apply(key));
                }

                @Override
                public ListenableFuture<Optional<V>> reload(final K key, final Optional<V> oldValue)
                        throws Exception {
                    final ListenableFuture<Optional<V>> future = executor.submit(() -> load(key));
                    notifyListeners(future, key, oldValue);
                    return future;
                }
            });
}

From source file:org.multibit.viewsystem.swing.action.SendAssetConfirmAction.java

/**
 * Complete the transaction to work out the fee) and then show the send bitcoin confirm dialog.
 *///from  w w w .  j  a va 2s.  co m
@Override
public void actionPerformed(ActionEvent e) {
    if (abort()) {
        return;
    }

    //        SendAssetConfirmDialog sendAssetConfirmDialog = null;
    AssetValidationErrorDialog validationErrorDialog = null;

    try {
        String sendAddress = dataProvider.getAddress();
        String sendAmount = Utils.bitcoinValueToPlainString(BitcoinModel.COINSPARK_SEND_MINIMUM_AMOUNT);
        String sendMessage = null;
        boolean canSendMessage = false;

        final int assetId = dataProvider.getAssetId();
        String assetAmount = dataProvider.getAssetAmount();
        boolean isSenderPays = dataProvider.isSenderPays();

        /* Is there a payment charge?  If yes, the asset amount will change */
        CSAsset asset = bitcoinController.getModel().getActiveWallet().CS.getAsset(assetId);
        CoinSparkGenesis genesis = asset.getGenesis();
        BigInteger assetAmountRawUnits = CSMiscUtils.getRawUnitsFromDisplayString(asset, assetAmount);
        // Was there any rounding, which the user did not see because they clicked on send first,
        // without losing focus to any other widget which corrects field?
        String typedAmount = dataProvider.getAssetAmountText();
        BigDecimal bd1 = new BigDecimal(typedAmount);
        BigDecimal bd2 = new BigDecimal(assetAmount);
        bd1 = bd1.stripTrailingZeros();
        bd2 = bd2.stripTrailingZeros();
        if (bd1.equals(bd2) == false) {
            String displayUnit = CSMiscUtils.getFormattedDisplayStringForRawUnits(asset, BigInteger.ONE);
            String s = "The smallest transactable unit is " + displayUnit
                    + ", so we have rounded the sum down.\nPlease confirm the final amount and click 'Send' again.";
            JOptionPane.showMessageDialog(mainFrame, s);
            return;
        }
        // End rounding check/warning

        long desiredRawUnits = assetAmountRawUnits.longValue();
        short chargeBasisPoints = genesis.getChargeBasisPoints();
        long rawFlatChargeAmount = genesis.getChargeFlat();
        boolean chargeExists = (rawFlatChargeAmount > 0 || chargeBasisPoints > 0);
        if (chargeExists) {
            if (isSenderPays) {
                long x = genesis.calcGross(desiredRawUnits);
                assetAmountRawUnits = new BigInteger(String.valueOf(x));
            } else {
                // We don't have to do anything if recipient pays, just send gross amount.
                // calcNet() returns what the recipient will receive, but it's not what we send.
                //          long x = genesis.calcNet(desiredRawUnits);
                //          assetAmountRawUnits = new BigInteger(String.valueOf(x));          
            }
        }

        // Todo: Allow invalid assets to be sent even if spendable balance is 0
        //if (CSMiscUtils.canSendInvalidAsset(bitcoinController) 
        final AssetValidator validator = new AssetValidator(super.bitcoinController);
        if (validator.validate(sendAddress, sendAmount, assetId, assetAmountRawUnits.toString())) {
            /* CoinSpark START */
            CoinSparkPaymentRef paymentRef = null;

            // We have already validated that the coinspark address and underlying bitcoin are good
            // and that the transfer flag is set on the coinspark address.  We just want the actual
            // underlying bitcoin address to send assets to, which is used to create SendRequest object.
            CoinSparkAddress csa = CSMiscUtils.decodeCoinSparkAddress(sendAddress);
            String btcAddress = CSMiscUtils.getBitcoinAddressStringFromCoinSparkAddress(csa);
            sendAddress = btcAddress;

            // Does a payment ref exist?
            int flags = csa.getAddressFlags();
            if ((flags & CoinSparkAddress.COINSPARK_ADDRESS_FLAG_PAYMENT_REFS) > 0) {
                paymentRef = csa.getPaymentRef();
                log.debug(">>>> CoinSpark address has payment refs flag set: " + paymentRef.toString());
            }

            // Messages - can send message and BTC to CoinSpark address, without any assets.
            sendMessage = dataProvider.getMessage();
            canSendMessage = (flags & CoinSparkAddress.COINSPARK_ADDRESS_FLAG_TEXT_MESSAGES) > 0;
            /* CoinSpark END */

            // Create a SendRequest.
            Address sendAddressObject;

            sendAddressObject = new Address(bitcoinController.getModel().getNetworkParameters(), sendAddress);
            //SendRequest sendRequest = SendRequest.to(sendAddressObject, Utils.toNanoCoins(sendAmount));

            //public static SendRequest to(Address destination,BigInteger value,int assetID, BigInteger assetValue,int split) {
            //BigInteger assetAmountRawUnits = new BigInteger(assetAmount);
            BigInteger bitcoinAmountSatoshis = Utils.toNanoCoins(sendAmount);

            final SendRequest sendRequest = SendRequest.to(sendAddressObject, bitcoinAmountSatoshis, assetId,
                    assetAmountRawUnits, 1);
            sendRequest.ensureMinRequiredFee = true;
            sendRequest.fee = BigInteger.ZERO;
            sendRequest.feePerKb = BitcoinModel.SEND_FEE_PER_KB_DEFAULT;

            // Note - Request is populated with the AES key in the SendBitcoinNowAction after the user has entered it on the SendBitcoinConfirm form.

            // Send with payment ref - if it exists and is not 0 which SparkBit treats semantically as null
            if (paymentRef != null && paymentRef.getRef() != 0) {
                sendRequest.setPaymentRef(paymentRef);
            }

            // Send a message if the address will take it and message is not empty
            boolean willSendMessage = false;
            if (canSendMessage) {
                boolean isEmptyMessage = false;
                if (sendMessage == null || sendMessage.isEmpty() || sendMessage.trim().length() == 0) {
                    isEmptyMessage = true;
                }
                if (!isEmptyMessage) {
                    willSendMessage = true;
                    //int numParts = 1;
                    CoinSparkMessagePart[] parts = {
                            CSMiscUtils.createPlainTextCoinSparkMessagePart(sendMessage) };
                    String[] serverURLs = CSMiscUtils.getMessageDeliveryServersArray(bitcoinController);
                    sendRequest.setMessage(parts, serverURLs);

                    log.debug(">>>> Messaging servers = " + ArrayUtils.toString(serverURLs));
                    log.debug(">>>> parts[0] = " + parts[0]);
                    log.debug(">>>> parts[0].fileName = " + parts[0].fileName);
                    log.debug(">>>> parts[0].mimeType = " + parts[0].mimeType);
                    log.debug(">>>> parts[0].content = " + new String(parts[0].content, "UTF-8"));
                    //String message = "Hello, the time is now..." + DateTime.now().toString();
                    //      parts[2].fileName = imagePath;
                    //      parts[2].mimeType = "image/png";
                    //      byte[] imageBytes = Files.readAllBytes(Paths.get(imagePath));
                    //      parts[2].content = imageBytes;

                }
            }

            //
            // When sending a message, show a modal dialog.
            // CompleteTX now occurs in background thread so UI does not block
            // when "Send" is clicked with widget updates frozen.
            //

            // Show dialog with indeterminate progress bar
            final JDialog dialog = CSMiscUtils.createModalMessageDialogWithIndeterminateProgress(mainFrame,
                    "SparkBit", "Contacting message delivery servers...");
            // Dialog is made visible after futures have been set up

            ListeningExecutorService service = MoreExecutors
                    .listeningDecorator(Executors.newSingleThreadExecutor()); //newFixedThreadPool(10));
            ListenableFuture<Boolean> future = service.submit(new Callable<Boolean>() {
                public Boolean call() throws Exception {
                    try {
                        // Complete it (which works out the fee) but do not sign it yet.
                        log.debug("Just about to complete the tx (and calculate the fee)...");
                        bitcoinController.getModel().getActiveWallet().completeTx(sendRequest, false);
                        log.debug("The fee after completing the transaction was " + sendRequest.fee);

                    } catch (Exception e) {
                        throw e;
                    }
                    return true;
                }
            });

            final BigInteger myAssetAmountRawUnits = assetAmountRawUnits;

            Futures.addCallback(future, new FutureCallback<Boolean>() {
                public void onSuccess(Boolean b) {

                    // There is enough money.
                    SwingUtilities.invokeLater(new Runnable() {
                        @Override
                        public void run() {
                            dialog.dispose();

                            SendAssetConfirmDialog mySendAssetConfirmDialog = new SendAssetConfirmDialog(
                                    bitcoinController, mainFrame, sendRequest, assetId, myAssetAmountRawUnits,
                                    validator);
                            mySendAssetConfirmDialog.setVisible(true);
                        }
                    });

                }

                public void onFailure(Throwable thrown) {
                    final String failureReason = thrown.getMessage();
                    final boolean isCSException = thrown instanceof org.coinspark.core.CSExceptions.CannotEncode;
                    final boolean isInsufficientMoney = thrown instanceof com.google.bitcoin.core.InsufficientMoneyException;
                    // There is not enough money.
                    // TODO setup validation parameters accordingly so that it displays ok.
                    SwingUtilities.invokeLater(new Runnable() {
                        @Override
                        public void run() {
                            dialog.dispose();

                            if (isCSException) {
                                JOptionPane
                                        .showMessageDialog(mainFrame,
                                                "SparkBit is unable to proceed with this transaction:\n\n"
                                                        + failureReason,
                                                "SparkBit Error", JOptionPane.ERROR_MESSAGE);
                            } else if (isInsufficientMoney) {
                                // Try to show a human friendly message, need to pass the missing satoshis from failure reason.
                                try {
                                    String numberOnly = failureReason.replaceAll("[^0-9]", "");
                                    BigInteger needed = new BigInteger(numberOnly);
                                    JOptionPane.showMessageDialog(mainFrame,
                                            "SparkBit is unable to proceed with this transaction.\n\nInsufficient money in wallet, require "
                                                    + Utils.bitcoinValueToFriendlyString(needed) + " BTC more.",
                                            "SparkBit Error", JOptionPane.ERROR_MESSAGE);
                                } catch (NumberFormatException e) {
                                    AssetValidationErrorDialog myValidationErrorDialog = new AssetValidationErrorDialog(
                                            bitcoinController, mainFrame, sendRequest, true, validator);
                                    myValidationErrorDialog.setVisible(true);
                                }
                            } else {

                                AssetValidationErrorDialog myValidationErrorDialog = new AssetValidationErrorDialog(
                                        bitcoinController, mainFrame, sendRequest, true, validator);
                                myValidationErrorDialog.setVisible(true);
                            }
                        }
                    });
                }
            });

            // Show message server dialog only if we are going to send
            if (willSendMessage) {
                dialog.setVisible(true);
            }

            /*      
                            // Complete it (which works out the fee) but do not sign it yet.
                            log.debug("Just about to complete the tx (and calculate the fee)...");
                            boolean completedOk;
                            try {
            bitcoinController.getModel().getActiveWallet().completeTx(sendRequest, false);
                              completedOk = true;
                              log.debug("The fee after completing the transaction was " + sendRequest.fee);
                            } catch (InsufficientMoneyException ime) {
                              completedOk = false;
                            }
                            if (completedOk) {
            // There is enough money.
                    
            sendAssetConfirmDialog = new SendAssetConfirmDialog(super.bitcoinController, mainFrame, sendRequest, assetId, assetAmountRawUnits, validator);
            sendAssetConfirmDialog.setVisible(true);
                            } else {
            // There is not enough money.
            // TODO setup validation parameters accordingly so that it displays ok.
            validationErrorDialog = new AssetValidationErrorDialog(super.bitcoinController, mainFrame, sendRequest, true, validator);
            validationErrorDialog.setVisible(true);
                            }
            */

        } else {
            validationErrorDialog = new AssetValidationErrorDialog(super.bitcoinController, mainFrame, null,
                    false, validator);
            validationErrorDialog.setVisible(true);
        }
    } catch (WrongNetworkException e1) {
        logMessage(e1);
    } catch (AddressFormatException e1) {
        logMessage(e1);
    } catch (KeyCrypterException e1) {
        logMessage(e1);
    } catch (NumberFormatException nfe) {
        JOptionPane.showMessageDialog(mainFrame, "Please enter a valid amount.");
    } catch (Exception e1) {
        logMessage(e1);
    }
}

From source file:com.appdynamics.monitors.azure.statsCollector.AzureServiceBusStatsCollector.java

public Map<String, String> collectQueueStats(final Azure azure, final String namespaceName,
        Set<String> queueNames, Set<String> queueStats, int queueThreads) throws TaskExecutionException {

    final Map<String, String> valueMap = createValueMap(azure, namespaceName, QUEUES, queueStats);

    ListeningExecutorService queueService = MoreExecutors
            .listeningDecorator(Executors.newFixedThreadPool(queueThreads));
    final Map<String, String> queueMetricMap = new HashMap<String, String>();
    final CountDownLatch countDownLatch = new CountDownLatch(queueNames.size());

    try {//from  w w  w  .j av  a  2  s .  c o  m
        for (final String queueName : queueNames) {
            valueMap.put("ResourceName", queueName);
            try {
                ListenableFuture<Map<String, String>> getQueueNames = queueService
                        .submit(new Callable<Map<String, String>>() {
                            public Map<String, String> call() throws IOException {
                                return getStatsFromAzure(azure, namespaceName, valueMap, queueName, QUEUES);
                            }
                        });

                Futures.addCallback(getQueueNames, new FutureCallback<Map<String, String>>() {
                    public void onSuccess(Map<String, String> queueStats) {
                        countDownLatch.countDown();
                        queueMetricMap.putAll(queueStats);
                    }

                    public void onFailure(Throwable thrown) {
                        countDownLatch.countDown();
                        logger.error("Unable to get stats for queue [" + queueName + "] in namespace ["
                                + namespaceName + "]", thrown);
                    }
                });

            } catch (Exception e) {
                logger.error("Error getting stats for queue [" + namespaceName + "/" + queueName + "]", e);
                throw new TaskExecutionException(
                        "Error getting stats for queue [" + namespaceName + "/" + queueName + "]", e);
            }
        }
    } finally {
        queueService.shutdown();
    }
    try {
        countDownLatch.await();
    } catch (InterruptedException e) {
        logger.error("Unable to wait till getting the queue stats", e);
    }
    return queueMetricMap;
}

From source file:com.appdynamics.monitors.azure.statsCollector.AzureServiceBusStatsCollector.java

public Map<String, String> collectTopicStats(final Azure azure, final String namespaceName,
        Set<String> topicNames, Set<String> topicStats, int topicThreads) throws TaskExecutionException {

    final Map<String, String> valueMap = createValueMap(azure, namespaceName, TOPICS, topicStats);

    ListeningExecutorService topicService = MoreExecutors
            .listeningDecorator(Executors.newFixedThreadPool(topicThreads));
    final Map<String, String> topicMetricMap = new ConcurrentHashMap<String, String>();
    final CountDownLatch countDownLatch = new CountDownLatch(topicNames.size());

    try {//w w w. java  2  s. c o m
        for (final String topicName : topicNames) {
            valueMap.put("ResourceName", topicName);
            try {
                ListenableFuture<Map<String, String>> getQueueNames = topicService
                        .submit(new Callable<Map<String, String>>() {
                            public Map<String, String> call() throws IOException {
                                return getStatsFromAzure(azure, namespaceName, valueMap, topicName, TOPICS);
                            }
                        });

                Futures.addCallback(getQueueNames, new FutureCallback<Map<String, String>>() {
                    public void onSuccess(Map<String, String> queueStats) {
                        countDownLatch.countDown();
                        topicMetricMap.putAll(queueStats);
                    }

                    public void onFailure(Throwable thrown) {
                        countDownLatch.countDown();
                        logger.error("Unable to get stats for topic [" + topicName + "] in namespace ["
                                + namespaceName + "]", thrown);
                    }
                });
            } catch (Exception e) {
                logger.error("Error getting stats for topic [" + namespaceName + "/" + topicName + "]", e);
                throw new TaskExecutionException(
                        "Error getting stats for topic [" + namespaceName + "/" + topicName + "]", e);
            }
        }
    } finally {
        topicService.shutdown();
    }

    try {
        countDownLatch.await();
    } catch (InterruptedException e) {
        logger.error("Unable to wait till getting the topic stats", e);
    }
    return topicMetricMap;
}

From source file:com.github.rinde.rinsim.experiment.LocalComputer.java

@Override
public ExperimentResults compute(Builder builder, Set<SimArgs> inputs) {
    final ImmutableList.Builder<ExperimentRunner> runnerBuilder = ImmutableList.builder();
    for (final SimArgs args : inputs) {
        runnerBuilder.add(new ExperimentRunner(args));
    }/* w  w w  . j a va2s. c  o  m*/

    final List<ExperimentRunner> runners = runnerBuilder.build();

    final int threads = Math.min(builder.numThreads, runners.size());
    final ListeningExecutorService executor;
    if (builder.showGui) {
        executor = MoreExecutors.newDirectExecutorService();
    } else {
        executor = MoreExecutors
                .listeningDecorator(Executors.newFixedThreadPool(threads, new LocalThreadFactory()));
    }

    final List<SimulationResult> results = Collections.synchronizedList(new ArrayList<SimulationResult>());
    final ResultCollector resultCollector = new ResultCollector(executor, results, builder.resultListeners);

    try {
        for (final ExperimentRunner r : runners) {
            checkForError(executor, resultCollector);
            final ListenableFuture<SimulationResult> f = executor.submit(r);
            Futures.addCallback(f, resultCollector);
        }
        while (results.size() < inputs.size() && !resultCollector.hasError()) {
            Thread.sleep(THREAD_SLEEP_TIME_MS);
        }
        checkForError(executor, resultCollector);
    } catch (final InterruptedException e) {
        LOGGER.trace("Interrupt, shutting down the executor.");
        executor.shutdownNow();
        LOGGER.trace("Waiting for executor to shutdown.");
        try {
            final boolean executorStopped = executor.awaitTermination(MAX_WAIT_FOR_SHUTDOWN_S,
                    TimeUnit.SECONDS);
            if (executorStopped) {
                LOGGER.trace("Executor is shutdown.");
            } else {
                LOGGER.warn("Executor did not stop, timed out after {} seconds.", MAX_WAIT_FOR_SHUTDOWN_S);
            }
        } catch (final InterruptedException e1) {
            LOGGER.warn("Waiting for executor to shutdown is interrupted.");
        }
        return ExperimentResults.create(builder, ImmutableSet.<SimulationResult>of());
    }

    checkForError(executor, resultCollector);
    executor.shutdown();

    final ExperimentResults er = ExperimentResults.create(builder, ImmutableSet.copyOf(results));
    for (final ResultListener rl : builder.resultListeners) {
        rl.doneComputing(er);
    }
    return er;
}

From source file:com.b2international.index.es.EsDocumentWriter.java

@Override
public void commit() throws IOException {
    if (indexOperations.isEmpty() && deleteOperations.isEmpty() && updateOperations.isEmpty()) {
        return;/*  w w w  .j a v  a 2  s.c om*/
    }

    final Set<DocumentMapping> mappingsToRefresh = Collections.synchronizedSet(newHashSet());
    final EsClient client = admin.client();
    // apply bulk updates first
    final ListeningExecutorService executor;
    if (updateOperations.size() > 1) {
        executor = MoreExecutors
                .listeningDecorator(Executors.newFixedThreadPool(Math.min(4, updateOperations.size())));
    } else {
        executor = MoreExecutors.newDirectExecutorService();
    }
    final List<ListenableFuture<?>> updateFutures = newArrayList();
    for (BulkUpdate<?> update : updateOperations) {
        updateFutures.add(executor.submit(() -> bulkUpdate(client, update, mappingsToRefresh)));
    }
    try {
        executor.shutdown();
        Futures.allAsList(updateFutures).get();
        executor.awaitTermination(10, TimeUnit.SECONDS);
    } catch (InterruptedException | ExecutionException e) {
        throw new IndexException("Couldn't execute bulk updates", e);
    }

    // then bulk indexes/deletes
    if (!indexOperations.isEmpty() || !deleteOperations.isEmpty()) {
        final BulkProcessor processor = client.bulk(new BulkProcessor.Listener() {
            @Override
            public void beforeBulk(long executionId, BulkRequest request) {
                admin.log().debug("Sending bulk request {}", request.numberOfActions());
            }

            @Override
            public void afterBulk(long executionId, BulkRequest request, Throwable failure) {
                admin.log().error("Failed bulk request", failure);
            }

            @Override
            public void afterBulk(long executionId, BulkRequest request, BulkResponse response) {
                admin.log().debug("Successfully processed bulk request ({}) in {}.", request.numberOfActions(),
                        response.getTook());
                if (response.hasFailures()) {
                    for (BulkItemResponse itemResponse : response.getItems()) {
                        checkState(!itemResponse.isFailed(), "Failed to commit bulk request in index '%s', %s",
                                admin.name(), itemResponse.getFailureMessage());
                    }
                }
            }
        }).setConcurrentRequests(getConcurrencyLevel()).setBulkActions(10_000)
                .setBulkSize(new ByteSizeValue(10L, ByteSizeUnit.MB)).build();

        for (Class<?> type : ImmutableSet.copyOf(indexOperations.rowKeySet())) {
            final Map<String, Object> indexOperationsForType = indexOperations.row(type);

            final DocumentMapping mapping = admin.mappings().getMapping(type);
            final String typeString = mapping.typeAsString();
            final String typeIndex = admin.getTypeIndex(mapping);

            mappingsToRefresh.add(mapping);

            for (Entry<String, Object> entry : Iterables.consumingIterable(indexOperationsForType.entrySet())) {
                final String id = entry.getKey();
                if (!deleteOperations.containsValue(id)) {
                    final Object obj = entry.getValue();
                    final Set<String> hashedFields = mapping.getHashedFields();
                    final byte[] _source;

                    if (!hashedFields.isEmpty()) {
                        final ObjectNode objNode = mapper.valueToTree(obj);
                        final ObjectNode hashedNode = mapper.createObjectNode();

                        // Preserve property order, share references with objNode
                        for (String hashedField : hashedFields) {
                            JsonNode value = objNode.get(hashedField);
                            if (value != null && !value.isNull()) {
                                hashedNode.set(hashedField, value);
                            }
                        }

                        final byte[] hashedBytes = mapper.writeValueAsBytes(hashedNode);
                        final HashCode hashCode = Hashing.sha1().hashBytes(hashedBytes);

                        // Inject the result as an extra field into the to-be-indexed JSON content
                        objNode.put(DocumentMapping._HASH, hashCode.toString());
                        _source = mapper.writeValueAsBytes(objNode);

                    } else {
                        _source = mapper.writeValueAsBytes(obj);
                    }

                    processor.add(new IndexRequest(typeIndex, typeString, id).opType(OpType.INDEX)
                            .source(_source, XContentType.JSON));
                }
            }

            for (String id : deleteOperations.removeAll(type)) {
                processor.add(new DeleteRequest(typeIndex, typeString, id));
            }

            // Flush processor between index boundaries
            processor.flush();
        }

        // Remaining delete operations can be executed on their own
        for (Class<?> type : ImmutableSet.copyOf(deleteOperations.keySet())) {
            final DocumentMapping mapping = admin.mappings().getMapping(type);
            final String typeString = mapping.typeAsString();
            final String typeIndex = admin.getTypeIndex(mapping);

            mappingsToRefresh.add(mapping);

            for (String id : deleteOperations.removeAll(type)) {
                processor.add(new DeleteRequest(typeIndex, typeString, id));
            }

            // Flush processor between index boundaries
            processor.flush();
        }

        try {
            processor.awaitClose(5, TimeUnit.MINUTES);
        } catch (InterruptedException e) {
            throw new IndexException("Interrupted bulk processing part of the commit", e);
        }
    }

    // refresh the index if there were only updates
    admin.refresh(mappingsToRefresh);
}

From source file:org.n52.lod.csw.CSWLoDEnabler.java

protected Map<String, GetRecordByIdResponseDocument> retrieveRecordsThreaded(int startPos, int maxRecords,
        long recordsInTotal) {
    log.info("Retrieve {} records, starting from {} of {}", maxRecords, startPos, recordsInTotal);

    // one thread for getting ids
    List<String> recordIdList = getRecordIds(startPos, maxRecords);

    // many threads getting records descriptions
    final Map<String, GetRecordByIdResponseDocument> recordDescriptions = Maps.newConcurrentMap();

    ListeningExecutorService executorService = MoreExecutors
            .listeningDecorator(Executors.newFixedThreadPool(maxRecords));

    for (String id : recordIdList) {
        final String recordId = id;
        log.debug("Adding {} to the model", recordId);

        CallableRecordDescription c = new CallableRecordDescription(id, csw);
        ListenableFuture<GetRecordByIdResponseDocument> responseFuture = executorService.submit(c);

        Futures.addCallback(responseFuture, new FutureCallback<GetRecordByIdResponseDocument>() {

            private final Logger logger = LoggerFactory.getLogger("Record Downloader");

            @Override/*from   ww  w  .  ja va  2s  .c o m*/
            public void onFailure(Throwable t) {
                logger.error("Error retrieving and parsing record {}", t);
                report.retrievalIssues.put(recordId, t);
            }

            @Override
            public void onSuccess(GetRecordByIdResponseDocument result) {
                logger.trace("SUCCESS with {}", result);
                recordDescriptions.put(recordId, result);

                report.added++;
                report.addedIds.add(recordId);
            }

        });
    }

    executorService.shutdown();
    while (!executorService.isTerminated()) {
        try {
            executorService.awaitTermination(Long.MAX_VALUE, TimeUnit.NANOSECONDS);
        } catch (InterruptedException e) {
            log.error("Could not await termination", e);
        }
    }

    log.info("Done with requests and parsing, have {} GetRecordById documents.", recordDescriptions.size());
    return recordDescriptions;
}

From source file:com.facebook.buck.cli.AdbHelper.java

/**
 * Execute an {@link AdbCallable} for all matching devices. This functions performs device
 * filtering based on three possible arguments:
 *
 *  -e (emulator-only) - only emulators are passing the filter
 *  -d (device-only) - only real devices are passing the filter
 *  -s (serial) - only device/emulator with specific serial number are passing the filter
 *
 *  If more than one device matches the filter this function will fail unless multi-install
 *  mode is enabled (-x). This flag is used as a marker that user understands that multiple
 *  devices will be used to install the apk if needed.
 *///from   w  w w. ja va 2s .  c o m
public boolean adbCall(AdbCallable adbCallable) {
    List<IDevice> devices;

    try (TraceEventLogger ignored = TraceEventLogger.start(buckEventBus, "set_up_adb_call")) {

        // Initialize adb connection.
        AndroidDebugBridge adb = createAdb(context);
        if (adb == null) {
            console.printBuildFailure("Failed to create adb connection.");
            return false;
        }

        // Build list of matching devices.
        devices = filterDevices(adb.getDevices());
        if (devices == null) {
            if (buckConfig.getRestartAdbOnFailure()) {
                console.printErrorText("No devices found with adb, restarting adb-server.");
                adb.restart();
                devices = filterDevices(adb.getDevices());
            }

            if (devices == null) {
                return false;
            }
        }
    }

    int adbThreadCount = options.getAdbThreadCount();
    if (adbThreadCount <= 0) {
        adbThreadCount = devices.size();
    }

    // Start executions on all matching devices.
    List<ListenableFuture<Boolean>> futures = Lists.newArrayList();
    ListeningExecutorService executorService = listeningDecorator(
            newMultiThreadExecutor(getClass().getSimpleName(), adbThreadCount));
    for (final IDevice device : devices) {
        futures.add(executorService.submit(adbCallable.forDevice(device)));
    }

    // Wait for all executions to complete or fail.
    List<Boolean> results = null;
    try {
        results = Futures.allAsList(futures).get();
    } catch (ExecutionException ex) {
        console.printBuildFailure("Failed: " + adbCallable);
        ex.printStackTrace(console.getStdErr());
        return false;
    } catch (InterruptedException ex) {
        console.printBuildFailure("Interrupted.");
        ex.printStackTrace(console.getStdErr());
        return false;
    } finally {
        executorService.shutdownNow();
    }

    int successCount = 0;
    for (Boolean result : results) {
        if (result) {
            successCount++;
        }
    }
    int failureCount = results.size() - successCount;

    // Report results.
    if (successCount > 0) {
        console.printSuccess(String.format("Successfully ran %s on %d device(s)", adbCallable, successCount));
    }
    if (failureCount > 0) {
        console.printBuildFailure(String.format("Failed to %s on %d device(s).", adbCallable, failureCount));
    }

    return failureCount == 0;
}

From source file:com.google.gerrit.pgm.RebuildNoteDb.java

@Override
public int run() throws Exception {
    mustHaveValidSite();//w  ww .ja v a 2s  .  c  om
    dbInjector = createDbInjector(MULTI_USER);
    threads = ThreadLimiter.limitThreads(dbInjector, threads);

    LifecycleManager dbManager = new LifecycleManager();
    dbManager.add(dbInjector);
    dbManager.start();

    sysInjector = createSysInjector();
    sysInjector.injectMembers(this);
    if (!notesMigration.enabled()) {
        throw die("NoteDb is not enabled.");
    }
    LifecycleManager sysManager = new LifecycleManager();
    sysManager.add(sysInjector);
    sysManager.start();

    ListeningExecutorService executor = newExecutor();
    System.out.println("Rebuilding the NoteDb");

    ImmutableListMultimap<Project.NameKey, Change.Id> changesByProject = getChangesByProject();
    boolean ok;
    Stopwatch sw = Stopwatch.createStarted();
    try (Repository allUsersRepo = repoManager.openRepository(allUsersName)) {
        deleteRefs(RefNames.REFS_DRAFT_COMMENTS, allUsersRepo);

        List<ListenableFuture<Boolean>> futures = new ArrayList<>();
        List<Project.NameKey> projectNames = Ordering.usingToString().sortedCopy(changesByProject.keySet());
        for (Project.NameKey project : projectNames) {
            ListenableFuture<Boolean> future = executor.submit(() -> {
                try (ReviewDb db = unwrapDb(schemaFactory.open())) {
                    return rebuildProject(db, changesByProject, project, allUsersRepo);
                } catch (Exception e) {
                    log.error("Error rebuilding project " + project, e);
                    return false;
                }
            });
            futures.add(future);
        }

        try {
            ok = Iterables.all(Futures.allAsList(futures).get(), Predicates.equalTo(true));
        } catch (InterruptedException | ExecutionException e) {
            log.error("Error rebuilding projects", e);
            ok = false;
        }
    }

    double t = sw.elapsed(TimeUnit.MILLISECONDS) / 1000d;
    System.out.format("Rebuild %d changes in %.01fs (%.01f/s)\n", changesByProject.size(), t,
            changesByProject.size() / t);
    return ok ? 0 : 1;
}