简体   繁体   English

从j2ee应用程序启动异步Java进程的最佳方法是什么

[英]what is the best way to start a asynchronous java process from j2ee application

I am working on an j2ee application from where i need to initiate a another java process which might run for around 10 min. 我正在开发一个j2ee应用程序,从那里我需要启动另一个Java进程,该进程可能会运行10分钟左右。 As UI will timeout, i have to initiate this process in asynchronous manner. 由于UI超时,我必须以异步方式启动此过程。 I cannot use threads as i have to reuse existing code and will land up in synchronizing issues. 我不能使用线程,因为我必须重用现有代码,并且将陷入同步问题。 So, please advice me on the best way to start the new asynchronous process? 因此,请向我建议启动新异步过程的最佳方法吗?

Recently, I had to start a java process from a j2ee application. 最近,我不得不从j2ee应用程序启动Java进程。

I spwaned a new JVM and gave it all necessary (classpatch, Main class, jvm arguments, program arguments ...) for running the process. 我换了个新的JVM,并为运行该进程提供了所有必需的(classpatch,Main class,jvm参数,程序参数...)。

How to spawn a JVM method? 如何产生JVM方法?

public static Process createProcess(final String optionsAsString, final String workingDir, final String mainClass, final String[] arguments) throws IOException {
    String jvm = System.getProperty("java.home") + File.separator + "bin" + File.separator + "java";

    String[] options = optionsAsString.split(" ");
    List<String> command = new ArrayList<String>();
    command.add(jvm);
    command.addAll(Arrays.asList(options));
    command.add(mainClass);
    command.addAll(Arrays.asList(arguments));

    //System.out.println(command);

    ProcessBuilder processBuilder = new ProcessBuilder(command);
    processBuilder.directory(new File(workingDir));

    return processBuilder.start();
}

Sample usage 样品用法

public static void makeItRun() {
   try {
      // Start JVM
      String classPath = buildClassPath();
      String workingDir = getSuitableWorkingDir();//or just "."
      Process java = createProcess("-cp \"" + classPath + "\"", workingDir, my.package.APP.class.getCanonicalName(), "-the -options -of -my -APP");

      // Communicate with your APP here ...

      // Stop JVM
      java.destroy();
   } catch(Throwable t) {
      t.printStackTrace();
   }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM