6 Replies Latest reply on Feb 12, 2009 9:22 PM by njiang

    Problem with choice statement

    brett_brettl

      I have a question about the use of choice in Camel. I was working on an example and ran into a problem with the following route:

       

       

       

      In this example if I send a message with GetCapabilitiesRequest (the first option with our choice statement above) then the code works as expected and everything is fine. However if I send a GetMetadataRequest message then I get an error with the following message:

       

      ...

      :-1:-1: Premature end of file.

      2009-02-11 14:44:16,063 INFO org.apache.camel.processor.interceptor.TraceInterceptor[btpool0-0]

      ID-corin/52810-1234381372574/2-1 -> choice(/sim:GetCapabilitiesRequest, /sim:GetMetadataRequest) , Pattern:InOut , Properties:{CamelCauseException=org.apache.camel.RuntimeCamelException: org.xml.sax.SAXParseException: Premature end of file.} , Headers:{Content-Length=252, Host=localhost:8084, User-Agent=Jakarta Commons-HttpClient/3.1, org.apache.camel.Redelivered=true, org.apache.camel.component.http.query=null, org.apache.camel.RedeliveryCounter=2, Content-Type=text/xml; charset=ISO-8859-1, http.requestMethod=POST} , BodyType:org.apache.camel.converter.stream.StreamCacheConverter.InputStreamCache , Body:

      org.apache.camel.RuntimeCamelException: org.xml.sax.SAXParseException: Premature end of file.

      ...

       

      If I change the order of my choice statement and place the check for GetMetadataRequest first then the code will work for GetMetadataRequest and fail for GetCapabilitiesRequest. Apparently after the first when statement my message is not passed on correctly to be checked again by other when statements. Why would this be happening and what should I do to fix this?

        • 1. Re: Problem with choice statement
          njiang

          I don't know if it caused by the , can you show me the code of simpleServiceXmlBeans?

          • 2. Re: Problem with choice statement
            brett_brettl

            The simpleServiceXmlBeans is not my own code, it uses the following class.

             

             

             

            Again I am getting similar results. A messages that matches the first when statement (I tested with both the route above and a version where I swapped the two xpath statements to make sure it was not dependent on the actual message) throws no errors and a call that matches the second when statement throws the Premature end of file error.

            • 3. Re: Problem with choice statement
              janstey

              Looks like the stream from jetty is getting read to the end during the first xpath expression. Try this as a work around:

               

              <route>
                <from uri="jetty:http://localhost:8084/services/simpleService"></from> 
                <convertBodyTo type="java.lang.String"></convertBodyTo>
                <choice>
                  <when>
                    <xpath>/sim:GetCapabilitiesRequest</xpath>
                  </when>
                  <when>
                    <xpath>/sim:GetMetadataRequest</xpath>
                  </when>
                </choice>
              </route>
              

               

              Cheers,

              Jon

              • 4. Re: Problem with choice statement
                brett_brettl

                That worked. Thanks a lot. Is it common to have to convert your input like this in a Camel program, or is this an issue more specific to using jetty and xpath?

                • 5. Re: Problem with choice statement
                  janstey

                  Glad I could help. Its really not typical to have to convert input manually; Camel usually does all this for you under the covers.

                   

                  In this particular case though the message is a stream type and doesn't get reset after the first read. For these kinda scenarios we have a streamCaching() option that you can add to your route like this

                   

                  from("jetty:http://localhost:9080/myworld").streamCaching()...
                  

                   

                  which would reset the input stream at each point in the route so it could be re-read. This option does not apply to expression evaluations in the choice() processor though... it should probably be extended to support this

                  • 6. Re: Problem with choice statement
                    njiang

                    Current Camel 2.0-snapshot should enable the streamCaching() by default.

                    I just dug the code with the help of janstey's unit test.  I found the expression evaluations does't reset the cached stream. So I filled a JIRA and a quick fix is coming up.