Java ApacheCamel:文件到BeanIO并基于id合并BeanIO对象
我有并行读取员工、地址和联系人文件的用例,并将其转换为beanIO对象,合并beanIO对象以生成完整的employeeDetails对象 Emp文件:Java ApacheCamel:文件到BeanIO并基于id合并BeanIO对象,java,apache-camel,integration,aggregator,Java,Apache Camel,Integration,Aggregator,我有并行读取员工、地址和联系人文件的用例,并将其转换为beanIO对象,合并beanIO对象以生成完整的employeeDetails对象 Emp文件: 1 Foo Engineer 2 Bar AssistantEngineer Emp联系文件: 1 8912345678 foo@org.com 2 7812345678 bar@org.com Emp地址文件: 1 city1 1234 2 city2 2345 Exchange中EmployeeDetailsBeanIOD
1 Foo Engineer
2 Bar AssistantEngineer
Emp联系文件:
1 8912345678 foo@org.com
2 7812345678 bar@org.com
Emp地址文件:
1 city1 1234
2 city2 2345
Exchange中EmployeeDetailsBeanIODataFormat对象中的预期输出:
1 Foo Engineer foo@org.com city1 1234
2 Bar AssistantEngineer bar@org.com city2 2345
我有以下路线
from("file://C:/cameltest/employee.txt").to("seda:beanIO");
from("file://C:/cameltest/employeeContact.txt").to("seda:beanIOContact");
from("file://C:/cameltest/employeeAddress.txt").to("seda:beanIOAddress");
每个文件都转换为beanio对象
BeanIODataFormat empFormat = new BeanIODataFormat("beanIO.xml","emp");
BeanIODataFormat empContactFormat = new BeanIODataFormat("beanIO.xml", "empContact");
BeanIODataFormat empAddressFormat = new BeanIODataFormat("beanIO.xml", "empAddress");
from("seda:beanIO").unmarshal(empFormat).log("body - ${body}");
from("seda:beanIOContact").unmarshal(empContactFormat).log("Contact body ${body}");
from("seda:beanIO").unmarshal(empAddressFormat).log("Address body - ${body}");
输出正确地记录bean对象
现在我需要合并这些对象以形成EmployeeDetails对象。有人能告诉我怎么做吗?我读过,似乎可以使用聚合器来完成这项工作,但不确定是否采用这种方法
关于这个问题的任何想法都会对您有所帮助。
欢迎您提出建议,是否建议首先根据员工id合并文件并从中创建对象?在这种情况下,我不想将合并的文件写入磁盘,因为IO会降低性能
提前感谢。解组后使用拆分器拆分每条邮件
from("seda:beanIO").unmarshal(empFormat).split(body()).to("seda:aggregate");
from("seda:beanIOContact").unmarshal(empContactFormat).split(body()).to("seda:aggregate");
from("seda:beanIOAddress").unmarshal(empAddressFormat).split(body()).to("seda:aggregate");
下面是聚合器的外观。
详细信息对象作为标题存储在olddExchange中。
最重要的参数如下
及 那么结果将是2个细节对象
Details - EmployeeDetails(id=1, name=Foo, job=Engineer, email=foo@org.com, city=city1, code=1234)
Details - EmployeeDetails(id=2, name=Bar, job=AssistantEnginee, email=bar@org.com, city=city2, code=2345)
它工作正常,但是员工的详细信息会一个接一个地打印出来。但我需要Exchange正文中的EmployeeDetails[]的最终输出,这样我就可以在下一个路径上传递它来处理最终列表并将其转换为Avro格式。是否需要再次聚合。。。。。。completionSize(3).to(“seda”formList”)?是否需要这样做,或者是否有其他选项来对所有消息进行分组。您可以再次聚合(尽管这是一个不同的问题)。setBody(header(“details”).aggregate(new groupedExchangeAgregationStrategy()).constant(true)。complettionSize(2),其中2是ids@Itsallas,尝试了GroupedExchangeAgregationStrategy,但在对交换进行分组时出错,为此问题创建了一个新问题。
private EmployeeDetails buildDetails(EmployeeDetails details, Exchange newExchange) {
Object newBody = newExchange.getIn().getBody();
if (newBody instanceof Employee) {
details.setId(((Employee) newBody).getId());
details.setName(((Employee) newBody).getName());
details.setJob(((Employee) newBody).getJob());
} else if (newBody instanceof EmployeeContact) {
details.setEmail(((EmployeeContact) newBody).getEmail());
} else if (newBody instanceof EmployeeAddress) {
details.setCity(((EmployeeAddress) newBody).getCity());
details.setCode(((EmployeeAddress) newBody).getCode());
}
return details;
}
Details - EmployeeDetails(id=1, name=Foo, job=Engineer, email=foo@org.com, city=city1, code=1234)
Details - EmployeeDetails(id=2, name=Bar, job=AssistantEnginee, email=bar@org.com, city=city2, code=2345)