Spring-integration scripting with Python

1.2k views Asked by At

I'm trying to use Python with spring-integration and jython-standalone-2.7.0:

Here is my application context:

<int:inbound-channel-adapter id="in" channel="exampleChannel" >
    <int:poller fixed-rate="1000" />
    <int-script:script lang="python" location="script/message.py" />
</int:inbound-channel-adapter>

<int:channel id="exampleChannel" />
<int-ip:udp-outbound-channel-adapter id="udpOut" channel="exampleChannel" host="192.168.0.1" port="11111" />

Here is my script in Python:

print "Python"
message="python-message"

When I start the application, I see "Python" in the console. This must mean that my script is launched by spring-integration but nothing is sent in udp.

I see in the code that in org.spring.framework.integration.scripting.js223.AbstractScriptExecutor:

result = scriptEngine.eval(script, new SimpleBindings(variables));

All the Python variables are in the Map variables and scriptEngine contains no reference to Python variables.

Thus, in org.spring.framework.integration.scripting.js223.PythonScriptExecutor:

scriptEngine.get(returnVariableName);

It returns null.

Is it a problem in Jython, in Spring-integration or may be I'm doing something wrong?

1

There are 1 answers

0
Gary Russell On BEST ANSWER

This is a bug in Spring Integration; I have opened a JIRA Issue.

        if (variables != null) {
            result = scriptEngine.eval(script, new SimpleBindings(variables));
        }
        else {
            result = scriptEngine.eval(script);
        }

When the first branch of the if test is taken, the result variable is added to the SimpleBindings object, and is not added to the engine scope map.

Even though, in your case, variables is empty, we still call the first branch.

EDIT:

Here is a work-around until we fix the bug...

public class Foo {

    private final ScriptExecutor executor = ScriptExecutorFactory.getScriptExecutor("python");

    private final ScriptSource source = new ResourceScriptSource(new ClassPathResource("/message.py"));

    public String script() {
        return (String) this.executor.executeScript(source);
    }

}

and

<int:inbound-channel-adapter id="in" channel="exampleChannel" method="script">
    <int:poller fixed-rate="1000" />
    <bean class="foo.Foo" />
</int:inbound-channel-adapter>