Search code examples
javastaticapache-camelspring-camel

Camel, public static variables and processors


Into a camel processor, I have set two static vars with property names:

 public static final String CI_PROPERTY = "ci";
 public static final String IS_PDF_PROPERTY = "isPdf";

and I assign like:

 exchange.setProperty(CI_PROPERTY, documentProperties.get(MAP_PARAMETER_ATTACHMENT_URI));
 exchange.setProperty(IS_PDF_PROPERTY, documentProperties.get(MAP_PARAMETER_IS_PDF));

These names should be used in other processors to retrieve properties.

The question is: other processors have access to these names? Or I should move them to another class? In case, where?


Solution

  • Yes, you can do that as you can be seen below:

    import org.apache.camel.Exchange;
    import org.apache.camel.Processor;
    
    public class FooProcessor implements Processor {
        public static final String FOO_PROPERTY = "FOO";
        @Override
        public void process(Exchange exchange) throws Exception {
            exchange.setProperty(FOO_PROPERTY, "This is a Foo property.");
        }
    }
    
    import org.apache.camel.Exchange;
    import org.apache.camel.Processor;
    
    public class BarProcessor implements Processor {
        @Override
        public void process(Exchange exchange) throws Exception {
            exchange.getMessage().setBody(exchange.getProperty(FooProcessor.FOO_PROPERTY, String.class));
        }
    }
    
    from("direct:mainRoute")
    .routeId("MainRoute")
        .log("MainRoute BEGINS: BODY: ${body}")
        .process(new FooProcessor())
        .process(new BarProcessor())
        .log("MainRoute ENDS: BODY: ${body}")
    .end()
    ;
    

    When the route above will be run the following is logged as expected:

    MainRoute BEGINS: BODY:
    MainRoute ENDS: BODY: This is a Foo property.
    

    However I don't think processors should have compile time (nor runtime) dependencies to other processors. As usual refactor the common parts to another class which is used by the processors.