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.InfoController.java

/**
 * Get information about document//from  ww w .  ja v  a 2s.  c  o  m
 * @param documentId
 * @param password  Password to open pdf
 * @return
 * @throws InterruptedException
 * @throws java.util.concurrent.ExecutionException
 * @throws java.util.concurrent.TimeoutException
 * @throws java.io.IOException
 * @throws Exception
 */
@ApiOperation(value = "Get information about document")
@RequestMapping(value = "/{documentId}/info/get", method = RequestMethod.GET, produces = "application/pdf")
public ResponseEntity<Object> getCachedPdfInfo(
        @ApiParam(name = "documentId", required = true) @RequestPart("documentId") String documentId,
        @ApiParam(name = "password", required = false) @RequestPart(value = "password", required = false) String password)
        throws InterruptedException, ExecutionException, TimeoutException, IOException, Exception {
    PdfInfoJob job = new PdfInfoJob();
    job.setDocumentId(documentId);
    if (password != null)
        job.setPassword(password);

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

    return ResponseEntityHelper.processObject(payload.getResult());
}

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

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

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

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

        Future<ByteArrayResult> future = exec
                .submit(new RemoveHeaderFooterCallable(props.getFile().getFileBytes(), 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:com.njmd.framework.utils.memcached.SpyMemcachedClient.java

/**
 * Set,1, ?false??.//from ww  w .  jav  a 2 s .  com
 */
public boolean safeSet(String key, Object value, int expiration) {
    Future<Boolean> future = memcachedClient.set(key, expiration, value);
    try {
        return future.get(1, TimeUnit.SECONDS);
    } catch (Exception e) {
        future.cancel(false);
    }
    return false;
}

From source file:io.fabric8.kit.common.ExternalCommand.java

private void stopStreamPump(Future<IOException> future) throws IOException {
    try {//from   w ww.j  ava 2s  .co  m
        IOException e = future.get(2, TimeUnit.SECONDS);
        if (e != null) {
            throw new IOException(
                    String.format("Failed to read process '%s' error stream", getCommandAsString()), e);
        }
    } catch (InterruptedException ignore) {
        Thread.currentThread().interrupt();
    } catch (ExecutionException | TimeoutException e) {
        throw new IOException(String.format("Failed to stop process '%s' error stream", getCommandAsString()),
                e);
    }
}

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

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

    try {/*from  w w  w . j  a  va2s . com*/
        long startTime = System.nanoTime();
        Grid grid = verifyGridConnection();

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

        Future<CollectionResult> future = exec
                .submit(new GenerateThumbnailCallable(props.getFile().getFileBytes(), props.getOptions()));

        CollectionResult _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:apiserver.services.pdf.service.ProtectPdfCFService.java

public Object execute(Message<?> message) throws ColdFusionException {
    SecurePdfResult props = (SecurePdfResult) message.getPayload();
    File tmpFile = null;//from   ww  w . ja  v  a2 s  . c om

    try {
        long startTime = System.nanoTime();
        Grid grid = verifyGridConnection();

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

        Future<ByteArrayResult> future = exec
                .submit(new ProtectPdfCallable(props.getFile().getFileBytes(), 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);
    } finally {
        if (tmpFile != null)
            tmpFile.delete();
    }
}

From source file:com.aliyun.oss.common.comm.TimeoutServiceClient.java

@Override
public ResponseMessage sendRequestCore(ServiceClient.Request request, ExecutionContext context)
        throws IOException {
    HttpRequestBase httpRequest = httpRequestFactory.createHttpRequest(request, context);
    HttpClientContext httpContext = HttpClientContext.create();
    httpContext.setRequestConfig(this.requestConfig);

    CloseableHttpResponse httpResponse = null;
    HttpRequestTask httpRequestTask = new HttpRequestTask(httpRequest, httpContext);
    Future<CloseableHttpResponse> future = executor.submit(httpRequestTask);

    try {/*from   w w w  .  j a  v a 2  s.c  om*/
        httpResponse = future.get(this.config.getRequestTimeout(), TimeUnit.MILLISECONDS);
    } catch (InterruptedException e) {
        logException("[ExecutorService]The current thread was interrupted while waiting: ", e);

        httpRequest.abort();
        throw new ClientException(e.getMessage(), e);
    } catch (ExecutionException e) {
        RuntimeException ex;
        httpRequest.abort();

        if (e.getCause() instanceof IOException) {
            ex = ExceptionFactory.createNetworkException((IOException) e.getCause());
        } else {
            ex = new OSSException(e.getMessage(), e);
        }

        logException("[ExecutorService]The computation threw an exception: ", ex);
        throw ex;
    } catch (TimeoutException e) {
        logException("[ExecutorService]The wait " + this.config.getRequestTimeout() + " timed out: ", e);

        httpRequest.abort();
        throw new ClientException(e.getMessage(), OSSErrorCode.REQUEST_TIMEOUT, "Unknown", e);
    }

    return buildResponse(request, httpResponse);
}

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

/**
 * Secure a pdf//  w w  w  . ja  v a  2  s . co m
 * @param file
 * @param password  Owner or user password of the source PDF document, if the document is password-protected.
 * @param newUserPassword   Password used to open PDF document.
 * @param newOwnerPassword  Password used to set permissions on a PDF document
 * @param encrypt   RC4_40|RC4_128|RC4_128M|AES_128|none
 * @return
 * @throws InterruptedException
 * @throws java.util.concurrent.ExecutionException
 * @throws java.util.concurrent.TimeoutException
 * @throws java.io.IOException
 * @throws Exception
 */
@ApiOperation(value = "Secure a pdf with a password")
@RequestMapping(value = "/protect", method = RequestMethod.POST, produces = "application/pdf")
public ResponseEntity<byte[]> protectPdf(
        @ApiParam(name = "file", required = true) @RequestPart("file") MultipartFile file,
        @ApiParam(name = "password", required = false) @RequestPart("password") String password,
        @ApiParam(name = "newUserPassword", required = true) @RequestPart("newUserPassword") String newUserPassword,
        @ApiParam(name = "newOwnerPassword", required = true) @RequestPart("newOwnerPassword") String newOwnerPassword,
        @ApiParam(name = "encrypt", required = false, allowableValues = "RC4_40,RC4_128,RC4_128M,AES_128,none") @RequestPart("encrypt") String encrypt,
        @ApiParam(name = "allowAssembly", required = false) @RequestPart("allowAssembly") Boolean allowAssembly,
        @ApiParam(name = "allowCopy", required = false) @RequestPart("allowCopy") Boolean allowCopy,
        @ApiParam(name = "allowDegradedPrinting", required = false) @RequestPart("allowDegradedPrinting") Boolean allowDegradedPrinting,
        @ApiParam(name = "allowFillIn", required = false) @RequestPart("allowFillIn") Boolean allowFillIn,
        @ApiParam(name = "allowModifyAnnotations", required = false) @RequestPart("allowModifyAnnotations") Boolean allowModifyAnnotations,
        @ApiParam(name = "allowPrinting", required = false) @RequestPart("allowPrinting") Boolean allowPrinting,
        @ApiParam(name = "allowScreenReaders", required = false) @RequestPart("allowScreenReaders") Boolean allowScreenReaders,
        @ApiParam(name = "allowSecure", required = false) @RequestPart("allowSecure") Boolean allowSecure)
        throws InterruptedException, ExecutionException, TimeoutException, IOException, Exception {
    SecurePdfJob job = new SecurePdfJob();
    //file
    job.setFile(new Document(file));
    if (password != null)
        job.setPassword(password);
    if (newUserPassword != null)
        job.setNewUserPassword(newUserPassword);
    if (newOwnerPassword != null)
        job.setNewOwnerPassword(newOwnerPassword);
    if (encrypt != null)
        job.setEncrypt(encrypt);
    // permission
    if (allowAssembly != null)
        job.setAllowAssembly(allowAssembly);
    if (allowCopy != null)
        job.setAllowCopy(allowCopy);
    if (allowDegradedPrinting != null)
        job.setAllowDegradedPrinting(allowDegradedPrinting);
    if (allowFillIn != null)
        job.setAllowFillIn(allowFillIn);
    if (allowModifyAnnotations != null)
        job.setAllowModifyAnnotations(allowModifyAnnotations);
    if (allowPrinting != null)
        job.setAllowPrinting(allowPrinting);
    if (allowScreenReaders != null)
        job.setAllowScreenReaders(allowScreenReaders);
    if (allowSecure != null)
        job.setAllowSecure(allowSecure);

    Future<Map> future = gateway.securePdf(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.InfoController.java

/**
 * Set information about document//w  w w . j  a  va2  s  .  com
 * @param file  PDF to update
 * @param info  Map of key/values to set
 * @param password  Password to open pdf
 * @return
 * @throws InterruptedException
 * @throws java.util.concurrent.ExecutionException
 * @throws java.util.concurrent.TimeoutException
 * @throws java.io.IOException
 * @throws Exception
 */
@ApiOperation(value = "Set information about document")
@RequestMapping(value = "/info/set", method = RequestMethod.POST, produces = "application/pdf")
public ResponseEntity<byte[]> setPdfInfo(
        @ApiParam(name = "file", required = true) @RequestPart("file") MultipartFile file,
        @ApiParam(name = "info", required = true) @RequestPart("info") Map info,
        @ApiParam(name = "password", required = false) @RequestPart(value = "password", required = false) String password)
        throws InterruptedException, ExecutionException, TimeoutException, IOException, Exception {
    PdfInfoJob job = new PdfInfoJob();
    job.setFile(new Document(file));
    job.setInfo(info);
    if (password != null)
        job.setPassword(password);

    Future<Map> future = gateway.pdfInfo(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.InfoController.java

/**
 * Set information about cached document
 * @param documentId  PDF to update//from   w  ww.j  av  a2  s. c  o m
 * @param info  Map of key/values to set
 * @param password  Password to open pdf
 * @return
 * @throws InterruptedException
 * @throws java.util.concurrent.ExecutionException
 * @throws java.util.concurrent.TimeoutException
 * @throws java.io.IOException
 * @throws Exception
 */
@ApiOperation(value = "Set information about cached document")
@RequestMapping(value = "/{documentId}/info/set", method = RequestMethod.GET, produces = "application/pdf")
public ResponseEntity<byte[]> setCachedPdfInfo(
        @ApiParam(name = "documentId", required = true) @RequestPart("documentId") String documentId,
        @ApiParam(name = "info", required = true) @RequestPart("info") Map info,
        @ApiParam(name = "password", required = false) @RequestPart(value = "password", required = false) String password)
        throws InterruptedException, ExecutionException, TimeoutException, IOException, Exception {
    PdfInfoJob job = new PdfInfoJob();
    job.setDocumentId(documentId);
    job.setInfo(info);
    if (password != null)
        job.setPassword(password);

    Future<Map> future = gateway.pdfInfo(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;
}