Example usage for org.springframework.web.context.request.async WebAsyncTask WebAsyncTask

List of usage examples for org.springframework.web.context.request.async WebAsyncTask WebAsyncTask

Introduction

In this page you can find the example usage for org.springframework.web.context.request.async WebAsyncTask WebAsyncTask.

Prototype

public WebAsyncTask(long timeout, Callable<V> callable) 

Source Link

Document

Create a WebAsyncTask with a timeout value and a Callable .

Usage

From source file:com.sishuok.chapter3.web.controller.WebAsyncTaskController.java

@RequestMapping("/webAsyncTask1")
public WebAsyncTask<String> webAsyncTask1(final Model model) {
    long timeout = 10L * 1000; // 10
    WebAsyncTask webAsyncTask = new WebAsyncTask(timeout, new Callable() {
        @Override//from w  w w . ja  va  2s .  c  o  m
        public String call() throws Exception {
            Thread.sleep(2L * 1000);
            model.addAttribute("msg", "hello web async task");
            String viewName = "msg";
            return viewName;
        }
    });
    return webAsyncTask;
}

From source file:com.sishuok.chapter3.web.controller.WebAsyncTaskController.java

@RequestMapping("/webAsyncTask2")
public WebAsyncTask<String> webAsyncTask2(final Model model) {
    long timeout = 10L * 1000; // 1
    WebAsyncTask webAsyncTask = new WebAsyncTask(timeout, new Callable() {
        @Override//from ww  w  .j  a  v  a2  s .  c  om
        public String call() throws Exception {
            Thread.sleep(2L * 1000);
            throw new RuntimeException("");
        }
    });
    return webAsyncTask;
}

From source file:com.wms.studio.controller.media.MusicController.java

@RequestMapping(value = "/media/music/add", method = RequestMethod.POST)
public WebAsyncTask<Void> addMovie(Model model, MultipartFile musicFile, MusicDto musicDto) {

    log.info("[" + UserUtils.getCurrentUserId() + "]?"
            + ToStringBuilder.reflectionToString(musicDto));

    return new WebAsyncTask<>(fileUploadTime,
            new MusicAsyncCallable(model, musicFile, musicDto, filePath, musicService));
}

From source file:com.sishuok.chapter3.web.listener.ListenerController.java

@RequestMapping("/listener3")
@ResponseBody/* w w  w  .  ja v a2 s  . c  om*/
public WebAsyncTask<String> listener3() {
    long timeout = 10L * 1000; // 10
    final WebAsyncTask webAsyncTask = new WebAsyncTask(timeout, new Callable() {
        @Override
        public String call() throws Exception {
            Thread.sleep(2L * 1000);
            return "success";
        }
    });

    webAsyncTask.onTimeout(new Callable() {
        @Override
        public Object call() throws Exception {
            System.out.println("====");
            return "error";
        }
    });

    webAsyncTask.onCompletion(new Runnable() {
        @Override
        public void run() {
            System.out.println("===?");
        }
    });

    return webAsyncTask;
}

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

/**
 * get embedded metadata/*from  w w w  .  j  a  v a  2s .c o m*/
 * @param documentId
 * @return   height,width
 */
@ApiOperation(value = "Get the embedded metadata", responseClass = "java.util.Map")
@RequestMapping(value = "/info/{documentId}/metadata", method = { RequestMethod.GET })
public WebAsyncTask<Map> imageMetadataByImage(
        @ApiParam(name = "documentId", required = true, defaultValue = "8D981024-A297-4169-8603-E503CC38EEDA") @PathVariable(value = "documentId") String documentId) {
    final String _documentId = documentId;

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

            Future<Map> imageFuture = imageMetadataGateway.getMetadata(args);
            FileMetadataJob payload = (FileMetadataJob) imageFuture.get(defaultTimeout, TimeUnit.MILLISECONDS);

            return payload.getMetadata();
        }
    };

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

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

/**
 * get embedded metadata/*from   w w w. java  2  s.co m*/
 *
 * @param documentId
 * @return height, width
 */
@ApiOperation(value = "Get the embedded metadata", response = Map.class)
@RequestMapping(value = "/info/{documentId}/metadata", method = { RequestMethod.GET })
public WebAsyncTask<Map> imageMetadataByImage(
        @ApiParam(name = "documentId", required = true, defaultValue = "8D981024-A297-4169-8603-E503CC38EEDA") @PathVariable(value = "documentId") String documentId) {
    final String _documentId = documentId;

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

            Future<Map> imageFuture = imageMetadataGateway.getMetadata(args);
            FileMetadataJob payload = (FileMetadataJob) imageFuture.get(defaultTimeout, TimeUnit.MILLISECONDS);

            return payload.getMetadata();
        }
    };

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

From source file:com.sishuok.chapter3.web.listener.ListenerController.java

@RequestMapping("/listener4")
@ResponseBody/* w  ww  . j  a  v  a 2s.  c o m*/
public WebAsyncTask<String> listener4() {
    long timeout = 10L * 1000; // 10
    final WebAsyncTask webAsyncTask = new WebAsyncTask(timeout, new Callable() {
        @Override
        public String call() throws Exception {
            Thread.sleep(20L * 1000);
            return "success";
        }
    });

    webAsyncTask.onTimeout(new Callable() {
        @Override
        public Object call() throws Exception {
            System.out.println("====");
            return "error";
        }
    });

    webAsyncTask.onCompletion(new Runnable() {
        @Override
        public void run() {
            System.out.println("===?");
        }
    });

    return webAsyncTask;
}

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

/**
 * get basic info about image.//from  w w w.  ja  va2 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);
}

From source file:apiserver.services.cache.controllers.CacheDocumentController.java

/**
 * put document into cache, usable for future manipulations APIs
 *
 * @param uploadedFile uploaded file//from   w  w  w .  ja  v  a  2  s.  c o  m
 * @param tags list of metadata tags
 * @return cache ID
 */
//@ApiOperation(value = "add a document to cache", multiValueResponse = true)
@RequestMapping(method = RequestMethod.POST, consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
public WebAsyncTask<String> addDocument(
        @ApiParam(name = "uploadedFile", required = true) @RequestParam(value = "uploadedFile", required = true) MultipartFile uploadedFile,

        @ApiParam(name = "tags", required = false) @RequestParam(required = false) String[] tags)
        throws InterruptedException, TimeoutException, ExecutionException {
    final MultipartFile _file = uploadedFile;
    final String[] _tags = tags;

    Callable<String> callable = new Callable<String>() {
        @Override
        public String call() throws Exception {
            UploadDocumentJob job = new UploadDocumentJob(_file);
            job.setTags(_tags);

            Future<DocumentJob> imageFuture = documentAddGateway.addDocument(job);
            DocumentJob payload = imageFuture.get(defaultTimeout, TimeUnit.MILLISECONDS);

            return payload.getDocument().getId();
        }
    };

    return new WebAsyncTask<String>(10000, callable);
}

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

/**
 * get basic info about image.//  www.j a v  a2  s.c o m
 *
 * @param documentId cache id
 * @return height, width, pixel size, transparency
 * , @RequestPart("meta-data") Object metadata
 * <p/>
 * , @RequestParam MultipartFile file
 */
@ApiOperation(value = "Get the height and width for the image", response = Map.class)
@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);
}