2018-01-19 14:51:18 +08:00
|
|
|
|
package cn.keking.service;
|
|
|
|
|
|
|
|
|
|
import cn.keking.model.FileAttribute;
|
|
|
|
|
import cn.keking.model.FileType;
|
2019-04-08 17:50:13 +08:00
|
|
|
|
import cn.keking.service.cache.CacheService;
|
2018-01-19 14:51:18 +08:00
|
|
|
|
import cn.keking.utils.FileUtils;
|
|
|
|
|
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
|
2019-04-08 17:50:13 +08:00
|
|
|
|
CacheService cacheService;
|
2018-01-19 14:51:18 +08:00
|
|
|
|
|
|
|
|
|
@Autowired
|
|
|
|
|
FileUtils fileUtils;
|
|
|
|
|
|
|
|
|
|
@PostConstruct
|
|
|
|
|
public void startTask(){
|
|
|
|
|
ExecutorService executorService = Executors.newFixedThreadPool(3);
|
2019-06-17 14:21:16 +08:00
|
|
|
|
executorService.submit(new ConverTask(previewFactory, cacheService, fileUtils));
|
2018-01-19 14:51:18 +08:00
|
|
|
|
logger.info("队列处理文件转换任务启动完成 ");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class ConverTask implements Runnable{
|
|
|
|
|
|
|
|
|
|
FilePreviewFactory previewFactory;
|
|
|
|
|
|
2019-04-08 17:50:13 +08:00
|
|
|
|
CacheService cacheService;
|
2018-01-19 14:51:18 +08:00
|
|
|
|
|
|
|
|
|
FileUtils fileUtils;
|
|
|
|
|
|
2019-06-17 14:21:16 +08:00
|
|
|
|
public ConverTask(FilePreviewFactory previewFactory, CacheService cacheService, FileUtils fileUtils) {
|
2018-01-19 14:51:18 +08:00
|
|
|
|
this.previewFactory = previewFactory;
|
2019-04-08 17:50:13 +08:00
|
|
|
|
this.cacheService = cacheService;
|
2018-01-19 14:51:18 +08:00
|
|
|
|
this.fileUtils=fileUtils;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void run() {
|
|
|
|
|
while (true) {
|
2020-05-14 10:11:15 +08:00
|
|
|
|
String url = null;
|
2018-01-19 14:51:18 +08:00
|
|
|
|
try {
|
2020-05-14 10:11:15 +08:00
|
|
|
|
url = cacheService.takeQueueTask();
|
2019-06-17 14:21:16 +08:00
|
|
|
|
if(url != null){
|
|
|
|
|
FileAttribute fileAttribute = fileUtils.getFileAttribute(url);
|
2020-05-14 10:11:15 +08:00
|
|
|
|
FileType fileType = fileAttribute.getType();
|
|
|
|
|
logger.info("正在处理预览转换任务,url:{},预览类型:{}", url, fileType);
|
|
|
|
|
if(fileType.equals(FileType.compress) || fileType.equals(FileType.office) || fileType.equals(FileType.cad)) {
|
|
|
|
|
FilePreview filePreview = previewFactory.get(fileAttribute);
|
2019-06-17 14:21:16 +08:00
|
|
|
|
filePreview.filePreviewHandle(url, new ExtendedModelMap(), fileAttribute);
|
2020-05-14 10:11:15 +08:00
|
|
|
|
} else {
|
|
|
|
|
logger.info("预览类型无需处理,url:{},预览类型:{}", url, fileType);
|
2018-01-19 14:51:18 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
try {
|
|
|
|
|
Thread.sleep(1000*10);
|
2020-05-14 10:11:15 +08:00
|
|
|
|
} catch (Exception ex){
|
2018-01-19 14:51:18 +08:00
|
|
|
|
ex.printStackTrace();
|
|
|
|
|
}
|
2020-05-14 10:11:15 +08:00
|
|
|
|
logger.info("处理预览转换任务异常,url:{}", url, e);
|
2018-01-19 14:51:18 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|