We *_*org 0 java spring spring-mvc
我正在使用Spring-MVC应用程序,其中有某些方法可以执行创建文件预览(例如doc,docx,ppt等)的任务。现在,所有这些方法的切入点都是一个方法。我正在使用docx4j,apache-poi等多种技术。
即使经过大量测试,有时转换也会失败,这不是问题,但是来自前端的请求尚未完成,并且该选项卡最终消失了。我想做的是给切入点方法指定一个超时,因此,如果转换在20秒内没有成功,则转换过程将暂停。
在Spring-MVC中可以做类似的事情吗?
代码:
@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) {
}
}
Run Code Online (Sandbox Code Playgroud)
//以下是我想为其设置超时的入口点方法
@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;
}
Run Code Online (Sandbox Code Playgroud)
实现需求的一种方法是异步调用方法。该方法将需要返回Future或CompletableFuture(如果您使用的是Java 8)。
因此,您将获得一个带有方法注释的方法@Async(您需要@EnableAsync在配置中启用async with ):
@Async
CompletableFuture<Void> createDocxToPdfAndThenToImage(String targetLocation)
Run Code Online (Sandbox Code Playgroud)
您的电话可能是(,还有很多其他选择CompletableFuture):
CompletableFuture<Void> future= createDocxToPdfAndThenToImage(targetLocation);
future.get( 10, SECONDS)
Run Code Online (Sandbox Code Playgroud)
这将引发TimeoutExceptionif方法花费的时间超过超时值。
由于您需要返回String,因此您可以
@Async
CompletableFuture<String> createDocxToPdfAndThenToImage(String targetLocation) ....
CompletableFuture<String> future=createDocxToPdfAndThenToImage(targetLocation);
return future.get( 10, SECONDS);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3003 次 |
| 最近记录: |