【发布时间】:2015-02-27 19:21:30
【问题描述】:
我正在考虑使用 Grails Spring Batch 插件在 Grails 中使用 Spring Batch。
如果我的输入文件中有许多固定长度的记录引用了一个实体,其中该记录的一部分指示该记录是新项目、应该更新的现有项目还是应该删除的现有项目,集成到 Spring Batch 模式中的最佳模式是什么?
所以如果我可能的记录是这样的:
// create new record of type AA, data is 12345
AAN12345
// update record of type AA, data is 12345 (assume that the data is the key and I can find the existing AA item using this key)
AAU12345
// delete record of type AA using 12345 as the key
AAD12345
我对 LineMapper 很满意,它从 FlatFileItemReader 获取一行并创建一个新项目并将其传递给写入器进行保存。
LineMapper 可能如下所示:
class AaLineMapper implements LineMapper<AaItem> {
@Override
AaItem mapLine(String line, int lineNumber) throws Exception {
switch (line[0..1]) {
case 'N':
AaItem item = new AaItem()
// set fields here based on line
return item
break
case 'U':
// possibly this?
AaItem item = AaItem.findByKey(someValueWithinLine)
// set fields here based on line
return item
break
case 'D':
// not sure on this one, deleting and returning null doesn't seem to work
// I thought the writer should delete the object?
break
}
}
}
但是,为了更新,我是否假设最好的方法是在 LineMapper 中使用 Item.findByKey(12345),然后修改 Item 并在 writer 中调用 save()?
如何实现删除?如果我从 LineMapper 返回 null,则应用程序似乎停止了。我认为作者应该删除对象,而不是这个?还是我只使用findByKey(12345),然后通过设置删除标志传递给作者?
抱歉,这是使用框架的第一天。我有兴趣了解最佳实践。
【问题讨论】:
标签: grails spring-batch