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

 

posted @ 2019-02-27 10:41  wozijisun  阅读(716)  评论(0编辑  收藏  举报