Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generate Multiple Files After aggregating in apache camel

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();
like image 975
MYName Avatar asked Jun 13 '20 02:06

MYName


People also ask

What is aggregation strategy in camel?

A strategy for aggregating two exchanges together into a single exchange.

What is Seda in Apache Camel?

The SEDA component provides asynchronous SEDA behavior, so that messages are exchanged on a BlockingQueue and consumers are invoked in a separate thread from the producer. Note that queues are only visible within a single CamelContext.

What is Noop in Apache Camel?

noop (consumer) If true, the file is not moved or deleted in any way. This option is good for readonly data, or for ETL type requirements. If noop=true, Camel will set idempotent=true as well, to avoid consuming the same files over and over again.


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.

like image 160
burki Avatar answered Oct 06 '22 17:10

burki