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-04-08 17:50:13 +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-04-08 17:50:13 +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) {
|
|
|
|
try {
|
2019-04-08 17:50:13 +08:00
|
|
|
String url = cacheService.takeQueueTask();
|
2018-01-19 14:51:18 +08:00
|
|
|
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();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|