Is it possible to use Spring Batch to run Python programs? Jython is not an option (because of dependency on many Python libraries).
2 Answers
The SystemCommandTasklet allows you to execute any shell command you want including a python script if you'd like. You can read the documentation here: http://docs.spring.io/spring-batch/apidocs/org/springframework/batch/core/step/tasklet/SystemCommandTasklet.html
Comments
You can call the python progam as an executable.
A toy python program to do so is:
import java.io.*;
class Interact {
static public void main(String[] args) throws IOException {
Process p = Runtime.getRuntime().exec("python3 python_program.py arg1 arg2");
BufferedReader in = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line;
while ((line = in.readLine()) != null) {
System.out.println(line);
}
}
}
if python_program.py is the following
import sys
if __name__ == "__main__"
print("my args are",sys.argv[1:])
the result should be
my args are ['arg1', 'arg2']
of course on a more complete program you should read the stderr, check the return code etc...
1 Comment
Michael Minella
This doesn't address executing Python code from Spring Batch.