Spring batch - rlip/java GitHub Wiki
-
ChunkSize - określa ile naraz przetwarza, w przypadku błędu całość będzie przetwarzana
-
saveState - jak true to w przypadku błędu zapisze ile rekordów przetworzyło i już ich potem nie przetworzy
-
Reader: saveStep: false - w przypadku błedu przetwarza od nowa ten step, a nie pomija jak jest domyślnie
-
Writer: alowStartCompleted - pozwala na restar stepu nawet jak ma status completed
-
Można robić beforeStep i afterStep, jak również za pomocą anotacji
@Bean
@Qualifier("fillBalanceStep")
public Step fillBalanceStep(JobRepository jobRepository, PlatformTransactionManager transactionManager,
@Qualifier("sourceDataSource") DataSource sourceDataSource) {
FillBalanceProcessor processor = new FillBalanceProcessor();
return new StepBuilder("fill-balance", jobRepository)
// Writing in chunks of size 10
.<BankTransaction, BalanceUpdate>chunk(10, transactionManager)
// Reading from source db using cursor-based technique
.reader(new JdbcCursorItemReaderBuilder<BankTransaction>()
.dataSource(sourceDataSource)
.name("bankTransactionReader")
.sql(BankTransaction.SELECT_ALL_QUERY)
.rowMapper(BankTransaction.ROW_MAPPER)
.build())
// Using instance of the processor, such that step execution is set properly
.processor(processor)
// Writer needs to update record's information by writing 'balance' column
.writer(new JdbcBatchItemWriterBuilder<BalanceUpdate>()
.dataSource(sourceDataSource)
.itemPreparedStatementSetter((item, ps) -> {
ps.setBigDecimal(1, item.getBalance());
ps.setLong(2, item.getId());
})
.sql("update bank_transaction_yearly set balance = ? where id = ?")
.build())
// Step execution listener to alter schema before step, and plug and unplug step execution for processor
.listener(new StepExecutionListener() {
@Override
public void beforeStep(StepExecution stepExecution) {
SourceManagementUtils.addBalanceColumn(sourceDataSource);
processor.setStepExecution(stepExecution);
}
@Override
public ExitStatus afterStep(StepExecution stepExecution) {
double totalBalance = processor.getLatestTransactionBalance();
processor.setStepExecution(null); // Clear step execution when step is executed
return new ExitStatus(totalBalance >= 0 ? POSITIVE : NEGATIVE);
}
})
// Always run step, regardless of whether same parameters step was completed
.allowStartIfComplete(true)
.build();
}
spring:
application:
name: PatientBatchLoader
batch:
job:
enabled: false
@Component
@EnableBatchProcessing
public class BatchConfiguration implements BatchConfigurer {
private JobRepository jobRepository;
private JobExplorer jobExplorer;
private JobLauncher jobLauncher;
@Autowired
@Qualifier(value = "batchTransactionManager")
private PlatformTransactionManager batchTransactionManager;
@Autowired
@Qualifier(value = "batchDataSource")
private DataSource batchDataSource;
@Override
public JobRepository getJobRepository() throws Exception {
return this.jobRepository;
}
@Override
public JobLauncher getJobLauncher() throws Exception {
return this.jobLauncher;
}
@Override
public JobExplorer getJobExplorer() throws Exception {
return this.jobExplorer;
}
@Override
public PlatformTransactionManager getTransactionManager() {
return this.batchTransactionManager;
}
@PostConstruct
public void afterPropertiesSet() throws Exception {
this.jobRepository = createJobRepository();
JobExplorerFactoryBean jobExplorerFactoryBean = new JobExplorerFactoryBean();
jobExplorerFactoryBean.setDataSource(this.batchDataSource);
jobExplorerFactoryBean.afterPropertiesSet();
this.jobExplorer = jobExplorerFactoryBean.getObject();
this.jobLauncher = createJobLauncher();
}
protected JobLauncher createJobLauncher() throws Exception {
SimpleJobLauncher jobLauncher = new SimpleJobLauncher();
jobLauncher.setJobRepository(jobRepository);
jobLauncher.afterPropertiesSet();
return jobLauncher;
}
protected JobRepository createJobRepository() throws Exception {
JobRepositoryFactoryBean factory = new JobRepositoryFactoryBean();
factory.setDataSource(this.batchDataSource);
factory.setTransactionManager(getTransactionManager());
factory.afterPropertiesSet();
return factory.getObject();
}
}