SyntaxHighlighter

Wednesday, June 5, 2013

A Simple Spring Batch Buffer Example

A Simple Spring Batch Buffer Example

here's a simple example of a Spring Batch app that has a built in buffer.  this was in response to a stackoverflow question about retrieving a recordset once from a stored procedure, then reading each line item out into a writer.  the original question stated that they couldn't really use the out of the box stored procedure one, so here's an example of the whole load-once-read-line-write of batch.

here's the reader

package de.incompleteco.spring.batch.item;

import java.util.PriorityQueue;
import java.util.Queue;
import java.util.UUID;

import org.springframework.batch.item.ItemReader;
import org.springframework.batch.item.NonTransientResourceException;
import org.springframework.batch.item.ParseException;
import org.springframework.batch.item.UnexpectedInputException;

import de.incompleteco.spring.batch.domain.Record;

public class SingleItemReader implements ItemReader {

 private Queue values = null;
 
 public Record read() throws Exception, UnexpectedInputException,ParseException, NonTransientResourceException {
  //check the queue
  if (values == null) {
   //load
   loadQueue();
  }//end if
  //return
  return getRecord(values.poll());
 }
 
 private Record getRecord(String value) {
  //init
  Record record = null;
  //check for null
  if (value != null) {
   record = new Record();
   record.setId(UUID.randomUUID());
   record.setValue(value);
  }//end if
  //return
  return record;  
 }
 
 private synchronized void loadQueue() {
  //generate a bunch of data and add to the queue
  if (values == null) {
   System.out.println("calling load of the queue");
   values = new PriorityQueue();
   for (int i=0;i<100 end="" for="" hello="" i="" if="" pre="" values.add="">
here's the writer
package de.incompleteco.spring.batch.item;

import java.util.List;

import org.springframework.batch.item.ItemWriter;

import de.incompleteco.spring.batch.domain.Record;

public class SystemItemWriter implements ItemWriter {

 public void write(List items) throws Exception {
  System.out.println("starting write...");
  System.out.println(items);
  System.out.println("...finished write");
 }

}
here's the batch config


 
  
   
    
   
  
 

 
 
 

 
  
 
  
  
 

 
  
 
 
 
 
 


and finally, the unit test
package de.incompleteco.spring.batch;

import static org.junit.Assert.assertFalse;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.JobExecution;
import org.springframework.batch.core.JobParameters;
import org.springframework.batch.core.JobParametersBuilder;
import org.springframework.batch.core.explore.JobExplorer;
import org.springframework.batch.core.launch.JobLauncher;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration({"classpath:/META-INF/spring/batch-context.xml"})
public class SingleReadJobIntegrationTest {

 @Autowired
 private Job job;
 
 @Autowired
 private JobLauncher jobLauncher;
 
 @Autowired
 private JobExplorer jobExplorer;
 
 @Test
 public void test() throws Exception {
  //setup the parameters
  JobParameters jobParameters = new JobParametersBuilder().addLong("runtime",System.currentTimeMillis()).toJobParameters();
  //start
  JobExecution execution = jobLauncher.run(job,jobParameters);
  //monitor
  while (jobExplorer.getJobExecution(execution.getId()).isRunning()) {
   Thread.sleep(100);
  }//end while
  //check
  execution = jobExplorer.getJobExecution(execution.getId());
  //validate
  assertFalse(execution.getStatus().isUnsuccessful());
 }
 
}

No comments:

Post a Comment