Thread-safe way to call an external process (and grab its output stream) from inside an EJB

…衆ロ難τιáo~ 提交于 2019-12-13 08:07:52

问题


How do I call an external process in a thread safe way from inside an EJB?

ProcessBuilder is not thread safe, as stated in the javadoc. Apache commons exec says nothing about thread-safety and I am not confident in Runtime.exec either.

What's the proper way?

let me add some code so people won't think I am abusing, this code sometimes works, sometimes not

public int startTask(Logger logger, String expectPath, String expectScriptPath, long ticket) throws IOException {
    Runtime r = Runtime.getRuntime();
    Process p = r.exec(expectPath+" "+expectScriptPath+" "+ticket);

    SessionLogger sysout = new SessionLogger(logger,p.getInputStream());
    sysout.start();

    SessionLogger syserr = new SessionLogger(logger,p.getErrorStream());
    syserr.start();

    try {
        return p.waitFor();
    } catch (InterruptedException e) {
        log.error(e.getMessage(),e);
        return -1;
    }

}

please don't close or downvote this question.

I know it's not thread safe. I just want to know how do it properly from inside an EJB.


回答1:


One of the points of EJBs is precisely that you don't have to worry about concurrency, you are guaranteed that only one thread will be invoking your EJB method at any given time. ProcessBuilder doesn't have to be thread-safe, as long as you don't make it static nor share the same instance between different instances of your EJBs.



来源:https://stackoverflow.com/questions/21744848/thread-safe-way-to-call-an-external-process-and-grab-its-output-stream-from-in

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!