2017-07-06 59 views
-1

一個Python腳本我需要執行從我的用戶界面傳遞參數的python腳本並顯示結果。我知道如何使用ProcessBuilder(下面)來做到這一點,但我認爲從相關的Spring @Service調用這段代碼並不是一個好主意(線程問題,併發運行的實例過多等)。最好的方法是什麼?運行從Spring @服務

@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; 
} 

(垃圾代碼,而無需任何顯著ErrorHandling中 - 只是爲了說明)

非常感謝,

一個

回答

1

顯然Spring集成提供支持,以執行用Python編寫的,JS,Groovy腳本等

https://github.com/spring-projects/spring-integration-samples/tree/master/applications/cafe-scripted

部分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>