You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@camel.apache.org by Franz Paul Forsthofer <em...@googlemail.com> on 2013/09/04 11:14:33 UTC

Improvement Suggestion for MarschallProcessor: Use CachedOutputStream instead of ByteArrayOutputStream

Hello,

the MarschallProcessor currently returns a byte array. This does mean that
large messages are kept in memory which can lead to performance problems. I
suggest to use StreamCache instead by using CachedOutputStream instead of
ByteArrayOutputStream.

To be more concret, instead of 

    public void process(Exchange exchange) throws Exception {
        ObjectHelper.notNull(dataFormat, "dataFormat");

        ByteArrayOutputStream buffer = new ByteArrayOutputStream();
        Message in = exchange.getIn();
        Object body = in.getBody();

        // lets setup the out message before we invoke the dataFormat
        // so that it can mutate it if necessary
        Message out = exchange.getOut();
        out.copyFrom(in);

        try {
            dataFormat.marshal(exchange, body, buffer);
            byte[] data = buffer.toByteArray();
            out.setBody(data);
        } catch (Exception e) {
            // remove OUT message, as an exception occurred
            exchange.setOut(null);
            throw e;
        }
    }

we should use

   public void process(Exchange exchange) throws Exception {
        ObjectHelper.notNull(dataFormat, "dataFormat");

        CachedOutputStream buffer = new CachedOutputStream(exchange,true);
        Message in = exchange.getIn();
        Object body = in.getBody();

        // lets setup the out message before we invoke the dataFormat
        // so that it can mutate it if necessary
        Message out = exchange.getOut();
        out.copyFrom(in);

        try {
            dataFormat.marshal(exchange, body, buffer);
            out.setBody(buffer.getStreamCache() );
        } catch (Exception e) {
            // remove OUT message, as an exception occurred
            exchange.setOut(null);
            throw e;
        }
    }


Regards Franz



--
View this message in context: http://camel.465427.n5.nabble.com/Improvement-Suggestion-for-MarschallProcessor-Use-CachedOutputStream-instead-of-ByteArrayOutputStream-tp5738654.html
Sent from the Camel Development mailing list archive at Nabble.com.

Re: Improvement Suggestion for MarschallProcessor: Use CachedOutputStream instead of ByteArrayOutputStream

Posted by Franz Paul Forsthofer <em...@googlemail.com>.
Hi,

I created the Jira Improvement
https://issues.apache.org/jira/browse/CAMEL-6718

Regards Franz


On Wed, Sep 4, 2013 at 11:36 AM, Claus Ibsen <cl...@gmail.com> wrote:

> Hi
>
> Good idea as from Camel 2.12 onwards we have a StreamCachingStrategy
> which allows us to safely use CachedOutputStream in components without
> forcing spooling to disk on end-users, without them knowing that.
>
> Fell free to log a JIRA ticket.
>
> On Wed, Sep 4, 2013 at 11:14 AM, Franz Paul Forsthofer
> <em...@googlemail.com> wrote:
> > Hello,
> >
> > the MarschallProcessor currently returns a byte array. This does mean
> that
> > large messages are kept in memory which can lead to performance
> problems. I
> > suggest to use StreamCache instead by using CachedOutputStream instead of
> > ByteArrayOutputStream.
> >
> > To be more concret, instead of
> >
> >     public void process(Exchange exchange) throws Exception {
> >         ObjectHelper.notNull(dataFormat, "dataFormat");
> >
> >         ByteArrayOutputStream buffer = new ByteArrayOutputStream();
> >         Message in = exchange.getIn();
> >         Object body = in.getBody();
> >
> >         // lets setup the out message before we invoke the dataFormat
> >         // so that it can mutate it if necessary
> >         Message out = exchange.getOut();
> >         out.copyFrom(in);
> >
> >         try {
> >             dataFormat.marshal(exchange, body, buffer);
> >             byte[] data = buffer.toByteArray();
> >             out.setBody(data);
> >         } catch (Exception e) {
> >             // remove OUT message, as an exception occurred
> >             exchange.setOut(null);
> >             throw e;
> >         }
> >     }
> >
> > we should use
> >
> >    public void process(Exchange exchange) throws Exception {
> >         ObjectHelper.notNull(dataFormat, "dataFormat");
> >
> >         CachedOutputStream buffer = new
> CachedOutputStream(exchange,true);
> >         Message in = exchange.getIn();
> >         Object body = in.getBody();
> >
> >         // lets setup the out message before we invoke the dataFormat
> >         // so that it can mutate it if necessary
> >         Message out = exchange.getOut();
> >         out.copyFrom(in);
> >
> >         try {
> >             dataFormat.marshal(exchange, body, buffer);
> >             out.setBody(buffer.getStreamCache() );
> >         } catch (Exception e) {
> >             // remove OUT message, as an exception occurred
> >             exchange.setOut(null);
> >             throw e;
> >         }
> >     }
> >
> >
> > Regards Franz
> >
> >
> >
> > --
> > View this message in context:
> http://camel.465427.n5.nabble.com/Improvement-Suggestion-for-MarschallProcessor-Use-CachedOutputStream-instead-of-ByteArrayOutputStream-tp5738654.html
> > Sent from the Camel Development 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
>

Re: Improvement Suggestion for MarschallProcessor: Use CachedOutputStream instead of ByteArrayOutputStream

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

Good idea as from Camel 2.12 onwards we have a StreamCachingStrategy
which allows us to safely use CachedOutputStream in components without
forcing spooling to disk on end-users, without them knowing that.

Fell free to log a JIRA ticket.

On Wed, Sep 4, 2013 at 11:14 AM, Franz Paul Forsthofer
<em...@googlemail.com> wrote:
> Hello,
>
> the MarschallProcessor currently returns a byte array. This does mean that
> large messages are kept in memory which can lead to performance problems. I
> suggest to use StreamCache instead by using CachedOutputStream instead of
> ByteArrayOutputStream.
>
> To be more concret, instead of
>
>     public void process(Exchange exchange) throws Exception {
>         ObjectHelper.notNull(dataFormat, "dataFormat");
>
>         ByteArrayOutputStream buffer = new ByteArrayOutputStream();
>         Message in = exchange.getIn();
>         Object body = in.getBody();
>
>         // lets setup the out message before we invoke the dataFormat
>         // so that it can mutate it if necessary
>         Message out = exchange.getOut();
>         out.copyFrom(in);
>
>         try {
>             dataFormat.marshal(exchange, body, buffer);
>             byte[] data = buffer.toByteArray();
>             out.setBody(data);
>         } catch (Exception e) {
>             // remove OUT message, as an exception occurred
>             exchange.setOut(null);
>             throw e;
>         }
>     }
>
> we should use
>
>    public void process(Exchange exchange) throws Exception {
>         ObjectHelper.notNull(dataFormat, "dataFormat");
>
>         CachedOutputStream buffer = new CachedOutputStream(exchange,true);
>         Message in = exchange.getIn();
>         Object body = in.getBody();
>
>         // lets setup the out message before we invoke the dataFormat
>         // so that it can mutate it if necessary
>         Message out = exchange.getOut();
>         out.copyFrom(in);
>
>         try {
>             dataFormat.marshal(exchange, body, buffer);
>             out.setBody(buffer.getStreamCache() );
>         } catch (Exception e) {
>             // remove OUT message, as an exception occurred
>             exchange.setOut(null);
>             throw e;
>         }
>     }
>
>
> Regards Franz
>
>
>
> --
> View this message in context: http://camel.465427.n5.nabble.com/Improvement-Suggestion-for-MarschallProcessor-Use-CachedOutputStream-instead-of-ByteArrayOutputStream-tp5738654.html
> Sent from the Camel Development 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