java - 如何使用executor和future

标签 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 返回一个错误,我读到了有关 ExecutorService 和 Future 的内容,但是...:(

最佳答案

尝试这样做:

  • 实现Callable接口(interface)并将代码放入call方法
  • 将您的任务提交至ExecutorService并获取 Future 对象的实例
  • 调用 get在 Future 对象上。该方法将阻塞直到返回响应,否则将抛出 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;
    }

关于java - 如何使用executor和future,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20884891/

相关文章:

database - PL/SQL : SQL Statement ignored?

java - 如何查找静态函数被调用 Mockito 的次数

java - 客户端-maven-插件 - verifyAssetCatalog 失败

oracle - 在 PL/SQL block 内的 SQL 中使用嵌套表变量/集合

sql - 在 sql EOF 中声明 bash 变量

oracle - 如何在 PL/SQL 对象类型中链接成员函数

java - 在 Android 应用程序中检索 Facebook 用户的个人资料图片时出现空指针

java - 在 Eclipse 中编写 Java 类时,是否可以插入指向网页的 URL 超链接?

java - 存储公钥和主 key 的安全方法是什么?

plsql - 定义包内的过程