如何使用执行者和未来

时间:2014-01-02 14:14:40

标签: java plsql

嗨,我想用java来'你',所以需要一些关于这个代码块的帮助 我正在调用pl / sql中的java代码

XXX.execute(java.lang.String, java.lang.String, java.lang.String) return java.lang.String

public class XXX {


 public static String execute(String executable, String workingdir, String input) {        
      String line = "";

      try {
          Command command = new Command(input, executable, "-a", workingdir + "ndrg.gg");
          command.execute();

          line = command.getErrorOutput();
          if (line.contains("1 records processed")) {
             line = command.getOutput();
             if (line.length() > input.length() + 1) {
                line = command.getOutput().substring(input.length() + 1);
             } else {
                line = "ERROR:1- " + line;
             }
          } else {
             line = "ERROR: " + line; 
          }

      } catch (Exception ex) {
          ex.printStackTrace();
          line = "ERROR: " + ex.getMessage();
      };

      return line;
  }

}

我需要检查command.execute是否响应10秒然后XXX.execute返回ERROR我读到有关ExecutorService和Future但是...... :(

1 个答案:

答案 0 :(得分:1)

尝试这样做:

  • 实施Callable界面并将您的代码置于调用方法
  • 将您的任务提交到ExecutorService并获取Future对象的实例
  • 在Future对象上调用get。此方法将阻塞,直到返回响应,否则将抛出TimeoutException。

解决方案:

public static String execute(final String executable,
            final String workingdir, final String input) {
        ExecutorService executor = Executors.newCachedThreadPool();
        Callable<String> task = new Callable<String>() {
            public String call() throws Exception {
                // Execution in another thread.
                String line = "";
                try {
                    Command command = new Command(input, executable, "-a",
                            workingdir + "ndrg.gg");
                    command.execute();
                    line = command.getErrorOutput();
                    if (line.contains("1 records processed")) {
                        line = command.getOutput();
                        if (line.length() > input.length() + 1) {
                            line = command.getOutput().substring(
                                    input.length() + 1);
                        } else {
                            line = "ERROR:1- " + line;
                        }
                    } else {
                        line = "ERROR: " + line;
                    }
                } catch (Exception ex) {
                    ex.printStackTrace();
                    line = "ERROR: " + ex.getMessage();
                }
                return line;
            }
        };
        Future<String> future = executor.submit(task);
        String response = "";
        try {
            // Wait 10s for response.
            response = future.get(10, TimeUnit.SECONDS);
        } catch (InterruptedException e) {
            e.printStackTrace();
            // Write something meaningful
            response = "InterruptedException"
        } catch (ExecutionException e) {
            e.printStackTrace();
            // Write something meaningful
            response = "ExecutionException"
        } catch (TimeoutException e) {
            e.printStackTrace();
            // Do something if response is not ready within 10s.
            response = "TimeoutException"
        }
    return response;
    }