@Path("/getVersion")
@POST
@Produces(MediaType.APPLICATION_JSON)
public List getVersion(String getVersionJson) throws InterruptedException {
List outputList = new ArrayList();
try {
JSONArray jsonArr = new JSONArray(getVersionJson);
for (int j = 0; j < jsonArr.length(); j++) {
JSONObject jsonObj = jsonArr.getJSONObject(j);
String ip = jsonObj.getString("ipaddress");
String username = jsonObj.getString("username");
String password = jsonObj.getString("password");
new Thread() {
public void run() {
final String connectionStatus = getSSHConnection(ip, username, password);
if (connectionStatus.equals("Connected")) {
expect.send("bwshowver" + "\n");
if (expect.expect("$") > -1) {
String contt = "";
contt = (expect.before);
if (contt != null && contt != "") {
contt = contt.replaceAll("\n+", "\n");
contt = contt.replaceAll(" +", " ");
String splitter[] = contt.split("\n");
for (int i = 0; i < splitter.length; i++) {
if (splitter[i].contains("Patches")) {
final String patches = splitter[i];
}
if (splitter[i].contains("version")) {
outputList.add(splitter[i]);
}
}
} else {
final String v1 = "Error in version check";
System.out.println("Error in version check");
}
}
} else {
outputList.add(connectionStatus);
}
}
}.start();
}
} catch (Exception e) {
final String v = "Error";
} finally {
stopSSH();
}
Thread.sleep(20000);
return outputList;
}
I created a method using Threads. In this method, I send jsonarray, which consists of json objects. I used to create this method without threads, and it worked correctly. Now, changing it to threads, I get different results at different times. Can you suggest me where I am wrong?
source
share