如何按顺序执行javaFX任务和服务

如何按顺序执行javaFX任务和服务,java,javafx,sequential,Java,Javafx,Sequential,对于我的控制器类,我必须按顺序执行几个IO命令(例如:SSH、带有一些参数值的RCP命令)。每个命令都将有一定的执行时间 我必须在每个命令开始执行时更新UI控制器。 然后根据执行结果(成功还是失败),我必须再次更新UI。 然后必须以相同的步骤执行下一个命令 每个命令的执行取决于上一个命令的结果。例如, for (IOCommand command : commandsList) { // Update the UI before start the command execution

对于我的控制器类,我必须按顺序执行几个IO命令(例如:SSH、带有一些参数值的RCP命令)。每个命令都将有一定的执行时间

我必须在每个命令开始执行时更新UI控制器。 然后根据执行结果(成功还是失败),我必须再次更新UI。 然后必须以相同的步骤执行下一个命令

每个命令的执行取决于上一个命令的结果。例如,

for (IOCommand command : commandsList) {

    // Update the UI before start the command execution  
    messageTextArea.append("Command " + command.getType() + " Stated");

    boolean result = commandExecutor(command);  

    if(result) {

      // Update the UI after successful execution  
      messageTextArea.append("Command " + command.getType() + " Successfully Executed");

      // Then go to next command execution 

    } else {

      // Update the UI after failure execution  
      messageTextArea.append("Command " + command.getType() + " Failed");

      // Fix the issue and do re execution
      commandReExecutor(command);       
    }
} 

为了完成这个渐进式的UI更新,我必须使用一些与JavaFX相关的任务或服务相关的特性(否则它将挂起应用程序,直到执行完所有命令,并且它还将一次性更新UI)。但由于性质或并发性,我无法在任务或服务的帮助下以顺序方式(不是一次全部,一个接一个)执行这些命令。我如何解决这个问题。提前谢谢

我知道项目中的确切要求,可以通过任务和服务来完成。您只需要一个正确的实现。
注意事项:
1.始终使用service或Platform.runLater启动后台任务。
2.如果要更新UI,必须从任务或服务中完成。
3.将任务的进度属性绑定到进度条的进度属性,以便平滑更新。
4.类似地,将标签的文本属性绑定到任务的消息属性,以平滑更新状态或其他内容。

为了执行shell等外部命令,我编写了以下类:

package utils;

import controller.ProgressController;
import java.io.BufferedReader;
import java.io.File;
import java.io.InputStreamReader;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import javafx.concurrent.Task;
import main.Installer;

public class ProcessExecutor extends Task<Integer>
{
    Logger logger =Logger.getLogger("ProcessExecutor");
    File dir;
   String []cmd;
    String cmds;
    int exitCode=-1;
    boolean NextStepExists=false;
    Task nextStep;
    public ProcessExecutor(String...cmd )
{
    this.cmd=cmd;
    this.dir=new File(System.getProperty("user.dir"));
    this.nextStep=null;
    NextStepExists=false;
}
public ProcessExecutor(Task nextStep,String...cmd )
{
    this.cmd=cmd;
    this.dir=new File(System.getProperty("user.dir"));
    this.nextStep=nextStep;
    NextStepExists=true;
}
public ProcessExecutor(Task nextStep,File dir,String...cmd)
{
    this.cmd=cmd;
    this.dir=dir;
    this.nextStep=nextStep;
    NextStepExists=true;
}


@Override
protected final Integer call()
{
    cmds=new String();
        for(String i:cmd)
            cmds+=i+" "; // just to log cmd array

    try
    {

        logger.info("Starting new process with cmd > "+cmds);

        ProcessBuilder processBuilder=new ProcessBuilder(cmd);
        processBuilder.directory(dir);
        processBuilder.redirectErrorStream(true);
        Map<String, String> env = processBuilder.environment();
        // create custom environment 
        env.put("JAVA_HOME", "/opt/jdk1.7.0_45/"); 

        Process pr=processBuilder.start();
        BufferedReader in = new BufferedReader(new InputStreamReader(pr.getInputStream()));
           String line = in.readLine();
            while (line != null) {
                logger.log(Level.FINE,line);
                ProgressController.instance.printToConsole(line);
                line = in.readLine();
            }
            BufferedReader er = new BufferedReader(new InputStreamReader(pr.getErrorStream()));
           String erLine = in.readLine();
            while (erLine != null) {
                logger.log(Level.FINE,erLine);
                ProgressController.instance.printToConsole(erLine);
                erLine = in.readLine();
            }


        exitCode=pr.waitFor();
        exitCode=pr.exitValue();
        logger.info("Exit Value="+exitCode);
        updateMessage("Completed Process");
        if(exitCode!=0 && exitCode!=1)
        {
            logger.info("Failed to execute process commands >"+cmds+" with exit code="+exitCode);
            failed();
        }

        else
        {
            logger.info("PE succeeded()");
            if(NextStepExists)
                Installer.pool.submit(nextStep);
            succeeded();
        }

    }
    catch(Exception e)
    {
        logger.log(Level.SEVERE,"Exception: Failed to execute process commands >"+cmds,e);
        updateMessage(e.getMessage());
    }

    return new Integer(exitCode);



}
@Override
public void failed()
{
    super.failed();
    logger.log(Level.SEVERE,"Failed to execute process commands >"+cmds+"; ExitCode="+exitCode);

}
}

通过这种方式,您可以一个接一个地执行批处理文件。
Executors.newSingleThreadExecutor()负责随时执行单个任务并查询新提交的任务。

我在这里编写了一个顺序执行的通用工作示例:


这是一个NetBeans JavaFX项目,它是一个项目的通用和精简版本。
希望这有助于

查看以下中的顺序执行示例:
public ExecutorService pool=Executors.newSingleThreadExecutor();
pool.submit(new ProcessExecutor("installTomcat.bat","tomcat7"));
pool.submit(new ProcessExecutor("installPostgres.bat","postgresql","5432"));