List of usage examples for java.util.concurrent Future isDone
boolean isDone();
From source file:org.geomajas.plugin.rasterizing.layer.RasterDirectLayer.java
@Override public void draw(Graphics2D graphics, MapContent map, MapViewport viewport) { try {/* w ww . j a va2 s . co m*/ if (tiles.size() > 0) { Collection<Callable<ImageResult>> callables = new ArrayList<Callable<ImageResult>>(tiles.size()); // Build the image downloading threads for (RasterTile tile : tiles) { RasterImageDownloadCallable downloadThread = new RasterImageDownloadCallable( DOWNLOAD_MAX_ATTEMPTS, tile); callables.add(downloadThread); } // Loop until all images are downloaded or timeout is reached long totalTimeout = DOWNLOAD_TIMEOUT + DOWNLOAD_TIMEOUT_ONE_TILE * tiles.size(); log.debug("=== total timeout (millis): {}", totalTimeout); ExecutorService service = Executors.newFixedThreadPool(DOWNLOAD_MAX_THREADS); List<Future<ImageResult>> futures = service.invokeAll(callables, totalTimeout, TimeUnit.MILLISECONDS); // determine the pixel bounds of the mosaic Bbox pixelBounds = getPixelBounds(tiles); // create the images for the mosaic List<RenderedImage> images = new ArrayList<RenderedImage>(); for (Future<ImageResult> future : futures) { ImageResult result = null; if (future.isDone()) { try { result = future.get(); // create a rendered image if (result.getImage() != null && result.getImage().length > 0) { RenderedImage image = JAI.create("stream", new ByteArraySeekableStream(result.getImage())); // convert to common direct color model (some images have their own indexed color model) RenderedImage colored = toDirectColorModel(image); // translate to the correct position in the tile grid double xOffset = result.getRasterImage().getCode().getX() * tileWidth - pixelBounds.getX(); double yOffset; // TODO: in some cases, the y-index is up (e.g. WMS), should be down for // all layers !!!! if (isYIndexUp(tiles)) { yOffset = result.getRasterImage().getCode().getY() * tileHeight - pixelBounds.getY(); } else { yOffset = (pixelBounds.getMaxY() - (result.getRasterImage().getCode().getY() + 1) * tileHeight); } log.debug("adding to(" + xOffset + "," + yOffset + "), url = " + result.getRasterImage().getUrl()); RenderedImage translated = TranslateDescriptor.create(colored, (float) xOffset, (float) yOffset, new InterpolationNearest(), null); images.add(translated); } } catch (ExecutionException e) { addLoadError(graphics, (ImageException) (e.getCause()), viewport); log.warn(MISSING_TILE_IN_MOSAIC + e.getMessage()); } catch (Exception e) { log.warn("Missing tile " + result.getRasterImage().getUrl()); log.warn(MISSING_TILE_IN_MOSAIC + e.getMessage()); } } } if (images.size() > 0) { ImageLayout imageLayout = new ImageLayout(0, 0, (int) pixelBounds.getWidth(), (int) pixelBounds.getHeight()); imageLayout.setTileWidth(tileWidth); imageLayout.setTileHeight(tileHeight); // create the mosaic image ParameterBlock pbMosaic = new ParameterBlock(); pbMosaic.add(MosaicDescriptor.MOSAIC_TYPE_OVERLAY); for (RenderedImage renderedImage : images) { pbMosaic.addSource(renderedImage); } RenderedOp mosaic = JAI.create("mosaic", pbMosaic, new RenderingHints(JAI.KEY_IMAGE_LAYOUT, imageLayout)); try { ByteArrayOutputStream baos = new ByteArrayOutputStream(); log.debug("rendering to buffer..."); ImageIO.write(mosaic, "png", baos); log.debug("rendering done, size = " + baos.toByteArray().length); RasterTile mosaicTile = new RasterTile(); mosaicTile.setBounds(getWorldBounds(tiles)); log.info("application bounds = " + mosaicTile.getBounds()); ImageResult mosaicResult = new ImageResult(mosaicTile); mosaicResult.setImage(baos.toByteArray()); addImage(graphics, mosaicResult, viewport); } catch (IOException e) { log.warn("could not write mosaic image " + e.getMessage()); } } } } catch (InterruptedException e) { log.warn("rendering {} to {} failed : ", getTitle(), viewport.getBounds()); } }
From source file:info.magnolia.imaging.caching.CachingImageStreamerRepositoryTest.java
@Test public void testRequestForSimilarUncachedImageOnlyGeneratesItOnce() throws Exception { final HierarchyManager srcHM = MgnlContext.getHierarchyManager("website"); final String srcPath = "/foo/bar"; ContentUtil.createPath(srcHM, srcPath); // ParameterProvider for tests - return a new instance of the same node everytime // if we'd return the same src instance everytime, the purpose of this test would be null final ParameterProviderFactory<Object, Content> ppf = new TestParameterProviderFactory(srcHM, srcPath); final OutputFormat png = new OutputFormat(); png.setFormatName("png"); final BufferedImage dummyImg = ImageIO.read(getClass().getResourceAsStream("/funnel.gif")); assertNotNull("Couldn't load dummy test image", dummyImg); final ImageGenerator<ParameterProvider<Content>> generator = mock(ImageGenerator.class); when(generator.getParameterProviderFactory()).thenReturn(ppf); when(generator.getName()).thenReturn("test"); when(generator.getOutputFormat(isA(ParameterProvider.class))).thenReturn(png); // aaaaand finally, here's the real reason for this test ! when(generator.generate(isA(ParameterProvider.class))).thenReturn(dummyImg); // yeah, we're using a "wrong" workspace for the image cache, to avoid having to setup a custom one in this test final HierarchyManager hm = MgnlContext.getHierarchyManager("config"); final ImageStreamer streamer = new CachingImageStreamer(hm, ppf.getCachingStrategy(), new DefaultImageStreamer()); // Generator instances will always be the same (including paramProvFac) // since they are instantiated with the module config and c2b. // ParamProv is a new instance every time. // streamer can (must) be the same - once single HM, one cache. // thread pool of 10, launching 8 requests, can we hit some concurrency please ? final ExecutorService executor = Executors.newFixedThreadPool(10); final ByteArrayOutputStream[] outs = new ByteArrayOutputStream[8]; final Future[] futures = new Future[8]; for (int i = 0; i < outs.length; i++) { outs[i] = new ByteArrayOutputStream(); futures[i] = executor.submit(new TestJob(generator, streamer, outs[i])); }/*w ww . j a va2s. c o m*/ executor.shutdown(); executor.awaitTermination(30, TimeUnit.SECONDS); for (Future<?> future : futures) { assertTrue(future.isDone()); assertFalse(future.isCancelled()); // ignore the results of TestJob - all we care about is if an exception was thrown // and if there was any, it is kept in Future until we call Future.get() future.get(); } final NodeData cachedNodeData = hm.getNodeData("/test/website/foo/bar/generated-image"); // update node meta data Content cachedNode = hm.getContent("/test/website/foo/bar"); cachedNode.getMetaData().setModificationDate(); cachedNode.save(); final InputStream res = cachedNodeData.getStream(); final ByteArrayOutputStream cachedOut = new ByteArrayOutputStream(); IOUtils.copy(res, cachedOut); // assert all outs are the same for (int i = 1; i < outs.length; i++) { // TODO assert they're all equals byte to byte to the source? or in size? can't as-is since we convert... final byte[] a = outs[i - 1].toByteArray(); final byte[] b = outs[i].toByteArray(); assertTrue(a.length > 0); assertEquals("Different sizes (" + Math.abs(a.length - b.length) + " bytes diff.) with i=" + i, a.length, b.length); assertTrue("not equals for outs/" + i, Arrays.equals(a, b)); outs[i - 1] = null; // cleanup all those byte[], or we'll soon run out of memory } assertTrue("failed comparing last thread's result with what we got from hierarchyManager", Arrays.equals(outs[outs.length - 1].toByteArray(), cachedOut.toByteArray())); outs[outs.length - 1] = null; // now start again another bunch of requests... they should ALL get their results from the cache final ExecutorService executor2 = Executors.newFixedThreadPool(10); final ByteArrayOutputStream[] outs2 = new ByteArrayOutputStream[8]; final Future[] futures2 = new Future[8]; for (int i = 0; i < outs2.length; i++) { outs2[i] = new ByteArrayOutputStream(); futures2[i] = executor2.submit(new TestJob(generator, streamer, outs2[i])); } executor2.shutdown(); executor2.awaitTermination(30, TimeUnit.SECONDS); for (Future<?> future : futures2) { assertTrue(future.isDone()); assertFalse(future.isCancelled()); // ignore the results of TestJob - all we care about is if an exception was thrown // and if there was any, it is kept in Future until we call Future.get() future.get(); } final NodeData cachedNodeData2 = hm.getNodeData("/test/website/foo/bar/generated-image"); final InputStream res2 = cachedNodeData2.getStream(); final ByteArrayOutputStream cachedOut2 = new ByteArrayOutputStream(); IOUtils.copy(res2, cachedOut2); // assert all outs are the same for (int i = 1; i < outs2.length; i++) { // TODO assert they're all equals byte to byte to the source? or in size? can't as-is since we re-save.. final byte[] a = outs2[i - 1].toByteArray(); final byte[] b = outs2[i].toByteArray(); assertTrue(a.length > 0); assertEquals("Different sizes (" + Math.abs(a.length - b.length) + " bytes diff.) with i=" + i, a.length, b.length); assertTrue("not equals for outs2/" + i, Arrays.equals(a, b)); outs2[i - 1] = null; } assertTrue("failed comparing last thread's result with what we got from hierarchyManager", Arrays.equals(outs2[outs2.length - 1].toByteArray(), cachedOut2.toByteArray())); outs2[outs2.length - 1] = null; }
From source file:com.mobiperf.speedometer.Checkin.java
private synchronized boolean checkGetCookie() { if (isTestingServer()) { authCookie = getFakeAuthCookie(); return true; }/*ww w. j a v a2 s . com*/ Future<Cookie> getCookieFuture = accountSelector.getCheckinFuture(); if (getCookieFuture == null) { Logger.i("checkGetCookie called too early"); return false; } if (getCookieFuture.isDone()) { try { authCookie = getCookieFuture.get(); Logger.i("Got authCookie: " + authCookie); return true; } catch (InterruptedException e) { Logger.e("Unable to get auth cookie", e); return false; } catch (ExecutionException e) { Logger.e("Unable to get auth cookie", e); return false; } } else { Logger.i("getCookieFuture is not yet finished"); return false; } }
From source file:nl.uva.sne.disambiguators.Wikidata.java
private Map<String, List<String>> getbroaderIDS(Set<Term> terms) throws MalformedURLException, InterruptedException, ExecutionException { Map<String, List<String>> map = new HashMap<>(); if (terms.size() > 0) { int maxT = 2; BlockingQueue<Runnable> workQueue = new ArrayBlockingQueue(maxT); ExecutorService pool = new ThreadPoolExecutor(maxT, maxT, 500L, TimeUnit.MICROSECONDS, workQueue); // ExecutorService pool = new ThreadPoolExecutor(maxT, maxT, // 5000L, TimeUnit.MILLISECONDS, // new ArrayBlockingQueue<>(maxT, true), new ThreadPoolExecutor.CallerRunsPolicy()); Set<Future<Map<String, List<String>>>> set1 = new HashSet<>(); String prop = "P31"; for (Term t : terms) { URL url = new URL( page + "?action=wbgetclaims&format=json&props=&property=" + prop + "&entity=" + t.getUID()); System.err.println(url); WikiRequestor req = new WikiRequestor(url, t.getUID(), 1); Future<Map<String, List<String>>> future = pool.submit(req); set1.add(future);/*from w w w . j a va 2 s. co m*/ } pool.shutdown(); for (Future<Map<String, List<String>>> future : set1) { while (!future.isDone()) { // Logger.getLogger(Wikipedia.class.getName()).log(Level.INFO, "Task is not completed yet...."); Thread.currentThread().sleep(10); } Map<String, List<String>> c = future.get(); if (c != null) { map.putAll(c); } } } return map; }
From source file:eu.edisonproject.training.wsd.Wikidata.java
private Map<CharSequence, List<CharSequence>> getbroaderIDS(Set<Term> terms) throws MalformedURLException, InterruptedException, ExecutionException { Map<CharSequence, List<CharSequence>> map = new HashMap<>(); if (terms.size() > 0) { int maxT = 2; BlockingQueue<Runnable> workQueue = new ArrayBlockingQueue(maxT); ExecutorService pool = new ThreadPoolExecutor(maxT, maxT, 500L, TimeUnit.MICROSECONDS, workQueue); // ExecutorService pool = new ThreadPoolExecutor(maxT, maxT, 5000L, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<>(maxT, true), new ThreadPoolExecutor.CallerRunsPolicy()); Set<Future<Map<CharSequence, List<CharSequence>>>> set1 = new HashSet<>(); String prop = "P31"; for (Term t : terms) { URL url = new URL( PAGE + "?action=wbgetclaims&format=json&props=&property=" + prop + "&entity=" + t.getUid()); Logger.getLogger(Wikidata.class.getName()).log(Level.FINE, url.toString()); WikiRequestor req = new WikiRequestor(url, t.getUid().toString(), 1); Future<Map<CharSequence, List<CharSequence>>> future = pool.submit(req); set1.add(future);/*from w ww .j a va2s . c om*/ } pool.shutdown(); for (Future<Map<CharSequence, List<CharSequence>>> future : set1) { while (!future.isDone()) { // Logger.getLogger(Wikipedia.class.getName()).log(Level.INFO, "Task is not completed yet...."); Thread.currentThread().sleep(10); } Map<CharSequence, List<CharSequence>> c = future.get(); if (c != null) { map.putAll(c); } } } return map; }
From source file:org.suren.autotest.web.framework.selenium.action.SeleniumFileUpload.java
@Override public boolean upload(Element element, final File file) { WebElement webEle = findElement(element); if (webEle != null) { logger.info("File upload, element is already found."); ExecutorService executor = Executors.newSingleThreadExecutor(); final AutoItCmd autoItCmd = new AutoItCmd(); try {// w w w .j av a2 s . c o m Future<?> future = executor.submit(new Runnable() { @Override public void run() { try { autoItCmd.execFileChoose(file); } catch (Exception e) { e.printStackTrace(); } } }); synchronized (autoItCmd) { autoItCmd.wait(); } click(element); if (!future.isDone()) { future.get(30, TimeUnit.SECONDS); } return true; } catch (InterruptedException | ExecutionException | TimeoutException e) { logger.error("File uplod error.", e); e.printStackTrace(); } finally { autoItCmd.close(); executor.shutdown(); } } else { logger.error("Can not found element, when prepare to upload file."); } return false; }
From source file:com.liferay.sync.engine.lan.session.LanSession.java
protected SyncLanClientQueryResult findSyncLanClient(SyncFile syncFile) throws Exception { SyncAccount syncAccount = SyncAccountService.fetchSyncAccount(syncFile.getSyncAccountId()); List<String> syncLanClientUuids = SyncLanEndpointService .findSyncLanClientUuids(syncAccount.getLanServerUuid(), syncFile.getRepositoryId()); if (syncLanClientUuids.isEmpty()) { return null; }//from www .ja va 2s . co m final List<Callable<SyncLanClientQueryResult>> syncLanClientQueryResultCallables = Collections .synchronizedList(new ArrayList<Callable<SyncLanClientQueryResult>>(syncLanClientUuids.size())); for (String syncLanClientUuid : syncLanClientUuids) { SyncLanClient syncLanClient = SyncLanClientService.fetchSyncLanClient(syncLanClientUuid); syncLanClientQueryResultCallables.add(createSyncLanClientQueryResultCallable(syncLanClient, syncFile)); } int queryPoolSize = Math.min(syncLanClientUuids.size(), PropsValues.SYNC_LAN_SESSION_QUERY_POOL_MAX_SIZE); List<Future<SyncLanClientQueryResult>> pendingSyncLanClientQueryResults = new ArrayList<>(queryPoolSize); ExecutorCompletionService<SyncLanClientQueryResult> executorCompletionService = new ExecutorCompletionService<>( getExecutorService()); for (int i = 0; i < queryPoolSize; i++) { Callable<SyncLanClientQueryResult> callable = new Callable<SyncLanClientQueryResult>() { @Override public synchronized SyncLanClientQueryResult call() throws Exception { if (syncLanClientQueryResultCallables.isEmpty()) { return null; } Callable<SyncLanClientQueryResult> syncLanClientQueryResultCallable = syncLanClientQueryResultCallables .remove(0); try { return syncLanClientQueryResultCallable.call(); } catch (Exception e) { return call(); } } }; pendingSyncLanClientQueryResults.add(executorCompletionService.submit(callable)); } List<Future<SyncLanClientQueryResult>> completedSyncLanClientQueryResult = new ArrayList<>(queryPoolSize); long timeout = PropsValues.SYNC_LAN_SESSION_QUERY_TOTAL_TIMEOUT; long endTime = System.currentTimeMillis() + timeout; for (int i = 0; i < queryPoolSize; i++) { Future<SyncLanClientQueryResult> future = executorCompletionService.poll(timeout, TimeUnit.MILLISECONDS); if (future == null) { for (Future<SyncLanClientQueryResult> pendingSyncLanClientQueryResult : pendingSyncLanClientQueryResults) { if (!pendingSyncLanClientQueryResult.isDone()) { pendingSyncLanClientQueryResult.cancel(true); } } break; } completedSyncLanClientQueryResult.add(future); timeout = endTime - System.currentTimeMillis(); } SyncLanClientQueryResult candidateSyncLanClientQueryResult = null; int candidateDownloadRatePerConnection = 0; for (Future<SyncLanClientQueryResult> completedFuture : completedSyncLanClientQueryResult) { SyncLanClientQueryResult syncLanClientQueryResult = null; try { syncLanClientQueryResult = completedFuture.get(); } catch (Exception e) { continue; } if (syncLanClientQueryResult == null) { continue; } if (syncLanClientQueryResult.getConnectionsCount() >= syncLanClientQueryResult.getMaxConnections()) { if (candidateSyncLanClientQueryResult == null) { candidateSyncLanClientQueryResult = syncLanClientQueryResult; } continue; } if (syncLanClientQueryResult.getConnectionsCount() == 0) { return syncLanClientQueryResult; } int downloadRatePerConnection = syncLanClientQueryResult.getDownloadRate() / (syncLanClientQueryResult.getConnectionsCount() + 1); if (downloadRatePerConnection >= candidateDownloadRatePerConnection) { candidateDownloadRatePerConnection = downloadRatePerConnection; candidateSyncLanClientQueryResult = syncLanClientQueryResult; } } return candidateSyncLanClientQueryResult; }
From source file:com.alibaba.otter.node.etl.extract.extractor.DatabaseExtractor.java
private void cancel(List<Future> futures) { for (int i = 0; i < futures.size(); i++) { Future future = futures.get(i); if (future.isDone() == false) { future.cancel(true);// ?? }/*from w w w. j a v a 2 s. c o m*/ } }
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 w w w . ja v a 2 s . c om }
From source file:org.apache.hadoop.yarn.util.TestFSDownload.java
@Test(timeout = 10000) public void testDirDownload() throws IOException, InterruptedException { Configuration conf = new Configuration(); FileContext files = FileContext.getLocalFSFileContext(conf); final Path basedir = files.makeQualified(new Path("target", TestFSDownload.class.getSimpleName())); files.mkdir(basedir, null, true);// www .j a v a 2 s.c o m conf.setStrings(TestFSDownload.class.getName(), basedir.toString()); Map<LocalResource, LocalResourceVisibility> rsrcVis = new HashMap<LocalResource, LocalResourceVisibility>(); Random rand = new Random(); long sharedSeed = rand.nextLong(); rand.setSeed(sharedSeed); System.out.println("SEED: " + sharedSeed); Map<LocalResource, Future<Path>> pending = new HashMap<LocalResource, Future<Path>>(); ExecutorService exec = Executors.newSingleThreadExecutor(); LocalDirAllocator dirs = new LocalDirAllocator(TestFSDownload.class.getName()); for (int i = 0; i < 5; ++i) { LocalResourceVisibility vis = LocalResourceVisibility.PRIVATE; if (i % 2 == 1) { vis = LocalResourceVisibility.APPLICATION; } Path p = new Path(basedir, "dir" + i + ".jar"); LocalResource rsrc = createJar(files, p, vis); rsrcVis.put(rsrc, vis); Path destPath = dirs.getLocalPathForWrite(basedir.toString(), conf); destPath = new Path(destPath, Long.toString(uniqueNumberGenerator.incrementAndGet())); FSDownload fsd = new FSDownload(files, UserGroupInformation.getCurrentUser(), conf, destPath, rsrc); pending.put(rsrc, exec.submit(fsd)); } exec.shutdown(); while (!exec.awaitTermination(1000, TimeUnit.MILLISECONDS)) ; for (Future<Path> path : pending.values()) { Assert.assertTrue(path.isDone()); } try { for (Map.Entry<LocalResource, Future<Path>> p : pending.entrySet()) { Path localized = p.getValue().get(); FileStatus status = files.getFileStatus(localized); System.out.println("Testing path " + localized); assert (status.isDirectory()); assert (rsrcVis.containsKey(p.getKey())); verifyPermsRecursively(localized.getFileSystem(conf), files, localized, rsrcVis.get(p.getKey())); } } catch (ExecutionException e) { throw new IOException("Failed exec", e); } }