Generate Multiple Files After aggregating in apache camel

Viewed 334

I have created a route that

  1. reads xml file
  2. split it
  3. aggregate it on basis of categories.After aggregation i am receiving arrays i-e array1 for category1, array2 for category2.
  4. When i write the file it results out to to print any random array from data set.

Question: How to print multiple files on basis of number of arrays generated by aggregator?

                    from("file:C:\\Users\\Desktop?fileName=books.xml&noop=true")
                    .split(xpath("/books/book"))
                    .process(new MyProcessor())
                    .setHeader("category", xpath("/book/@category").stringResult())
                    .aggregate(header("category"), new SetAggregationStrategy()).completionTimeout(500)
                    .process(new MyProcessor())
                    .convertBodyTo(String.class)
                    .to("file:C:\\Users\\Desktop\\New")
                    .end();
1 Answers

I think you just overwrite the same file with every new aggregation. To avoid this, you have to give every aggregation another (dynamic) filename to write it to.

For example to set [categoryname].xml as filename for every aggreation you can use

.setHeader(Exchange.FILE_NAME, simple("${headers.category}.xml"))

Exchange.FILE_NAME is a Camel constant for the filename header and with the Simple expression language you can concatenate the category name you set as header in your route and a static file suffix.

If the same category is aggregated over an over again, you have to extend the filename further (otherwise a category overwrites its previous file). For example with a timestamp or similar.

Related