Advertisement
Guest User

Untitled

a guest
Mar 24th, 2016
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.95 KB | None | 0 0
  1. package org.springBatch.configuration;
  2.  
  3.  
  4. import javax.sql.DataSource;
  5.  
  6. import org.apache.commons.dbcp.BasicDataSource;
  7. import org.springBatch.entity.Country;
  8. import org.springBatch.listener.JobCompletionNotificationListener;
  9. import org.springBatch.processor.CountryItemProcessor;
  10. import org.springframework.batch.core.Job;
  11. import org.springframework.batch.core.JobExecutionListener;
  12. import org.springframework.batch.core.Step;
  13. import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
  14. import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
  15. import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
  16. import org.springframework.batch.core.launch.support.RunIdIncrementer;
  17. import org.springframework.batch.item.database.BeanPropertyItemSqlParameterSourceProvider;
  18. import org.springframework.batch.item.database.JdbcBatchItemWriter;
  19. import org.springframework.batch.item.file.FlatFileItemReader;
  20. import org.springframework.batch.item.file.mapping.BeanWrapperFieldSetMapper;
  21. import org.springframework.batch.item.file.mapping.DefaultLineMapper;
  22. import org.springframework.batch.item.file.transform.DelimitedLineTokenizer;
  23. import org.springframework.beans.factory.annotation.Autowired;
  24. import org.springframework.boot.autoconfigure.jdbc.DataSourceBuilder;
  25. import org.springframework.context.annotation.Bean;
  26. import org.springframework.context.annotation.Configuration;
  27. import org.springframework.core.io.ClassPathResource;
  28. import org.springframework.jdbc.core.JdbcTemplate;
  29.  
  30. @Configuration
  31. @EnableBatchProcessing
  32. public class BatchConfiguration {
  33. @Autowired
  34. public JobBuilderFactory jobBuilderFactory;
  35.  
  36. @Autowired
  37. public StepBuilderFactory stepBuilderFactory;
  38.  
  39. @Bean
  40. public FlatFileItemReader<Country> reader() {
  41. FlatFileItemReader<Country> reader = new FlatFileItemReader<Country>();
  42. reader.setResource(new ClassPathResource("countries.csv"));
  43. reader.setLineMapper(new DefaultLineMapper<Country>() {{
  44. setLineTokenizer(new DelimitedLineTokenizer() {{
  45. setNames(new String[] { "id", "name", "currency" });
  46. }});
  47. setFieldSetMapper(new BeanWrapperFieldSetMapper<Country>() {{
  48. setTargetType(Country.class);
  49. }});
  50. }});
  51. return reader;
  52. }
  53.  
  54. @Bean
  55. public CountryItemProcessor processor() {
  56. return new CountryItemProcessor();
  57. }
  58.  
  59. @Bean
  60. public JdbcBatchItemWriter<Country> writer(DataSource dataSource) {
  61. JdbcBatchItemWriter<Country> writer = new JdbcBatchItemWriter<Country>();
  62. writer.setItemSqlParameterSourceProvider(new BeanPropertyItemSqlParameterSourceProvider<Country>());
  63. writer.setSql("INSERT INTO countries (id, name, currency) VALUES (:id, :name, :currency)");
  64. writer.setDataSource(dataSource);
  65. return writer;
  66. }
  67.  
  68. @Bean
  69. public Job importUserJob(DataSource dataSource) {
  70. return jobBuilderFactory.get("importUserJob")
  71. .incrementer(new RunIdIncrementer())
  72. .listener(listener(dataSource))
  73. .flow(step1(dataSource))
  74. .end()
  75. .build();
  76. }
  77.  
  78. @Bean
  79. public Step step1(DataSource dataSource) {
  80. return stepBuilderFactory.get("step1")
  81. .<Country, Country> chunk(10)
  82. .reader(reader())
  83. .processor(processor())
  84. .writer(writer(dataSource))
  85. .build();
  86. }
  87.  
  88. @Bean
  89. public JobExecutionListener listener(DataSource dataSource) {
  90. return new JobCompletionNotificationListener(new JdbcTemplate(dataSource));
  91. }
  92.  
  93. @Bean
  94. public DataSource getDataSource() {
  95. BasicDataSource dataSource = new BasicDataSource();
  96. dataSource.setDriverClassName("com.mysql.jdbc.Driver");
  97. dataSource.setUrl("jdbc:mysql://localhost:3306/testdb");
  98. dataSource.setUsername("root");
  99. dataSource.setPassword("");
  100. return dataSource;
  101. }
  102.  
  103. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement