java - Apache Camel : File to BeanIO and merge beanIO objects based on id

标签 java apache-camel integration aggregator

我有并行读取员工、地址和联系人文件并将其转换为 beanIO 对象并合并 beanIO 对象以生成完整的 employeeDetails 对象的用例。

Emp 文件:

1 Foo Engineer
2 Bar AssistantEngineer

员工联系文件:

1 8912345678  foo@org.com
2 7812345678    bar@org.com

员工地址文件:

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

下面是聚合器的样子。 详细信息对象作为 header 存储在 olddExchange 中。 最重要的参数如下

  1. correlationExpression: simple("${body.id}") 关联具有相同 id(1 或 2)的所有消息
  2. 完成大小=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)

关于java - Apache Camel : File to BeanIO and merge beanIO objects based on id,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46860535/

相关文章:

java - 变量为 null 但它真的不是吗?

java - 如何解析 JSON 文件并使用它来创建类对象?

java - 向 Camel 路由添加延迟会导致 Atomikos 超时

apache-camel - 管理读取 JMS 消息并将其记录在 DB 中的事务

apache-camel - Apache Camel : switch routes based on value

java - 如何在 Android Studio 中将 OpenCV Mat 输入帧转换为 Tensorflow 张量?

java - Printwriter 用空格换行

testing - 功能集成测试

grails - 使用 "Getting Started with Grails"电子书进行集成测试时出现编译错误

spring - 如何使用 Spring Integration 移入 GCP Storage 后从本地目录中删除文件