0
votes

Hi I am new to spring batch .

I have situation with spring batch like below :

I need to run a batch for all Promotions [ List of Promotions]

  • For Each Promotion I need to prepare dynamic query with promotion attributes and get all the transaction history and compute promotional points to each user

    And here the with above dynamic query I wanted to read again from batch as it returns results minimum of 50 thousand records .

below is the process I am expecting, is this is possible in spring batch?

  • Read Promotions[Reader reads promotions one by one]
    • create query and put it on context
    • pass it to the next reader
      • reader reads transactions one by one
      • process transactions and calculate points
    • processor

My problem here is Not able write nested chunks [ one for read promotions and one for reading transactions].

One alternative is :

Partition the job for each Promotion , in this all promotions will run concurrently , But we need to run one after one .

Can any body suggest any way for this ?

1
Did you find another solution? I'm interested because, not too far in time, I will face the same problem - Luca Basso Ricci

1 Answers

1
votes

You can't have nested chunk so you can code your own CompositeItemReader like(pseudo-code):

class CompositeItemReader implements ItemReader<Transaction> {
  ItemReader<Promotion> promotions;
  ItemReader<Transaction> transactions;

  public void setPromotions(ItemReader<Promotion> promotions) {...}

  public Transaction read() {
    Transaction item = transactions.read();
    if(null == item) {
      Promotion p = promotions.read();
      if(null != p) {
        // Close transactions reader, create dynamic query and open transaction reader
        // Re-read item!
        item = this.read();
      }
    }

   return item;
  }
}

but remember to register promotions and transactions as stream and remember to manage restartability.
Or, if you are sure each Promotion have a limited List<> of associated Transactions, you can use a ItemProcessor<Promotion,List<Transaction>> that convert a single Promotion like (Driving Query Based ItemReaders):

class PromotionProcessor implements ItemProcessor<Promotion,List<Transaction>> {
  public List<Transaction> process(Promotion item) {
    Query q = <create dynamic query>;
    return q.list();
  }
}

Else a solution based on PeekableItemReader and data holder like one described here

Hope this considerations can be helpful! Good job!