【问题标题】:Spring-integration scripting with Python使用 Python 编写 Spring 集成脚本
【发布时间】:2015-06-23 02:55:52
【问题描述】:

我正在尝试将 Python 与 spring-integration 和 jython-standalone-2.7.0 一起使用:

这是我的应用程序上下文:

<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" />

这是我的 Python 脚本:

print "Python"
message="python-message"

当我启动应用程序时,我在控制台中看到“Python”。这一定意味着我的脚本是由 spring-integration 启动的,但 udp 中没有发送任何内容。

我在代码中看到org.spring.framework.integration.scripting.js223.AbstractScriptExecutor

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

所有 Python 变量都在 Map 变量中,并且 scriptEngine 不包含对 Python 变量的引用。

因此,在org.spring.framework.integration.scripting.js223.PythonScriptExecutor:

scriptEngine.get(returnVariableName);

它返回空值。

这是 Jython、Spring 集成中的问题还是我做错了什么?

【问题讨论】:

  • 我不确定问题出在哪里,但我转载了;调查...

标签: python spring-integration jython


【解决方案1】:

这是 Spring Integration 中的一个错误;我开了一个JIRA Issue

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

当进行if 测试的第一个分支时,结果变量被添加到SimpleBindings 对象中,而不是添加到引擎范围映射中。

即使在你的情况下,变量是空的,我们仍然调用第一个分支。

编辑

在我们修复错误之前,这是一个解决方法...

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

}

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

【讨论】:

    猜你喜欢
    • 2017-08-19
    • 2010-11-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-07
    • 1970-01-01
    相关资源
    最近更新 更多