lyhzzz 2 سال پیش
والد
کامیت
3892eee903
1فایلهای تغییر یافته به همراه27 افزوده شده و 12 حذف شده
  1. 27 12
      src/main/java/com/fdkankan/fusion/service/impl/ModelServiceImpl.java

+ 27 - 12
src/main/java/com/fdkankan/fusion/service/impl/ModelServiceImpl.java

@@ -1,10 +1,14 @@
 package com.fdkankan.fusion.service.impl;
 import java.io.File;
+import java.io.IOException;
 import java.util.*;
+import java.util.concurrent.CompletableFuture;
+import java.util.concurrent.ExecutorService;
 import java.util.concurrent.ThreadPoolExecutor;
 import java.util.stream.Collectors;
 
 import cn.hutool.core.io.FileUtil;
+import cn.hutool.core.thread.ThreadUtil;
 import com.alibaba.fastjson.JSONArray;
 import com.alibaba.fastjson.JSONObject;
 import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
@@ -25,6 +29,7 @@ import com.fdkankan.fusion.service.IModelService;
 import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
 import com.fdkankan.redis.util.RedisUtil;
 import lombok.SneakyThrows;
+import lombok.extern.slf4j.Slf4j;
 import org.apache.commons.lang3.StringUtils;
 import org.springframework.beans.BeanUtils;
 import org.springframework.beans.factory.annotation.Autowired;
@@ -42,6 +47,7 @@ import org.springframework.web.multipart.MultipartFile;
  * @since 2022-08-03
  */
 @Service
+@Slf4j
 public class ModelServiceImpl extends ServiceImpl<IModelMapper, Model> implements IModelService {
 
     @Autowired
@@ -113,50 +119,54 @@ public class ModelServiceImpl extends ServiceImpl<IModelMapper, Model> implement
     }
 
     public void runThread(MultipartFile file,Model model,IModelService modelService){
-        new Thread(new Runnable() {
-            @SneakyThrows
-            @Override
-            public void run() {
+        ExecutorService executor = ThreadUtil.newSingleExecutor();
+        try {
+            CompletableFuture.runAsync(() -> {
                 String fileName = UUID.randomUUID().toString().replace("-","") +".zip";
                 File newObjFile = null;
                 File objPathFile = null;
                 String objPath = String.format(FilePath.OBJ_LOCAL_PATH,environment , "modelId_"+model.getModelId()) ;
+                log.info("uploadObj--ThreadStart-fileName:{},modeId:{}",fileName,model.getModelId());
 
                 newObjFile = new File(objPath +"/" + fileName);
                 if(!newObjFile.getParentFile().exists()){
                     newObjFile.mkdirs();
                 }
-                file.transferTo(newObjFile);
+                try {
+                    file.transferTo(newObjFile);
+                } catch (IOException e) {
+                    e.printStackTrace();
+                }
                 if(fileName.toLowerCase().endsWith(".zip")){
                     ShellUtil.unZip(newObjFile.getPath(),objPath);
                 }
                 objPathFile = new File(objPath );
                 if(!objPathFile.isDirectory()){
                     setCreateStatus(model,-1);
-                    throw new BusinessException(ResultCode.UPLOAD_FILE_TYPE_ERROR);
+                    return;
                 }
                 List<File> fileList = new ArrayList<>();
                 FileWriterUtil.getCanRunList(fileList,objPathFile);
 
                 if(fileList.size() <=0){
                     setCreateStatus(model,-1);
-                    throw new BusinessException(ResultCode.UPLOAD_FILE_MSG_ERROR);
+                    return;
                 }
 
                 File file1 = fileList.get(0);
                 if(file1 == null){
                     setCreateStatus(model,-1);
-                    throw new BusinessException(ResultCode.UPLOAD_FILE_OBJ_ERROR);
+                    return;
                 }
                 if(com.fdkankan.fusion.common.util.StringUtils.isChinese(file1.getName())){
                     setCreateStatus(model,-1);
-                    throw new BusinessException(ResultCode.UPLOAD_FILE_CHINA_NAME);
+                    return;
                 }
 
                 if(file1.getName().endsWith(".b3dm")  ){
                     if(!FileWriterUtil.checkB3dmTileset(objPathFile)){
                         setCreateStatus(model,-1);
-                        throw new BusinessException(ResultCode.UPLOAD_FILE_MSG_ERROR);
+                        return;
                     }
                 }
                 redisUtil.set(RedisKeyUtil.modelUpload+model.getModelId(),"20");
@@ -240,8 +250,13 @@ public class ModelServiceImpl extends ServiceImpl<IModelMapper, Model> implement
                         FileUtil.del(mntFile.getParentFile());
                     }
                 }
-            }
-        }).start();
+            }, executor).whenComplete((reslut, e) -> {
+                log.info("uploadObj--ThreadEnd-modeId:{}",model.getModelId());
+            });
+        }catch (Exception e){
+            setCreateStatus(model,-1);
+            log.info("uploadObj--ThreadError-modeId:{},error:{}",model.getModelId(),e);
+        }
     }
 
     @Override