camel-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Claus Ibsen <claus.ib...@gmail.com>
Subject Re: .split(beanExpression(new MyCustomIteratorFactory(), "iterator"))
Date Fri, 16 Dec 2011 08:23:23 GMT
On Fri, Dec 16, 2011 at 8:16 AM, marks1900 <marks1900-user@yahoo.com.au> wrote:
> Unfortunately, I am targeting a release of ServiceMix and I am not dealing
> with XML.
>
> ServiceMix 4.3 = Camel 2.6.0
> ServiceMix 4.4 = Camel 2.8.3
>
> The following is some sample code with some parts stripped and modified for
> this post (Warning:  I have not tested the below code).
>
> Does it look like I am heading down the correct path?
>

Yes. I suggest to develop the iterator in pure Java, and create an
unit test to test that it works as expected.
The iterator does not need to depend on neither Camel, nor SMX.


>
> +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
>
> // The Camel Route
>
> //  The input files I am dealing with are standard .txt and .csv list dumps
> and not xml.
> public void configure() throws Exception
> {
>    from(
> "file://file-item-list/general?initialDelay=1000&delay=5000&delete=true" )
>    .to( "activemq:queue:my-queue-01" )
>    .split( ExpressionBuilder.beanExpression(
>         new MyFileIteratorFactory(),  "createIterator" ) )  // split big file of
> items into smaller chunks
>    .streaming()
>    .to( "activemq:queue:my-queue-02" );
> }
>
> +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
>
> public class MyFileIteratorFactory
> {
>
>    int batchSize;
>
>    public MyFileIteratorFactory()
>    {
>        this.batchSize = 10000;
>    }
>
>    public MyFileIteratorFactory( int batchSize )
>    {
>        this.batchSize = batchSize;
>    }
>
>    public Iterator<String> createIterator( final InputStream inputStream )
>    {
>        return new MyFileIterator( inputStream, batchSize );
>    }
>
>    /**
>     * {@inheritDoc}
>     */
>    @Override
>    public String toString()
>    {
>        return "MyFileIteratorFactory []";
>    }
>
> }
>
> +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
>
> public class MyFileIterator implements Iterator<String>
> {
>
>
>
>    int batchSize;
>
>    BufferedReader bufferedReader;
>
>    String nextItemList;
>
>
>    public MyFileIterator( final InputStream inputStream, int batchSize )
>    {
>
>        bufferedReader = new BufferedReader( new InputStreamReader(
> inputStream ) );
>        this.batchSize =  batchSize;
>        tick();
>
>    }
>
>    @Override
>    public boolean hasNext()
>    {
>        return nextItemList != null;
>    }
>
>    @Override
>    public String next()
>    {
>
>        if ( null == nextItemList )
>        {
>            throw new NoSuchElementException();
>        }
>
>        String resultValue = nextItemList;
>
>        tick();
>
>        return resultValue;
>    }
>
>    @Override
>    public void remove()
>    {
>        throw new UnsupportedOperationException();
>    }
>
>    protected void tick()
>    {
>        try
>        {
>            StringBuilder output = null;
>            String currentLine;
>            int currentLineItemCountInOutput = 0;
>
>            readLoop:  while ( ( currentLine = bufferedReader.readLine() )
> != null )
>            {
>
>                if ( null == output )
>                {
>                    output = new StringBuilder();
>                }
>                else
>                {
>                    output.append( "," );
>                }
>
>                output.append( MyFileTool.processLine( currentLine ) );
>
>                currentLineItemCountInOutput =
> MyFileTool.getLineItemCountFromLineLength( output.length() );
>                if ( currentLineItemCountInOutput >=  batchSize )
>                {
>                    break readLoop;
>                }
>            }
>
>
>            if ( null == output )
>            {
>                nextItemList = null;
>                bufferedReader.close();
>            }
>            else
>            {
>                nextItemList = output.toString();
>            }
>
>        }
>        catch ( IOException e )
>        {
>            throw new IllegalArgumentException( e );
>        }
>    }
>
> }
>
> +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
>
> public final class MyFileTool
> {
>        // ...
> }
>
> +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
>
>
> --
> View this message in context: http://camel.465427.n5.nabble.com/split-Iterator-streaming-tp5078583p5079537.html
> Sent from the Camel - Users mailing list archive at Nabble.com.



-- 
Claus Ibsen
-----------------
FuseSource
Email: cibsen@fusesource.com
Web: http://fusesource.com
Twitter: davsclaus, fusenews
Blog: http://davsclaus.blogspot.com/
Author of Camel in Action: http://www.manning.com/ibsen/

Mime
View raw message