容错机制

异常处理

参考博客 官网

重试机制

官网

多个Writer在一个step中进行

博客记不到了 直接上我的代码把

Writer_1

  1. import lombok.extern.slf4j.Slf4j;
  2. import org.springframework.batch.item.ItemWriter;
  3. import org.springframework.batch.item.database.BeanPropertyItemSqlParameterSourceProvider;
  4. import org.springframework.batch.item.database.JdbcBatchItemWriter;
  5. import org.springframework.batch.item.database.builder.JdbcBatchItemWriterBuilder;
  6. import org.springframework.beans.factory.annotation.Qualifier;
  7. import org.springframework.context.annotation.Bean;
  8. import org.springframework.stereotype.Component;
  9. import javax.sql.DataSource;
  10. /**
  11. * 将处理好的数据写入数据库
  12. * 写入用户表
  13. * @author tn
  14. * @className UserDb2DbItemWriter
  15. * @date 2021-06-23 10:21
  16. */
  17. @Component
  18. @Slf4j
  19. public class UserDb2DbItemWriter {
  20. @Bean
  21. public ItemWriter userDb2DbWriter(@Qualifier("datasource") DataSource swuplDatasource) {
  22. final String userSql = " sql ";
  23. try {
  24. JdbcBatchItemWriter<UserBean> build = new JdbcBatchItemWriterBuilder<UserBean>()
  25. .itemSqlParameterSourceProvider(new BeanPropertyItemSqlParameterSourceProvider<>())
  26. .sql(userSql)
  27. .dataSource(swuplDatasource)
  28. .build();
  29. return build;
  30. } catch (Exception e) {
  31. log.error("插入错误", e);
  32. }
  33. return null;
  34. }
  35. }

Writer_2

  1. import lombok.extern.slf4j.Slf4j;
  2. import org.springframework.batch.item.ItemWriter;
  3. import org.springframework.batch.item.database.BeanPropertyItemSqlParameterSourceProvider;
  4. import org.springframework.batch.item.database.JdbcBatchItemWriter;
  5. import org.springframework.batch.item.database.builder.JdbcBatchItemWriterBuilder;
  6. import org.springframework.beans.factory.annotation.Qualifier;
  7. import org.springframework.context.annotation.Bean;
  8. import org.springframework.stereotype.Component;
  9. import javax.sql.DataSource;
  10. /**
  11. * 将处理好的数据写入数据库
  12. *
  13. * @author tn
  14. * @className StudentDb2DbItemWriter
  15. * @date 2021-06-23 10:21
  16. */
  17. @Component
  18. @Slf4j
  19. public class StudentDb2DbItemWriter {
  20. @Bean
  21. public ItemWriter studentDb2DbWriter(@Qualifier("datasource") DataSource swuplDatasource) {
  22. final String studentSql = "sql";
  23. try {
  24. JdbcBatchItemWriter<ClassesInstructorBean> build = new JdbcBatchItemWriterBuilder<ClassesInstructorBean>()
  25. .itemSqlParameterSourceProvider(new BeanPropertyItemSqlParameterSourceProvider<>())
  26. .sql(studentSql)
  27. .dataSource(swuplDatasource)
  28. .build();
  29. return build;
  30. } catch (Exception e) {
  31. log.error("插入错误", e);
  32. }
  33. return null;
  34. }
  35. }

组合两个 Writer

  1. /**
  2. * 同时写两个表
  3. * @param studentWriter
  4. * @param userWriter
  5. * @return
  6. * @throws Exception
  7. */
  8. @Bean
  9. public CompositeItemWriter s2uCompositeWriter(
  10. @Qualifier("studentDb2DbWriter")
  11. ItemWriter studentWriter ,
  12. @Qualifier("userDb2DbWriter")
  13. ItemWriter userWriter) throws Exception {
  14. CompositeItemWriter itemWriter = new CompositeItemWriter<>();
  15. // 指定多个输出对象
  16. itemWriter.setDelegates(Arrays.asList(studentWriter,userWriter));
  17. itemWriter.afterPropertiesSet(); return itemWriter;
  18. }

step中使用

  1. @Bean
  2. public Step studentDb2DbStep(@Qualifier("studentDb2DbReader")
  3. ItemReader reader,
  4. ItemProcessor studentDb2DbProcessor,
  5. @Qualifier("s2uCompositeWriter")
  6. ItemWriter writer) {
  7. String funcName = Thread.currentThread().getStackTrace()[1].getMethodName();
  8. return stepBuilderFactory.get(funcName)
  9. .<Bean1,Bean2>chunk(10)
  10. .reader(reader)
  11. .processor(studentDb2DbProcessor)
  12. .writer(writer)
  13. .faultTolerant()
  14. .skipPolicy(new ExceptionSkipPolicy())
  15. .build();
  16. }

不同方式的读取数据

参考博客 参考博客2

FlatFileItemReader

从CVS文件中读取数据

StaxEventItemReader

从XML文件中读取数据

MultiResourceItemReader

从多个文件读取数据

JdbcCursorItemReader

使用游标读取数据集

HibernateCursorItemReader

通过Hibernate框架进行游标的控制

StoredProcedureItemReader

JdbcCursorItemReader一致,但是底层逻辑是先执行存储过程,然后返回存储过程执行结果游标。不同的数据库存储过程游标返回会有一些差异

JdbcPagingItemReader

分页查询