简体   繁体   English

如何为Apache骆驼Groovy脚本组件设置属性

[英]How to set properties to apache camel groovy script component

I'm using apache camel script component to call a external groovy file. 我正在使用apache骆驼脚本组件来调用外部groovy文件。

     from("activemq:queue:test.ChooseIManger")
     .script().groovy("resource:classpath:tests/port/test.gsh")

I want to pass some properties when calling this script. 我想在调用此脚本时传递一些属性。 I can do that with simple java code as follows. 我可以使用以下简单的Java代码来做到这一点。

        Binding binding = new Binding();
        binding.setProperty("INPUTS", inputs);
        binding.setProperty("RESULT", results);

        GroovyShell shell = new GroovyShell(binding); 
        Object script = shell.evaluate(getScript("tests/port/test.gsh"));

But how we can bind properties in camel router like this. 但是我们如何像这样绑定骆驼路由器中的属性。

Thanx 感谢名单

According to the documentation it seems you should be able to overload the default Groovy instance by using a custom GroovyShellFactory. 根据文档 ,您似乎应该可以通过使用自定义GroovyShellFactory重载默认Groovy实例。

Something like this according to the information you've provided: 根据您提供的信息,类似这样的内容:

public class CustomGroovyShellFactory implements GroovyShellFactory {

  public GroovyShell createGroovyShell(Exchange exchange) {
    Binding binding = new Binding();
    binding.setProperty("INPUTS", inputs);
    binding.setProperty("RESULT", results);
    return new GroovyShell(binding);
  }
}

And then add that bean to your context. 然后将该bean添加到您的上下文中。

This may not be possible in Camel. 在骆驼中这可能是不可能的。 In method evaluate of class org.apache.camel.language.groovy.GroovyExpression , all previously set bindings simply get overridden with the Camel bindings (eg camelContext ) instead of being merged. 在类org.apache.camel.language.groovy.GroovyExpression方法evaluate中,所有先前设置的绑定都只会被Camel绑定(例如camelContext )覆盖,而不是被合并。

public <T> T evaluate(Exchange exchange, Class<T> type) {
    Script script = instantiateScript(exchange);
    script.setBinding(createBinding(exchange));
    Object value = script.run();

    return exchange.getContext().getTypeConverter().convertTo(type, value);
}

Thus all of your bindings are lost. 因此,您所有的绑定都丢失了。 I guess this cannot be resolved without changing Camel itself. 我猜想如果不更改骆驼本身就无法解决。

But, if anyone has a solution for this, I'd really like to know it. 但是,如果有人对此有解决方案,我真的很想知道。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM