I am new to Talend and I am creating a bean, to aggregate Four messages to One . I am using cFile componnent which consume 4 files from the directory, followed by aggregrator and then processor. The code for the aggregated Bean is as follow:
package beans;
import java.util.HashSet;
import java.util.Set;
import org.apache.camel.Exchange;
import org.apache.camel.processor.aggregate.AggregationStrategy;
public class AggregateBody implements AggregationStrategy{
public Exchange aggregate(Exchange oldEx, Exchange newEx) {
Set<String> fileNameSet = new HashSet<String>();
Set<String> bodySet = new HashSet<String>();
if(oldEx==null){
fileNameSet.add((String) newEx.getIn().getHeader("fileName"));
bodySet.add(newEx.getIn().getBody(String.class));
newEx.setProperty("fileName",fileNameSet);
System.out.println(fileNameSet);
newEx.setProperty("body",bodySet);
System.out.println(bodySet);
return newEx;
}
oldEx.getProperty("fileName",fileNameSet);
fileNameSet.add((String) oldEx.getIn().getHeader("fileName"));
oldEx.setProperty("fileName",fileNameSet);
System.out.println(fileNameSet);
oldEx.getProperty("body",bodySet);
bodySet.add(oldEx.getIn().getBody(String.class));
oldEx.setProperty("body",bodySet);
return oldEx;
}
}
So I am not getting reason why the sysout for fileName is [null].
The reason for getting null filename is , the filenameset was not updating. The code should be like this:
package beans;
import java.util.ArrayList;
import java.util.List;
import org.apache.camel.Exchange;
import org.apache.camel.processor.aggregate.AggregationStrategy;
public class AggregateBody implements AggregationStrategy {
@SuppressWarnings("unchecked")
public Exchange aggregate(Exchange oldEx, Exchange newEx) {
List<String> fileNameSet = new ArrayList<String>();
List<String> bodySet = new ArrayList<String>();
if (oldEx == null) {
fileNameSet.add((String) newEx.getIn().getHeader("CamelFileName"));
newEx.setProperty("CamelFileName", fileNameSet);
bodySet.add(newEx.getIn().getBody(String.class));
newEx.setProperty("body", bodySet);
return newEx;
}
fileNameSet = (List<String>) oldEx.getProperty("CamelFileName", fileNameSet);
bodySet = (List<String>) oldEx.getProperty("body", bodySet);
fileNameSet.add((String) newEx.getIn().getHeader("CamelFileName"));
bodySet.add(newEx.getIn().getBody(String.class));
oldEx.setProperty("CamelFileName", fileNameSet);
oldEx.setProperty("body", bodySet);
return oldEx;
}
}