Servlet循环函数调用直到while循环结果

时间:2018-05-15 16:25:20

标签: java servlets while-loop

我有这个问题:

我有一个功能正常,但需要的时间比关闭窗口所能处理的时间长。所以我有另一个函数,如果另一个窗口完成,它将检查任务列表调用。 它只检查一次所以我需要一次又一次地检查它直到完成。

检查功能:

private int getWin() {
     Process Checkprocess;
     try {
     String Checkcommand = "cmd /c tasklist /V /FI \"WINDOWTITLE eq Admin:*\"";
        Checkprocess = Runtime.getRuntime().exec(Checkcommand);
        String Checkline;
        String Checkval="PID";

        is = new BufferedReader(new InputStreamReader(Checkprocess.getInputStream()));
         while((Checkline = is.readLine()) != null) {
             retval = Checkline.contains(Checkval);
             if (retval)
             test = 1;
             else
             test = 0;
         }
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return test;     
 }

如果为false,则应返回0;如果存在具有该标题的窗口,则返回1。

整个来源:

 import javax.servlet.RequestDispatcher;
 import javax.servlet.ServletException;
 import javax.servlet.http.HttpServlet;
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;

 import java.io.BufferedReader;
 import java.io.File;
 import java.io.FileReader;
 import java.io.IOException;
 import java.io.InputStreamReader;
 import java.io.PrintWriter;
 import java.sql.Timestamp;
 import java.text.SimpleDateFormat;
 import java.util.concurrent.TimeUnit;



 public class RunCommand extends HttpServlet {

/**
 * 
 */
private static final long serialVersionUID = 5711290708294275382L;
String commands= null;
//this could be set to a specific directory, if desired
File dir = null;
BufferedReader is = null;
BufferedReader es = null;
boolean retval;
int test;
private BufferedReader outfile;

 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
     performTask(request, response);
 }

 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {

        performTask(request, response);
 }


 private int getWin() {
     Process Checkprocess;
     try {
     String Checkcommand = "cmd /c tasklist /V /FI \"WINDOWTITLE eq Admin:*\"";
        Checkprocess = Runtime.getRuntime().exec(Checkcommand);
        String Checkline;
        String Checkval="PID";


        is = new BufferedReader(new InputStreamReader(Checkprocess.getInputStream()));
         while((Checkline = is.readLine()) != null) {
             retval = Checkline.contains(Checkval);
             if(retval)
             test = 1;
             else
             test = 0;

             System.out.println("HERE test" + retval);

         }
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return test;     
 }

private void performTask (HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {  
    response.setContentType("text/html;charset=UTF-8");
    PrintWriter out= response.getWriter();
    try
{
    String commandbuild = request.getParameter("commandinput");
    String commandfilename = request.getParameter("commandfile");

    final SimpleDateFormat datefor = new SimpleDateFormat("ddMMyyyy_HHmmss");
    Timestamp timestamp = new Timestamp(System.currentTimeMillis());
    String timeadd = datefor.format(timestamp);
    System.out.println(timeadd);
    String completefilename = "C:\\tmp\\" + commandfilename +"_"+ timeadd + ".txt ";

    String commandfull = "db2 -tvz "+ completefilename + commandbuild ;

    commands= "cmd /c db2cwadmin.bat " + commandfull;
    //System.out.println(commands);
    Process process;
    if (dir != null)
        process = Runtime.getRuntime().exec(commands, null, dir);
    else
        process = Runtime.getRuntime().exec(commands);
    String line;
    StringBuilder data = new StringBuilder();

    is = new BufferedReader(new InputStreamReader(process.getInputStream()));
    data.append("Command: " + commands);
    data.append("\n\n\n");


    while((line = is.readLine()) != null) {
        //System.out.println(line);

        data.append(line);
        data.append("\n");

    }
    es = new BufferedReader(new InputStreamReader(process.getErrorStream()));
    while((line = es.readLine()) != null)
        System.err.println(line);


    int exitCode = process.waitFor();
    if (exitCode == 0) {
        System.out.println("It worked");

        while(test != 1) {
            getWin();
            if (test == 1)
            break;
        }

        Process EndProcess;
        String Endcommands = "taskkill /F /FI \"WINDOWTITLE eq Admin:*\"";
        EndProcess = Runtime.getRuntime().exec(Endcommands);
        int exitCodeEnd = EndProcess.waitFor();
        System.out.println(exitCodeEnd);

        //TimeUnit.SECONDS.sleep(10);


        data.append("Window closed!");
        data.append("\n");
        data.append("\n");


        outfile = new BufferedReader(new FileReader(completefilename));
        String outfilelines;

        while( (outfilelines = outfile.readLine()) != null ) {
            data.append(outfilelines);
            data.append("\n");

        }

    request.setAttribute("data", data);
    out.append(data);
    RequestDispatcher dispatcher= request.getRequestDispatcher("runCommand.jsp");
    dispatcher.forward(request, response);

    }
    else
        System.out.println("Something bad happend. Exit code: " + exitCode);


} //try
catch(Exception e)
{
    System.out.println("Something when wrong: " + e.getMessage());
    e.printStackTrace();
} //catch
finally
{
    if (is != null)
        try { is.close(); } catch (IOException e) {}
    if (es != null)
        try { es.close(); } catch (IOException e) {}
} //finally


 }}

问题是,即使getWin返回true(有一个println()来检查),while循环也没有退出,并且prog没有继续。此外,它返回true而不是继续并返回几个false而不是true,依此类推

当test为1时,如何让while循环退出?

由于

1 个答案:

答案 0 :(得分:3)

  

当test为1时,如何让while循环退出?

您可以使用break

  if (retval){
   test = 1;
   break; //exit loop
  }else{
   test = 0;
  }