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.images.controllers.filters.GaussianController.java

/**
 * This filter performs a Gaussian blur on an uploaded image.
 *
 * @param documentId/*  ww w .ja v a  2 s .  c  om*/
 * @param radius
 * @param format
 * @return
 * @throws java.util.concurrent.TimeoutException
 * @throws java.util.concurrent.ExecutionException
 * @throws InterruptedException
 * @throws java.io.IOException
 */
@ApiOperation(value = "This filter performs a Gaussian blur on an image.")
@RequestMapping(value = "/filter/{documentId}/gaussian.{format}", method = { RequestMethod.GET })
public ResponseEntity<byte[]> imageDespeckleByFile(
        @ApiParam(name = "documentId", required = true, defaultValue = "8D981024-A297-4169-8603-E503CC38EEDA") @PathVariable(value = "documentId") String documentId,
        @ApiParam(name = "radius", required = true, defaultValue = "2") @RequestParam(required = false, defaultValue = "2") int radius,
        @ApiParam(name = "format", required = true, defaultValue = "jpg") @PathVariable(value = "format") String format

) throws TimeoutException, ExecutionException, InterruptedException, IOException {
    String _contentType = MimeType.getMimeType(format).contentType;
    if (!MimeType.getMimeType(format).isSupportedImage()) {
        return new ResponseEntity<byte[]>(HttpStatus.UNSUPPORTED_MEDIA_TYPE);
    }

    GaussianJob args = new GaussianJob();
    args.setDocumentId(documentId);
    args.setRadius(radius);

    Future<Map> imageFuture = imageFilterGaussianGateway.imageGaussianFilter(args);
    ImageDocumentJob payload = (ImageDocumentJob) imageFuture.get(defaultTimeout, TimeUnit.MILLISECONDS);

    ResponseEntity<byte[]> result = ResponseEntityHelper.processImage(payload.getBufferedImage(), _contentType,
            false);
    return result;
}

From source file:apiserver.services.images.controllers.filters.GrayscaleController.java

/**
 * This filter converts an uploaded image to a grayscale image. To do this it finds the brightness of each pixel and sets the red, green and blue of the output to the brightness value. But what is the brightness? The simplest answer might be that it is the average of the RGB components, but that neglects the way in which the human eye works. The eye is much more sensitive to green and red than it is to blue, and so we need to take less acount of the blue and more account of the green. The weighting used by GrayscaleFilter is: luma = 77R + 151G + 28B
 *
 * @param file//from  w  w w . j  a  v a 2 s.c o  m
 * @param format
 * @return
 * @throws java.util.concurrent.TimeoutException
 * @throws java.util.concurrent.ExecutionException
 * @throws InterruptedException
 * @throws java.io.IOException
 */
@ApiOperation(value = "This filter converts an image to a grayscale image. To do this it finds the brightness of each pixel and sets the red, green and blue of the output to the brightness value. But what is the brightness? The simplest answer might be that it is the average of the RGB components, but that neglects the way in which the human eye works. The eye is much more sensitive to green and red than it is to blue, and so we need to take less acount of the blue and more account of the green. The weighting used by GrayscaleFilter is: luma = 77R + 151G + 28B")
@RequestMapping(value = "/filter/grayscale", method = { RequestMethod.POST })
public ResponseEntity<byte[]> imageGrayscaleByFile(HttpServletRequest request, HttpServletResponse response,
        @ApiParam(name = "file", required = false) @RequestParam(value = "file", required = false) MultipartFile file,
        @ApiParam(name = "format", required = false) @RequestParam(value = "format", required = false) String format

) throws TimeoutException, ExecutionException, InterruptedException, IOException {
    Document _file = null;
    MimeType _outputMimeType = null;
    String _outputContentType = null;

    MultipartFile _mFile = fileUploadHelper.getFileFromRequest(file, request);
    _file = new Document(_mFile);
    _outputMimeType = fileUploadHelper.getOutputFileFormat(format, _file.getContentType());
    _outputContentType = _outputMimeType.contentType;
    if (!_file.getContentType().isSupportedImage() || !_outputMimeType.isSupportedImage()) {
        return new ResponseEntity<>(HttpStatus.UNSUPPORTED_MEDIA_TYPE);
    }

    ImageDocumentJob job = new ImageDocumentJob();
    job.setDocumentId(null);
    job.setDocument(new Document(file));

    Future<Map> imageFuture = imageFilterGrayScaleGateway.imageGrayScaleFilter(job);
    ImageDocumentJob payload = (ImageDocumentJob) imageFuture.get(defaultTimeout, TimeUnit.MILLISECONDS);

    ResponseEntity<byte[]> result = ResponseEntityHelper.processImage(payload.getBufferedImage(),
            _outputContentType, false);
    return result;
}

From source file:apiserver.services.images.controllers.filters.MaximumController.java

/**
 * This filter blurs an uploaded image very slightly using a 3x3 blur kernel.
 *
 * @param file//from   w w w. j  a  v a 2s.  com
 * @param format
 * @return
 * @throws java.util.concurrent.TimeoutException
 * @throws java.util.concurrent.ExecutionException
 * @throws InterruptedException
 * @throws java.io.IOException
 */
@ApiOperation(value = "This filter replaces each pixel by the maximum of the input pixel and its eight neighbours. Each of the RGB channels is considered separately.")
@RequestMapping(value = "/filter/maximum", method = { RequestMethod.POST })
@ResponseBody
public ResponseEntity<byte[]> imageMaximumByFile(HttpServletRequest request, HttpServletResponse response,
        @ApiParam(name = "file", required = false) @RequestParam(value = "file", required = false) MultipartFile file,
        @ApiParam(name = "format", required = false) @RequestParam(value = "format", required = false) String format

) throws TimeoutException, ExecutionException, InterruptedException, IOException {
    Document _file = null;
    MimeType _outputMimeType = null;
    String _outputContentType = null;

    MultipartFile _mFile = fileUploadHelper.getFileFromRequest(file, request);
    _file = new Document(_mFile);
    _outputMimeType = fileUploadHelper.getOutputFileFormat(format, _file.getContentType());
    _outputContentType = _outputMimeType.contentType;
    if (!_file.getContentType().isSupportedImage() || !_outputMimeType.isSupportedImage()) {
        return new ResponseEntity<>(HttpStatus.UNSUPPORTED_MEDIA_TYPE);
    }

    ImageDocumentJob job = new ImageDocumentJob();
    job.setDocumentId(null);
    job.setDocument(new Document(file));

    Future<Map> imageFuture = imageFilterMaximumGateway.imageMaximumFilter(job);
    ImageDocumentJob payload = (ImageDocumentJob) imageFuture.get(defaultTimeout, TimeUnit.MILLISECONDS);

    ResponseEntity<byte[]> result = ResponseEntityHelper.processImage(payload.getBufferedImage(),
            _outputContentType, false);
    return result;
}

From source file:apiserver.services.images.controllers.filters.MinimumController.java

/**
 * This filter replaces each pixel by the median of the input pixel and its eight neighbours. Each of the RGB channels is considered separately.
 *
 * @param file/* w  w w . ja  va 2  s  .  co  m*/
 * @param format
 * @return
 * @throws java.util.concurrent.TimeoutException
 * @throws java.util.concurrent.ExecutionException
 * @throws InterruptedException
 * @throws java.io.IOException
 */
@ApiOperation(value = "This filter replaces each pixel by the minimum of the input pixel and its eight neighbours. Each of the RGB channels is considered separately.")
@RequestMapping(value = "/filter/minimum", method = { RequestMethod.POST })
@ResponseBody
public ResponseEntity<byte[]> imageMinimumByFile(HttpServletRequest request, HttpServletResponse response,
        @ApiParam(name = "file", required = false) @RequestParam(value = "file", required = false) MultipartFile file,
        @ApiParam(name = "format", required = false) @RequestParam(value = "format", required = false) String format

) throws TimeoutException, ExecutionException, InterruptedException, IOException {
    Document _file = null;
    MimeType _outputMimeType = null;
    String _outputContentType = null;

    MultipartFile _mFile = fileUploadHelper.getFileFromRequest(file, request);
    _file = new Document(_mFile);
    _outputMimeType = fileUploadHelper.getOutputFileFormat(format, _file.getContentType());
    _outputContentType = _outputMimeType.contentType;
    if (!_file.getContentType().isSupportedImage() || !_outputMimeType.isSupportedImage()) {
        return new ResponseEntity<>(HttpStatus.UNSUPPORTED_MEDIA_TYPE);
    }

    ImageDocumentJob job = new ImageDocumentJob();
    job.setDocumentId(null);
    job.setDocument(new Document(file));

    Future<Map> imageFuture = imageFilterMinimumGateway.imageMinimumFilter(job);
    ImageDocumentJob payload = (ImageDocumentJob) imageFuture.get(defaultTimeout, TimeUnit.MILLISECONDS);

    ResponseEntity<byte[]> result = ResponseEntityHelper.processImage(payload.getBufferedImage(),
            _outputContentType, false);
    return result;
}

From source file:apiserver.services.images.controllers.filters.DespeckleController.java

/**
 * This filter reduces light noise in an image using the eight hull algorithm described in Applied Optics, Vol. 24, No. 10, 15 May 1985, "Geometric filter for Speckle Reduction", by Thomas R Crimmins. Basically, it tries to move each pixel closer in value to its neighbours. As it only has a small effect, you may need to apply it several times. This is good for removing small levels of noise from an image but does give the image some fuzziness.
 *
 * @param file/*ww w.jav a2s. c  o m*/
 * @param format
 * @return
 * @throws java.util.concurrent.TimeoutException
 * @throws java.util.concurrent.ExecutionException
 * @throws InterruptedException
 * @throws java.io.IOException
 */
@ApiOperation(value = "This filter reduces light noise in an image using the eight hull algorithm described in Applied Optics, Vol. 24, No. 10, 15 May 1985, \"Geometric filter for Speckle Reduction\", by Thomas R Crimmins. Basically, it tries to move each pixel closer in value to its neighbours. As it only has a small effect, you may need to apply it several times. This is good for removing small levels of noise from an image but does give the image some fuzziness.")
@RequestMapping(value = "/filter/despeckle", method = { RequestMethod.POST })
public ResponseEntity<byte[]> imageDespeckleByFile(HttpServletRequest request, HttpServletResponse response,
        @ApiParam(name = "file", required = false) @RequestParam(value = "file", required = false) MultipartFile file,
        @ApiParam(name = "format", required = false) @RequestParam(value = "format", required = false) String format

) throws TimeoutException, ExecutionException, InterruptedException, IOException {
    Document _file = null;
    MimeType _outputMimeType = null;
    String _outputContentType = null;

    MultipartFile _mFile = fileUploadHelper.getFileFromRequest(file, request);
    _file = new Document(_mFile);
    _outputMimeType = fileUploadHelper.getOutputFileFormat(format, _file.getContentType());
    _outputContentType = _outputMimeType.contentType;
    if (!_file.getContentType().isSupportedImage() || !_outputMimeType.isSupportedImage()) {
        return new ResponseEntity<>(HttpStatus.UNSUPPORTED_MEDIA_TYPE);
    }

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

    Future<Map> imageFuture = imageFilterDespeckleGateway.imageDespeckleFilter(job);
    ImageDocumentJob payload = (ImageDocumentJob) imageFuture.get(defaultTimeout, TimeUnit.MILLISECONDS);

    ResponseEntity<byte[]> result = ResponseEntityHelper.processImage(payload.getBufferedImage(),
            _outputContentType, false);
    return result;

}

From source file:apiserver.services.images.controllers.filters.OilController.java

/**
 * This filter replaces each pixel by the median of the input pixel and its eight neighbours. Each of the RGB channels is considered separately.
 *
 * @param documentId/*from  w  w w  .ja v  a 2  s.  c om*/
 * @param level
 * @param range
 * @param format
 * @return
 * @throws java.util.concurrent.TimeoutException
 * @throws java.util.concurrent.ExecutionException
 * @throws InterruptedException
 * @throws java.io.IOException
 */
@ApiOperation(value = "This filter produces an oil painting effect as described in the book \"Beyond Photography - The Digital Darkroom\". You can specify the smearing radius. It's quite a slow filter especially with a large radius.")
@RequestMapping(value = "/filter/{documentId}/oil.{format}", method = { RequestMethod.GET })
@ResponseBody
public ResponseEntity<byte[]> imageOilBlurByFile(
        @ApiParam(name = "documentId", required = true, defaultValue = "8D981024-A297-4169-8603-E503CC38EEDA") @PathVariable(value = "documentId") String documentId,
        @ApiParam(name = "level", required = true, defaultValue = "3") @RequestParam(value = "angle", required = false, defaultValue = "3") int level,
        @ApiParam(name = "range", required = true, defaultValue = "256") @RequestParam(value = "range", required = false, defaultValue = "256") int range,
        @ApiParam(name = "format", required = true, defaultValue = "jpg") @PathVariable(value = "format") String format

) throws TimeoutException, ExecutionException, InterruptedException, IOException {
    String _contentType = MimeType.getMimeType(format).contentType;
    if (!MimeType.getMimeType(format).isSupportedImage()) {
        return new ResponseEntity<byte[]>(HttpStatus.UNSUPPORTED_MEDIA_TYPE);
    }

    OilJob args = new OilJob();
    args.setDocumentId(documentId);
    args.setLevels(level);
    args.setRange(range);

    Future<Map> imageFuture = imageFilterOilGateway.imageOilFilter(args);
    ImageDocumentJob payload = (ImageDocumentJob) imageFuture.get(defaultTimeout, TimeUnit.MILLISECONDS);

    ResponseEntity<byte[]> result = ResponseEntityHelper.processImage(payload.getBufferedImage(), _contentType,
            false);
    return result;
}

From source file:org.apache.mina.springrpc.example.gettingstarted.AbstractHelloServiceClientTests.java

private void executeUseConcurrent(List<Callable<HelloResponse>> tasks) {
    ExecutorService executor = Executors.newFixedThreadPool(CONCURRENT_SIZE);
    List<Future<HelloResponse>> futures = new ArrayList<Future<HelloResponse>>(RUN_TIMES);

    for (Callable<HelloResponse> task : tasks) {
        Future<HelloResponse> future = executor.submit(task);
        futures.add(future);/*from   w  ww .  j av a2s  .c o  m*/
    }
    for (Future<HelloResponse> future : futures) {
        try {
            future.get(1000, TimeUnit.MILLISECONDS);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            continue;
        }
    }

    logger.info("Successful task count : " + counter.get());
}

From source file:apiserver.services.images.controllers.manipulations.ResizeController.java

/**
 * Resize an image//from www  . ja  v a 2s .c  om
 *
 * @param file
 * @param interpolation - highestQuality,highQuality,mediumQuality,highestPerformance,highPerformance,mediumPerformance,nearest,bilinear,bicubic,bessel,blackman,hamming,hanning,hermite,lanczos,mitchell,quadratic
 * @param width
 * @param height
 * @return
 */
@ApiOperation(value = "Resize an uploaded image")
@RequestMapping(value = "/modify/resize", method = { RequestMethod.POST })
@ResponseBody
public ResponseEntity<byte[]> resizeImageByImage(HttpServletRequest request, HttpServletResponse response,
        @ApiParam(name = "file", required = false) @RequestParam(value = "file", required = false) MultipartFile file,
        @ApiParam(name = "width", required = true, defaultValue = "200") @RequestParam(required = true) Integer width,
        @ApiParam(name = "height", required = true, defaultValue = "200") @RequestParam(required = true) Integer height,
        @ApiParam(name = "interpolation", required = false, defaultValue = "bicubic") @RequestParam(required = false, defaultValue = "bicubic") String interpolation,
        @ApiParam(name = "scaleToFit", required = false, defaultValue = "false") @RequestParam(required = false, defaultValue = "false") Boolean scaleToFit,
        @ApiParam(name = "format", required = false) @RequestParam(value = "format", required = false) String format

) throws IOException, InterruptedException, ExecutionException, TimeoutException {
    Document _file = null;
    MimeType _outputMimeType = null;
    String _outputContentType = null;

    MultipartFile _mFile = fileUploadHelper.getFileFromRequest(file, request);
    _file = new Document(_mFile);
    _outputMimeType = fileUploadHelper.getOutputFileFormat(format, _file.getContentType());
    _outputContentType = _outputMimeType.contentType;
    if (!_file.getContentType().isSupportedImage() || !_outputMimeType.isSupportedImage()) {
        return new ResponseEntity<>(HttpStatus.UNSUPPORTED_MEDIA_TYPE);
    }

    FileResizeJob job = new FileResizeJob();
    job.setDocumentId(null);
    job.setDocument(_file);
    //job.getDocument().setContentType(MimeType.getMimeType(file.getContentType()) );
    //job.getDocument().setFileName(file.getOriginalFilename());
    job.setWidth(width);
    job.setHeight(height);
    job.setInterpolation(interpolation.toUpperCase());
    job.setScaleToFit(scaleToFit);

    Future<Map> imageFuture = imageResizeGateway.resizeImage(job);
    FileResizeJob payload = (FileResizeJob) imageFuture.get(defaultTimeout, TimeUnit.MILLISECONDS);

    //pass CF Response back to the client
    return payload.getHttpResponse();
}

From source file:apiserver.services.images.controllers.filters.GaussianController.java

/**
 * This filter performs a Gaussian blur on an uploaded image.
 *
 * @param file/* w  w  w .j av a  2 s.c  om*/
 * @param radius
 * @param format
 * @return
 * @throws java.util.concurrent.TimeoutException
 * @throws java.util.concurrent.ExecutionException
 * @throws InterruptedException
 * @throws java.io.IOException
 */
@ApiOperation(value = "This filter performs a Gaussian blur on an image.")
@RequestMapping(value = "/filter/gaussian", method = { RequestMethod.POST })
public ResponseEntity<byte[]> imageDespeckleByFile(HttpServletRequest request, HttpServletResponse response,
        @ApiParam(name = "file", required = false) @RequestParam(value = "file", required = false) MultipartFile file,
        @ApiParam(name = "radius", required = true, defaultValue = "2") @RequestParam(required = false, defaultValue = "2") int radius,
        @ApiParam(name = "format", required = false) @RequestParam(value = "format", required = false) String format

) throws TimeoutException, ExecutionException, InterruptedException, IOException {
    Document _file = null;
    MimeType _outputMimeType = null;
    String _outputContentType = null;

    MultipartFile _mFile = fileUploadHelper.getFileFromRequest(file, request);
    _file = new Document(_mFile);
    _outputMimeType = fileUploadHelper.getOutputFileFormat(format, _file.getContentType());
    _outputContentType = _outputMimeType.contentType;
    if (!_file.getContentType().isSupportedImage() || !_outputMimeType.isSupportedImage()) {
        return new ResponseEntity<>(HttpStatus.UNSUPPORTED_MEDIA_TYPE);
    }

    GaussianJob job = new GaussianJob();
    job.setDocumentId(null);
    job.setDocument(new Document(file));
    job.setRadius(radius);

    Future<Map> imageFuture = imageFilterGaussianGateway.imageGaussianFilter(job);
    ImageDocumentJob payload = (ImageDocumentJob) imageFuture.get(defaultTimeout, TimeUnit.MILLISECONDS);

    ResponseEntity<byte[]> result = ResponseEntityHelper.processImage(payload.getBufferedImage(),
            _outputContentType, false);
    return result;
}

From source file:com.verigreen.common.concurrency.timeboundedexecuter.TimeBoundedExecuter.java

private <T> T loopWhileStillNotFinished(final Action<T> actionDelegate, long timeBoundInMillis,
        Future<T> future, TimeBoundedPolicy policy) {

    int times = 1;
    while (true) {
        try {/* ww  w. j ava  2  s.co  m*/

            return future.get(timeBoundInMillis, TimeUnit.MILLISECONDS);
        } catch (InterruptedException e) {
            // was interrupted; should cancel the execution.
            future.cancel(true);
            throw new CancellationException();
        } catch (ExecutionException e) {
            // execution ended with an exception.
            _logger.error("Catastrophic failure when executing a TimeBoundedThread! Exception details: "
                    + e.toString(), e);
            throw new RuntimeException(e);
        } catch (TimeoutException e) {
            // timed out
            reportAndActAccordingToTimeoutOption(actionDelegate, timeBoundInMillis, future, times, policy);
            times += 1;
        } catch (CancellationException e) {
            // was canceled
            throw e;
        }
    }
}