You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@camel.apache.org by Niels <ni...@kabisa.nl> on 2015/03/04 09:27:43 UTC

Safely copy exchange body

Hey there,

I was hoping I could pick some brain to gather a idea that can work. But
first let me explain my intentions.
So the idea is to save the body and headers of the exchange for auditing
purposes. 

To get this to work, we're using a Tracer and configured the destination uri
to point to a route we defined to process the exchanges. At the route we
have a custom processor which will filter out the information we need from
the exchange.

One step of the process is to convert the body of the exchange to a string.
What we've noticed, is that if we use the traced exchange and convert the
body to a string. This sometimes has an effect on the body of the original
exchange and consequently has an effect on the original routing. 
This is of course because no deep copying is performed on the exchange.

So we tried to work around the problem and see if we could come up with a
kind of deep copy of our own. We ended up trying this approach:

    private Exchange copyExchange(Exchange exchange) {
        CamelContext camelContext = exchange.getContext();

        DefaultExchangeHolder exchangeHolder =
DefaultExchangeHolder.marshal(exchange);
        Exchange newExchange = new DefaultExchange(camelContext);

        DefaultExchangeHolder.unmarshal(newExchange, exchangeHolder);

        return newExchange;
    }

This approach seemed to work fine, but we encountered some issues. When we
for example receive a GenericFile from a File component we can't unmarshal
that request. I got this issue: 

org.apache.camel.RuntimeExchangeException: Message body of type
org.apache.camel.component.file.GenericFile is not supported by this
marshaller. on the exchange: Exchange[test.txt]
    at
org.apache.camel.impl.DefaultExchangeHolder.marshal(DefaultExchangeHolder.java:93)[156:org.apache.camel.camel-core:2.13.2]
    at
org.apache.camel.impl.DefaultExchangeHolder.marshal(DefaultExchangeHolder.java:77)[156:org.apache.camel.camel-core:2.13.2]
    at
nl.kabisa.flux.transactions.TraceProcessor.copyExchange(TraceProcessor.java:53)[272:nl.kabisa.flux.transaction-logger:2.1.1.SNAPSHOT]

Eventually I only need to make sure I can convert the body safely to a
string without interfering with the original exchange body. Is there anybody
with some advise how I could accomplished that? 

PS: using Camel 2.13.2 in Karaf 2.3.6

Thanks in advance!



--
View this message in context: http://camel.465427.n5.nabble.com/Safely-copy-exchange-body-tp5763551.html
Sent from the Camel - Users mailing list archive at Nabble.com.

Re: Safely copy exchange body

Posted by Niels <ni...@kabisa.nl>.
Hey Claus,

I already feared there wasn't a generic way. 
Thanks for confirming it.



--
View this message in context: http://camel.465427.n5.nabble.com/Safely-copy-exchange-body-tp5763551p5764691.html
Sent from the Camel - Users mailing list archive at Nabble.com.

Re: Safely copy exchange body

Posted by Claus Ibsen <cl...@gmail.com>.
Hi

There is no 100% safe way for all kind of message body types.

But enabling stream caching helps.
Then files can be converted as well. But mind that for files you may
want to limit the size, in case the files are very big etc. So its
more a case by case.



On Wed, Mar 4, 2015 at 9:27 AM, Niels <ni...@kabisa.nl> wrote:
> Hey there,
>
> I was hoping I could pick some brain to gather a idea that can work. But
> first let me explain my intentions.
> So the idea is to save the body and headers of the exchange for auditing
> purposes.
>
> To get this to work, we're using a Tracer and configured the destination uri
> to point to a route we defined to process the exchanges. At the route we
> have a custom processor which will filter out the information we need from
> the exchange.
>
> One step of the process is to convert the body of the exchange to a string.
> What we've noticed, is that if we use the traced exchange and convert the
> body to a string. This sometimes has an effect on the body of the original
> exchange and consequently has an effect on the original routing.
> This is of course because no deep copying is performed on the exchange.
>
> So we tried to work around the problem and see if we could come up with a
> kind of deep copy of our own. We ended up trying this approach:
>
>     private Exchange copyExchange(Exchange exchange) {
>         CamelContext camelContext = exchange.getContext();
>
>         DefaultExchangeHolder exchangeHolder =
> DefaultExchangeHolder.marshal(exchange);
>         Exchange newExchange = new DefaultExchange(camelContext);
>
>         DefaultExchangeHolder.unmarshal(newExchange, exchangeHolder);
>
>         return newExchange;
>     }
>
> This approach seemed to work fine, but we encountered some issues. When we
> for example receive a GenericFile from a File component we can't unmarshal
> that request. I got this issue:
>
> org.apache.camel.RuntimeExchangeException: Message body of type
> org.apache.camel.component.file.GenericFile is not supported by this
> marshaller. on the exchange: Exchange[test.txt]
>     at
> org.apache.camel.impl.DefaultExchangeHolder.marshal(DefaultExchangeHolder.java:93)[156:org.apache.camel.camel-core:2.13.2]
>     at
> org.apache.camel.impl.DefaultExchangeHolder.marshal(DefaultExchangeHolder.java:77)[156:org.apache.camel.camel-core:2.13.2]
>     at
> nl.kabisa.flux.transactions.TraceProcessor.copyExchange(TraceProcessor.java:53)[272:nl.kabisa.flux.transaction-logger:2.1.1.SNAPSHOT]
>
> Eventually I only need to make sure I can convert the body safely to a
> string without interfering with the original exchange body. Is there anybody
> with some advise how I could accomplished that?
>
> PS: using Camel 2.13.2 in Karaf 2.3.6
>
> Thanks in advance!
>
>
>
> --
> View this message in context: http://camel.465427.n5.nabble.com/Safely-copy-exchange-body-tp5763551.html
> Sent from the Camel - Users mailing list archive at Nabble.com.



-- 
Claus Ibsen
-----------------
Red Hat, Inc.
Email: cibsen@redhat.com
Twitter: davsclaus
Blog: http://davsclaus.com
Author of Camel in Action: http://www.manning.com/ibsen
hawtio: http://hawt.io/
fabric8: http://fabric8.io/