retatu
retatu

Reputation: 453

Problems in Splitter with Camel

I am have problems in an application...

when I split the XML in 4 parts then I want to get them all together in another XML, but it overwrites and only the last one stand

.split(body(String.class).tokenizePair("TITTLE",
        "ISSN",false)).log("Line: ${body}")
        .to("file:E:/tst")
//method that I created for use AggregatorStrategy
if (oldExchange == null) {
        System.out.println(newExchange.getIn().getBody(String.class));
        return newExchange;
    }
    String oldBody = oldExchange.getIn().getBody(String.class);
    String newBody = newExchange.getIn().getBody(String.class);
    oldExchange.getIn().setBody(oldBody + "+ " + newBody);
    System.out.println(oldExchange.getIn().getBody(String.class));
    return oldExchange;
}

System.out.println return this: Return

but in my final file just have this: Final File

Can anyone help me?

Upvotes: 0

Views: 228

Answers (2)

Vinay
Vinay

Reputation: 63

Try .to("file:E:/tst?fileExist=Append")

Upvotes: 0

Mahesh Biradar
Mahesh Biradar

Reputation: 391

You have to use splitter with aggregator pattern.

.split(body(String.class).tokenizePair("TITTLE",
        "ISSN",false), new CustomAggregatorStrategy()).log("Line: ${body}")
        .to("file:E:/tst")

CustomAggregatorStrategy should implement AggregationStrategy and it provides new exchange and old exchange in aggregate method.

Each time splitter completes the job then aggregate the new exchange and old exchange and in the end you would get list of all outputs of split processing .

Your method should look like

public Exchange aggregate(Exchange oldExchange, Exchange newExchange) {
                Message newIn = newExchange.getIn();
                Object newBody = newIn.getBody();
                ArrayList list = null;
                if (oldExchange == null) {
                        list = new ArrayList();
                        list.add(newBody);
                        newIn.setBody(list);
                        return newExchange;
                } else {
                        Message in = oldExchange.getIn();
                        list = in.getBody(ArrayList.class);
                        list.add(newBody);
                        return oldExchange;
                }
        }

Upvotes: 1

Related Questions