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.

Reply via email to