diff --git a/jodconverter-web/src/main/java/cn/keking/param/ReturnResponse.java b/jodconverter-web/src/main/java/cn/keking/model/ReturnResponse.java similarity index 97% rename from jodconverter-web/src/main/java/cn/keking/param/ReturnResponse.java rename to jodconverter-web/src/main/java/cn/keking/model/ReturnResponse.java index b97539c9..6595341d 100644 --- a/jodconverter-web/src/main/java/cn/keking/param/ReturnResponse.java +++ b/jodconverter-web/src/main/java/cn/keking/model/ReturnResponse.java @@ -1,4 +1,4 @@ -package cn.keking.param; +package cn.keking.model; import java.io.Serializable; diff --git a/jodconverter-web/src/main/java/cn/keking/service/FileConverQueueTask.java b/jodconverter-web/src/main/java/cn/keking/service/FileConverQueueTask.java new file mode 100644 index 00000000..59a05e4c --- /dev/null +++ b/jodconverter-web/src/main/java/cn/keking/service/FileConverQueueTask.java @@ -0,0 +1,84 @@ +package cn.keking.service; + +import cn.keking.model.FileAttribute; +import cn.keking.model.FileType; +import cn.keking.utils.FileUtils; +import org.redisson.api.RBlockingQueue; +import org.redisson.api.RedissonClient; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; +import org.springframework.ui.ExtendedModelMap; +import javax.annotation.PostConstruct; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; + +/** + * Created by kl on 2018/1/19. + * Content :消费队列中的转换文件 + */ +@Service +public class FileConverQueueTask { + + Logger logger= LoggerFactory.getLogger(getClass()); + public static final String queueTaskName="FileConverQueueTask"; + + @Autowired + FilePreviewFactory previewFactory; + + @Autowired + RedissonClient redissonClient; + + @Autowired + FileUtils fileUtils; + + @PostConstruct + public void startTask(){ + ExecutorService executorService = Executors.newFixedThreadPool(3); + executorService.submit(new ConverTask(previewFactory,redissonClient,fileUtils)); + logger.info("队列处理文件转换任务启动完成 "); + } + + class ConverTask implements Runnable{ + + FilePreviewFactory previewFactory; + + RedissonClient redissonClient; + + FileUtils fileUtils; + + public ConverTask(FilePreviewFactory previewFactory, RedissonClient redissonClient,FileUtils fileUtils) { + this.previewFactory = previewFactory; + this.redissonClient = redissonClient; + this.fileUtils=fileUtils; + } + + @Override + public void run() { + while (true) { + try { + final RBlockingQueue queue = redissonClient.getBlockingQueue(FileConverQueueTask.queueTaskName); + String url = queue.take(); + if(url!=null){ + FileAttribute fileAttribute=fileUtils.getFileAttribute(url); + logger.info("正在处理转换任务,文件名称【{}】",fileAttribute.getName()); + FileType fileType=fileAttribute.getType(); + if(fileType.equals(FileType.compress) || fileType.equals(FileType.office)){ + FilePreview filePreview=previewFactory.get(url); + filePreview.filePreviewHandle(url,new ExtendedModelMap()); + } + } + } catch (Exception e) { + try { + Thread.sleep(1000*10); + }catch (Exception ex){ + ex.printStackTrace(); + } + e.printStackTrace(); + } + } + } + } + +} diff --git a/jodconverter-web/src/main/java/cn/keking/service/impl/CompressFilePreviewImpl.java b/jodconverter-web/src/main/java/cn/keking/service/impl/CompressFilePreviewImpl.java index 712cdf5c..347520cf 100644 --- a/jodconverter-web/src/main/java/cn/keking/service/impl/CompressFilePreviewImpl.java +++ b/jodconverter-web/src/main/java/cn/keking/service/impl/CompressFilePreviewImpl.java @@ -1,7 +1,7 @@ package cn.keking.service.impl; import cn.keking.model.FileAttribute; -import cn.keking.param.ReturnResponse; +import cn.keking.model.ReturnResponse; import cn.keking.service.FilePreview; import cn.keking.utils.DownloadUtils; import cn.keking.utils.FileUtils; diff --git a/jodconverter-web/src/main/java/cn/keking/service/impl/OfficeFilePreviewImpl.java b/jodconverter-web/src/main/java/cn/keking/service/impl/OfficeFilePreviewImpl.java index 220376d7..7aa80513 100644 --- a/jodconverter-web/src/main/java/cn/keking/service/impl/OfficeFilePreviewImpl.java +++ b/jodconverter-web/src/main/java/cn/keking/service/impl/OfficeFilePreviewImpl.java @@ -1,7 +1,7 @@ package cn.keking.service.impl; import cn.keking.model.FileAttribute; -import cn.keking.param.ReturnResponse; +import cn.keking.model.ReturnResponse; import cn.keking.service.FilePreview; import cn.keking.utils.DownloadUtils; import cn.keking.utils.FileUtils; @@ -9,6 +9,7 @@ import cn.keking.utils.OfficeToPdf; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Service; +import org.springframework.ui.ExtendedModelMap; import org.springframework.ui.Model; import org.springframework.util.StringUtils; diff --git a/jodconverter-web/src/main/java/cn/keking/service/impl/SimTextFilePreviewImpl.java b/jodconverter-web/src/main/java/cn/keking/service/impl/SimTextFilePreviewImpl.java index b2ac6b22..7a25e4b8 100644 --- a/jodconverter-web/src/main/java/cn/keking/service/impl/SimTextFilePreviewImpl.java +++ b/jodconverter-web/src/main/java/cn/keking/service/impl/SimTextFilePreviewImpl.java @@ -1,8 +1,7 @@ package cn.keking.service.impl; import cn.keking.model.FileAttribute; -import cn.keking.model.FileType; -import cn.keking.param.ReturnResponse; +import cn.keking.model.ReturnResponse; import cn.keking.service.FilePreview; import cn.keking.utils.FileUtils; import cn.keking.utils.SimTextUtil; @@ -10,8 +9,6 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.ui.Model; -import java.net.URLDecoder; - /** * Created by kl on 2018/1/17. * Content :处理文本文件 diff --git a/jodconverter-web/src/main/java/cn/keking/utils/DownloadUtils.java b/jodconverter-web/src/main/java/cn/keking/utils/DownloadUtils.java index b7bb90b2..0b00f994 100644 --- a/jodconverter-web/src/main/java/cn/keking/utils/DownloadUtils.java +++ b/jodconverter-web/src/main/java/cn/keking/utils/DownloadUtils.java @@ -1,11 +1,10 @@ package cn.keking.utils; -import cn.keking.param.ReturnResponse; +import cn.keking.model.ReturnResponse; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; import java.io.*; import java.net.*; -import java.nio.charset.Charset; import java.util.UUID; /** diff --git a/jodconverter-web/src/main/java/cn/keking/utils/SimTextUtil.java b/jodconverter-web/src/main/java/cn/keking/utils/SimTextUtil.java index 1eac8707..90396552 100644 --- a/jodconverter-web/src/main/java/cn/keking/utils/SimTextUtil.java +++ b/jodconverter-web/src/main/java/cn/keking/utils/SimTextUtil.java @@ -1,6 +1,6 @@ package cn.keking.utils; -import cn.keking.param.ReturnResponse; +import cn.keking.model.ReturnResponse; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; diff --git a/jodconverter-web/src/main/java/cn/keking/web/controller/FileController.java b/jodconverter-web/src/main/java/cn/keking/web/controller/FileController.java index bbab5b54..12a47d1c 100644 --- a/jodconverter-web/src/main/java/cn/keking/web/controller/FileController.java +++ b/jodconverter-web/src/main/java/cn/keking/web/controller/FileController.java @@ -4,7 +4,7 @@ import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; import com.google.common.collect.ImmutableMap; import com.google.common.collect.Lists; -import cn.keking.param.ReturnResponse; +import cn.keking.model.ReturnResponse; import cn.keking.utils.FileUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; diff --git a/jodconverter-web/src/main/java/cn/keking/web/controller/OnlinePreviewController.java b/jodconverter-web/src/main/java/cn/keking/web/controller/OnlinePreviewController.java index 8448382f..6b77a600 100644 --- a/jodconverter-web/src/main/java/cn/keking/web/controller/OnlinePreviewController.java +++ b/jodconverter-web/src/main/java/cn/keking/web/controller/OnlinePreviewController.java @@ -1,14 +1,20 @@ package cn.keking.web.controller; +import cn.keking.service.FileConverQueueTask; import cn.keking.service.FilePreview; import cn.keking.service.FilePreviewFactory; import org.apache.commons.io.IOUtils; +import org.redisson.api.RBlockingQueue; +import org.redisson.api.RedissonClient; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; +import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; +import org.springframework.web.bind.annotation.ResponseBody; + import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; @@ -31,6 +37,9 @@ public class OnlinePreviewController { @Autowired FilePreviewFactory previewFactory; + @Autowired + RedissonClient redissonClient; + /** * @param url * @param model @@ -38,9 +47,9 @@ public class OnlinePreviewController { */ @RequestMapping(value = "onlinePreview", method = RequestMethod.GET) public String onlinePreview(String url, Model model, HttpServletRequest req) { - req.setAttribute("fileKey",req.getParameter("fileKey")); - FilePreview filePreview=previewFactory.get(url); - return filePreview.filePreviewHandle(url,model); + req.setAttribute("fileKey", req.getParameter("fileKey")); + FilePreview filePreview = previewFactory.get(url); + return filePreview.filePreviewHandle(url, model); } /** @@ -95,4 +104,16 @@ public class OnlinePreviewController { } } + /** + * 通过api接口入队 + * @param url 请编码后在入队 + */ + @GetMapping("/addTask") + @ResponseBody + public String addQueueTask(String url) { + final RBlockingQueue queue = redissonClient.getBlockingQueue(FileConverQueueTask.queueTaskName); + queue.addAsync(url); + return "success"; + } + }