فهرست منبع

消费端修改超时回调机制

wuweihao 4 سال پیش
والد
کامیت
3143c1e551

+ 0 - 13
720yun_fd_consumer/src/main/java/com/gis/ServletInitializer.java

@@ -1,13 +0,0 @@
-//package com.gis;
-//
-//import org.springframework.boot.builder.SpringApplicationBuilder;
-//import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
-//
-//public class ServletInitializer extends SpringBootServletInitializer {
-//
-//    @Override
-//    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
-//        return application.sources(ConsumerApplication.class);
-//    }
-//
-//}

+ 5 - 1
720yun_fd_consumer/src/main/java/com/gis/constant/CmdConstant.java

@@ -8,7 +8,11 @@ package com.gis.constant;
 public class CmdConstant {
 
 
-    /** krpano 全景矢量切图 */
+    /**
+     * krpano 全景矢量切图
+     *
+     * yes y | 出现提醒,强制执行
+     * */
     public final static String PANO_KRPANO = "krpanotools makepano -config=templates/vtour-multires.config ";
 
     /**

+ 84 - 17
720yun_fd_consumer/src/main/java/com/gis/listener/Fd720Listener.java

@@ -13,6 +13,9 @@ import org.springframework.amqp.rabbit.annotation.RabbitListener;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Component;
 
+import java.io.PrintWriter;
+import java.io.StringWriter;
+import java.util.concurrent.*;
 
 
 /**
@@ -39,42 +42,106 @@ public class Fd720Listener {
             e.printStackTrace();
         }
         log.warn("run Fd720Listener id: " + param);
-        cmdPano(param);
+        processPano(param);
         log.warn("end Fd720Listener id: "+ param);
     }
 
 
-    public void cmdPano(Long id) {
-        log.info("start task cmdPano");
-//        FodderEntity entity = fodderMapper.selectByPrimaryKey(id);
+    /**
+     * 切图会超时控制
+     * @param id
+     */
+    public void processPano(Long id)  {
+
+        log.warn("run processPano : " + id);
+        long start = System.currentTimeMillis();
         FodderEntity entity = fodderMapper.findById(id);
         if (entity == null) {
             log.error("场景不存在: " + id);
             // 直接结束,抛异常的话,会造成死循环,产生大量日志,而且队列也跑不下去
             return;
         }
+        String panoPath = entity.getFilePath();
+        String sceneCode = entity.getSceneCode();
+        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.getFilePath();
-            String sceneCode = entity.getSceneCode();
-            String cmd = CmdConstant.PANO_KRPANO + panoPath;
-            log.info("cmd: " + cmd);
-            CmdUtils.cmdPano(cmd);
-            long end = System.currentTimeMillis();
-            log.info("切图完成耗时: {} s" ,(end-start)/1000);
-            uploadOss(sceneCode);
+            String obj = future.get(3, TimeUnit.MINUTES); //任务处理超时时间设为 3分钟
             entity.setStatus(3);
-        }  catch (Exception e) {
+            // 上传切图
+            uploadOss(sceneCode);
+            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 {
             fodderMapper.updateByPrimaryKey(entity);
         }
-
     }
 
+
+//    public void cmdPano(Long id) {
+//        log.info("start task cmdPano");
+////        FodderEntity entity = fodderMapper.selectByPrimaryKey(id);
+//        FodderEntity entity = fodderMapper.findById(id);
+//        if (entity == null) {
+//            log.error("场景不存在: " + id);
+//            // 直接结束,抛异常的话,会造成死循环,产生大量日志,而且队列也跑不下去
+//            return;
+//        }
+//        try {
+//
+//            // 切图
+//            long start = System.currentTimeMillis();
+//            String panoPath = entity.getFilePath();
+//            String sceneCode = entity.getSceneCode();
+//            String cmd = CmdConstant.PANO_KRPANO + panoPath;
+////            log.info("cmd: " + cmd);
+//            CmdUtils.cmdPano(cmd);
+//            long end = System.currentTimeMillis();
+//            log.info("切图完成耗时: {} s" ,(end-start)/1000);
+//            uploadOss(sceneCode);
+//            entity.setStatus(3);
+//        }  catch (Exception e) {
+//            entity.setStatus(2);
+//            e.printStackTrace();
+//        } finally {
+//            fodderMapper.updateByPrimaryKey(entity);
+//        }
+//
+//    }
+
     /**
      * 上传切图目录
      */

+ 54 - 3
720yun_fd_consumer/src/main/java/com/gis/util/CmdUtils.java

@@ -1,7 +1,7 @@
 package com.gis.util;
 
 import cn.hutool.core.util.RuntimeUtil;
-import lombok.extern.log4j.Log4j2;
+import lombok.extern.slf4j.Slf4j;
 
 import java.io.BufferedReader;
 import java.io.IOException;
@@ -10,7 +10,7 @@ import java.io.InputStreamReader;
 /**
  * Created by owen on 2021/1/4 0004 14:53
  */
-@Log4j2
+@Slf4j
 public class CmdUtils {
 
 
@@ -41,7 +41,7 @@ public class CmdUtils {
      * @throws InterruptedException
      */
     public static int cmdPano(String command) throws IOException, InterruptedException {
-        log.info("run cmdPano");
+        log.info("cmd: {}", command);
 //        String[] cmd = new String[]{"/bin/sh", "-c", command};
 //        Process exec = Runtime.getRuntime().exec(cmd);
 
@@ -85,6 +85,57 @@ public class CmdUtils {
     }
 
 
+    /**
+     * 调用算法 xx.sh 脚本
+     * @param command
+     */
+//    public static void callPano(String command){
+//        log.info("cmd: " + command);
+//        try {
+//            Process process = Runtime.getRuntime().exec(command);
+//            StreamGobblerLine errorGobbler = new StreamGobblerLine(process.getErrorStream(), "ERROR");
+//            errorGobbler.start();
+//            // 200行打印一次日志
+//            StreamGobblerLine outGobbler = new StreamGobblerLine(process.getInputStream(), "STDOUT", 200);
+//            outGobbler.start();
+//            process.waitFor();
+//        } catch (Exception e) {
+//            e.printStackTrace();
+//        }
+//    }
+
+
+    public static void callLine(String command){
+        callLine(command, null);
+
+    }
+
+    /**
+     *
+     * @param command 命令
+     * @param lineSize 日志输出行数 ,可以为null
+     */
+    public static void callLine(String command, Integer lineSize){
+        log.info("cmd: " + command);
+        try {
+            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();
+        } catch (Exception e) {
+            e.printStackTrace();
+        }
+
+
+
+
+    }
+
+
 
 
 }

+ 79 - 0
720yun_fd_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();  
+            }  
+        }  
+    }  
+}

+ 1 - 0
720yun_fd_manage/remark-m.md

@@ -54,6 +54,7 @@
     13112311178    11111111Aa
     15015980188 Hao123456
     doc: http://120.25.146.52:8001/doc.html
+    rabbitMQ: http://120.25.146.52:15672/ guest  guest
     
  
 # 交换状态逻辑