Spring batch - ItemReader within another itemreader or Itemprocessor

时间:2015-05-04 19:37:53

标签: spring spring-batch

Here is my requirement : Create a batch job that 1. Fetches discount programs from Discount table for specific search critieria 2. For each discount program fetched in Step1, Get sales records for sales that fit the discount program dates Get additional details for sales from some other tables 3. Write the sales records into ReportTable 4. update discount table with status

3 options for implementations I could think of are : Option 1: (a) Create a DiscountReader that extends JdbcCursorItemReader that fetches discount programs based on dates and other criteria _ returns objects of type DiscountObj. (b) Created a SalesProcessor that implements ItemProcessor, with a SalesReader and SalesWriter. -> SalesReader should read records from salestable based on the current DiscountObj. -> SalesWriter to write the date fetched by SalesReader into database. (c) create DiscountWriter to update status into Discount table.

Option 2: (a) Create DiscountReader with a SalesReader inside ( ItemReader within ItemReader approach.)- which calls Salesreader for each DiscountObj being read. (b) Create SalesWriter to write the date fetched by SalesReader into database.

In any case, the parameters for the query executed by SalesReader is dynamic since it has to be extracted from the current DiscountObj. Are these the only 2 approaches or is there a better approach ?

If I inject the SalesReader into SalesProcessor/ DiscountReader, the open() method is being called before the preparedstatementsetter is set with the query parameters.

If I create instances of the SalesReader in SalesProcessor/ DiscountReader I get the exception that the Reader is not open.


Option 3: Create DiscountReader to read discounts. Create SalesProcessor which calls a DAO to get the sales records based on the discpuntObj Pass the data returned from the DAO to the writer.

Please help.

2 个答案:

答案 0 :(得分:1)

尽可能地将读者,处理者和作家分开:每个部分都有特定的工作,混合通常是一个糟糕的实践。
你的用例是常见的 - 如果我理解正确 - 你的困难与读者有关 将它拆分为两个部分:

  • Reader:使用基于折扣+销售表之间联接的单个基于SQL游标的阅读器
  • 处理器:对于每个提取的记录,使用DAO来使用附加数据来丰富记录

只要您需要更多数据操作,就可以添加更多处理器,而不是写作应该是直截了当的

这只是解决方案的提议;我希望你能从中获得匮乏。干得好!)

答案 1 :(得分:0)

我觉得你可以写一个连接查询,因为一切都是基于你之前提取的东西。

如果您需要使用JobExecutionListenerSupport(请参阅beforeJob,afterJob)之后的某些信息,您也可以为读者/编写者编写一个列表器。

如果按照上面的说法进行操作,这将是一个简单的春季批次入门用例。