SHARE
TWEET

Untitled

a guest Jul 22nd, 2019 63 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package org.springframework.batch.sample;
  2.  
  3. import java.util.Arrays;
  4.  
  5. import org.springframework.batch.core.Job;
  6. import org.springframework.batch.core.JobParameters;
  7. import org.springframework.batch.core.Step;
  8. import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
  9. import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
  10. import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
  11. import org.springframework.batch.core.launch.JobLauncher;
  12. import org.springframework.batch.item.ItemReader;
  13. import org.springframework.batch.item.ItemWriter;
  14. import org.springframework.batch.item.file.FlatFileItemWriter;
  15. import org.springframework.batch.item.file.builder.FlatFileItemWriterBuilder;
  16. import org.springframework.batch.item.file.transform.PassThroughLineAggregator;
  17. import org.springframework.batch.item.support.ClassifierCompositeItemWriter;
  18. import org.springframework.batch.item.support.ListItemReader;
  19. import org.springframework.classify.Classifier;
  20. import org.springframework.context.ApplicationContext;
  21. import org.springframework.context.annotation.AnnotationConfigApplicationContext;
  22. import org.springframework.context.annotation.Bean;
  23. import org.springframework.context.annotation.Configuration;
  24. import org.springframework.core.io.FileSystemResource;
  25.  
  26. @Configuration
  27. @EnableBatchProcessing
  28. public class MyJob {
  29.  
  30.     private JobBuilderFactory jobBuilderFactory;
  31.  
  32.     private StepBuilderFactory stepBuilderFactory;
  33.  
  34.     public MyJob(JobBuilderFactory jobBuilderFactory, StepBuilderFactory stepBuilderFactory) {
  35.         this.jobBuilderFactory = jobBuilderFactory;
  36.         this.stepBuilderFactory = stepBuilderFactory;
  37.     }
  38.  
  39.     @Bean
  40.     public ItemReader<Person> itemReader() {
  41.         Person foo1 = new Person();foo1.setId(1);foo1.setName("foo1");
  42.         Person foo2 = new Person();foo2.setId(2);foo2.setName("foo2");
  43.         Person bar1 = new Person();bar1.setId(3);bar1.setName("bar1");
  44.         Person bar2 = new Person();bar2.setId(4);bar2.setName("bar2");
  45.         return new ListItemReader<>(Arrays.asList(foo1, foo2, bar1, bar2));
  46.     }
  47.  
  48.     @Bean
  49.     public ClassifierCompositeItemWriter<Person> classifierCompositeItemWriter(ItemWriter<Person> fooItemWriter, ItemWriter<Person> barItemWriter) {
  50.         ClassifierCompositeItemWriter<Person> classifierCompositeItemWriter = new ClassifierCompositeItemWriter<>();
  51.         classifierCompositeItemWriter.setClassifier((Classifier<Person, ItemWriter<? super Person>>) person -> {
  52.             if (person.getName().startsWith("foo")) {
  53.                 return fooItemWriter;
  54.             } else {
  55.                 return barItemWriter;
  56.             }
  57.         });
  58.         return classifierCompositeItemWriter;
  59.     }
  60.  
  61.     @Bean
  62.     public FlatFileItemWriter<Person> fooItemWriter() {
  63.         return new FlatFileItemWriterBuilder<Person>()
  64.                 .name("fooItemWriter")
  65.                 .resource(new FileSystemResource("foos.txt"))
  66.                 .lineAggregator(new PassThroughLineAggregator<>())
  67.                 .build();
  68.     }
  69.  
  70.     @Bean
  71.     public FlatFileItemWriter<Person> barItemWriter() {
  72.         return new FlatFileItemWriterBuilder<Person>()
  73.                 .name("barItemWriter")
  74.                 .resource(new FileSystemResource("bars.txt"))
  75.                 .lineAggregator(new PassThroughLineAggregator<>())
  76.                 .build();
  77.     }
  78.  
  79.     @Bean
  80.     public Step dataExtractionStep() {
  81.         return stepBuilderFactory.get("dataExtractionStep")
  82.                 .<Person, Person>chunk(2)
  83.                 .reader(itemReader())
  84.                 .writer(classifierCompositeItemWriter(fooItemWriter(), barItemWriter()))
  85.                 .stream(fooItemWriter())
  86.                 .stream(barItemWriter())
  87.                 .build();
  88.     }
  89.  
  90.     @Bean
  91.     public Job dataExtractionJob() {
  92.         return jobBuilderFactory.get("dataExtractionJob")
  93.                 .start(dataExtractionStep())
  94.                 .build();
  95.     }
  96.  
  97.     public static void main(String[] args) throws Exception {
  98.         ApplicationContext context = new AnnotationConfigApplicationContext(MyJob.class);
  99.         JobLauncher jobLauncher = context.getBean(JobLauncher.class);
  100.         Job job = context.getBean(Job.class);
  101.         jobLauncher.run(job, new JobParameters());
  102.     }
  103.  
  104.     public static class Person {
  105.  
  106.         private int id;
  107.  
  108.         private String name;
  109.  
  110.         public Person() {
  111.         }
  112.  
  113.         public Person(int id, String name) {
  114.             this.id = id;
  115.             this.name = name;
  116.         }
  117.  
  118.         public int getId() {
  119.             return id;
  120.         }
  121.  
  122.         public void setId(int id) {
  123.             this.id = id;
  124.         }
  125.  
  126.         public String getName() {
  127.             return name;
  128.         }
  129.  
  130.         public void setName(String name) {
  131.             this.name = name;
  132.         }
  133.  
  134.         @Override
  135.         public String toString() {
  136.             return "Person{" +
  137.                     "id=" + id +
  138.                     ", name='" + name + '\'' +
  139.                     '}';
  140.         }
  141.     }
  142.  
  143. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top