Reputation: 429
I need to execute a python script with arguments passed from my user interface and display the results. I know how to do this using ProcessBuilder (below) but I presume that just calling this code from the relevant Spring @Service isn't a good idea (threading issues, too many instances running concurrently, etc). What would be the best approach?
@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;
}
(crap code without any significant errorhandling - just to illustrate)
Many thanks,
A
Upvotes: 0
Views: 3147
Reputation: 2273
Apparently Spring Integration provides support to execute scripts written in Python, JS, Groovy etc.
https://github.com/spring-projects/spring-integration-samples/tree/master/applications/cafe-scripted
Part of the Python related config XML looks as follows
<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>
Upvotes: 2