List of usage examples for java.util.concurrent Future isCancelled
boolean isCancelled();
From source file:voldemort.store.routed.ThreadPoolRoutedStore.java
private <R> List<R> get(final ByteArray key, final byte[] transforms, StoreOp<R> fetcher, Function<List<GetResult<R>>, Void> preReturnProcedure) throws VoldemortException { StoreUtils.assertValidKey(key);/*from ww w .ja v a2 s .com*/ final List<Node> nodes = availableNodes(routingStrategy.routeRequest(key.get())); // quickly fail if there aren't enough nodes to meet the requirement checkRequiredReads(nodes); final List<GetResult<R>> retrieved = Lists.newArrayList(); // A count of the number of successful operations int successes = 0; // A list of thrown exceptions, indicating the number of failures final List<Throwable> failures = Lists.newArrayListWithCapacity(3); // Do the preferred number of reads in parallel int attempts = Math.min(this.storeDef.getPreferredReads(), nodes.size()); int nodeIndex = 0; List<Callable<GetResult<R>>> callables = Lists.newArrayListWithCapacity(attempts); for (; nodeIndex < attempts; nodeIndex++) { final Node node = nodes.get(nodeIndex); callables.add(new GetCallable<R>(node, key, transforms, fetcher)); } List<Future<GetResult<R>>> futures; long timeoutMs = (fetcher == VERSION_OP) ? timeoutConfig.getOperationTimeout(VoldemortOpCode.GET_VERSION_OP_CODE) : timeoutConfig.getOperationTimeout(VoldemortOpCode.GET_OP_CODE); try { futures = executor.invokeAll(callables, timeoutMs, TimeUnit.MILLISECONDS); } catch (InterruptedException e) { throw new InsufficientOperationalNodesException("Get operation interrupted!", e); } for (Future<GetResult<R>> f : futures) { if (f.isCancelled()) { logger.warn("Get operation timed out after " + timeoutMs + " ms."); continue; } try { GetResult<R> getResult = f.get(); if (getResult.exception != null) { if (getResult.exception instanceof VoldemortApplicationException) { throw (VoldemortException) getResult.exception; } failures.add(getResult.exception); continue; } ++successes; retrieved.add(getResult); } catch (InterruptedException e) { throw new InsufficientOperationalNodesException("Get operation interrupted!", e); } catch (ExecutionException e) { // We catch all Throwable subclasses apart from Error in the // callable, so the else // part should never happen. if (e.getCause() instanceof Error) throw (Error) e.getCause(); else logger.error(e.getMessage(), e); } } // Now if we had any failures we will be short a few reads. Do serial // reads to make up for these. while (successes < this.storeDef.getPreferredReads() && nodeIndex < nodes.size()) { Node node = nodes.get(nodeIndex); long startNs = System.nanoTime(); try { retrieved.add(new GetResult<R>(node, key, fetcher.execute(innerStores.get(node.getId()), key, transforms), null)); ++successes; recordSuccess(node, startNs); } catch (UnreachableStoreException e) { failures.add(e); recordException(node, startNs, e); } catch (VoldemortApplicationException e) { throw e; } catch (Exception e) { logger.warn("Error in GET on node " + node.getId() + "(" + node.getHost() + ")", e); failures.add(e); } nodeIndex++; } if (logger.isTraceEnabled()) logger.trace("GET retrieved the following node values: " + formatNodeValues(retrieved)); if (preReturnProcedure != null) preReturnProcedure.apply(retrieved); if (successes >= this.storeDef.getRequiredReads()) { List<R> result = Lists.newArrayListWithExpectedSize(retrieved.size()); for (GetResult<R> getResult : retrieved) result.addAll(getResult.retrieved); return result; } else throw new InsufficientOperationalNodesException( this.storeDef.getRequiredReads() + " reads required, but " + successes + " succeeded.", failures); }
From source file:ubic.gemma.core.analysis.preprocess.batcheffects.ComBat.java
private void runNonParametric(final DoubleMatrix2D sdata, DoubleMatrix2D gammastar, DoubleMatrix2D deltastar) { final ConcurrentHashMap<String, DoubleMatrix1D[]> results = new ConcurrentHashMap<>(); int numThreads = Math.min(batches.size(), Runtime.getRuntime().availableProcessors()); ComBat.log.info("Runing nonparametric estimation on " + numThreads + " threads"); Future<?>[] futures = new Future[numThreads]; ExecutorService service = Executors.newCachedThreadPool(); /*/*from w w w . j av a 2 s . c om*/ * Divvy up batches over threads. */ int batchesPerThread = batches.size() / numThreads; final String[] batchIds = batches.keySet().toArray(new String[] {}); for (int i = 0; i < numThreads; i++) { final int firstBatch = i * batchesPerThread; final int lastBatch = i == (numThreads - 1) ? batches.size() : firstBatch + batchesPerThread; futures[i] = service.submit(new Runnable() { @Override public void run() { for (int k = firstBatch; k < lastBatch; k++) { String batchId = batchIds[k]; DoubleMatrix2D batchData = ComBat.this.getBatchData(sdata, batchId); DoubleMatrix1D[] batchResults = ComBat.this.nonParametricFit(batchData, gammaHat.viewRow(k), deltaHat.viewRow(k)); results.put(batchId, batchResults); } } }); } service.shutdown(); boolean allDone = false; do { for (Future<?> f : futures) { allDone = true; if (!f.isDone() && !f.isCancelled()) { allDone = false; break; } } } while (!allDone); for (int i = 0; i < batchIds.length; i++) { String batchId = batchIds[i]; DoubleMatrix1D[] batchResults = results.get(batchId); for (int j = 0; j < batchResults[0].size(); j++) { gammastar.set(i, j, batchResults[0].get(j)); } for (int j = 0; j < batchResults[1].size(); j++) { deltastar.set(i, j, batchResults[1].get(j)); } } }
From source file:org.apache.lens.driver.es.ESDriver.java
@Override public void updateStatus(QueryContext context) { final QueryHandle queryHandle = context.getQueryHandle(); final Future<LensResultSet> lensResultSetFuture = resultSetMap.get(queryHandle); if (lensResultSetFuture == null) { context.getDriverStatus().setState(DriverQueryStatus.DriverQueryState.CLOSED); context.getDriverStatus().setStatusMessage(queryHandle + " closed"); context.getDriverStatus().setResultSetAvailable(false); } else if (lensResultSetFuture.isDone()) { context.getDriverStatus().setState(DriverQueryStatus.DriverQueryState.SUCCESSFUL); context.getDriverStatus().setStatusMessage(queryHandle + " successful"); context.getDriverStatus().setResultSetAvailable(true); } else if (lensResultSetFuture.isCancelled()) { context.getDriverStatus().setState(DriverQueryStatus.DriverQueryState.CANCELED); context.getDriverStatus().setStatusMessage(queryHandle + " cancelled"); context.getDriverStatus().setResultSetAvailable(false); }/*from ww w . j a va 2 s . co m*/ }
From source file:org.cleverbus.component.externalcall.ExternalCallComponentTest.java
private Future<String> getStringBodyFuture(final Future<Exchange> reply) { return new Future<String>() { @Override/* w w w .j a va 2 s .c o m*/ public boolean cancel(boolean mayInterruptIfRunning) { return reply.cancel(mayInterruptIfRunning); } @Override public boolean isCancelled() { return reply.isCancelled(); } @Override public boolean isDone() { return reply.isDone(); } @Override public String get() throws InterruptedException, ExecutionException { return getReplyString(reply.get()); } @Override public String get(long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException { return getReplyString(reply.get(timeout, unit)); } private String getReplyString(Exchange exchange) throws InterruptedException, ExecutionException { throwExceptionOptionally(exchange.getProperty(Exchange.EXCEPTION_CAUGHT, Exception.class)); throwExceptionOptionally(exchange.getException()); return exchange.getOut().getBody(String.class); } private void throwExceptionOptionally(Exception exc) throws InterruptedException, ExecutionException { if (exc != null) { if (exc instanceof InterruptedException) { throw (InterruptedException) exc; } else if (exc instanceof ExecutionException) { throw (ExecutionException) exc; } else { throw new ExecutionException(exc); } } } }; }
From source file:voldemort.store.routed.RoutedStore.java
private <R> List<R> get(final ByteArray key, StoreOp<R> fetcher, Function<List<GetResult<R>>, Void> preReturnProcedure) throws VoldemortException { StoreUtils.assertValidKey(key);/*from w w w .j a v a 2s . co m*/ final List<Node> nodes = availableNodes(routingStrategy.routeRequest(key.get())); // quickly fail if there aren't enough nodes to meet the requirement checkRequiredReads(nodes); final List<GetResult<R>> retrieved = Lists.newArrayList(); // A count of the number of successful operations int successes = 0; // A list of thrown exceptions, indicating the number of failures final List<Throwable> failures = Lists.newArrayListWithCapacity(3); // Do the preferred number of reads in parallel int attempts = Math.min(this.storeDef.getPreferredReads(), nodes.size()); int nodeIndex = 0; List<Callable<GetResult<R>>> callables = Lists.newArrayListWithCapacity(attempts); for (; nodeIndex < attempts; nodeIndex++) { final Node node = nodes.get(nodeIndex); callables.add(new GetCallable<R>(node, key, fetcher)); } List<Future<GetResult<R>>> futures; try { futures = executor.invokeAll(callables, timeoutMs, TimeUnit.MILLISECONDS); } catch (InterruptedException e) { throw new InsufficientOperationalNodesException("Get operation interrupted!", e); } for (Future<GetResult<R>> f : futures) { if (f.isCancelled()) { logger.warn("Get operation timed out after " + timeoutMs + " ms."); continue; } try { GetResult<R> getResult = f.get(); if (getResult.exception != null) { if (getResult.exception instanceof VoldemortApplicationException) { throw (VoldemortException) getResult.exception; } failures.add(getResult.exception); continue; } ++successes; retrieved.add(getResult); } catch (InterruptedException e) { throw new InsufficientOperationalNodesException("Get operation interrupted!", e); } catch (ExecutionException e) { // We catch all Throwable subclasses apart from Error in the // callable, so the else // part should never happen. if (e.getCause() instanceof Error) throw (Error) e.getCause(); else logger.error(e.getMessage(), e); } } // Now if we had any failures we will be short a few reads. Do serial // reads to make up for these. while (successes < this.storeDef.getPreferredReads() && nodeIndex < nodes.size()) { Node node = nodes.get(nodeIndex); long startNs = System.nanoTime(); try { retrieved.add( new GetResult<R>(node, key, fetcher.execute(innerStores.get(node.getId()), key), null)); ++successes; recordSuccess(node, startNs); } catch (UnreachableStoreException e) { failures.add(e); recordException(node, startNs, e); } catch (VoldemortApplicationException e) { throw e; } catch (Exception e) { logger.warn("Error in GET on node " + node.getId() + "(" + node.getHost() + ")", e); failures.add(e); } nodeIndex++; } if (logger.isTraceEnabled()) logger.trace("GET retrieved the following node values: " + formatNodeValues(retrieved)); if (preReturnProcedure != null) preReturnProcedure.apply(retrieved); if (successes >= this.storeDef.getRequiredReads()) { List<R> result = Lists.newArrayListWithExpectedSize(retrieved.size()); for (GetResult<R> getResult : retrieved) result.addAll(getResult.retrieved); if (logger.isTraceEnabled()) logger.trace("return " + result.size() + "items from routedstore"); return result; } else throw new InsufficientOperationalNodesException( this.storeDef.getRequiredReads() + " reads required, but " + successes + " succeeded.", failures); }
From source file:voldemort.store.routed.ThreadPoolRoutedStore.java
@Override public Map<ByteArray, List<Versioned<byte[]>>> getAll(Iterable<ByteArray> keys, Map<ByteArray, byte[]> transforms) throws VoldemortException { StoreUtils.assertValidKeys(keys);//from w ww.j a v a 2s .com Map<ByteArray, List<Versioned<byte[]>>> result = StoreUtils.newEmptyHashMap(keys); // Keys for each node needed to satisfy storeDef.getPreferredReads() if // no failures. Map<Node, List<ByteArray>> nodeToKeysMap = Maps.newHashMap(); // Keep track of nodes per key that might be needed if there are // failures during getAll Map<ByteArray, List<Node>> keyToExtraNodesMap = Maps.newHashMap(); for (ByteArray key : keys) { List<Node> availableNodes = availableNodes(routingStrategy.routeRequest(key.get())); // quickly fail if there aren't enough nodes to meet the requirement checkRequiredReads(availableNodes); int preferredReads = storeDef.getPreferredReads(); List<Node> preferredNodes = Lists.newArrayListWithCapacity(preferredReads); List<Node> extraNodes = Lists.newArrayListWithCapacity(3); for (Node node : availableNodes) { if (preferredNodes.size() < preferredReads) preferredNodes.add(node); else extraNodes.add(node); } for (Node node : preferredNodes) { List<ByteArray> nodeKeys = nodeToKeysMap.get(node); if (nodeKeys == null) { nodeKeys = Lists.newArrayList(); nodeToKeysMap.put(node, nodeKeys); } nodeKeys.add(key); } if (!extraNodes.isEmpty()) { List<Node> nodes = keyToExtraNodesMap.get(key); if (nodes == null) keyToExtraNodesMap.put(key, extraNodes); else nodes.addAll(extraNodes); } } List<Callable<GetAllResult>> callables = Lists.newArrayList(); for (Map.Entry<Node, List<ByteArray>> entry : nodeToKeysMap.entrySet()) { final Node node = entry.getKey(); final Collection<ByteArray> nodeKeys = entry.getValue(); if (failureDetector.isAvailable(node)) callables.add(new GetAllCallable(node, nodeKeys, transforms)); } // A list of thrown exceptions, indicating the number of failures List<Throwable> failures = Lists.newArrayList(); List<NodeValue<ByteArray, byte[]>> nodeValues = Lists.newArrayList(); Map<ByteArray, MutableInt> keyToSuccessCount = Maps.newHashMap(); for (ByteArray key : keys) keyToSuccessCount.put(key, new MutableInt(0)); List<Future<GetAllResult>> futures; long timeoutMs = timeoutConfig.getOperationTimeout(VoldemortOpCode.GET_ALL_OP_CODE); try { // TODO What to do about timeouts? They should be longer as getAll // is likely to // take longer. At the moment, it's just timeoutMs * 3, but should // this be based on the number of the keys? futures = executor.invokeAll(callables, timeoutMs * 3, TimeUnit.MILLISECONDS); } catch (InterruptedException e) { throw new InsufficientOperationalNodesException("getAll operation interrupted.", e); } for (Future<GetAllResult> f : futures) { if (f.isCancelled()) { logger.warn("Get operation timed out after " + timeoutMs + " ms."); continue; } try { GetAllResult getResult = f.get(); if (getResult.exception != null) { if (getResult.exception instanceof VoldemortApplicationException) { throw (VoldemortException) getResult.exception; } failures.add(getResult.exception); continue; } for (ByteArray key : getResult.callable.nodeKeys) { List<Versioned<byte[]>> retrieved = getResult.retrieved.get(key); MutableInt successCount = keyToSuccessCount.get(key); successCount.increment(); /* * retrieved can be null if there are no values for the key * provided */ if (retrieved != null) { List<Versioned<byte[]>> existing = result.get(key); if (existing == null) result.put(key, Lists.newArrayList(retrieved)); else existing.addAll(retrieved); } } nodeValues.addAll(getResult.nodeValues); } catch (InterruptedException e) { throw new InsufficientOperationalNodesException("getAll operation interrupted.", e); } catch (ExecutionException e) { // We catch all Throwables apart from Error in the callable, so // the else part // should never happen if (e.getCause() instanceof Error) throw (Error) e.getCause(); else logger.error(e.getMessage(), e); } } for (ByteArray key : keys) { MutableInt successCountWrapper = keyToSuccessCount.get(key); int successCount = successCountWrapper.intValue(); if (successCount < storeDef.getPreferredReads()) { List<Node> extraNodes = keyToExtraNodesMap.get(key); if (extraNodes != null) { for (Node node : extraNodes) { long startNs = System.nanoTime(); try { List<Versioned<byte[]>> values = innerStores.get(node.getId()).get(key, transforms == null ? null : transforms.get(key)); fillRepairReadsValues(nodeValues, key, node, values); List<Versioned<byte[]>> versioneds = result.get(key); if (versioneds == null) result.put(key, Lists.newArrayList(values)); else versioneds.addAll(values); recordSuccess(node, startNs); if (++successCount >= storeDef.getPreferredReads()) break; } catch (UnreachableStoreException e) { failures.add(e); recordException(node, startNs, e); } catch (VoldemortApplicationException e) { throw e; } catch (Exception e) { logger.warn("Error in GET_ALL on node " + node.getId() + "(" + node.getHost() + ")", e); failures.add(e); } } } } successCountWrapper.setValue(successCount); } repairReads(nodeValues, repairReads && (transforms == null || transforms.size() == 0)); for (Map.Entry<ByteArray, MutableInt> mapEntry : keyToSuccessCount.entrySet()) { int successCount = mapEntry.getValue().intValue(); if (successCount < storeDef.getRequiredReads()) throw new InsufficientOperationalNodesException( this.storeDef.getRequiredReads() + " reads required, but " + successCount + " succeeded.", failures); } return result; }
From source file:voldemort.store.routed.RoutedStore.java
public Map<ByteArray, List<Versioned<byte[]>>> getAll(Iterable<ByteArray> keys) throws VoldemortException { StoreUtils.assertValidKeys(keys);//from w w w. ja va2 s. co m Map<ByteArray, List<Versioned<byte[]>>> result = StoreUtils.newEmptyHashMap(keys); // Keys for each node needed to satisfy storeDef.getPreferredReads() if // no failures. Map<Node, List<ByteArray>> nodeToKeysMap = Maps.newHashMap(); // Keep track of nodes per key that might be needed if there are // failures during getAll Map<ByteArray, List<Node>> keyToExtraNodesMap = Maps.newHashMap(); for (ByteArray key : keys) { List<Node> availableNodes = availableNodes(routingStrategy.routeRequest(key.get())); // quickly fail if there aren't enough nodes to meet the requirement checkRequiredReads(availableNodes); int preferredReads = storeDef.getPreferredReads(); List<Node> preferredNodes = Lists.newArrayListWithCapacity(preferredReads); List<Node> extraNodes = Lists.newArrayListWithCapacity(3); for (Node node : availableNodes) { if (preferredNodes.size() < preferredReads) preferredNodes.add(node); else extraNodes.add(node); } for (Node node : preferredNodes) { List<ByteArray> nodeKeys = nodeToKeysMap.get(node); if (nodeKeys == null) { nodeKeys = Lists.newArrayList(); nodeToKeysMap.put(node, nodeKeys); } nodeKeys.add(key); } if (!extraNodes.isEmpty()) { List<Node> nodes = keyToExtraNodesMap.get(key); if (nodes == null) keyToExtraNodesMap.put(key, extraNodes); else nodes.addAll(extraNodes); } } List<Callable<GetAllResult>> callables = Lists.newArrayList(); for (Map.Entry<Node, List<ByteArray>> entry : nodeToKeysMap.entrySet()) { final Node node = entry.getKey(); final Collection<ByteArray> nodeKeys = entry.getValue(); if (failureDetector.isAvailable(node)) callables.add(new GetAllCallable(node, nodeKeys)); } // A list of thrown exceptions, indicating the number of failures List<Throwable> failures = Lists.newArrayList(); List<NodeValue<ByteArray, byte[]>> nodeValues = Lists.newArrayList(); Map<ByteArray, MutableInt> keyToSuccessCount = Maps.newHashMap(); for (ByteArray key : keys) keyToSuccessCount.put(key, new MutableInt(0)); List<Future<GetAllResult>> futures; try { // TODO What to do about timeouts? They should be longer as getAll // is likely to // take longer. At the moment, it's just timeoutMs * 3, but should // this be based on the number of the keys? futures = executor.invokeAll(callables, timeoutMs * 3, TimeUnit.MILLISECONDS); } catch (InterruptedException e) { throw new InsufficientOperationalNodesException("getAll operation interrupted.", e); } for (Future<GetAllResult> f : futures) { if (f.isCancelled()) { logger.warn("Get operation timed out after " + timeoutMs + " ms."); continue; } try { GetAllResult getResult = f.get(); if (getResult.exception != null) { if (getResult.exception instanceof VoldemortApplicationException) { throw (VoldemortException) getResult.exception; } failures.add(getResult.exception); continue; } for (ByteArray key : getResult.callable.nodeKeys) { List<Versioned<byte[]>> retrieved = getResult.retrieved.get(key); MutableInt successCount = keyToSuccessCount.get(key); successCount.increment(); /* * retrieved can be null if there are no values for the key * provided */ if (retrieved != null) { List<Versioned<byte[]>> existing = result.get(key); if (existing == null) result.put(key, Lists.newArrayList(retrieved)); else existing.addAll(retrieved); } } nodeValues.addAll(getResult.nodeValues); } catch (InterruptedException e) { throw new InsufficientOperationalNodesException("getAll operation interrupted.", e); } catch (ExecutionException e) { // We catch all Throwables apart from Error in the callable, so // the else part // should never happen if (e.getCause() instanceof Error) throw (Error) e.getCause(); else logger.error(e.getMessage(), e); } } for (ByteArray key : keys) { MutableInt successCountWrapper = keyToSuccessCount.get(key); int successCount = successCountWrapper.intValue(); if (successCount < storeDef.getPreferredReads()) { List<Node> extraNodes = keyToExtraNodesMap.get(key); if (extraNodes != null) { for (Node node : extraNodes) { long startNs = System.nanoTime(); try { List<Versioned<byte[]>> values = innerStores.get(node.getId()).get(key); fillRepairReadsValues(nodeValues, key, node, values); List<Versioned<byte[]>> versioneds = result.get(key); if (versioneds == null) result.put(key, Lists.newArrayList(values)); else versioneds.addAll(values); recordSuccess(node, startNs); if (++successCount >= storeDef.getPreferredReads()) break; } catch (UnreachableStoreException e) { failures.add(e); recordException(node, startNs, e); } catch (VoldemortApplicationException e) { throw e; } catch (Exception e) { logger.warn("Error in GET_ALL on node " + node.getId() + "(" + node.getHost() + ")", e); failures.add(e); } } } } successCountWrapper.setValue(successCount); } repairReads(nodeValues); for (Map.Entry<ByteArray, MutableInt> mapEntry : keyToSuccessCount.entrySet()) { int successCount = mapEntry.getValue().intValue(); if (successCount < storeDef.getRequiredReads()) throw new InsufficientOperationalNodesException( this.storeDef.getRequiredReads() + " reads required, but " + successCount + " succeeded.", failures); } return result; }
From source file:ubic.gemma.analysis.preprocess.batcheffects.ComBat.java
/** * Multithreaded//from w w w . j a v a2 s .c o m * * @param sdata * @param gammastar * @param deltastar */ private void runNonParametric(final DoubleMatrix2D sdata, DoubleMatrix2D gammastar, DoubleMatrix2D deltastar) { final ConcurrentHashMap<String, DoubleMatrix1D[]> results = new ConcurrentHashMap<String, DoubleMatrix1D[]>(); int numThreads = Math.min(batches.size(), Runtime.getRuntime().availableProcessors()); log.info("Runing nonparametric estimation on " + numThreads + " threads"); Future<?>[] futures = new Future[numThreads]; ExecutorService service = Executors.newCachedThreadPool(); /* * Divvy up batches over threads. */ int batchesPerThread = batches.size() / numThreads; final String[] batchIds = batches.keySet().toArray(new String[] {}); for (int i = 0; i < numThreads; i++) { final int firstBatch = i * batchesPerThread; final int lastBatch = i == (numThreads - 1) ? batches.size() : firstBatch + batchesPerThread; futures[i] = service.submit(new Runnable() { @Override public void run() { for (int k = firstBatch; k < lastBatch; k++) { String batchId = batchIds[k]; DoubleMatrix2D batchData = getBatchData(sdata, batchId); DoubleMatrix1D[] batchResults = nonParametricFit(batchData, gammaHat.viewRow(k), deltaHat.viewRow(k)); results.put(batchId, batchResults); } } }); } service.shutdown(); boolean allDone = false; do { for (Future<?> f : futures) { allDone = true; if (!f.isDone() && !f.isCancelled()) { allDone = false; break; } } } while (!allDone); for (int i = 0; i < batchIds.length; i++) { String batchId = batchIds[i]; DoubleMatrix1D[] batchResults = results.get(batchId); for (int j = 0; j < batchResults[0].size(); j++) { gammastar.set(i, j, batchResults[0].get(j)); } for (int j = 0; j < batchResults[1].size(); j++) { deltastar.set(i, j, batchResults[1].get(j)); } } }
From source file:org.apache.hive.hcatalog.templeton.JobRequestExecutor.java
private void cancelExecutePoolThread(Future<T> future) { int retryCount = 0; while (retryCount < this.maxTaskCancelRetryCount && !future.isDone()) { LOG.info("Task is still executing the job request. Cancelling it with retry count: " + retryCount); if (future.cancel(true)) { /*/* w ww. java2 s . c o m*/ * Cancelled the job request and return to client. */ LOG.info("Cancel job request issued successfully."); return; } retryCount++; try { Thread.sleep(this.maxTaskCancelRetryWaitTimeInMs); } catch (InterruptedException e) { /* * Nothing to do. Just retry. */ } } LOG.warn("Failed to cancel the job. isCancelled: " + future.isCancelled() + " Retry count: " + retryCount); }
From source file:org.apache.http.impl.nio.conn.TestPoolingHttpClientAsyncConnectionManager.java
@Test public void testRequestConnectionCancelled() throws Exception { final HttpHost target = new HttpHost("localhost"); final HttpRoute route = new HttpRoute(target); final Future<NHttpClientConnection> future = connman.requestConnection(route, "some state", 1000L, 2000L, TimeUnit.MILLISECONDS, null); Assert.assertNotNull(future);/*from w w w .j ava 2s . c o m*/ Mockito.verify(pool).lease(Matchers.same(route), Matchers.eq("some state"), Matchers.eq(1000L), Matchers.eq(2000L), Matchers.eq(TimeUnit.MILLISECONDS), poolEntryCallbackCaptor.capture()); final FutureCallback<CPoolEntry> callaback = poolEntryCallbackCaptor.getValue(); callaback.cancelled(); Assert.assertTrue(future.isDone()); Assert.assertTrue(future.isCancelled()); Assert.assertNull(future.get()); }