camel-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From kikou1984 <>
Subject Cache Streaming Treat large file
Date Thu, 07 Apr 2016 13:05:19 GMT

I’m working on a subject “Using Cache with Apache Camel and How to treat
large file”.

The purpose is to treat large file with camel without loading the file into
memory because it’s a huge file over 5 GO.

We found several tracks, the first track is to use the splitter component,
to allow us to read the file for example line by line or block by block,
however if we use the splitter we are not able to read again the file from
the beginning, a functional need is to be able to read some part of the file
even when the split is finished. 

So we have to use a cache system, to put blocks in the cache to reuse them.

So we thought that is was compulsory to use the class CachedOutputStream to
write on disk some part of the file after the splitter , this class also
provides the ability to encrypt data on disk.

example below :

<camelContext xmlns="" trace="false"

    <streamCaching id="myCacheConfig"  spoolDirectory="target/cachedir"

    <route id="SPLIT-FLOW" streamCache="true">
        <from uri="file:src/data/forSplitCaching\SimpleRecord?noop=true"/>
        <split streaming="true">
            <tokenize token="\n"/>
            <to uri="direct:PROCESS-BUSINESS"/>

    <route id="PROCESS-BUSINESS" streamCache="true">
        <from uri="direct:PROCESS-BUSINESS"/>
        <bean ref="ProcessBusiness" method="dealRecord"/>
                <simple>${in.header.CamelSplitComplete} == "true"</simple>
                <to uri="direct:STREAM-CACHING"/>

    <route id="STREAM-CACHING">
        <from uri="direct:STREAM-CACHING"/>
        <bean ref="ProcessStreamCaching" method="usingStream"/>
        <setHeader headerName="CamelFileName">
        <to uri="file:src/out"/>


The method dealRecord  puts each line splitted into a cache: 

public void dealRecord(Exchange exchange) throws Exception {

   String body;
   File file;
   String[] files;
   boolean isSplitComplete;

   body = (String) exchange.getIn().getBody();
   isSplitComplete = (boolean)

   CachedOutputStream cos = new CachedOutputStream(exchange, false);

   file = new File("target/cachedir");
   files = file.list();
   for (String nameTmpfile : files) {"Genered File [" + nameTmpfile + "]");


The method usingStream,can use each cache existing in the header 

public byte[] usingStream(Exchange exchange) throws InputStreamException {

   final ArrayList<CachedOutputStream> lstcache;
   byte[] bytesMessage;
   StringBuilder messageCompleteOut = new StringBuilder();
   InputStream is = null;

   lstcache = (ArrayList<CachedOutputStream>)
   for (CachedOutputStream oneCache : lstcache) {
      try {
         is = oneCache.getWrappedInputStream();
         String messageInputstream = toString(is);"Message of Cache ["+ messageInputstream +"]");
      } catch (IOException e) {
         throw new
      // On ferme le flux
   bytesMessage =
   return bytesMessage;

Is that solution seems ok  ? or maybe there is a better way ?


View this message in context:
Sent from the Camel - Users mailing list archive at

View raw message