Apache Camel:文件到BeanIO并基于id合并beanIO对象

时间:2017-10-21 06:03:49

标签: 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中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会降低性能。

先谢谢。

1 个答案:

答案 0 :(得分:1)

在解组后使用拆分器拆分每条消息

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");

接下来是聚合器的外观。 details对象存储为olddExchange中的标头。 最重要的参数如下

  1. correlationExpression:simple(" $ {body.id}")关联所有具有相同ID(1或2)的邮件
  2. completionSize = 3。每个文件一个。
  3. from("seda:aggregate").aggregate(simple("${body.id}"), (oldExchange,newExchange) -> {
            if (oldExchange == null) {
                EmployeeDetails details = buildDetails(new EmployeeDetails(), newExchange);
                newExchange.getIn().setHeader("details", details);
                return newExchange;
            }
            EmployeeDetails details = oldExchange.getIn().getHeader("details", EmployeeDetails.class);
            buildDetails(details, newExchange);
            oldExchange.getIn().setHeader("details", details);
            return oldExchange;
        }).completionSize(3).log("Details - ${header.details}")
    

    并且

    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;
        }
    

    然后结果将是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)