浏览代码

更新:
新的分支, 加入队列超时机制

wuweihao 4 年之前
父节点
当前提交
2e5e99a62b

+ 4 - 0
cms_pano_consumer/src/main/java/com/gis/constant/ConfigConstant.java

@@ -44,5 +44,9 @@ public class ConfigConstant {
     @Value("${oss.domain}")
     public  String ossDomain;
 
+    @Value("${queue.timeout}")
+    public  Integer queueTimeout;
+
+
 
 }

+ 83 - 17
cms_pano_consumer/src/main/java/com/gis/listener/PanoConsumer.java

@@ -19,6 +19,9 @@ import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Component;
 
 import java.io.IOException;
+import java.io.PrintWriter;
+import java.io.StringWriter;
+import java.util.concurrent.*;
 
 
 /**
@@ -49,41 +52,104 @@ public class PanoConsumer {
         } catch (InterruptedException e) {
             e.printStackTrace();
         }
-        cmdPano(param);
+//        cmdPano(param);
+        processPano(param);
         log.warn("end Listener PanoConsumer: "+ param);
     }
 
 
-    public void cmdPano(String id) {
-        log.info("start task cmdPano");
+    /**
+     * 切图会超时控制
+     * @param id
+     */
+    public void processPano(String id)  {
+
+        log.warn("run processPano : " + id);
+        long start = System.currentTimeMillis();
         SceneEntity entity = sceneService.findById(id);
         if (entity == null) {
             log.error("场景不存在: " + id);
             return;
         }
+        // 切图
+        String panoPath = entity.getPath();
+        String cmd = CmdConstant.PANO_KRPANO + panoPath;
+
+
+        // 超时处理机制
+        final ExecutorService exec = Executors.newFixedThreadPool(1);
+        Callable<String> call = new Callable<String>() {
+            @Override
+            public String call() throws Exception {
+                CmdUtils.callLine(cmd, 200);
+                long end = System.currentTimeMillis();
+                log.info("切图完成耗时: {} s" ,(end-start)/1000);
+                log.warn("end processListener : "+ id);
+                return "执行完成";
+            }
+        };
+        // 超时回调
+        Future<String> future = exec.submit(call);
         try {
-
-            // 切图
-            long start = System.currentTimeMillis();
-            String panoPath = entity.getPath();
-            String cmd = CmdConstant.PANO_KRPANO + panoPath;
-            log.info("cmd: " + cmd);
-            CmdUtils.cmdPano(cmd);
-            long end = System.currentTimeMillis();
-            log.info("切图完成耗时: {} s" ,(end-start)/1000);
-//            uploadOss(entity);
-            uploadOssDir(entity);
-
+            String obj = future.get(configConstant.queueTimeout, TimeUnit.MINUTES); //任务处理超时时间设为 3分钟
             entity.setStatus(3);
-        }  catch (Exception e) {
+            // 上传切图
+            uploadOssDir(entity);
+            log.info("任务成功返回: " + obj);
+        } catch (InterruptedException e) {
+            e.printStackTrace();
+        } catch (ExecutionException e) {
+            e.printStackTrace();
+            StringWriter trace=new StringWriter();
+            e.printStackTrace(new PrintWriter(trace));
+            log.error("超时了 1");
+            // 异常日志要打印,不然不会出现在日志文件中,只会出现在控制台
+            log.error(trace.toString());
             entity.setStatus(2);
+            future.cancel(true);
+        } catch (TimeoutException e) {
             e.printStackTrace();
+            StringWriter trace=new StringWriter();
+            e.printStackTrace(new PrintWriter(trace));
+            log.error("超时了 2");
+            log.error(trace.toString());
+            entity.setStatus(2);
+            future.cancel(true);
         } finally {
             sceneService.update(entity);
         }
-
     }
 
+//    public void cmdPano(String id) {
+//        log.info("start task cmdPano");
+//        SceneEntity entity = sceneService.findById(id);
+//        if (entity == null) {
+//            log.error("场景不存在: " + id);
+//            return;
+//        }
+//        try {
+//
+//            // 切图
+//            long start = System.currentTimeMillis();
+//            String panoPath = entity.getPath();
+//            String cmd = CmdConstant.PANO_KRPANO + panoPath;
+//            log.info("cmd: " + cmd);
+//            CmdUtils.cmdPano(cmd);
+//            long end = System.currentTimeMillis();
+//            log.info("切图完成耗时: {} s" ,(end-start)/1000);
+////            uploadOss(entity);
+//            uploadOssDir(entity);
+//
+//            entity.setStatus(3);
+//        }  catch (Exception e) {
+//            entity.setStatus(2);
+//            e.printStackTrace();
+//        } finally {
+//            sceneService.update(entity);
+//        }
+//
+//    }
+
 
     /**
      * 2021-04-25 新增

+ 27 - 0
cms_pano_consumer/src/main/java/com/gis/util/CmdUtils.java

@@ -85,6 +85,33 @@ public class CmdUtils {
     }
 
 
+    public static void callLine(String command) throws Exception{
+        callLine(command, null);
+
+    }
+
+    /**
+     *
+     * @param command 命令
+     * @param lineSize 日志输出行数 ,可以为null
+     */
+    public static void callLine(String command, Integer lineSize) throws Exception{
+        log.info("cmd: " + command);
+
+            Process process = Runtime.getRuntime().exec(command);
+            log.info("开始运行");
+            StreamGobbler errorGobbler = new StreamGobbler(process.getErrorStream(), "ERROR");
+            errorGobbler.start();
+            // 200行打印一次日志
+            StreamGobblerLine outGobbler = new StreamGobblerLine(process.getInputStream(), "STDOUT", lineSize);
+            outGobbler.start();
+            process.waitFor();
+
+
+    }
+
+
+
 
 
 }

+ 79 - 0
cms_pano_consumer/src/main/java/com/gis/util/StreamGobblerLine.java

@@ -0,0 +1,79 @@
+package com.gis.util;
+
+import lombok.extern.slf4j.Slf4j;
+
+import java.io.*;
+
+@Slf4j
+public class StreamGobblerLine extends Thread {
+
+	InputStream is;
+    String type;
+    OutputStream os;
+    Integer lineSize;  // 多少行打印日志一次
+
+    public StreamGobblerLine(InputStream is, String type) {
+        this(is, type, null, null);
+    }
+
+    public StreamGobblerLine(InputStream is, String type, Integer lineSize) {
+        this(is, type, null, lineSize);
+    }
+
+    StreamGobblerLine(InputStream is, String type, OutputStream redirect, Integer lineSize) {
+        this.is = is;  
+        this.type = type;  
+        this.os = redirect;
+        this.lineSize = lineSize;
+    }  
+
+    public void run() {
+        log.info("run StreamGobblerLine");
+
+        InputStreamReader isr = null;  
+        BufferedReader br = null;  
+        PrintWriter pw = null;  
+        try {  
+            if (os != null)  
+                pw = new PrintWriter(os);  
+
+            isr = new InputStreamReader(is);  
+            br = new BufferedReader(isr);  
+            String line=null;
+            int i = 1;
+            while ( (line = br.readLine()) != null) {
+                if (lineSize != null) {
+                    if (i % lineSize == 0) {
+                        log.info(type + "," + i +" : >" + line);
+                    }
+                } else {
+                    log.info(type + ","  + i +" : >" + line);
+                }
+                i++;
+
+            }
+
+            if (pw != null)  
+                pw.flush();  
+        } catch (IOException ioe) {  
+            ioe.printStackTrace();    
+        } finally{  
+            try {  
+            	if(pw!=null)
+            	{
+            		 pw.close();  
+            	}
+            	if(br!=null)
+            	{
+            		br.close();  
+            	}
+            	if(isr!=null)
+            	{
+            		isr.close();  
+            	}
+            } catch (IOException e) {  
+                e.printStackTrace();  
+            }  
+        }  
+    }  
+}

+ 2 - 0
cms_pano_consumer/src/main/resources/application-dev.properties

@@ -71,6 +71,8 @@ logging.level.com.gis=debug
 file.path=F:\\test\\ngin\\${project.name}_data\\
 server.domain=192
 
+# µ¥Î»ÊÇ·ÖÖÓ
+queue.timeout=3
 
 
 

+ 3 - 0
cms_pano_consumer/src/main/resources/application-pro.properties

@@ -65,6 +65,9 @@ logging.level.com.gis=debug
 # 掛華悵湔繚噤
 file.path=/mnt/cms_pano_fcb_data/
 
+# 等弇岆煦笘
+queue.timeout=3
+
 
 
 

+ 3 - 0
cms_pano_consumer/src/main/resources/application-sit.properties

@@ -75,6 +75,9 @@ logging.level.com.gis=debug
 file.path=/mnt/cms_pano_fcb_data/
 server.domain=192
 
+# µ¥Î»ÊÇ·ÖÖÓ
+queue.timeout=3
+
 
 
 

+ 3 - 0
cms_pano_consumer/src/main/resources/application-uat.properties

@@ -71,6 +71,9 @@ logging.level.com.gis=debug
 file.path=/mnt/cms_pano_fcb_data/
 server.domain=192
 
+# µ¥Î»ÊÇ·ÖÖÓ
+queue.timeout=3
+
 
 
 

+ 1 - 3
cms_pano_fcb/gis_service/src/main/java/com/gis/service/impl/SceneServiceImpl.java

@@ -1,6 +1,5 @@
 package com.gis.service.impl;
 
-import cn.hutool.core.img.Img;
 import cn.hutool.core.io.FileTypeUtil;
 import cn.hutool.core.io.FileUtil;
 import com.alibaba.fastjson.JSON;
@@ -33,7 +32,6 @@ import org.springframework.web.multipart.MultipartFile;
 import javax.transaction.Transactional;
 import javax.validation.constraints.NotBlank;
 import java.io.*;
-import java.time.LocalDateTime;
 import java.util.*;
 
 
@@ -120,7 +118,7 @@ public class SceneServiceImpl extends IBaseServiceImpl<SceneEntity, String> impl
 
         try {
 
-
+            // 次方法可以读取到图片内部结构
             String type = FileTypeUtil.getType(file.getInputStream());
             if (!FileUtils.getType(type).equals("image")) {
                 log.error("非图片类型");