I'm writing some routes with camel, and I want to make some transformations using a Processor. I have a properties file and it is working ok.
from(URI_LOG)
.routeId("{{PREFIX_LOG}}.prepareForMQ")
.log("Mail to: {{MAIL}}") //The value is read from a property file
.process(new ProcessorPrepareMail())
.log("${body}");
Now... I want to read the value of {{MAIL}} inside the processor but I don't know how.
I tried these things:
public class ProcessorPrepareMail implements Processor
{
@Override
public void process(Exchange exchange) throws Exception
{
//Plan A: Does not work.... I get an empty String
String mail = exchange.getProperty("MAIL", String.class);
//Plan B: Does not work.... I get the String "{{MAIL}}"
Language simple = exchange.getContext().resolveLanguage("simple");
Expression expresion = simple.createExpression("{{MAIL}}");
String valor = expresion.evaluate(exchange, String.class);
//Plan C: Does not work. It activates the default error handler
Language simple = exchange.getContext().resolveLanguage("simple");
Expression expresion = simple.createExpression("${MAIL}");
String valor = expresion.evaluate(exchange, String.class);
}
}
Can you help me?
Thanks
From a Java route, you can use a processor where you can get hold of CamelContext where you can then call the getter for global options where you can then get the property you stored there.
The CamelContext is the runtime system, which holds everything together as depicted in the figure below. The CamelContext provides access to many useful services, the most notable being components, type converters, a registry, endpoints, routes, data formats, and languages.
The context:property-placeholder tag is used to externalize properties in a separate file. It automatically configures PropertyPlaceholderConfigurer , which replaces the ${} placeholders, which are resolved against a specified properties file (as a Spring resource location).
There is API on CamelContext to do that:
String mail = exchange.getContext().resolvePropertyPlaceholders("{{MAIL}}");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With