From 9b2b856661948c6bec5bfee2c8191d203c8cf191 Mon Sep 17 00:00:00 2001 From: Devendra Desale Date: Fri, 11 Dec 2015 15:16:48 +0800 Subject: [PATCH 1/4] Adding spring batch project --- spring-batch-intro/.classpath | 27 ++++++++ spring-batch-intro/.project | 23 +++++++ spring-batch-intro/pom.xml | 45 ++++++++++++++ .../org/baeldung/spring_batch_intro/App.java | 28 +++++++++ .../spring_batch_intro/model/Transaction.java | 54 ++++++++++++++++ .../service/CustomItemProcessor.java | 14 +++++ .../service/RecordFieldSetMapper.java | 33 ++++++++++ .../src/main/resources/input/record.csv | 3 + .../src/main/resources/spring-batch-intro.xml | 61 +++++++++++++++++++ .../src/main/resources/spring.xml | 44 +++++++++++++ spring-batch-intro/xml/output.xml | 1 + 11 files changed, 333 insertions(+) create mode 100644 spring-batch-intro/.classpath create mode 100644 spring-batch-intro/.project create mode 100644 spring-batch-intro/pom.xml create mode 100644 spring-batch-intro/src/main/java/org/baeldung/spring_batch_intro/App.java create mode 100644 spring-batch-intro/src/main/java/org/baeldung/spring_batch_intro/model/Transaction.java create mode 100644 spring-batch-intro/src/main/java/org/baeldung/spring_batch_intro/service/CustomItemProcessor.java create mode 100644 spring-batch-intro/src/main/java/org/baeldung/spring_batch_intro/service/RecordFieldSetMapper.java create mode 100644 spring-batch-intro/src/main/resources/input/record.csv create mode 100644 spring-batch-intro/src/main/resources/spring-batch-intro.xml create mode 100644 spring-batch-intro/src/main/resources/spring.xml create mode 100644 spring-batch-intro/xml/output.xml diff --git a/spring-batch-intro/.classpath b/spring-batch-intro/.classpath new file mode 100644 index 0000000000..395dbde027 --- /dev/null +++ b/spring-batch-intro/.classpath @@ -0,0 +1,27 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/spring-batch-intro/.project b/spring-batch-intro/.project new file mode 100644 index 0000000000..032f8a9541 --- /dev/null +++ b/spring-batch-intro/.project @@ -0,0 +1,23 @@ + + + spring-batch-intro + + + + + + org.eclipse.jdt.core.javabuilder + + + + + org.eclipse.m2e.core.maven2Builder + + + + + + org.eclipse.jdt.core.javanature + org.eclipse.m2e.core.maven2Nature + + diff --git a/spring-batch-intro/pom.xml b/spring-batch-intro/pom.xml new file mode 100644 index 0000000000..c68608d4ae --- /dev/null +++ b/spring-batch-intro/pom.xml @@ -0,0 +1,45 @@ + + 4.0.0 + + org.baeldung + spring-batch-intro + 0.1-SNAPSHOT + jar + + spring-batch-intro + http://maven.apache.org + + + UTF-8 + 4.0.2.RELEASE + 3.0.5.RELEASE + 3.8.11.2 + + + + + + org.xerial + sqlite-jdbc + ${sqlite.version} + + + org.springframework + spring-oxm + ${spring.version} + + + org.springframework + spring-jdbc + ${spring.version} + + + + org.springframework.batch + spring-batch-core + ${spring.batch.version} + + + + diff --git a/spring-batch-intro/src/main/java/org/baeldung/spring_batch_intro/App.java b/spring-batch-intro/src/main/java/org/baeldung/spring_batch_intro/App.java new file mode 100644 index 0000000000..7e6b080851 --- /dev/null +++ b/spring-batch-intro/src/main/java/org/baeldung/spring_batch_intro/App.java @@ -0,0 +1,28 @@ +package org.baeldung.spring_batch_intro; + +import org.springframework.batch.core.Job; +import org.springframework.batch.core.JobExecution; +import org.springframework.batch.core.JobParameters; +import org.springframework.batch.core.launch.JobLauncher; +import org.springframework.context.ApplicationContext; +import org.springframework.context.support.ClassPathXmlApplicationContext; + +public class App { + public static void main(String[] args) { + + ApplicationContext context = new ClassPathXmlApplicationContext( + "/spring-batch-intro.xml"); + + JobLauncher jobLauncher = (JobLauncher) context.getBean("jobLauncher"); + Job job = (Job) context.getBean("firstBatchJob"); + System.out.println("Starting the batch job"); + try { + JobExecution execution = jobLauncher.run(job, new JobParameters()); + System.out.println("Job Status : " + execution.getStatus()); + System.out.println("Job completed"); + } catch (Exception e) { + e.printStackTrace(); + System.out.println("Job failed"); + } + } +} \ No newline at end of file diff --git a/spring-batch-intro/src/main/java/org/baeldung/spring_batch_intro/model/Transaction.java b/spring-batch-intro/src/main/java/org/baeldung/spring_batch_intro/model/Transaction.java new file mode 100644 index 0000000000..815af78cd4 --- /dev/null +++ b/spring-batch-intro/src/main/java/org/baeldung/spring_batch_intro/model/Transaction.java @@ -0,0 +1,54 @@ +package org.baeldung.spring_batch_intro.model; + +import java.util.Date; + +import javax.xml.bind.annotation.XmlRootElement; + +@SuppressWarnings("restriction") +@XmlRootElement(name = "transactionRecord") +public class Transaction { + private String username; + private int userId; + private Date transactionDate; + private double amount; + + public String getUsername() { + return username; + } + + public void setUsername(String username) { + this.username = username; + } + + public int getUserId() { + return userId; + } + + public void setUserId(int userId) { + this.userId = userId; + } + + public Date getTransactionDate() { + return transactionDate; + } + + public void setTransactionDate(Date transactionDate) { + this.transactionDate = transactionDate; + } + + public double getAmount() { + return amount; + } + + public void setAmount(double amount) { + this.amount = amount; + } + + @Override + public String toString() { + return "Transaction [username=" + username + ", userId=" + userId + + ", transactionDate=" + transactionDate + ", amount=" + amount + + "]"; + } + +} diff --git a/spring-batch-intro/src/main/java/org/baeldung/spring_batch_intro/service/CustomItemProcessor.java b/spring-batch-intro/src/main/java/org/baeldung/spring_batch_intro/service/CustomItemProcessor.java new file mode 100644 index 0000000000..be1127c5e7 --- /dev/null +++ b/spring-batch-intro/src/main/java/org/baeldung/spring_batch_intro/service/CustomItemProcessor.java @@ -0,0 +1,14 @@ +package org.baeldung.spring_batch_intro.service; + + +import org.baeldung.spring_batch_intro.model.Transaction; +import org.springframework.batch.item.ItemProcessor; + +public class CustomItemProcessor implements ItemProcessor { + + public Transaction process(Transaction item) throws Exception { + + System.out.println("Processing..." + item); + return item; + } +} \ No newline at end of file diff --git a/spring-batch-intro/src/main/java/org/baeldung/spring_batch_intro/service/RecordFieldSetMapper.java b/spring-batch-intro/src/main/java/org/baeldung/spring_batch_intro/service/RecordFieldSetMapper.java new file mode 100644 index 0000000000..2b8f897e2a --- /dev/null +++ b/spring-batch-intro/src/main/java/org/baeldung/spring_batch_intro/service/RecordFieldSetMapper.java @@ -0,0 +1,33 @@ +package org.baeldung.spring_batch_intro.service; + +import java.text.ParseException; +import java.text.SimpleDateFormat; + +import org.baeldung.spring_batch_intro.model.Transaction; +import org.springframework.batch.item.file.mapping.FieldSetMapper; +import org.springframework.batch.item.file.transform.FieldSet; +import org.springframework.validation.BindException; + +public class RecordFieldSetMapper implements FieldSetMapper { + + public Transaction mapFieldSet(FieldSet fieldSet) throws BindException { + + SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy"); + Transaction transaction = new Transaction(); + + transaction.setUsername(fieldSet.readString("username")); + transaction.setUserId(fieldSet.readInt(1)); + transaction.setAmount(fieldSet.readDouble(3)); + //Converting the date + String dateString = fieldSet.readString(2); + try { + transaction.setTransactionDate(dateFormat.parse(dateString)); + } catch (ParseException e) { + e.printStackTrace(); + } + + return transaction; + + } + +} diff --git a/spring-batch-intro/src/main/resources/input/record.csv b/spring-batch-intro/src/main/resources/input/record.csv new file mode 100644 index 0000000000..3a1437eed5 --- /dev/null +++ b/spring-batch-intro/src/main/resources/input/record.csv @@ -0,0 +1,3 @@ +devendra, 1234, 31/10/2015, 10000 +john, 2134, 3/12/2015, 12321 +robin, 2134, 2/02/2015, 23411 \ No newline at end of file diff --git a/spring-batch-intro/src/main/resources/spring-batch-intro.xml b/spring-batch-intro/src/main/resources/spring-batch-intro.xml new file mode 100644 index 0000000000..06918d8754 --- /dev/null +++ b/spring-batch-intro/src/main/resources/spring-batch-intro.xml @@ -0,0 +1,61 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + org.baeldung.spring_batch_intro.model.Transaction + + + + + + + + + + + + + + diff --git a/spring-batch-intro/src/main/resources/spring.xml b/spring-batch-intro/src/main/resources/spring.xml new file mode 100644 index 0000000000..d286662002 --- /dev/null +++ b/spring-batch-intro/src/main/resources/spring.xml @@ -0,0 +1,44 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/spring-batch-intro/xml/output.xml b/spring-batch-intro/xml/output.xml new file mode 100644 index 0000000000..9e57fa38f2 --- /dev/null +++ b/spring-batch-intro/xml/output.xml @@ -0,0 +1 @@ +10000.02015-10-31T00:00:00+08:001234devendra12321.02015-12-03T00:00:00+08:002134john23411.02015-02-02T00:00:00+08:002134robin \ No newline at end of file From 57b27f6c9c952a96aed157935873d54dd814bd4e Mon Sep 17 00:00:00 2001 From: Devendra Desale Date: Sun, 13 Dec 2015 22:12:09 +0800 Subject: [PATCH 2/4] adding java based spring batch configuration --- spring-batch-intro/pom.xml | 75 ++++++------- .../org/baeldung/spring_batch_intro/App.java | 42 ++++--- .../spring_batch_intro/SpringBatchConfig.java | 92 +++++++++++++++ .../spring_batch_intro/SpringConfig.java | 75 +++++++++++++ .../spring_batch_intro/model/Transaction.java | 68 ++++++------ .../service/CustomItemProcessor.java | 13 +-- .../service/RecordFieldSetMapper.java | 34 +++--- .../src/main/resources/spring-batch-intro.xml | 105 +++++++++--------- .../src/main/resources/spring.xml | 67 +++++------ 9 files changed, 376 insertions(+), 195 deletions(-) create mode 100644 spring-batch-intro/src/main/java/org/baeldung/spring_batch_intro/SpringBatchConfig.java create mode 100644 spring-batch-intro/src/main/java/org/baeldung/spring_batch_intro/SpringConfig.java diff --git a/spring-batch-intro/pom.xml b/spring-batch-intro/pom.xml index c68608d4ae..28d48c594e 100644 --- a/spring-batch-intro/pom.xml +++ b/spring-batch-intro/pom.xml @@ -1,45 +1,44 @@ - 4.0.0 + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> + 4.0.0 - org.baeldung - spring-batch-intro - 0.1-SNAPSHOT - jar + org.baeldung + spring-batch-intro + 0.1-SNAPSHOT + jar - spring-batch-intro - http://maven.apache.org + spring-batch-intro + http://maven.apache.org - - UTF-8 - 4.0.2.RELEASE - 3.0.5.RELEASE - 3.8.11.2 - + + UTF-8 + 4.2.0.RELEASE + 3.0.5.RELEASE + 3.8.11.2 + - - - - org.xerial - sqlite-jdbc - ${sqlite.version} - - - org.springframework - spring-oxm - ${spring.version} - - - org.springframework - spring-jdbc - ${spring.version} - - - - org.springframework.batch - spring-batch-core - ${spring.batch.version} - - + + + + org.xerial + sqlite-jdbc + ${sqlite.version} + + + org.springframework + spring-oxm + ${spring.version} + + + org.springframework + spring-jdbc + ${spring.version} + + + org.springframework.batch + spring-batch-core + ${spring.batch.version} + + diff --git a/spring-batch-intro/src/main/java/org/baeldung/spring_batch_intro/App.java b/spring-batch-intro/src/main/java/org/baeldung/spring_batch_intro/App.java index 7e6b080851..a2f8f38e0f 100644 --- a/spring-batch-intro/src/main/java/org/baeldung/spring_batch_intro/App.java +++ b/spring-batch-intro/src/main/java/org/baeldung/spring_batch_intro/App.java @@ -1,28 +1,38 @@ package org.baeldung.spring_batch_intro; +import javax.swing.Spring; + import org.springframework.batch.core.Job; import org.springframework.batch.core.JobExecution; import org.springframework.batch.core.JobParameters; import org.springframework.batch.core.launch.JobLauncher; import org.springframework.context.ApplicationContext; +import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class App { - public static void main(String[] args) { + public static void main(String[] args) { + // Spring Java config + AnnotationConfigApplicationContext context = new + AnnotationConfigApplicationContext(); + context.register(SpringConfig.class); + context.register(SpringBatchConfig.class); + context.refresh(); - ApplicationContext context = new ClassPathXmlApplicationContext( - "/spring-batch-intro.xml"); - - JobLauncher jobLauncher = (JobLauncher) context.getBean("jobLauncher"); - Job job = (Job) context.getBean("firstBatchJob"); - System.out.println("Starting the batch job"); - try { - JobExecution execution = jobLauncher.run(job, new JobParameters()); - System.out.println("Job Status : " + execution.getStatus()); - System.out.println("Job completed"); - } catch (Exception e) { - e.printStackTrace(); - System.out.println("Job failed"); - } - } + // Spring xml config +// ApplicationContext context = new ClassPathXmlApplicationContext( +// "spring-batch-intro.xml"); + + JobLauncher jobLauncher = (JobLauncher) context.getBean("jobLauncher"); + Job job = (Job) context.getBean("firstBatchJob"); + System.out.println("Starting the batch job"); + try { + JobExecution execution = jobLauncher.run(job, new JobParameters()); + System.out.println("Job Status : " + execution.getStatus()); + System.out.println("Job completed"); + } catch (Exception e) { + e.printStackTrace(); + System.out.println("Job failed"); + } + } } \ No newline at end of file diff --git a/spring-batch-intro/src/main/java/org/baeldung/spring_batch_intro/SpringBatchConfig.java b/spring-batch-intro/src/main/java/org/baeldung/spring_batch_intro/SpringBatchConfig.java new file mode 100644 index 0000000000..c892e24fd8 --- /dev/null +++ b/spring-batch-intro/src/main/java/org/baeldung/spring_batch_intro/SpringBatchConfig.java @@ -0,0 +1,92 @@ +package org.baeldung.spring_batch_intro; + +import java.net.MalformedURLException; +import java.text.ParseException; + +import org.baeldung.spring_batch_intro.model.Transaction; +import org.baeldung.spring_batch_intro.service.CustomItemProcessor; +import org.baeldung.spring_batch_intro.service.RecordFieldSetMapper; +import org.springframework.batch.core.Job; +import org.springframework.batch.core.Step; +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.core.io.Resource; +import org.springframework.oxm.Marshaller; +import org.springframework.oxm.jaxb.Jaxb2Marshaller; + +public class SpringBatchConfig { + @Autowired + private JobBuilderFactory jobs; + + @Autowired + private StepBuilderFactory steps; + + @Value("input/record.csv") + private Resource inputCsv; + + @Value("file:xml/output.xml") + private Resource outputXml; + + @Bean + public ItemReader itemReader() + throws UnexpectedInputException, ParseException, Exception { + FlatFileItemReader reader = new FlatFileItemReader(); + DelimitedLineTokenizer tokenizer = new DelimitedLineTokenizer(); + String[] tokens = { "username", "userid", "transactiondate", "amount" }; + tokenizer.setNames(tokens); + reader.setResource(inputCsv); + DefaultLineMapper lineMapper = new DefaultLineMapper(); + lineMapper.setLineTokenizer(tokenizer); + lineMapper.setFieldSetMapper(new RecordFieldSetMapper()); + reader.setLineMapper(lineMapper); + return reader; + } + + @Bean + public ItemProcessor itemProcessor() { + return new CustomItemProcessor(); + } + + @Bean + public ItemWriter itemWriter(Marshaller marshaller) + throws MalformedURLException { + StaxEventItemWriter itemWriter = new StaxEventItemWriter(); + itemWriter.setMarshaller(marshaller); + itemWriter.setRootTagName("transactionRecord"); + itemWriter.setResource(outputXml); + return itemWriter; + } + + @Bean + public Marshaller marshaller() { + Jaxb2Marshaller marshaller = new Jaxb2Marshaller(); + marshaller.setClassesToBeBound(new Class[] { Transaction.class }); + return marshaller; + } + + @Bean + protected Step step1(ItemReader reader, + ItemProcessor processor, + ItemWriter writer) { + return steps.get("step1"). chunk(10) + .reader(reader).processor(processor).writer(writer).build(); + } + + @Bean(name = "firstBatchJob") + public Job job(@Qualifier("step1") Step step1) { + return jobs.get("firstBatchJob").start(step1).build(); + } + +} diff --git a/spring-batch-intro/src/main/java/org/baeldung/spring_batch_intro/SpringConfig.java b/spring-batch-intro/src/main/java/org/baeldung/spring_batch_intro/SpringConfig.java new file mode 100644 index 0000000000..79676fbe4e --- /dev/null +++ b/spring-batch-intro/src/main/java/org/baeldung/spring_batch_intro/SpringConfig.java @@ -0,0 +1,75 @@ +package org.baeldung.spring_batch_intro; + +import java.net.MalformedURLException; + +import javax.sql.DataSource; + +import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing; +import org.springframework.batch.core.launch.JobLauncher; +import org.springframework.batch.core.launch.support.SimpleJobLauncher; +import org.springframework.batch.core.repository.JobRepository; +import org.springframework.batch.core.repository.support.JobRepositoryFactoryBean; +import org.springframework.batch.support.transaction.ResourcelessTransactionManager; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.core.io.Resource; +import org.springframework.jdbc.datasource.DriverManagerDataSource; +import org.springframework.jdbc.datasource.init.DataSourceInitializer; +import org.springframework.jdbc.datasource.init.ResourceDatabasePopulator; +import org.springframework.transaction.PlatformTransactionManager; + +@Configuration +@EnableBatchProcessing +public class SpringConfig { + + @Value("org/springframework/batch/core/schema-drop-sqlite.sql") + private Resource dropReopsitoryTables; + + @Value("org/springframework/batch/core/schema-sqlite.sql") + private Resource dataReopsitorySchema; + + @Bean + public DataSource dataSource() { + DriverManagerDataSource dataSource = new DriverManagerDataSource(); + dataSource.setDriverClassName("org.sqlite.JDBC"); + dataSource.setUrl("jdbc:sqlite:repository.sqlite"); + return dataSource; + } + + @Bean + public DataSourceInitializer dataSourceInitializer(DataSource dataSource) + throws MalformedURLException { + ResourceDatabasePopulator databasePopulator = new ResourceDatabasePopulator(); + + databasePopulator.addScript(dropReopsitoryTables); + databasePopulator.addScript(dataReopsitorySchema); + databasePopulator.setIgnoreFailedDrops(true); + + DataSourceInitializer initializer = new DataSourceInitializer(); + initializer.setDataSource(dataSource); + initializer.setDatabasePopulator(databasePopulator); + + return initializer; + } + + private JobRepository getJobRepository() throws Exception { + JobRepositoryFactoryBean factory = new JobRepositoryFactoryBean(); + factory.setDataSource(dataSource()); + factory.setTransactionManager(getTransactionManager()); + factory.afterPropertiesSet(); + return (JobRepository) factory.getObject(); + } + + private PlatformTransactionManager getTransactionManager() { + return new ResourcelessTransactionManager(); + } + + public JobLauncher getJobLauncher() throws Exception { + SimpleJobLauncher jobLauncher = new SimpleJobLauncher(); + jobLauncher.setJobRepository(getJobRepository()); + jobLauncher.afterPropertiesSet(); + return jobLauncher; + } + +} \ No newline at end of file diff --git a/spring-batch-intro/src/main/java/org/baeldung/spring_batch_intro/model/Transaction.java b/spring-batch-intro/src/main/java/org/baeldung/spring_batch_intro/model/Transaction.java index 815af78cd4..0108dcf501 100644 --- a/spring-batch-intro/src/main/java/org/baeldung/spring_batch_intro/model/Transaction.java +++ b/spring-batch-intro/src/main/java/org/baeldung/spring_batch_intro/model/Transaction.java @@ -7,48 +7,48 @@ import javax.xml.bind.annotation.XmlRootElement; @SuppressWarnings("restriction") @XmlRootElement(name = "transactionRecord") public class Transaction { - private String username; - private int userId; - private Date transactionDate; - private double amount; + private String username; + private int userId; + private Date transactionDate; + private double amount; - public String getUsername() { - return username; - } + public String getUsername() { + return username; + } - public void setUsername(String username) { - this.username = username; - } + public void setUsername(String username) { + this.username = username; + } - public int getUserId() { - return userId; - } + public int getUserId() { + return userId; + } - public void setUserId(int userId) { - this.userId = userId; - } + public void setUserId(int userId) { + this.userId = userId; + } - public Date getTransactionDate() { - return transactionDate; - } + public Date getTransactionDate() { + return transactionDate; + } - public void setTransactionDate(Date transactionDate) { - this.transactionDate = transactionDate; - } + public void setTransactionDate(Date transactionDate) { + this.transactionDate = transactionDate; + } - public double getAmount() { - return amount; - } + public double getAmount() { + return amount; + } - public void setAmount(double amount) { - this.amount = amount; - } + public void setAmount(double amount) { + this.amount = amount; + } - @Override - public String toString() { - return "Transaction [username=" + username + ", userId=" + userId - + ", transactionDate=" + transactionDate + ", amount=" + amount - + "]"; - } + @Override + public String toString() { + return "Transaction [username=" + username + ", userId=" + userId + + ", transactionDate=" + transactionDate + ", amount=" + amount + + "]"; + } } diff --git a/spring-batch-intro/src/main/java/org/baeldung/spring_batch_intro/service/CustomItemProcessor.java b/spring-batch-intro/src/main/java/org/baeldung/spring_batch_intro/service/CustomItemProcessor.java index be1127c5e7..487dbb5efb 100644 --- a/spring-batch-intro/src/main/java/org/baeldung/spring_batch_intro/service/CustomItemProcessor.java +++ b/spring-batch-intro/src/main/java/org/baeldung/spring_batch_intro/service/CustomItemProcessor.java @@ -1,14 +1,13 @@ package org.baeldung.spring_batch_intro.service; - import org.baeldung.spring_batch_intro.model.Transaction; import org.springframework.batch.item.ItemProcessor; -public class CustomItemProcessor implements ItemProcessor { +public class CustomItemProcessor implements + ItemProcessor { - public Transaction process(Transaction item) throws Exception { - - System.out.println("Processing..." + item); - return item; - } + public Transaction process(Transaction item) throws Exception { + System.out.println("Processing..." + item); + return item; + } } \ No newline at end of file diff --git a/spring-batch-intro/src/main/java/org/baeldung/spring_batch_intro/service/RecordFieldSetMapper.java b/spring-batch-intro/src/main/java/org/baeldung/spring_batch_intro/service/RecordFieldSetMapper.java index 2b8f897e2a..0955b57c76 100644 --- a/spring-batch-intro/src/main/java/org/baeldung/spring_batch_intro/service/RecordFieldSetMapper.java +++ b/spring-batch-intro/src/main/java/org/baeldung/spring_batch_intro/service/RecordFieldSetMapper.java @@ -10,24 +10,24 @@ import org.springframework.validation.BindException; public class RecordFieldSetMapper implements FieldSetMapper { - public Transaction mapFieldSet(FieldSet fieldSet) throws BindException { - - SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy"); - Transaction transaction = new Transaction(); - - transaction.setUsername(fieldSet.readString("username")); - transaction.setUserId(fieldSet.readInt(1)); - transaction.setAmount(fieldSet.readDouble(3)); - //Converting the date - String dateString = fieldSet.readString(2); - try { - transaction.setTransactionDate(dateFormat.parse(dateString)); - } catch (ParseException e) { - e.printStackTrace(); - } + public Transaction mapFieldSet(FieldSet fieldSet) throws BindException { - return transaction; + SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy"); + Transaction transaction = new Transaction(); - } + transaction.setUsername(fieldSet.readString("username")); + transaction.setUserId(fieldSet.readInt(1)); + transaction.setAmount(fieldSet.readDouble(3)); + // Converting the date + String dateString = fieldSet.readString(2); + try { + transaction.setTransactionDate(dateFormat.parse(dateString)); + } catch (ParseException e) { + e.printStackTrace(); + } + + return transaction; + + } } diff --git a/spring-batch-intro/src/main/resources/spring-batch-intro.xml b/spring-batch-intro/src/main/resources/spring-batch-intro.xml index 06918d8754..6daa0358d9 100644 --- a/spring-batch-intro/src/main/resources/spring-batch-intro.xml +++ b/spring-batch-intro/src/main/resources/spring-batch-intro.xml @@ -1,61 +1,66 @@ - - - - - + - + - - - - - - - - - + - - - - - - - - - - - - + - - - - org.baeldung.spring_batch_intro.model.Transaction - - - + + + + + + + + + - - - - - - - - - + + + + + + + + + + + + + + + + + org.baeldung.spring_batch_intro.model.Transaction + + + + + + + + + + + + + + diff --git a/spring-batch-intro/src/main/resources/spring.xml b/spring-batch-intro/src/main/resources/spring.xml index d286662002..dea261c5e6 100644 --- a/spring-batch-intro/src/main/resources/spring.xml +++ b/spring-batch-intro/src/main/resources/spring.xml @@ -1,44 +1,45 @@ + http://www.springframework.org/schema/jdbc/spring-jdbc-4.2.xsd"> - - - - - - - + + + + + + + - - - - - + + + + + - - + + - - - - - - + + + + + + - + - - - + + + \ No newline at end of file From 09bc7399ddddd88c744b37afad6bbf321a36c4e0 Mon Sep 17 00:00:00 2001 From: Devendra Desale Date: Mon, 14 Dec 2015 14:35:09 +0800 Subject: [PATCH 3/4] updating generic Exception and linewrappings. --- .../org/baeldung/spring_batch_intro/App.java | 16 +++++++--------- .../spring_batch_intro/SpringBatchConfig.java | 10 +++++----- .../spring_batch_intro/SpringConfig.java | 6 +++++- .../spring_batch_intro/model/Transaction.java | 6 ++++-- .../service/CustomItemProcessor.java | 4 ++-- .../service/RecordFieldSetMapper.java | 6 ++++-- .../src/main/resources/spring-batch-intro.xml | 2 -- 7 files changed, 27 insertions(+), 23 deletions(-) diff --git a/spring-batch-intro/src/main/java/org/baeldung/spring_batch_intro/App.java b/spring-batch-intro/src/main/java/org/baeldung/spring_batch_intro/App.java index a2f8f38e0f..db963fff20 100644 --- a/spring-batch-intro/src/main/java/org/baeldung/spring_batch_intro/App.java +++ b/spring-batch-intro/src/main/java/org/baeldung/spring_batch_intro/App.java @@ -1,7 +1,5 @@ package org.baeldung.spring_batch_intro; -import javax.swing.Spring; - import org.springframework.batch.core.Job; import org.springframework.batch.core.JobExecution; import org.springframework.batch.core.JobParameters; @@ -10,18 +8,18 @@ import org.springframework.context.ApplicationContext; import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; + public class App { public static void main(String[] args) { // Spring Java config - AnnotationConfigApplicationContext context = new - AnnotationConfigApplicationContext(); - context.register(SpringConfig.class); - context.register(SpringBatchConfig.class); - context.refresh(); + AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(); + context.register(SpringConfig.class); + context.register(SpringBatchConfig.class); + context.refresh(); // Spring xml config -// ApplicationContext context = new ClassPathXmlApplicationContext( -// "spring-batch-intro.xml"); + // ApplicationContext context = new ClassPathXmlApplicationContext( + // "spring-batch-intro.xml"); JobLauncher jobLauncher = (JobLauncher) context.getBean("jobLauncher"); Job job = (Job) context.getBean("firstBatchJob"); diff --git a/spring-batch-intro/src/main/java/org/baeldung/spring_batch_intro/SpringBatchConfig.java b/spring-batch-intro/src/main/java/org/baeldung/spring_batch_intro/SpringBatchConfig.java index c892e24fd8..f0e3b364b6 100644 --- a/spring-batch-intro/src/main/java/org/baeldung/spring_batch_intro/SpringBatchConfig.java +++ b/spring-batch-intro/src/main/java/org/baeldung/spring_batch_intro/SpringBatchConfig.java @@ -41,7 +41,7 @@ public class SpringBatchConfig { @Bean public ItemReader itemReader() - throws UnexpectedInputException, ParseException, Exception { + throws UnexpectedInputException, ParseException { FlatFileItemReader reader = new FlatFileItemReader(); DelimitedLineTokenizer tokenizer = new DelimitedLineTokenizer(); String[] tokens = { "username", "userid", "transactiondate", "amount" }; @@ -61,7 +61,7 @@ public class SpringBatchConfig { @Bean public ItemWriter itemWriter(Marshaller marshaller) - throws MalformedURLException { + throws MalformedURLException { StaxEventItemWriter itemWriter = new StaxEventItemWriter(); itemWriter.setMarshaller(marshaller); itemWriter.setRootTagName("transactionRecord"); @@ -78,10 +78,10 @@ public class SpringBatchConfig { @Bean protected Step step1(ItemReader reader, - ItemProcessor processor, - ItemWriter writer) { + ItemProcessor processor, + ItemWriter writer) { return steps.get("step1"). chunk(10) - .reader(reader).processor(processor).writer(writer).build(); + .reader(reader).processor(processor).writer(writer).build(); } @Bean(name = "firstBatchJob") diff --git a/spring-batch-intro/src/main/java/org/baeldung/spring_batch_intro/SpringConfig.java b/spring-batch-intro/src/main/java/org/baeldung/spring_batch_intro/SpringConfig.java index 79676fbe4e..91366d8721 100644 --- a/spring-batch-intro/src/main/java/org/baeldung/spring_batch_intro/SpringConfig.java +++ b/spring-batch-intro/src/main/java/org/baeldung/spring_batch_intro/SpringConfig.java @@ -39,7 +39,7 @@ public class SpringConfig { @Bean public DataSourceInitializer dataSourceInitializer(DataSource dataSource) - throws MalformedURLException { + throws MalformedURLException { ResourceDatabasePopulator databasePopulator = new ResourceDatabasePopulator(); databasePopulator.addScript(dropReopsitoryTables); @@ -57,6 +57,8 @@ public class SpringConfig { JobRepositoryFactoryBean factory = new JobRepositoryFactoryBean(); factory.setDataSource(dataSource()); factory.setTransactionManager(getTransactionManager()); + // JobRepositoryFactoryBean's methods Throws Generic Exception, + // it would have been better to have a specific one factory.afterPropertiesSet(); return (JobRepository) factory.getObject(); } @@ -67,6 +69,8 @@ public class SpringConfig { public JobLauncher getJobLauncher() throws Exception { SimpleJobLauncher jobLauncher = new SimpleJobLauncher(); + // SimpleJobLauncher's methods Throws Generic Exception, + // it would have been better to have a specific one jobLauncher.setJobRepository(getJobRepository()); jobLauncher.afterPropertiesSet(); return jobLauncher; diff --git a/spring-batch-intro/src/main/java/org/baeldung/spring_batch_intro/model/Transaction.java b/spring-batch-intro/src/main/java/org/baeldung/spring_batch_intro/model/Transaction.java index 0108dcf501..6e80298ff0 100644 --- a/spring-batch-intro/src/main/java/org/baeldung/spring_batch_intro/model/Transaction.java +++ b/spring-batch-intro/src/main/java/org/baeldung/spring_batch_intro/model/Transaction.java @@ -12,6 +12,8 @@ public class Transaction { private Date transactionDate; private double amount; + /* getters and setters for the attributes */ + public String getUsername() { return username; } @@ -47,8 +49,8 @@ public class Transaction { @Override public String toString() { return "Transaction [username=" + username + ", userId=" + userId - + ", transactionDate=" + transactionDate + ", amount=" + amount - + "]"; + + ", transactionDate=" + transactionDate + ", amount=" + amount + + "]"; } } diff --git a/spring-batch-intro/src/main/java/org/baeldung/spring_batch_intro/service/CustomItemProcessor.java b/spring-batch-intro/src/main/java/org/baeldung/spring_batch_intro/service/CustomItemProcessor.java index 487dbb5efb..baabe79409 100644 --- a/spring-batch-intro/src/main/java/org/baeldung/spring_batch_intro/service/CustomItemProcessor.java +++ b/spring-batch-intro/src/main/java/org/baeldung/spring_batch_intro/service/CustomItemProcessor.java @@ -4,9 +4,9 @@ import org.baeldung.spring_batch_intro.model.Transaction; import org.springframework.batch.item.ItemProcessor; public class CustomItemProcessor implements - ItemProcessor { + ItemProcessor { - public Transaction process(Transaction item) throws Exception { + public Transaction process(Transaction item) { System.out.println("Processing..." + item); return item; } diff --git a/spring-batch-intro/src/main/java/org/baeldung/spring_batch_intro/service/RecordFieldSetMapper.java b/spring-batch-intro/src/main/java/org/baeldung/spring_batch_intro/service/RecordFieldSetMapper.java index 0955b57c76..94f9e7d94e 100644 --- a/spring-batch-intro/src/main/java/org/baeldung/spring_batch_intro/service/RecordFieldSetMapper.java +++ b/spring-batch-intro/src/main/java/org/baeldung/spring_batch_intro/service/RecordFieldSetMapper.java @@ -14,9 +14,11 @@ public class RecordFieldSetMapper implements FieldSetMapper { SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy"); Transaction transaction = new Transaction(); - + // you can either use the indices or custom names + // I personally prefer the custom names easy for debugging and + // validating the pipelines transaction.setUsername(fieldSet.readString("username")); - transaction.setUserId(fieldSet.readInt(1)); + transaction.setUserId(fieldSet.readInt("userid")); transaction.setAmount(fieldSet.readDouble(3)); // Converting the date String dateString = fieldSet.readString(2); diff --git a/spring-batch-intro/src/main/resources/spring-batch-intro.xml b/spring-batch-intro/src/main/resources/spring-batch-intro.xml index 6daa0358d9..3b1f11521f 100644 --- a/spring-batch-intro/src/main/resources/spring-batch-intro.xml +++ b/spring-batch-intro/src/main/resources/spring-batch-intro.xml @@ -8,8 +8,6 @@ - - From b7bbd201557feeaa79e129c5284234e15687a71d Mon Sep 17 00:00:00 2001 From: Devendra Desale Date: Wed, 23 Dec 2015 14:17:31 +0530 Subject: [PATCH 4/4] updated header to csv --- .../org/baeldung/spring_batch_intro/App.java | 2 +- .../spring_batch_intro/SpringBatchConfig.java | 1 + .../src/main/resources/input/record.csv | 1 + .../src/main/resources/spring-batch-intro.xml | 31 +++++++------------ spring-batch-intro/xml/output.xml | 2 +- 5 files changed, 16 insertions(+), 21 deletions(-) diff --git a/spring-batch-intro/src/main/java/org/baeldung/spring_batch_intro/App.java b/spring-batch-intro/src/main/java/org/baeldung/spring_batch_intro/App.java index db963fff20..d6af37595c 100644 --- a/spring-batch-intro/src/main/java/org/baeldung/spring_batch_intro/App.java +++ b/spring-batch-intro/src/main/java/org/baeldung/spring_batch_intro/App.java @@ -27,7 +27,7 @@ public class App { try { JobExecution execution = jobLauncher.run(job, new JobParameters()); System.out.println("Job Status : " + execution.getStatus()); - System.out.println("Job completed"); + System.out.println("Job succeeded"); } catch (Exception e) { e.printStackTrace(); System.out.println("Job failed"); diff --git a/spring-batch-intro/src/main/java/org/baeldung/spring_batch_intro/SpringBatchConfig.java b/spring-batch-intro/src/main/java/org/baeldung/spring_batch_intro/SpringBatchConfig.java index f0e3b364b6..a024cbc04e 100644 --- a/spring-batch-intro/src/main/java/org/baeldung/spring_batch_intro/SpringBatchConfig.java +++ b/spring-batch-intro/src/main/java/org/baeldung/spring_batch_intro/SpringBatchConfig.java @@ -50,6 +50,7 @@ public class SpringBatchConfig { DefaultLineMapper lineMapper = new DefaultLineMapper(); lineMapper.setLineTokenizer(tokenizer); lineMapper.setFieldSetMapper(new RecordFieldSetMapper()); + reader.setLinesToSkip(1); reader.setLineMapper(lineMapper); return reader; } diff --git a/spring-batch-intro/src/main/resources/input/record.csv b/spring-batch-intro/src/main/resources/input/record.csv index 3a1437eed5..e554becb2a 100644 --- a/spring-batch-intro/src/main/resources/input/record.csv +++ b/spring-batch-intro/src/main/resources/input/record.csv @@ -1,3 +1,4 @@ +username, user_id, transaction_date, transaction_amount devendra, 1234, 31/10/2015, 10000 john, 2134, 3/12/2015, 12321 robin, 2134, 2/02/2015, 23411 \ No newline at end of file diff --git a/spring-batch-intro/src/main/resources/spring-batch-intro.xml b/spring-batch-intro/src/main/resources/spring-batch-intro.xml index 3b1f11521f..93606d232f 100644 --- a/spring-batch-intro/src/main/resources/spring-batch-intro.xml +++ b/spring-batch-intro/src/main/resources/spring-batch-intro.xml @@ -1,18 +1,15 @@ - + http://www.springframework.org/schema/batch/spring-batch-3.0.xsd + http://www.springframework.org/schema/beans + http://www.springframework.org/schema/beans/spring-beans-4.2.xsd +"> - - + - @@ -20,23 +17,21 @@ + value="username,userid,transaction_date,transaction_amount" /> - + - + - + @@ -45,13 +40,11 @@ - org.baeldung.spring_batch_intro.model.Transaction - + org.baeldung.spring_batch_intro.model.Transaction - diff --git a/spring-batch-intro/xml/output.xml b/spring-batch-intro/xml/output.xml index 9e57fa38f2..acf4969341 100644 --- a/spring-batch-intro/xml/output.xml +++ b/spring-batch-intro/xml/output.xml @@ -1 +1 @@ -10000.02015-10-31T00:00:00+08:001234devendra12321.02015-12-03T00:00:00+08:002134john23411.02015-02-02T00:00:00+08:002134robin \ No newline at end of file +10000.02015-10-31T00:00:00+05:301234devendra12321.02015-12-03T00:00:00+05:302134john23411.02015-02-02T00:00:00+05:302134robin \ No newline at end of file