Uploaded image for project: 'Camel'
  1. Camel
  2. CAMEL-20889

camel-core: Stream is not reset when Message.getBody(class) is invoked ans stream caching is enabled

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Resolved
    • Major
    • Resolution: Fixed
    • None
    • 4.7.0
    • came-core
    • None
    • Unknown

    Description

      Having the following Camel route on Camel  2.x

      from("timer:mytimer2?repeatCount=1&delay=1000")
          .routeId("generate-route-marshall")
          .streamCaching()
          .transform(constant("\{\"maskme\":\"json payload\"}"))
          .marshal().json(JsonLibrary.Jackson)
          .process(exchange ->
          {
               LOGGER.info("body class name -> " + exchange.getIn().getBody().getClass().getName());
               LOGGER.info("consumed once  using .getBody(String.class) -> " + exchange.getIn().getBody(String.class));
              LOGGER.info("consumed twice using .getBody(String.class) -> " + exchange.getIn().getBody(String.class));
          })
          .to("jms:queue:INCOMING");
      
      

       

      Shows that the consumption of the message payload  in the processor via 

      exchange.getIn().getBody(String.class));\{code}
       works as expected, as the body is shown twice on the logs, as follows.
      {code:java}
      body class name -> org.apache.camel.converter.stream.InputStreamCache
      consumed once  using .getBody(String.class) -> "\{\"maskme\":\"json payload\"}"
      consumed twice using .getBody(String.class) -> "\{\"maskme\":\"json payload\"}"
      
      

       

      But if you run this same route from  Camel 3.x to the latest 4.x one,  you will get the following output:

      body class name -> org.apache.camel.converter.stream.InputStreamCache
      consumed once  using .getBody(String.class) -> "\{\"maskme\":\"json payload\"}"
      consumed once  using .getBody(String.class) ->
      

       

      The problem is that on Camel 2.x, when
      {{ exchange.getIn().getBody(String.class))}}
      is called,  the org.apache.camel.converter.IOConverterOptimised is triggered to convert the body from org.apache.camel.converter.stream.InputStreamCache to java.lang.String, which at the beginning of the convertion, it reset the stream:

       

      IOConverterOptimised.java
      public final class IOConverterOptimised {
          private IOConverterOptimised() {
          }
      
          public static Object convertTo(final Class<?> type, final Exchange exchange, final Object value) throws Exception {
              Class fromType = value.getClass();
              if (value instanceof StreamCache) {
                  ((StreamCache)value).reset();      // <------ HERE
              }
      

      From Camel 3.x, org.apache.camel.converter.IOConverterOptimised has been dropped, and org.apache.camel.converter.IOConverter is used instead, which doesn't reset the stream.

      Attachments

        Issue Links

          Activity

            People

              ldemasi Luigi De Masi
              ldemasi Luigi De Masi
              Votes:
              0 Vote for this issue
              Watchers:
              4 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved: