Example usage for java.util.concurrent Future get

List of usage examples for java.util.concurrent Future get

Introduction

In this page you can find the example usage for java.util.concurrent Future get.

Prototype

V get(long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException;

Source Link

Document

Waits if necessary for at most the given time for the computation to complete, and then retrieves its result, if available.

Usage

From source file:apiserver.services.pdf.controllers.PagesController.java

/**
 * Delete one or more pages from a pdf/*from  w  w w.  j  a v a2 s .com*/
 * @param file
 * @return
 * @throws InterruptedException
 * @throws java.util.concurrent.ExecutionException
 * @throws java.util.concurrent.TimeoutException
 * @throws java.io.IOException
 * @throws Exception
 */
@ApiOperation(value = "Delete one or more pages from a pdf")
@RequestMapping(value = "/modify/pages", method = RequestMethod.DELETE, produces = "application/pdf")
public ResponseEntity<byte[]> deletePagesFromPdf(
        @ApiParam(name = "file", required = true) @RequestPart("file") MultipartFile file,
        @ApiParam(name = "pages", required = true) @RequestPart("pages") String pages)
        throws InterruptedException, ExecutionException, TimeoutException, IOException, Exception {
    DeletePdfPagesJob job = new DeletePdfPagesJob();
    //file
    job.setFile(new Document(file));
    job.setPages(pages);

    Future<Map> future = gateway.deletePages(job);
    BinaryJob payload = (BinaryJob) future.get(defaultTimeout, TimeUnit.MILLISECONDS);

    byte[] fileBytes = payload.getPdfBytes();
    String contentType = "application/pdf";
    ResponseEntity<byte[]> result = ResponseEntityHelper.processFile(fileBytes, contentType, false);
    return result;
}

From source file:apiserver.services.pdf.controllers.PagesController.java

/**
 * Delete one or more pages from a pdf/*  w w w .j  a v  a  2 s  .  com*/
 * @param documentId
 * @return
 * @throws InterruptedException
 * @throws java.util.concurrent.ExecutionException
 * @throws java.util.concurrent.TimeoutException
 * @throws java.io.IOException
 * @throws Exception
 */
@ApiOperation(value = "Delete one or more pages from a pdf")
@RequestMapping(value = "/modify/{documentId}/pages", method = RequestMethod.DELETE, produces = "application/pdf")
public ResponseEntity<byte[]> deletePagesFromCachedPdf(
        @ApiParam(name = "documentId", required = true) @RequestPart("documentId") String documentId,
        @ApiParam(name = "pages", required = true) @RequestPart("pages") String pages)
        throws InterruptedException, ExecutionException, TimeoutException, IOException, Exception {
    DeletePdfPagesJob job = new DeletePdfPagesJob();
    //file
    job.setDocumentId(documentId);
    job.setPages(pages);

    Future<Map> future = gateway.deletePages(job);
    BinaryJob payload = (BinaryJob) future.get(defaultTimeout, TimeUnit.MILLISECONDS);

    byte[] fileBytes = payload.getPdfBytes();
    String contentType = "application/pdf";
    ResponseEntity<byte[]> result = ResponseEntityHelper.processFile(fileBytes, contentType, false);
    return result;
}

From source file:io.cloudslang.content.utilities.util.ProcessExecutor.java

public ProcessResponseEntity execute(String commandLine, int timeout)
        throws IOException, ExecutionException, InterruptedException, TimeoutException {
    Process process = new ProcessBuilder().command(processCommand(commandLine)).start();

    ProcessStreamConsumer processStreamConsumer = new ProcessStreamConsumer(process);
    ExecutorService executor = Executors.newFixedThreadPool(1);
    Future<ProcessResponseEntity> futureResult = executor.submit(processStreamConsumer);

    try {/*from w  w  w  . j ava  2  s . c o  m*/
        return futureResult.get(timeout, MILLISECONDS);
    } catch (InterruptedException | ExecutionException | TimeoutException e) {
        stopProcess(process, futureResult);
        throw e;
    } finally {
        executor.shutdownNow();
    }
}

From source file:apiserver.services.pdf.service.ExtractPdfFormFieldsCFService.java

public Object execute(Message<?> message) throws ColdFusionException {
    ExtractPdfFormResult props = (ExtractPdfFormResult) message.getPayload();

    try {//  ww  w. j  a  v a 2 s  .  c  om
        long startTime = System.nanoTime();
        Grid grid = verifyGridConnection();

        // Get grid-enabled executor service for nodes where attribute 'worker' is defined.
        ExecutorService exec = getColdFusionExecutor();

        Future<MapResult> future = exec.submit(new ExtractFormFieldsCallable(props.getFile().getFileBytes()));

        MapResult _result = future.get(defaultTimeout, TimeUnit.SECONDS);
        props.setResult(_result.getData());

        long endTime = System.nanoTime();
        log.debug("execution times: CF=" + _result.getStats().getExecutionTime() + "ms -- total="
                + (endTime - startTime) + "ms");

        return props;
    } catch (Exception ge) {
        throw new RuntimeException(ge);
    }
}

From source file:apiserver.services.pdf.service.UrlToPdfCFService.java

public Object execute(Message<?> message) throws ColdFusionException {
    Url2PdfResult props = (Url2PdfResult) message.getPayload();

    try {//from   w  w  w  .j av a2s . c om
        long startTime = System.nanoTime();
        Grid grid = verifyGridConnection();

        // Get grid-enabled executor service for nodes where attribute 'coldfusion-worker' is defined.
        ExecutorService exec = getColdFusionExecutor();

        Future<ByteArrayResult> future = exec.submit(new UrlToPdfCallable(props.getPath(), props.getOptions()));

        ByteArrayResult _result = future.get(defaultTimeout, TimeUnit.SECONDS);
        props.setResult(_result.getBytes());

        long endTime = System.nanoTime();
        log.debug("execution times: CF=" + _result.getStats().getExecutionTime() + "ms -- total="
                + (endTime - startTime) + "ms");

        return props;
    } catch (Exception ge) {
        throw new RuntimeException(ge);
    }
}

From source file:module.d.SearchController.java

@RequestMapping("/search")
public ModelAndView searchItemPage(String searchFormName) {
    System.out.println("Invoking controller: " + searchFormName);
    ModelAndView mv = new ModelAndView();
    mv.setViewName("search");
    //      String searchForm = this.searchFormGateway.renderSearchUiForm(searchFormName);
    String searchForm = null;//from w  ww.j  a  va2 s .  co  m
    try {
        Future<String> future = null;
        try {
            future = this.searchFormGateway.renderSearchUiForm(searchFormName);
            searchForm = future.get(1000, TimeUnit.MILLISECONDS);
        } catch (Exception e) {
            future = this.searchFormGateway.renderSearchUiForm(searchFormName);
            searchForm = future.get(1000, TimeUnit.MILLISECONDS);
        }
    } catch (Exception e) {
        searchForm = "<b>'Search Item' form page is currently unavailable, please try again later</b>";
    }
    mv.addObject("searchForm", searchForm);
    return mv;
}

From source file:apiserver.services.images.controllers.ImageController.java

/**
 * get basic info about image./*from  www.ja va  2  s  .  c o  m*/
 * @param file
 * @return
 * @throws java.util.concurrent.ExecutionException
 * @throws java.util.concurrent.TimeoutException
 * @throws InterruptedException
 */
@ApiOperation(value = "Get the height and width for the image", responseClass = "java.util.Map")
@RequestMapping(value = "/info/size", method = { RequestMethod.POST })
public WebAsyncTask<Map> imageInfoByImageAsync(
        @ApiParam(name = "file", required = true) @RequestParam(value = "file", required = true) MultipartFile file)
        throws ExecutionException, TimeoutException, InterruptedException {
    final MultipartFile _file = file;

    Callable<Map> callable = new Callable<Map>() {
        @Override
        public Map call() throws Exception {
            FileInfoJob job = new FileInfoJob();
            job.setDocumentId(null);
            job.setDocument(new Document(_file));
            job.getDocument().setContentType(MimeType.getMimeType(_file.getContentType()));
            job.getDocument().setFileName(_file.getOriginalFilename());

            Future<Map> imageFuture = gateway.imageSize(job);
            Map payload = imageFuture.get(defaultTimeout, TimeUnit.MILLISECONDS);

            return payload;
        }
    };

    return new WebAsyncTask<Map>(defaultTimeout, callable);
}

From source file:apiserver.services.images.controllers.info.SizeController.java

/**
 * get basic info about image./*from  w  w w.j  a  va2s.c  o  m*/
 *
 * @param file
 * @return
 * @throws java.util.concurrent.ExecutionException
 * @throws java.util.concurrent.TimeoutException
 * @throws InterruptedException
 */
@ApiOperation(value = "Get the height and width for the image")
@RequestMapping(value = "/info/size", method = { RequestMethod.POST })
public ResponseEntity<Map> imageInfoByImageAsync(HttpServletRequest request, HttpServletResponse response,
        @ApiParam(name = "file", required = false) @RequestParam(value = "file", required = false) MultipartFile file)
        throws ExecutionException, TimeoutException, InterruptedException, IOException {
    Document _file = null;
    MimeType _fileMimeType = null;

    MultipartFile _mFile = fileUploadHelper.getFileFromRequest(file, request);
    _file = new Document(_mFile);
    _fileMimeType = _file.getContentType();
    if (!_fileMimeType.isSupportedImage()) {
        return new ResponseEntity<>(HttpStatus.UNSUPPORTED_MEDIA_TYPE);
    }

    FileInfoJob job = new FileInfoJob();
    job.setDocumentId(null);
    job.setDocument(_file);

    Future<Map> imageFuture = gateway.imageSize(job);
    Map payload = imageFuture.get(defaultTimeout, TimeUnit.MILLISECONDS);

    ResponseEntity<Map> result = new ResponseEntity(payload, HttpStatus.OK);
    return result;
}

From source file:org.obm.servlet.filter.qos.util.AsyncServletRequestUtils.java

public StatusLine retrieveRequestStatus(Future<StatusLine> request)
        throws InterruptedException, ExecutionException, TimeoutException {
    return request.get(getTimeout(), getTimeUnit());
}

From source file:apiserver.services.images.controllers.ImageController.java

/**
 * get basic info about image./*from w  ww .j  av  a2  s.c  o m*/
 * @param documentId cache id
 * @return   height,width, pixel size, transparency
 * , @RequestPart("meta-data") Object metadata
 *
, @RequestParam MultipartFile file
        
 */
@ApiOperation(value = "Get the height and width for the image", responseClass = "java.util.Map")
@RequestMapping(value = "/info/{documentId}/size", method = { RequestMethod.GET })
public WebAsyncTask<ResponseEntity<Map>> imageInfoByImageAsync(
        @ApiParam(name = "documentId", required = true, defaultValue = "8D981024-A297-4169-8603-E503CC38EEDA") @PathVariable(value = "documentId") String documentId)
        throws ExecutionException, TimeoutException, InterruptedException {
    final String _documentId = documentId;

    Callable<ResponseEntity<Map>> callable = new Callable<ResponseEntity<Map>>() {
        @Override
        public ResponseEntity<Map> call() throws Exception {
            FileInfoJob args = new FileInfoJob();
            args.setDocumentId(_documentId);

            Future<Map> imageFuture = gateway.imageSize(args);
            Map payload = imageFuture.get(defaultTimeout, TimeUnit.MILLISECONDS);

            HttpHeaders headers = new HttpHeaders();
            headers.setContentType(MediaType.APPLICATION_JSON);
            ResponseEntity<Map> result = new ResponseEntity<Map>(payload, headers, HttpStatus.OK);
            return result;
        }
    };

    return new WebAsyncTask<ResponseEntity<Map>>(defaultTimeout, callable);
}