功能是要對好幾台機器下相同的command,command大概有二十多個。
不過現在發現只有某三支command每次都會有問題。
但是這情形只有在多執行序的時候才會出現,如果不用多執行序的時候就可以正常進行。
下command都會習慣在後面加 echo $?; 來檢查是否有正確回傳,
有問題的command讀取到的內容都是只能讀到第一行
Will,use,per-minute,statistics
134
以下是我的寫法:
ExecutorService executor = Executors.newFixedThreadPool(3);
List<CompletableFuture<Void>> cfs = new ArrayList<CompletableFuture<Void>>();
AppConfig.nimblesMap.forEach((k, v)->{
CompletableFuture<Void> cf = CompletableFuture.runAsync(()->{
String machineName = v.getMachineName();
Session session = new JSch().getSession(v.getUserName(), null);
session.setPassword(v.getPassWord());
Properties config = new Properties();
config.put("StrictHostKeyChecking", "no");
session.setConfig(config);
session.connect();
ChannelExec channelExec = (ChannelExec) session.openChannel("exec");
channelExec.setCommand("command"+";echo $?;");
channelExec.setInputStream(null);
channelExec.setErrStream(System.err);
channelExec.getErrStream();
channelExec.connect();
InputStream in = channelExec.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(in));
ArrayList<String> dataList = new ArrayList<String>();
String lineData;
while((lineData = br.readLine()) != null) {
lineData = lineData.replaceAll("\\s+", ",");
}
in.close();
channelExec.disconnect();
session.disconnect();
},executor);
cfs.add(CompletableFuture.allOf(cf));
});
cfs.stream().forEach(v-> v.join() );
executor.shutdown();