为什么 fileNameSet 为空?
Why fileNameSet is null?
我是 Talend 的新手,我正在创建一个 bean,以将四条消息聚合为一条。我正在使用 cFile 组件,它使用目录中的 4 个文件,然后是聚合器,然后是处理器。聚合Bean的代码如下:
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;
}
}
所以我不明白为什么 fileName 的系统输出是 [null]。
得到空文件名的原因是,filenameset 没有更新。代码应该是这样的:
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;
}
}
我是 Talend 的新手,我正在创建一个 bean,以将四条消息聚合为一条。我正在使用 cFile 组件,它使用目录中的 4 个文件,然后是聚合器,然后是处理器。聚合Bean的代码如下:
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;
}
}
所以我不明白为什么 fileName 的系统输出是 [null]。
得到空文件名的原因是,filenameset 没有更新。代码应该是这样的:
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;
}
}