2 Replies Latest reply on Jan 4, 2012 10:43 AM by rchallapalli

    Extract camel header as String

    rchallapalli

      Hi,

       

      Camel version: 2.8.3

       

      The camel route is as below..

       

      from("direct:start")                    

                .setHeader(MyConstants.MESSAGE_PATTERN,

                                    constant(MyUtil.getMessagePattern(

                                              header("action"),

                                              header("service")))

                                              .to(...)

       

      The route need to populate a new header (messagePattern) based on two existing headers action & service using method MyUtil.getMessagePattern(String, String ).

       

      Problem here is I am unable to get the value of header("action") as a String value.

      It returns the ValueBuilder and I tried below:

       

         header("action").toString()  // retunrs header(action)

       

         header("action").convertToString().toString()  // retunrs header(action)

       

      .....

       

      I need the String value of the header(...)

       

      Please help!!

       

      Thanks in advance,

      ravi

       

      Edited by: rchallapalli on Jan 4, 2012 12:14 PM

        • 1. Re: Extract camel header as String
          davsclaus

          The DSL is for building a model of the route as a kind of design time coding.

           

          So the header in the DSL will return an Expression which is used as the model to describe what header to get.

           

          Camel then takes the route model and turns that into a runtime route, where messages gets processed using Processor's.

           

          So you need to use the method call expression instead to set the header value, instead of a constant (which is a constant).

           

          .setHeader("foo").method(MyUtil.class, "getMessagePattern"))

           

          Then you can use the Camel bean parameter binding to tell Camel to pass in the headers you want at runtime. For example using the @Header annotation

           

          See details at

          http://camel.apache.org/bean-binding.html

           

          And the Camel in Action book chapter, is all about using beans with Camel.

          • 2. Re: Extract camel header as String
            rchallapalli

            Thanks Claus. Got it!