Im trying to implement a scenario using threads to execute concurrent commands on a server. The commands just send requests to a service running on the server and in return get the required output.
The commands are generated based on reading a file, whose path the user gives as input to the application. Based every line that is in the aforementioned file, a command is generated and run on the server. Im running these commands by invoking an instance of the bash process on the linux box. A brief pseudo implementation of the above is as follows.
Class A {
public static void main(blah blah)
//take all user inputs, one of which is the location of the file that is to be read.
try {
Runnable runnable =new bckwork(//pass the inputs taken from the user//);
thread = new Thread(runnable);
thread.start();
}
catch (Exception e) {
e.printStackTrace();
}
}
and the class that does the actual work --
public class bckwork implements Runnable {
bckwork(//takes in all input that the other class passes on) {
//assigns to local variables
}
public void run() {
// TODO Auto-generated method stub
try{
Process proc = null;
proc = Runtime.getRuntime().exec("/bin/bash", null, dir);
if (proc != null) {
String cmd="";
BufferedReader in = new BufferedReader(new InputStreamReader(proc.getInputStream()));
PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(proc.getOutputStream())), true);
BufferedReader br = new BufferedReader(blah blah blah);//reads every line in the file that was passed onto this class.
String strLine;
while ((strLine = br.readLine()) != null) {
cmd=blah+blah+blah//the command is generated,whatever is read off the file is a part of the command.
out.println(cmd);
String line;
while ((line = in.readLine()).length()>1) {
line = in.readLine();
System.out.println(line);
proc.waitFor();
in.close();
out.close();
proc.destroy();
}
}
}
}catch(Exception e){}
}
}
The problem with the above pieces of code is that they run only for the first line in the file that is being read and then the executions goes into some kind of deadlock. Im guessing that the next line from the file is never being read.My intention was to kick of a thread for every line that was being read or essentially every command that was being constructed.
Any pointers on how this bug can be corrected would be greatly appreciated.