Camel Exchange body 记录大小
Camel Exchange body record size
场景:
- CSV 文件被发送到我的端点,Pojo 转换 java 的数据,消息被发送到我的路由之一,比如(“direct:consume”)路由,然后处理器处理文件操作消息并创建新的输出。
问题:
- 文件只包含一行代码中断
- 文件包含多行代码有效
尝试过:
- 试图找到一种方法来确定 exchange.getIn().getBody()
中的记录量
- 阅读 Whosebug
- 阅读有关交换的 camel 文档
- 检查 java 代码 Object/Objects 到不知道记录数量的列表转换
代码:
public void process(Exchange exchange) throws Exception {
List<Cars> output = new ArrayList<Cars>();
**List<Wehicle> rows = (List<Wehicle>) exchange.getIn().getBody(); <-- Fails**
for (Wehicle row: rows) {
output.add(new Cars(row));
}
exchange.getIn().setBody(output);
exchange.getIn().setHeader("CamelOverruleFileName", "CarEntries.csv");
}
汽车
...
@CsvRecord(separator = ",", skipFirstLine = true, crlf = "UNIX")
public class Wehicle {
@DataField(pos = 1)
public String CouponCode;
@DataField(pos = 2)
public String Price;
}
...
汽车
@CsvRecord(separator = ",", crlf = "UNIX", generateHeaderColumns = true)
public class Cars {
@DataField(pos = 1, columnName = "CouponCode")
private String CouponCode;
@DataField(pos = 2, columnName = "Price")
private String Price;
public Cars(Wehicle origin) {
this.CouponCode = Utilities.addQuotesToString(origin.CouponCode);
this.Price = origin.Price;
}
}
输入:
"CouponCode","Price"
"ASD/785", 1900000
"BWM/758", 2000000
问题:
- 如何创建动态列表,无论我得到一个 object 还是多个 object?
-- exchange.getIn().getBody() returns object
- 如何查看camel交换消息的记录数?
-- exchange.getIn().getBody() 无 size/length 方法
- 还有其他方法吗?
好久没用java,加之camel新手安静
重新检查官方文档后,似乎以下更改正在解决问题。
代码:
public void process(Exchange exchange) throws Exception {
List<Cars> output = new ArrayList<Cars>();
List records = exchange.getIn().getBody(List.class);
(List<Wehicle>) rows = (List<Wehicle>) records;
for (Wehicle row: rows) {
output.add(new Cars(row));
}
exchange.getIn().setBody(output);
exchange.getIn().setHeader("CamelOverruleFileName", "CarEntries.csv");
}
场景:
- CSV 文件被发送到我的端点,Pojo 转换 java 的数据,消息被发送到我的路由之一,比如(“direct:consume”)路由,然后处理器处理文件操作消息并创建新的输出。
问题:
- 文件只包含一行代码中断
- 文件包含多行代码有效
尝试过:
- 试图找到一种方法来确定 exchange.getIn().getBody() 中的记录量
- 阅读 Whosebug
- 阅读有关交换的 camel 文档
- 检查 java 代码 Object/Objects 到不知道记录数量的列表转换
代码:
public void process(Exchange exchange) throws Exception {
List<Cars> output = new ArrayList<Cars>();
**List<Wehicle> rows = (List<Wehicle>) exchange.getIn().getBody(); <-- Fails**
for (Wehicle row: rows) {
output.add(new Cars(row));
}
exchange.getIn().setBody(output);
exchange.getIn().setHeader("CamelOverruleFileName", "CarEntries.csv");
}
汽车
...
@CsvRecord(separator = ",", skipFirstLine = true, crlf = "UNIX")
public class Wehicle {
@DataField(pos = 1)
public String CouponCode;
@DataField(pos = 2)
public String Price;
}
...
汽车
@CsvRecord(separator = ",", crlf = "UNIX", generateHeaderColumns = true)
public class Cars {
@DataField(pos = 1, columnName = "CouponCode")
private String CouponCode;
@DataField(pos = 2, columnName = "Price")
private String Price;
public Cars(Wehicle origin) {
this.CouponCode = Utilities.addQuotesToString(origin.CouponCode);
this.Price = origin.Price;
}
}
输入:
"CouponCode","Price"
"ASD/785", 1900000
"BWM/758", 2000000
问题:
- 如何创建动态列表,无论我得到一个 object 还是多个 object? -- exchange.getIn().getBody() returns object
- 如何查看camel交换消息的记录数? -- exchange.getIn().getBody() 无 size/length 方法
- 还有其他方法吗?
好久没用java,加之camel新手安静
重新检查官方文档后,似乎以下更改正在解决问题。
代码:
public void process(Exchange exchange) throws Exception {
List<Cars> output = new ArrayList<Cars>();
List records = exchange.getIn().getBody(List.class);
(List<Wehicle>) rows = (List<Wehicle>) records;
for (Wehicle row: rows) {
output.add(new Cars(row));
}
exchange.getIn().setBody(output);
exchange.getIn().setHeader("CamelOverruleFileName", "CarEntries.csv");
}