对于某些任务,我需要实时从jenkins获取构建日志。我正在使用以下代码来检索日志。但是,这样做的问题是,由于我实时运行以下代码(运行jenkins作业时),因此我会得到部分数据,因为构建日志会随着作业的进行而不断更新。例如,假设作业从下午12点开始,持续10分钟,当下面的代码在下午12:01运行时,它只会得到部分日志,因为日志会随着作业的进行而不断追加。
有没有一种有效的方法来保持轮询并获取每个请求的所有增量。
谢谢。
public static InputStream getInputStream(){
URL urls = null;
try {
urls = new URL("https://jenkins-prod/job/Tests/job/Pipeline_Tests/lastBuild/consoleText");
} catch (MalformedURLException e) {
e.printStackTrace();
}
HttpURLConnection connection = null;
try {
connection = (HttpURLConnection) urls.openConnection();
} catch (IOException e) {
e.printStackTrace();
}
connection.setDoOutput(true);
connection.setRequestProperty("User-Agent","Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.4; en-US; rv:1.9.2.2) Gecko/20100316 Firefox/3.6.2");
System.setProperty("http.agent","Chrome");
connection.setInstanceFollowRedirects(false);
try {
connection.setRequestMethod("GET");
} catch (protocolexception e) {
e.printStackTrace();
}
connection.setRequestProperty("Content-Type","application/json");
try {
BufferedReader br = new BufferedReader(new InputStreamReader(
(connection.getInputStream())));
String line;
do {
line = br.readLine();
System.out.println(line);
System.out.flush();
} while ((line = br.readLine())!= null);
return connection.getInputStream();
} catch (IOException ex) {
ex.printStackTrace();
}
return null;
}