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文件。

  • 相关阅读:
    nginx预防常见攻击
    nginx性能优化(针对于高并发量仅供参考,并不是方案)
    nginx平滑升级(1.14--1.15)
    LAMP动静分离安装(源码安装)
    洛谷-P1098 字符串的展开
    洛谷-P1086 花生采摘
    洛谷-P1042 乒乓球
    洛谷-P1031 均分纸牌
    洛谷-P1023 税收与补贴问题
    洛谷-P1125 笨小猴
  • 原文地址:https://www.cnblogs.com/wozijisun/p/10442082.html
Copyright © 2011-2022 走看看