I'm using apache camel script component to call a external groovy file.
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.
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.
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.
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.
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.