Merge pull request #12855 from hmdrzsharifi/BAEL-3176
Bael 3176: Update spring-batch project and articles
This commit is contained in:
commit
4da0255a71
@ -8,7 +8,9 @@ import org.springframework.batch.core.JobParameters;
|
|||||||
import org.springframework.batch.core.JobParametersBuilder;
|
import org.springframework.batch.core.JobParametersBuilder;
|
||||||
import org.springframework.batch.core.launch.JobLauncher;
|
import org.springframework.batch.core.launch.JobLauncher;
|
||||||
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
|
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
|
||||||
|
import org.springframework.context.annotation.Profile;
|
||||||
|
|
||||||
|
@Profile("spring")
|
||||||
public class App {
|
public class App {
|
||||||
|
|
||||||
private static final Logger LOGGER = LoggerFactory.getLogger(App.class);
|
private static final Logger LOGGER = LoggerFactory.getLogger(App.class);
|
||||||
|
@ -23,12 +23,14 @@ import org.springframework.beans.factory.annotation.Autowired;
|
|||||||
import org.springframework.beans.factory.annotation.Qualifier;
|
import org.springframework.beans.factory.annotation.Qualifier;
|
||||||
import org.springframework.beans.factory.annotation.Value;
|
import org.springframework.beans.factory.annotation.Value;
|
||||||
import org.springframework.context.annotation.Bean;
|
import org.springframework.context.annotation.Bean;
|
||||||
|
import org.springframework.context.annotation.Profile;
|
||||||
import org.springframework.core.io.Resource;
|
import org.springframework.core.io.Resource;
|
||||||
import org.springframework.oxm.Marshaller;
|
import org.springframework.oxm.Marshaller;
|
||||||
import org.springframework.oxm.jaxb.Jaxb2Marshaller;
|
import org.springframework.oxm.jaxb.Jaxb2Marshaller;
|
||||||
|
|
||||||
import java.text.ParseException;
|
import java.text.ParseException;
|
||||||
|
|
||||||
|
@Profile("spring")
|
||||||
public class SpringBatchConfig {
|
public class SpringBatchConfig {
|
||||||
@Autowired
|
@Autowired
|
||||||
private JobBuilderFactory jobBuilderFactory;
|
private JobBuilderFactory jobBuilderFactory;
|
||||||
|
@ -13,6 +13,7 @@ import org.springframework.batch.support.transaction.ResourcelessTransactionMana
|
|||||||
import org.springframework.beans.factory.annotation.Value;
|
import org.springframework.beans.factory.annotation.Value;
|
||||||
import org.springframework.context.annotation.Bean;
|
import org.springframework.context.annotation.Bean;
|
||||||
import org.springframework.context.annotation.Configuration;
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
import org.springframework.context.annotation.Profile;
|
||||||
import org.springframework.core.io.Resource;
|
import org.springframework.core.io.Resource;
|
||||||
import org.springframework.jdbc.datasource.DriverManagerDataSource;
|
import org.springframework.jdbc.datasource.DriverManagerDataSource;
|
||||||
import org.springframework.jdbc.datasource.init.DataSourceInitializer;
|
import org.springframework.jdbc.datasource.init.DataSourceInitializer;
|
||||||
@ -21,6 +22,7 @@ import org.springframework.transaction.PlatformTransactionManager;
|
|||||||
|
|
||||||
@Configuration
|
@Configuration
|
||||||
@EnableBatchProcessing
|
@EnableBatchProcessing
|
||||||
|
@Profile("spring")
|
||||||
public class SpringConfig {
|
public class SpringConfig {
|
||||||
|
|
||||||
@Value("org/springframework/batch/core/schema-drop-sqlite.sql")
|
@Value("org/springframework/batch/core/schema-drop-sqlite.sql")
|
||||||
|
@ -0,0 +1,15 @@
|
|||||||
|
package com.baeldung.batch.springboot;
|
||||||
|
|
||||||
|
import org.springframework.boot.SpringApplication;
|
||||||
|
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||||
|
|
||||||
|
@SpringBootApplication
|
||||||
|
public class SpringBatchApplication {
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
SpringApplication springApp = new SpringApplication(SpringBatchApplication.class);
|
||||||
|
springApp.setAdditionalProfiles("spring-boot");
|
||||||
|
springApp.run(args);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,151 @@
|
|||||||
|
package com.baeldung.batch.springboot;
|
||||||
|
|
||||||
|
import com.baeldung.batch.model.Transaction;
|
||||||
|
import com.baeldung.batch.service.*;
|
||||||
|
import org.springframework.batch.core.Job;
|
||||||
|
import org.springframework.batch.core.Step;
|
||||||
|
import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
|
||||||
|
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
|
||||||
|
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
|
||||||
|
import org.springframework.batch.item.ItemProcessor;
|
||||||
|
import org.springframework.batch.item.ItemReader;
|
||||||
|
import org.springframework.batch.item.ItemWriter;
|
||||||
|
import org.springframework.batch.item.UnexpectedInputException;
|
||||||
|
import org.springframework.batch.item.file.FlatFileItemReader;
|
||||||
|
import org.springframework.batch.item.file.mapping.DefaultLineMapper;
|
||||||
|
import org.springframework.batch.item.file.transform.DelimitedLineTokenizer;
|
||||||
|
import org.springframework.batch.item.xml.StaxEventItemWriter;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.beans.factory.annotation.Qualifier;
|
||||||
|
import org.springframework.beans.factory.annotation.Value;
|
||||||
|
import org.springframework.context.annotation.Bean;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
import org.springframework.context.annotation.Profile;
|
||||||
|
import org.springframework.core.io.Resource;
|
||||||
|
import org.springframework.oxm.Marshaller;
|
||||||
|
import org.springframework.oxm.jaxb.Jaxb2Marshaller;
|
||||||
|
|
||||||
|
import java.text.ParseException;
|
||||||
|
|
||||||
|
@Configuration
|
||||||
|
@EnableBatchProcessing
|
||||||
|
@Profile("spring-boot")
|
||||||
|
public class SpringBootBatchConfig {
|
||||||
|
@Autowired
|
||||||
|
private JobBuilderFactory jobBuilderFactory;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private StepBuilderFactory stepBuilderFactory;
|
||||||
|
|
||||||
|
@Value("input/record.csv")
|
||||||
|
private Resource inputCsv;
|
||||||
|
|
||||||
|
@Value("input/recordWithInvalidData.csv")
|
||||||
|
private Resource invalidInputCsv;
|
||||||
|
|
||||||
|
@Value("file:xml/output.xml")
|
||||||
|
private Resource outputXml;
|
||||||
|
|
||||||
|
public ItemReader<Transaction> itemReader(Resource inputData) throws UnexpectedInputException, ParseException {
|
||||||
|
FlatFileItemReader<Transaction> reader = new FlatFileItemReader<>();
|
||||||
|
DelimitedLineTokenizer tokenizer = new DelimitedLineTokenizer();
|
||||||
|
String[] tokens = {"username", "userid", "transactiondate", "amount"};
|
||||||
|
tokenizer.setNames(tokens);
|
||||||
|
reader.setResource(inputData);
|
||||||
|
DefaultLineMapper<Transaction> lineMapper = new DefaultLineMapper<>();
|
||||||
|
lineMapper.setLineTokenizer(tokenizer);
|
||||||
|
lineMapper.setFieldSetMapper(new RecordFieldSetMapper());
|
||||||
|
reader.setLinesToSkip(1);
|
||||||
|
reader.setLineMapper(lineMapper);
|
||||||
|
return reader;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public ItemProcessor<Transaction, Transaction> itemProcessor() {
|
||||||
|
return new CustomItemProcessor();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public ItemProcessor<Transaction, Transaction> skippingItemProcessor() {
|
||||||
|
return new SkippingItemProcessor();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public ItemWriter<Transaction> itemWriter3(Marshaller marshaller) {
|
||||||
|
StaxEventItemWriter<Transaction> itemWriter3 = new StaxEventItemWriter<>();
|
||||||
|
itemWriter3.setMarshaller(marshaller);
|
||||||
|
itemWriter3.setRootTagName("transactionRecord");
|
||||||
|
itemWriter3.setResource(outputXml);
|
||||||
|
return itemWriter3;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public Marshaller marshaller3() {
|
||||||
|
Jaxb2Marshaller marshaller3 = new Jaxb2Marshaller();
|
||||||
|
marshaller3.setClassesToBeBound(Transaction.class);
|
||||||
|
return marshaller3;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
protected Step step1(@Qualifier("itemProcessor") ItemProcessor<Transaction, Transaction> processor, ItemWriter<Transaction> itemWriter3) throws ParseException {
|
||||||
|
return stepBuilderFactory
|
||||||
|
.get("step1")
|
||||||
|
.<Transaction, Transaction> chunk(10)
|
||||||
|
.reader(itemReader(inputCsv))
|
||||||
|
.processor(processor)
|
||||||
|
.writer(itemWriter3)
|
||||||
|
.build();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Bean(name = "firstBatchJob")
|
||||||
|
public Job job(@Qualifier("step1") Step step1) {
|
||||||
|
return jobBuilderFactory.get("firstBatchJob").start(step1).build();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public Step skippingStep(@Qualifier("skippingItemProcessor") ItemProcessor<Transaction, Transaction> processor,
|
||||||
|
ItemWriter<Transaction> itemWriter3) throws ParseException {
|
||||||
|
return stepBuilderFactory
|
||||||
|
.get("skippingStep")
|
||||||
|
.<Transaction, Transaction>chunk(10)
|
||||||
|
.reader(itemReader(invalidInputCsv))
|
||||||
|
.processor(processor)
|
||||||
|
.writer(itemWriter3)
|
||||||
|
.faultTolerant()
|
||||||
|
.skipLimit(2)
|
||||||
|
.skip(MissingUsernameException.class)
|
||||||
|
.skip(NegativeAmountException.class)
|
||||||
|
.build();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Bean(name = "skippingBatchJob")
|
||||||
|
public Job skippingJob(@Qualifier("skippingStep") Step skippingStep) {
|
||||||
|
return jobBuilderFactory
|
||||||
|
.get("skippingBatchJob")
|
||||||
|
.start(skippingStep)
|
||||||
|
.build();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public Step skipPolicyStep(@Qualifier("skippingItemProcessor") ItemProcessor<Transaction, Transaction> processor,
|
||||||
|
ItemWriter<Transaction> itemWriter3) throws ParseException {
|
||||||
|
return stepBuilderFactory
|
||||||
|
.get("skipPolicyStep")
|
||||||
|
.<Transaction, Transaction>chunk(10)
|
||||||
|
.reader(itemReader(invalidInputCsv))
|
||||||
|
.processor(processor)
|
||||||
|
.writer(itemWriter3)
|
||||||
|
.faultTolerant()
|
||||||
|
.skipPolicy(new CustomSkipPolicy())
|
||||||
|
.build();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Bean(name = "skipPolicyBatchJob")
|
||||||
|
public Job skipPolicyBatchJob(@Qualifier("skipPolicyStep") Step skipPolicyStep) {
|
||||||
|
return jobBuilderFactory
|
||||||
|
.get("skipPolicyBatchJob")
|
||||||
|
.start(skipPolicyStep)
|
||||||
|
.build();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user