Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/350.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 从Spring@服务运行Python脚本_Java_Python_Spring - Fatal编程技术网

Java 从Spring@服务运行Python脚本

Java 从Spring@服务运行Python脚本,java,python,spring,Java,Python,Spring,我需要使用从用户界面传递的参数执行python脚本,并显示结果。我知道如何使用ProcessBuilder(如下)实现这一点,但我认为仅仅从相关的Spring@Service调用这段代码不是一个好主意(线程问题、并发运行的实例太多等等)。最好的方法是什么 @Override public String executeLatestAlgorithm(String json) { try { ProcessBuilder probuilder = new ProcessBu

我需要使用从用户界面传递的参数执行python脚本,并显示结果。我知道如何使用ProcessBuilder(如下)实现这一点,但我认为仅仅从相关的Spring@Service调用这段代码不是一个好主意(线程问题、并发运行的实例太多等等)。最好的方法是什么

@Override
public String executeLatestAlgorithm(String json) {

    try {
        ProcessBuilder probuilder = new ProcessBuilder("somescript.py", json);
        Process p = probuilder.start();
        BufferedReader in = new BufferedReader(new InputStreamReader(p.getInputStream()));
        return in.readLine();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return null;
}
(没有任何重大错误处理的垃圾代码-仅用于说明)

非常感谢,


Spring集成为执行用Python、JS、Groovy等编写的脚本提供了支持

与Python相关的部分配置XML如下所示

<service-activator input-channel="hotDrinks"
    output-channel="preparedDrinks">
    <script:script lang="python" location="file:scripts/python/barista.py">
        <script:variable name="timeToPrepare" value="5" />
    </script:script>
</service-activator>

显然,Spring集成支持执行用Python、JS、Groovy等编写的脚本

与Python相关的部分配置XML如下所示

<service-activator input-channel="hotDrinks"
    output-channel="preparedDrinks">
    <script:script lang="python" location="file:scripts/python/barista.py">
        <script:variable name="timeToPrepare" value="5" />
    </script:script>
</service-activator>