lundi 20 juin 2016

Java, Spring : Setting timeout for a method so the code returns to method end.

I am working on a Spring-MVC application in which I have certain methods which do task of creating previews of files like doc, docx, ppt, etc. Now, the entry point for all these methods is a single method. I am using multiple technologies like docx4j, apache-poi, etc.

Even after a lot of tests, sometimes the conversion fails, which is not a problem, but the request from front-end is not finished and the tab eventually dies. WHat I would like to do is to give a timeout for the entry-point method, so if the conversion doesn't succeed in 20 seconds, then the conversion process is halted.

IS there anything similar I can do in Spring-MVC.

Code :

@Service
@Transactional
public class GroupAttachmentsServiceImpl implements GroupAttachmentsService {

  @Override
    public boolean addAttachment(byte[] bytes, String fileName){
    // Attachment to file-system persistence code
  try {

                     attachment.setImageThumbnail(createFilePreviewForFiles(fileName, bytes));
                    } catch (Exception ignored) {
                    }

}

// Below is entry-point method for which I would like to set some timeout

 @Override
    public String createFilePreviewForFiles(String fileName, byte[] fileBytes) {

        try {
            if (!(fileBytes == null)) {

                String targetLocation = zipLocation + String.valueOf(new BigInteger(130, random).toString(32));

                FileUtils.writeByteArrayToFile(new File(targetLocation), fileBytes);

                String extension = fileName.substring(fileName.lastIndexOf(".") + 1, fileName.length()).toLowerCase();

                if (extension.toLowerCase().equals("pdf")) {
                    return (createPdfPreview(targetLocation));
                }

                if ((extension.toLowerCase().equals("pptx"))) {
                    return (createPPtxPreview(targetLocation));
                }

                if (extension.toLowerCase().equals("ppt")) {
                    //  return createPptPreview(targetLocation);
                    return null;
                }

                if (extension.toLowerCase().equals("doc")) {
                    return createDocToPDfAndThenToImage(targetLocation);
                    // return null;
                }

                if ((extension.toLowerCase().equals("docx"))) {
                    return (createDocxToPdfAndThenToImage(targetLocation));
                }


                if (extension.toLowerCase().equals("xls")) {
                    return (convertXlsToPDfAndToImage(targetLocation));
                }

                if (extension.toLowerCase().equals("xlsx")) {
                    return (convertXlsxToPdfAndToImage(targetLocation));
                }

                if (extension.toLowerCase().equals("png")) {
                    return createThumbNailWithoutPathAndReturnImage(fileBytes);
                }


                if (extension.toLowerCase().equals("jpg")) {
                    return createThumbNailWithoutPathAndReturnImage(fileBytes);
                }


                if (extension.toLowerCase().equals("jpeg")) {
                    return createThumbNailWithoutPathAndReturnImage(fileBytes);
                }

                if (extension.toLowerCase().equals("mp4")) {
                    return createPreviewForVideos(targetLocation);
                }

            }
        } catch (Exception ignored) {
            return "";
        }

        return null;
    }

Aucun commentaire:

Enregistrer un commentaire