簡體   English   中英

如何為Apache駱駝Groovy腳本組件設置屬性

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

我正在使用apache駱駝腳本組件來調用外部groovy文件。

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

我想在調用此腳本時傳遞一些屬性。 我可以使用以下簡單的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"));

但是我們如何像這樣綁定駱駝路由器中的屬性。

感謝名單

根據文檔 ,您似乎應該可以通過使用自定義GroovyShellFactory重載默認Groovy實例。

根據您提供的信息,類似這樣的內容:

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

然后將該bean添加到您的上下文中。

在駱駝中這可能是不可能的。 在類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);
}

因此,您所有的綁定都丟失了。 我猜想如果不更改駱駝本身就無法解決。

但是,如果有人對此有解決方案,我真的很想知道。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM