0

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

1 Answer 1

2

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>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.