zoukankan      html  css  js  c++  java
  • java操作JacocClient下载dump文件

          记录瞬间

    import org.jacoco.core.data.ExecutionDataWriter;
    import org.jacoco.core.runtime.RemoteControlReader;
    import org.jacoco.core.runtime.RemoteControlWriter;
    
    import java.io.File;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.net.InetAddress;
    import java.net.Socket;
    import java.util.List;
    
    /**
     * This example connects to a coverage agent that run in output mode
     * <code>tcpserver</code> and requests execution data. The collected data is
     * dumped to a local file.
     */
    public class ExecutionDataClient {
    
        public String downLoadDump(String path, List<String> ipPort) {
            int flag = 0;
            String getDirName = getDirNum(path);
            for (String getIPPost : ipPort){
                String dirName = getDirName + "/" + getIPPost.replace(":", "_") + "_jacoco.exec";
                String ip = getIPPost.split(":")[0];
                int port = Integer.parseInt(getIPPost.split(":")[1]);
                try {
                    final FileOutputStream localFile = new FileOutputStream(dirName);
                    final ExecutionDataWriter localWriter = new ExecutionDataWriter(
                            localFile);
                    // Open a socket to the coverage agent:
                    final Socket socket = new Socket(InetAddress.getByName(ip), port);
                    final RemoteControlWriter writer = new RemoteControlWriter(
                            socket.getOutputStream());
                    final RemoteControlReader reader = new RemoteControlReader(
                            socket.getInputStream());
                    reader.setSessionInfoVisitor(localWriter);
                    reader.setExecutionDataVisitor(localWriter);
    
                    // Send a dump command and read the response:
                    writer.visitDumpCommand(true, false);
                    if (!reader.read()) {
                        throw new IOException("Socket closed unexpectedly.");
                    }
    
                    socket.close();
                    localFile.close();
                    System.out.println("Download file success....");
                    System.out.println("File path is : " + dirName);
                    flag ++;
                } catch (IOException ioe) {
                    ioe.printStackTrace();
                }
            }
            if (flag != 0) {
                return getDirName;
            } else {
                return "path";
            }
        }
    
        /**
         *
         * @param path
         * @return
         */
        private String getDirNum(String path){
            File file = null;
            String newPath = "";
            for (int i = 0 ; ; i++) {
                newPath = path + "/" + i;
                file = new File(newPath);
                System.out.println("new path = "+newPath);
                if (! file.isDirectory()) {
                    System.out.println(newPath);
                    file.mkdirs();
                    break;
                }
            }
            return newPath;
        }
    }

    记录通过Jacoco 客户端,获取远端服务器上的dump文件。

  • 相关阅读:
    如何解决aws解绑银行卡问题?
    如何解决macbook pro摄像头不工作的问题
    Window安装AutoCAD
    Mac应用程序无法打开,提示不明开发者或文件损坏的处理方法
    Android硬件抽象层(HAL)深入剖析(三)【转】
    Android硬件抽象层(HAL)深入剖析(二)【转】
    Android硬件抽象层(HAL)深入剖析(一)【转】
    Glide的用法
    Gradle-5.3:依赖-管理依赖的版本(传递(transitive)排除(exclude)强制(force)动态版本(+))
    Android 7.0 FileProvider 使用说明
  • 原文地址:https://www.cnblogs.com/wozijisun/p/10442082.html
Copyright © 2011-2022 走看看