|
| 1 | +package org.baeldung.spring_batch_intro.partitioner; |
| 2 | + |
| 3 | +import java.io.IOException; |
| 4 | +import java.net.MalformedURLException; |
| 5 | +import java.text.ParseException; |
| 6 | + |
| 7 | +import javax.sql.DataSource; |
| 8 | + |
| 9 | +import org.baeldung.spring_batch_intro.model.Transaction; |
| 10 | +import org.baeldung.spring_batch_intro.service.RecordFieldSetMapper; |
| 11 | +import org.springframework.batch.core.Job; |
| 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.configuration.annotation.StepScope; |
| 17 | +import org.springframework.batch.core.launch.JobLauncher; |
| 18 | +import org.springframework.batch.core.launch.support.SimpleJobLauncher; |
| 19 | +import org.springframework.batch.core.repository.JobRepository; |
| 20 | +import org.springframework.batch.core.repository.support.JobRepositoryFactoryBean; |
| 21 | +import org.springframework.batch.item.UnexpectedInputException; |
| 22 | +import org.springframework.batch.item.file.FlatFileItemReader; |
| 23 | +import org.springframework.batch.item.file.mapping.DefaultLineMapper; |
| 24 | +import org.springframework.batch.item.file.transform.DelimitedLineTokenizer; |
| 25 | +import org.springframework.batch.item.xml.StaxEventItemWriter; |
| 26 | +import org.springframework.batch.support.transaction.ResourcelessTransactionManager; |
| 27 | +import org.springframework.beans.factory.annotation.Autowired; |
| 28 | +import org.springframework.beans.factory.annotation.Value; |
| 29 | +import org.springframework.context.annotation.Bean; |
| 30 | +import org.springframework.context.annotation.Configuration; |
| 31 | +import org.springframework.core.io.ClassPathResource; |
| 32 | +import org.springframework.core.io.FileSystemResource; |
| 33 | +import org.springframework.core.io.Resource; |
| 34 | +import org.springframework.core.io.support.ResourcePatternResolver; |
| 35 | +import org.springframework.core.task.TaskExecutor; |
| 36 | +import org.springframework.jdbc.datasource.embedded.EmbeddedDatabase; |
| 37 | +import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder; |
| 38 | +import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType; |
| 39 | +import org.springframework.oxm.Marshaller; |
| 40 | +import org.springframework.oxm.jaxb.Jaxb2Marshaller; |
| 41 | +import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor; |
| 42 | +import org.springframework.transaction.PlatformTransactionManager; |
| 43 | + |
| 44 | +@Configuration |
| 45 | +@EnableBatchProcessing |
| 46 | +public class SpringbatchPartitionConfig { |
| 47 | + |
| 48 | + @Autowired |
| 49 | + ResourcePatternResolver resoursePatternResolver; |
| 50 | + |
| 51 | + @Autowired |
| 52 | + private JobBuilderFactory jobs; |
| 53 | + |
| 54 | + @Autowired |
| 55 | + private StepBuilderFactory steps; |
| 56 | + |
| 57 | + @Bean(name = "partitionerJob") |
| 58 | + public Job partitionerJob() throws UnexpectedInputException, MalformedURLException, ParseException { |
| 59 | + return jobs.get("partitionerJob") |
| 60 | + .start(partitionStep()) |
| 61 | + .build(); |
| 62 | + } |
| 63 | + |
| 64 | + @Bean |
| 65 | + public Step partitionStep() throws UnexpectedInputException, MalformedURLException, ParseException { |
| 66 | + return steps.get("partitionStep") |
| 67 | + .partitioner("slaveStep", partitioner()) |
| 68 | + .step(slaveStep()) |
| 69 | + .taskExecutor(taskExecutor()) |
| 70 | + .build(); |
| 71 | + } |
| 72 | + |
| 73 | + @Bean |
| 74 | + public Step slaveStep() throws UnexpectedInputException, MalformedURLException, ParseException { |
| 75 | + return steps.get("slaveStep") |
| 76 | + .<Transaction, Transaction> chunk(1) |
| 77 | + .reader(itemReader(null)) |
| 78 | + .writer(itemWriter(marshaller(), null)) |
| 79 | + .build(); |
| 80 | + } |
| 81 | + |
| 82 | + @Bean |
| 83 | + public CustomMultiResourcePartitioner partitioner() { |
| 84 | + CustomMultiResourcePartitioner partitioner = new CustomMultiResourcePartitioner(); |
| 85 | + Resource[] resources; |
| 86 | + try { |
| 87 | + resources = resoursePatternResolver.getResources("file:src/main/resources/input/partitioner/*.csv"); |
| 88 | + } catch (IOException e) { |
| 89 | + throw new RuntimeException("I/O problems when resolving the input file pattern.", e); |
| 90 | + } |
| 91 | + partitioner.setResources(resources); |
| 92 | + return partitioner; |
| 93 | + } |
| 94 | + |
| 95 | + @Bean |
| 96 | + @StepScope |
| 97 | + public FlatFileItemReader<Transaction> itemReader(@Value("#{stepExecutionContext[fileName]}") String filename) throws UnexpectedInputException, ParseException { |
| 98 | + FlatFileItemReader<Transaction> reader = new FlatFileItemReader<Transaction>(); |
| 99 | + DelimitedLineTokenizer tokenizer = new DelimitedLineTokenizer(); |
| 100 | + String[] tokens = { "username", "userid", "transactiondate", "amount" }; |
| 101 | + tokenizer.setNames(tokens); |
| 102 | + reader.setResource(new ClassPathResource("input/partitioner/" + filename)); |
| 103 | + DefaultLineMapper<Transaction> lineMapper = new DefaultLineMapper<Transaction>(); |
| 104 | + lineMapper.setLineTokenizer(tokenizer); |
| 105 | + lineMapper.setFieldSetMapper(new RecordFieldSetMapper()); |
| 106 | + reader.setLinesToSkip(1); |
| 107 | + reader.setLineMapper(lineMapper); |
| 108 | + return reader; |
| 109 | + } |
| 110 | + |
| 111 | + @Bean(destroyMethod = "") |
| 112 | + @StepScope |
| 113 | + public StaxEventItemWriter<Transaction> itemWriter(Marshaller marshaller, @Value("#{stepExecutionContext[opFileName]}") String filename) throws MalformedURLException { |
| 114 | + StaxEventItemWriter<Transaction> itemWriter = new StaxEventItemWriter<>(); |
| 115 | + itemWriter.setMarshaller(marshaller); |
| 116 | + itemWriter.setRootTagName("transactionRecord"); |
| 117 | + itemWriter.setResource(new FileSystemResource("src/main/resources/output/" + filename)); |
| 118 | + return itemWriter; |
| 119 | + } |
| 120 | + |
| 121 | + @Bean |
| 122 | + public Marshaller marshaller() { |
| 123 | + Jaxb2Marshaller marshaller = new Jaxb2Marshaller(); |
| 124 | + marshaller.setClassesToBeBound(new Class[] { Transaction.class }); |
| 125 | + return marshaller; |
| 126 | + } |
| 127 | + |
| 128 | + @Bean |
| 129 | + public TaskExecutor taskExecutor() { |
| 130 | + ThreadPoolTaskExecutor taskExecutor = new ThreadPoolTaskExecutor(); |
| 131 | + taskExecutor.setMaxPoolSize(5); |
| 132 | + taskExecutor.setCorePoolSize(5); |
| 133 | + taskExecutor.setQueueCapacity(5); |
| 134 | + taskExecutor.afterPropertiesSet(); |
| 135 | + return taskExecutor; |
| 136 | + } |
| 137 | + |
| 138 | + private JobRepository getJobRepository() throws Exception { |
| 139 | + JobRepositoryFactoryBean factory = new JobRepositoryFactoryBean(); |
| 140 | + factory.setDataSource(dataSource()); |
| 141 | + factory.setTransactionManager(getTransactionManager()); |
| 142 | + // JobRepositoryFactoryBean's methods Throws Generic Exception, |
| 143 | + // it would have been better to have a specific one |
| 144 | + factory.afterPropertiesSet(); |
| 145 | + return (JobRepository) factory.getObject(); |
| 146 | + } |
| 147 | + |
| 148 | + private DataSource dataSource() { |
| 149 | + EmbeddedDatabaseBuilder builder = new EmbeddedDatabaseBuilder(); |
| 150 | + EmbeddedDatabase db = builder.setType(EmbeddedDatabaseType.HSQL) |
| 151 | + .addScript("classpath:org/springframework/batch/core/schema-drop-h2.sql") |
| 152 | + .addScript("classpath:org/springframework/batch/core/schema-h2.sql") |
| 153 | + .build(); |
| 154 | + return db; |
| 155 | + } |
| 156 | + |
| 157 | + private PlatformTransactionManager getTransactionManager() { |
| 158 | + return new ResourcelessTransactionManager(); |
| 159 | + } |
| 160 | + |
| 161 | + public JobLauncher getJobLauncher() throws Exception { |
| 162 | + SimpleJobLauncher jobLauncher = new SimpleJobLauncher(); |
| 163 | + // SimpleJobLauncher's methods Throws Generic Exception, |
| 164 | + // it would have been better to have a specific one |
| 165 | + jobLauncher.setJobRepository(getJobRepository()); |
| 166 | + jobLauncher.afterPropertiesSet(); |
| 167 | + return jobLauncher; |
| 168 | + } |
| 169 | +} |
0 commit comments