Advertisement
Guest User

Spring Batch Test Custom Reader

a guest
Nov 8th, 2016
178
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package com.chrisbeech.batch.step;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.List;
  5.  
  6. import org.slf4j.Logger;
  7. import org.slf4j.LoggerFactory;
  8. import org.springframework.batch.item.adapter.ItemReaderAdapter;
  9. import org.springframework.beans.factory.annotation.Autowired;
  10. import org.springframework.stereotype.Component;
  11.  
  12. import com.chrisbeech.batch.mongodb.collections.Client;
  13.  
  14.  
  15.  
  16.  
  17. @Component
  18. public class Reader extends ItemReaderAdapter<Client> {
  19.    
  20.      private static final Logger log = LoggerFactory.getLogger(Reader.class);
  21.    
  22.     private int nextClientIndex;
  23.     private List<Client> clients;
  24.    
  25.     @Autowired
  26.     public Reader(){
  27.         init();
  28.     }
  29.  
  30.     // populate in memory array
  31.     private void init(){
  32.  
  33.         clients = new ArrayList<Client>();
  34.        
  35.         Client beryl = new Client();
  36.         beryl.setFirstName("Beryl");
  37.         beryl.setLastName("A");
  38.         clients.add(beryl);
  39.        
  40.         Client frank = new Client();
  41.         beryl.setFirstName("Frank");
  42.         beryl.setLastName("A");
  43.         clients.add(frank);
  44.        
  45.         Client paul = new Client();
  46.         beryl.setFirstName("Paul");
  47.         beryl.setLastName("A");
  48.         clients.add(paul);
  49.        
  50.         Client judith = new Client();
  51.         beryl.setFirstName("Judith");
  52.         beryl.setLastName("A");
  53.         clients.add(judith);
  54.        
  55.         Client lizzy = new Client();
  56.         beryl.setFirstName("Lizzy");
  57.         beryl.setLastName("A");
  58.         clients.add(lizzy);
  59.        
  60.         Client erin = new Client();
  61.         beryl.setFirstName("Erin");
  62.         beryl.setLastName("A");
  63.         clients.add(erin);
  64.        
  65.         Client norma = new Client();
  66.         beryl.setFirstName("Norma");
  67.         beryl.setLastName("A");
  68.         clients.add(norma);
  69.        
  70.         Client danko = new Client();
  71.         beryl.setFirstName("Daniel");
  72.         beryl.setLastName("A");
  73.         clients.add(danko);
  74.        
  75.         Client kyle = new Client();
  76.         beryl.setFirstName("Kyle");
  77.         beryl.setLastName("A");
  78.         clients.add(kyle);
  79.        
  80.         Client oscar = new Client();
  81.         beryl.setFirstName("Oscar");
  82.         beryl.setLastName("A");
  83.         clients.add(oscar);
  84.        
  85.         nextClientIndex = 0;
  86.     }
  87.    
  88.     // read the people
  89.     public Client read() throws Exception {
  90.         Client nextClient = null;
  91.        
  92.         if(nextClientIndex < clients.size()){
  93.             nextClient = clients.get(nextClientIndex);
  94.             nextClientIndex++;
  95.             log.info("### Reading in " + nextClientIndex + " : " + nextClient);
  96.         }
  97.        
  98.         return nextClient;
  99.     }
  100. }
Advertisement
Advertisement
Advertisement
RAW Paste Data Copied
Advertisement