Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use property placeholder inside a Camel Processor

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

like image 453
Desenfoque Avatar asked Aug 04 '16 16:08

Desenfoque


People also ask

How do you buy a Camel property?

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.

What is CamelContext in Apache Camel?

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.

What is placeholder Spring?

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).


1 Answers

There is API on CamelContext to do that:

String mail = exchange.getContext().resolvePropertyPlaceholders("{{MAIL}}");
like image 107
Claus Ibsen Avatar answered Oct 06 '22 00:10

Claus Ibsen