List of usage examples for java.util.concurrent Future isDone
boolean isDone();
From source file:org.apache.olingo.fit.proxy.AsyncTestITCase.java
@Test public void retrieveEntitySet() throws InterruptedException, ExecutionException { final Future<CustomerCollection> futureCustomers = container.getCustomers().executeAsync(); assertNotNull(futureCustomers);/*from www .j a v a2 s. c om*/ while (!futureCustomers.isDone()) { Thread.sleep(1000L); } final CustomerCollection customers = futureCustomers.get(); assertNotNull(customers); assertFalse(customers.isEmpty()); for (Customer customer : customers) { assertNotNull(customer); } }
From source file:org.apache.olingo.fit.v3.AsyncTestITCase.java
@Test public void updateEntity() throws InterruptedException, ExecutionException { final URI uri = client.newURIBuilder(testStaticServiceRootURL).appendEntitySetSegment("Product") .appendKeySegment(-10).build(); final ODataRetrieveResponse<ODataEntity> entityRes = client.getRetrieveRequestFactory() .getEntityRequest(uri).execute(); final ODataEntity entity = entityRes.getBody(); entity.getAssociationLinks().clear(); entity.getNavigationLinks().clear(); entity.getMediaEditLinks().clear();/*from w w w. j a va2s . c o m*/ entity.getProperties().remove(entity.getProperty("Description")); getClient().getBinder().add(entity, client.getObjectFactory().newPrimitiveProperty("Description", client.getObjectFactory().newPrimitiveValueBuilder().setValue("AsyncTest#updateEntity").build())); final ODataEntityUpdateRequest<ODataEntity> updateReq = client.getCUDRequestFactory() .getEntityUpdateRequest(uri, UpdateType.MERGE, entity); updateReq.setIfMatch(entityRes.getETag()); final Future<ODataEntityUpdateResponse<ODataEntity>> futureRes = updateReq.asyncExecute(); while (!futureRes.isDone()) { Thread.sleep(1000L); } final ODataEntityUpdateResponse<ODataEntity> res = futureRes.get(); assertNotNull(res); assertEquals(204, res.getStatusCode()); }
From source file:com.magnet.plugin.common.helpers.URLHelper.java
public static InputStream loadUrl(final String url) throws Exception { final InputStream[] inputStreams = new InputStream[] { null }; final Exception[] exception = new Exception[] { null }; Future<?> downloadThreadFuture = ApplicationManager.getApplication().executeOnPooledThread(new Runnable() { public void run() { try { HttpURLConnection connection; if (ApplicationManager.getApplication() != null) { connection = HttpConfigurable.getInstance().openHttpConnection(url); } else { connection = (HttpURLConnection) new URL(url).openConnection(); connection.setReadTimeout(CONNECTION_TIMEOUT); connection.setConnectTimeout(CONNECTION_TIMEOUT); }// w w w.j a v a 2s . c o m connection.connect(); inputStreams[0] = connection.getInputStream(); } catch (IOException e) { exception[0] = e; } } }); try { downloadThreadFuture.get(5, TimeUnit.SECONDS); } catch (TimeoutException ignored) { } if (!downloadThreadFuture.isDone()) { downloadThreadFuture.cancel(true); throw new Exception(IdeBundle.message("updates.timeout.error")); } if (exception[0] != null) throw exception[0]; return inputStreams[0]; }
From source file:com.vmware.bdd.service.impl.ClusterSyncService.java
public void syncUp(String clusterName, boolean updateClusterStatus) { if (logger.isDebugEnabled()) { logger.debug("start to sync cluster: " + clusterName); }//from www . ja va2s.co m List<NodeEntity> nodes = clusterEntityMgr.findAllNodes(clusterName); boolean allNodesDown = true; List<Future<NodeRead>> refreshedNodeList = new ArrayList<>(); for (NodeEntity node : nodes) { refreshedNodeList.add(nodeSyncService.asyncRefreshNodeStatus(node.getVmName())); } //wait all node refresh is done long elapsed = 0l; while (CollectionUtils.isNotEmpty(refreshedNodeList)) { for (Iterator<Future<NodeRead>> futureItr = refreshedNodeList.iterator(); futureItr.hasNext();) { Future<NodeRead> refreshedNodeFuture = futureItr.next(); if (refreshedNodeFuture.isDone()) { try { NodeRead refreshedNode = refreshedNodeFuture.get(); if (logger.isDebugEnabled()) { logger.debug("got sync node result: " + refreshedNode.getName()); } if (NodeStatus.fromString(refreshedNode.getStatus()).ordinal() >= NodeStatus.POWERED_ON .ordinal()) { allNodesDown = false; } } catch (InterruptedException e) { logger.error("failed to get async refresh node result", e); } catch (ExecutionException e) { logger.error("failed to get async refresh node result", e); } finally { futureItr.remove(); } } } try { Thread.sleep(TIME_SLICE); elapsed += TIME_SLICE; if (elapsed >= MAX_WAIT) { break; } } catch (InterruptedException e) { //nothing to do } if (logger.isDebugEnabled()) { logger.debug("sync cluster: " + clusterName); } } if (CollectionUtils.isNotEmpty(refreshedNodeList)) { logger.warn("failed to sync all nodes status in given time interval: " + clusterName); } else { logger.info(String.format("sync all node status of cluster: %1s in %2s milliseconds", clusterName, elapsed)); } if (updateClusterStatus && allNodesDown) { ClusterEntity cluster = clusterEntityMgr.findByName(clusterName); if (cluster.getStatus() == ClusterStatus.RUNNING) { logger.info("All nodes are powered off, switch cluster status to stopped."); cluster.setStatus(ClusterStatus.STOPPED); } } }
From source file:com.ras.updater.Downloader.java
/** * This method will check for updates on all {@link #m_fileProviders} and download anything with an update. * @return true if at least one file was updated or false if no files were updated *//* ww w . j a v a2 s.c o m*/ public boolean update() { ArrayList<Future<Boolean>> results = new ArrayList<Future<Boolean>>(); ExecutorService es = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors()); for (IFileProvider fileProvider : m_fileProviders) { FileUpdaterCallable task = new FileUpdaterCallable(fileProvider); results.add(es.submit(task)); } es.shutdown(); try { if (!es.awaitTermination(m_downloadTimeout, m_downloadTimeUnit)) es.shutdownNow(); } catch (InterruptedException e) { m_statusCallback.handleError(e); es.shutdownNow(); Thread.currentThread().interrupt(); } //Loop through the results for update values for (Future<Boolean> result : results) { try { if (result.isDone() && result.get() != null && result.get()) return true; } catch (InterruptedException e) { //This should never happen m_statusCallback.handleError(e); } catch (ExecutionException e) { m_statusCallback.handleError(e); } } return false; }
From source file:com.msopentech.odatajclient.engine.it.AsyncTestITCase.java
/** * @see MediaEntityTest#createMediaEntity(com.msopentech.odatajclient.engine.format.ODataPubFormat) *///from w ww . jav a2 s.c o m @Test public void createMediaEntity() throws InterruptedException, ExecutionException, IOException { URIBuilder builder = client.getURIBuilder(testDefaultServiceRootURL).appendEntitySetSegment("Car"); final String TO_BE_UPDATED = "async buffered stream sample"; final InputStream input = IOUtils.toInputStream(TO_BE_UPDATED); final ODataMediaEntityCreateRequest createReq = client.getStreamedRequestFactory() .getMediaEntityCreateRequest(builder.build(), input); final ODataMediaEntityCreateRequest.MediaEntityCreateStreamManager streamManager = createReq.execute(); final Future<ODataMediaEntityCreateResponse> futureCreateRes = streamManager.getAsyncResponse(); while (!futureCreateRes.isDone()) { } final ODataMediaEntityCreateResponse createRes = futureCreateRes.get(); assertEquals(201, createRes.getStatusCode()); final ODataEntity created = createRes.getBody(); assertNotNull(created); assertEquals(2, created.getProperties().size()); final int id = "VIN".equals(created.getProperties().get(0).getName()) ? created.getProperties().get(0).getPrimitiveValue().<Integer>toCastValue() : created.getProperties().get(1).getPrimitiveValue().<Integer>toCastValue(); builder = client.getURIBuilder(testDefaultServiceRootURL).appendEntityTypeSegment("Car") .appendKeySegment(id).appendValueSegment(); final ODataMediaRequest retrieveReq = client.getRetrieveRequestFactory().getMediaRequest(builder.build()); final ODataRetrieveResponse<InputStream> retrieveRes = retrieveReq.execute(); assertEquals(200, retrieveRes.getStatusCode()); assertEquals(TO_BE_UPDATED, IOUtils.toString(retrieveRes.getBody())); }
From source file:br.unb.cic.bionimbuz.plugin.AbstractPlugin.java
private void checkPendingGets() { for (final Future<PluginGetFile> f : this.pendingGets) { if (f.isDone()) { this.pendingGets.remove(f); }// ww w. j a va2 s .co m } }
From source file:org.apache.olingo.client.core.it.v3.AsyncTestITCase.java
/** * @see MediaEntityTest#createMediaEntity(com.msopentech.odatajclient.engine.format.ODataPubFormat) */// w ww . j a va 2s. co m @Test @Ignore public void createMediaEntity() throws Exception { CommonURIBuilder<?> builder = client.getURIBuilder(testStaticServiceRootURL).appendEntitySetSegment("Car"); final String TO_BE_UPDATED = "async buffered stream sample"; final InputStream input = IOUtils.toInputStream(TO_BE_UPDATED); final ODataMediaEntityCreateRequest createReq = client.getStreamedRequestFactory() .getMediaEntityCreateRequest(builder.build(), input); final MediaEntityCreateStreamManager streamManager = createReq.execute(); final Future<ODataMediaEntityCreateResponse> futureCreateRes = streamManager.getAsyncResponse(); while (!futureCreateRes.isDone()) { Thread.sleep(1000L); } final ODataMediaEntityCreateResponse createRes = futureCreateRes.get(); assertEquals(201, createRes.getStatusCode()); final ODataEntity created = createRes.getBody(); assertNotNull(created); assertEquals(2, created.getProperties().size()); final int id = "VIN".equals(created.getProperties().get(0).getName()) ? created.getProperties().get(0).getPrimitiveValue().toCastValue(Integer.class) : created.getProperties().get(1).getPrimitiveValue().toCastValue(Integer.class); builder = client.getURIBuilder(testStaticServiceRootURL).appendEntitySetSegment("Car").appendKeySegment(id) .appendValueSegment(); final ODataMediaRequest retrieveReq = client.getRetrieveRequestFactory().getMediaRequest(builder.build()); final ODataRetrieveResponse<InputStream> retrieveRes = retrieveReq.execute(); assertEquals(200, retrieveRes.getStatusCode()); assertEquals(TO_BE_UPDATED, IOUtils.toString(retrieveRes.getBody())); }
From source file:com.flipkart.poseidon.serviceclients.FutureTaskResultToDomainObjectPromiseWrapper.java
@Override public boolean isRealized() { for (Future<TaskResult> future : futureList) { if (!future.isDone()) { return false; }//from w w w. j a va 2 s .co m } return true; }
From source file:br.unb.cic.bionimbuz.plugin.AbstractPlugin.java
private void checkPendingSaves() { for (final Future<PluginFile> f : this.pendingSaves) { if (!f.isDone()) { continue; }//from w w w .ja v a2s .c om try { final PluginFile file = f.get(); final List<String> pluginIds = new ArrayList<>(); pluginIds.add(this.getId()); file.setPluginId(pluginIds); this.pendingSaves.remove(f); this.pluginFiles.put(file.getId(), file); } catch (InterruptedException | ExecutionException e) { e.printStackTrace(); // TODO criar mensagem de erro? } } }