|
| 1 | +package org.baeldung.taskletsvschunks.config; |
| 2 | + |
| 3 | +import org.baeldung.taskletsvschunks.chunks.LineProcessor; |
| 4 | +import org.baeldung.taskletsvschunks.chunks.LineReader; |
| 5 | +import org.baeldung.taskletsvschunks.chunks.LinesWriter; |
| 6 | +import org.baeldung.taskletsvschunks.model.Line; |
| 7 | +import org.springframework.batch.core.Job; |
| 8 | +import org.springframework.batch.core.Step; |
| 9 | +import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing; |
| 10 | +import org.springframework.batch.core.configuration.annotation.JobBuilderFactory; |
| 11 | +import org.springframework.batch.core.configuration.annotation.StepBuilderFactory; |
| 12 | +import org.springframework.batch.core.launch.JobLauncher; |
| 13 | +import org.springframework.batch.core.launch.support.SimpleJobLauncher; |
| 14 | +import org.springframework.batch.core.repository.JobRepository; |
| 15 | +import org.springframework.batch.core.repository.support.MapJobRepositoryFactoryBean; |
| 16 | +import org.springframework.batch.item.ItemProcessor; |
| 17 | +import org.springframework.batch.item.ItemReader; |
| 18 | +import org.springframework.batch.item.ItemWriter; |
| 19 | +import org.springframework.batch.support.transaction.ResourcelessTransactionManager; |
| 20 | +import org.springframework.batch.test.JobLauncherTestUtils; |
| 21 | +import org.springframework.beans.factory.annotation.Autowired; |
| 22 | +import org.springframework.context.annotation.Bean; |
| 23 | +import org.springframework.context.annotation.Configuration; |
| 24 | +import org.springframework.transaction.PlatformTransactionManager; |
| 25 | + |
| 26 | +@Configuration |
| 27 | +@EnableBatchProcessing |
| 28 | +public class ChunksConfig { |
| 29 | + |
| 30 | + @Autowired private JobBuilderFactory jobs; |
| 31 | + |
| 32 | + @Autowired private StepBuilderFactory steps; |
| 33 | + |
| 34 | + @Bean |
| 35 | + public JobLauncherTestUtils jobLauncherTestUtils() { |
| 36 | + return new JobLauncherTestUtils(); |
| 37 | + } |
| 38 | + |
| 39 | + @Bean |
| 40 | + public JobRepository jobRepository() throws Exception { |
| 41 | + MapJobRepositoryFactoryBean factory = new MapJobRepositoryFactoryBean(); |
| 42 | + factory.setTransactionManager(transactionManager()); |
| 43 | + return (JobRepository) factory.getObject(); |
| 44 | + } |
| 45 | + |
| 46 | + @Bean |
| 47 | + public PlatformTransactionManager transactionManager() { |
| 48 | + return new ResourcelessTransactionManager(); |
| 49 | + } |
| 50 | + |
| 51 | + @Bean |
| 52 | + public JobLauncher jobLauncher() throws Exception { |
| 53 | + SimpleJobLauncher jobLauncher = new SimpleJobLauncher(); |
| 54 | + jobLauncher.setJobRepository(jobRepository()); |
| 55 | + return jobLauncher; |
| 56 | + } |
| 57 | + |
| 58 | + @Bean |
| 59 | + public ItemReader<Line> itemReader() { |
| 60 | + return new LineReader(); |
| 61 | + } |
| 62 | + |
| 63 | + @Bean |
| 64 | + public ItemProcessor<Line, Line> itemProcessor() { |
| 65 | + return new LineProcessor(); |
| 66 | + } |
| 67 | + |
| 68 | + @Bean |
| 69 | + public ItemWriter<Line> itemWriter() { |
| 70 | + return new LinesWriter(); |
| 71 | + } |
| 72 | + |
| 73 | + @Bean |
| 74 | + protected Step processLines(ItemReader<Line> reader, ItemProcessor<Line, Line> processor, ItemWriter<Line> writer) { |
| 75 | + return steps.get("processLines").<Line, Line> chunk(2) |
| 76 | + .reader(reader) |
| 77 | + .processor(processor) |
| 78 | + .writer(writer) |
| 79 | + .build(); |
| 80 | + } |
| 81 | + |
| 82 | + @Bean |
| 83 | + public Job job() { |
| 84 | + return jobs |
| 85 | + .get("chunksJob") |
| 86 | + .start(processLines(itemReader(), itemProcessor(), itemWriter())) |
| 87 | + .build(); |
| 88 | + } |
| 89 | + |
| 90 | +} |
0 commit comments