Skip to content

Commit 9b2b856

Browse files
Adding spring batch project
1 parent 93b74b1 commit 9b2b856

11 files changed

Lines changed: 333 additions & 0 deletions

File tree

‎spring-batch-intro/.classpath‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<classpath>
3+
<classpathentry kind="src" output="target/classes" path="src/main/java">
4+
<attributes>
5+
<attribute name="optional" value="true"/>
6+
<attribute name="maven.pomderived" value="true"/>
7+
</attributes>
8+
</classpathentry>
9+
<classpathentry kind="src" output="target/test-classes" path="src/test/java">
10+
<attributes>
11+
<attribute name="optional" value="true"/>
12+
<attribute name="maven.pomderived" value="true"/>
13+
</attributes>
14+
</classpathentry>
15+
<classpathentry including="**/*.java" kind="src" path="src/main/resources"/>
16+
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/J2SE-1.5">
17+
<attributes>
18+
<attribute name="maven.pomderived" value="true"/>
19+
</attributes>
20+
</classpathentry>
21+
<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
22+
<attributes>
23+
<attribute name="maven.pomderived" value="true"/>
24+
</attributes>
25+
</classpathentry>
26+
<classpathentry kind="output" path="target/classes"/>
27+
</classpath>

‎spring-batch-intro/.project‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<projectDescription>
3+
<name>spring-batch-intro</name>
4+
<comment></comment>
5+
<projects>
6+
</projects>
7+
<buildSpec>
8+
<buildCommand>
9+
<name>org.eclipse.jdt.core.javabuilder</name>
10+
<arguments>
11+
</arguments>
12+
</buildCommand>
13+
<buildCommand>
14+
<name>org.eclipse.m2e.core.maven2Builder</name>
15+
<arguments>
16+
</arguments>
17+
</buildCommand>
18+
</buildSpec>
19+
<natures>
20+
<nature>org.eclipse.jdt.core.javanature</nature>
21+
<nature>org.eclipse.m2e.core.maven2Nature</nature>
22+
</natures>
23+
</projectDescription>

‎spring-batch-intro/pom.xml‎

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
5+
<groupId>org.baeldung</groupId>
6+
<artifactId>spring-batch-intro</artifactId>
7+
<version>0.1-SNAPSHOT</version>
8+
<packaging>jar</packaging>
9+
10+
<name>spring-batch-intro</name>
11+
<url>http://maven.apache.org</url>
12+
13+
<properties>
14+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
15+
<spring.version>4.0.2.RELEASE</spring.version>
16+
<spring.batch.version>3.0.5.RELEASE</spring.batch.version>
17+
<sqlite.version>3.8.11.2</sqlite.version>
18+
</properties>
19+
20+
<dependencies>
21+
<!-- SQLite database driver -->
22+
<dependency>
23+
<groupId>org.xerial</groupId>
24+
<artifactId>sqlite-jdbc</artifactId>
25+
<version>${sqlite.version}</version>
26+
</dependency>
27+
<dependency>
28+
<groupId>org.springframework</groupId>
29+
<artifactId>spring-oxm</artifactId>
30+
<version>${spring.version}</version>
31+
</dependency>
32+
<dependency>
33+
<groupId>org.springframework</groupId>
34+
<artifactId>spring-jdbc</artifactId>
35+
<version>${spring.version}</version>
36+
</dependency>
37+
38+
<dependency>
39+
<groupId>org.springframework.batch</groupId>
40+
<artifactId>spring-batch-core</artifactId>
41+
<version>${spring.batch.version}</version>
42+
</dependency>
43+
</dependencies>
44+
45+
</project>
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package org.baeldung.spring_batch_intro;
2+
3+
import org.springframework.batch.core.Job;
4+
import org.springframework.batch.core.JobExecution;
5+
import org.springframework.batch.core.JobParameters;
6+
import org.springframework.batch.core.launch.JobLauncher;
7+
import org.springframework.context.ApplicationContext;
8+
import org.springframework.context.support.ClassPathXmlApplicationContext;
9+
10+
public class App {
11+
public static void main(String[] args) {
12+
13+
ApplicationContext context = new ClassPathXmlApplicationContext(
14+
"/spring-batch-intro.xml");
15+
16+
JobLauncher jobLauncher = (JobLauncher) context.getBean("jobLauncher");
17+
Job job = (Job) context.getBean("firstBatchJob");
18+
System.out.println("Starting the batch job");
19+
try {
20+
JobExecution execution = jobLauncher.run(job, new JobParameters());
21+
System.out.println("Job Status : " + execution.getStatus());
22+
System.out.println("Job completed");
23+
} catch (Exception e) {
24+
e.printStackTrace();
25+
System.out.println("Job failed");
26+
}
27+
}
28+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package org.baeldung.spring_batch_intro.model;
2+
3+
import java.util.Date;
4+
5+
import javax.xml.bind.annotation.XmlRootElement;
6+
7+
@SuppressWarnings("restriction")
8+
@XmlRootElement(name = "transactionRecord")
9+
public class Transaction {
10+
private String username;
11+
private int userId;
12+
private Date transactionDate;
13+
private double amount;
14+
15+
public String getUsername() {
16+
return username;
17+
}
18+
19+
public void setUsername(String username) {
20+
this.username = username;
21+
}
22+
23+
public int getUserId() {
24+
return userId;
25+
}
26+
27+
public void setUserId(int userId) {
28+
this.userId = userId;
29+
}
30+
31+
public Date getTransactionDate() {
32+
return transactionDate;
33+
}
34+
35+
public void setTransactionDate(Date transactionDate) {
36+
this.transactionDate = transactionDate;
37+
}
38+
39+
public double getAmount() {
40+
return amount;
41+
}
42+
43+
public void setAmount(double amount) {
44+
this.amount = amount;
45+
}
46+
47+
@Override
48+
public String toString() {
49+
return "Transaction [username=" + username + ", userId=" + userId
50+
+ ", transactionDate=" + transactionDate + ", amount=" + amount
51+
+ "]";
52+
}
53+
54+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package org.baeldung.spring_batch_intro.service;
2+
3+
4+
import org.baeldung.spring_batch_intro.model.Transaction;
5+
import org.springframework.batch.item.ItemProcessor;
6+
7+
public class CustomItemProcessor implements ItemProcessor<Transaction, Transaction> {
8+
9+
public Transaction process(Transaction item) throws Exception {
10+
11+
System.out.println("Processing..." + item);
12+
return item;
13+
}
14+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package org.baeldung.spring_batch_intro.service;
2+
3+
import java.text.ParseException;
4+
import java.text.SimpleDateFormat;
5+
6+
import org.baeldung.spring_batch_intro.model.Transaction;
7+
import org.springframework.batch.item.file.mapping.FieldSetMapper;
8+
import org.springframework.batch.item.file.transform.FieldSet;
9+
import org.springframework.validation.BindException;
10+
11+
public class RecordFieldSetMapper implements FieldSetMapper<Transaction> {
12+
13+
public Transaction mapFieldSet(FieldSet fieldSet) throws BindException {
14+
15+
SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
16+
Transaction transaction = new Transaction();
17+
18+
transaction.setUsername(fieldSet.readString("username"));
19+
transaction.setUserId(fieldSet.readInt(1));
20+
transaction.setAmount(fieldSet.readDouble(3));
21+
//Converting the date
22+
String dateString = fieldSet.readString(2);
23+
try {
24+
transaction.setTransactionDate(dateFormat.parse(dateString));
25+
} catch (ParseException e) {
26+
e.printStackTrace();
27+
}
28+
29+
return transaction;
30+
31+
}
32+
33+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
devendra, 1234, 31/10/2015, 10000
2+
john, 2134, 3/12/2015, 12321
3+
robin, 2134, 2/02/2015, 23411
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
<beans xmlns="http://www.springframework.org/schema/beans"
2+
xmlns:batch="http://www.springframework.org/schema/batch" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://www.springframework.org/schema/batch
4+
http://www.springframework.org/schema/batch/spring-batch-3.0.xsd
5+
http://www.springframework.org/schema/beans
6+
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
7+
">
8+
9+
<import resource="spring.xml" />
10+
11+
<bean id="record" class="org.baeldung.spring_batch_intro.model.Transaction"></bean>
12+
13+
<bean id="itemReader" class="org.springframework.batch.item.file.FlatFileItemReader">
14+
15+
<property name="resource" value="input/record.csv" />
16+
17+
<property name="lineMapper">
18+
<bean class="org.springframework.batch.item.file.mapping.DefaultLineMapper">
19+
<property name="lineTokenizer">
20+
<bean
21+
class="org.springframework.batch.item.file.transform.DelimitedLineTokenizer">
22+
<property name="names" value="username,userid,transactiondate,amount" />
23+
</bean>
24+
</property>
25+
<property name="fieldSetMapper">
26+
<bean
27+
class="org.baeldung.spring_batch_intro.service.RecordFieldSetMapper" />
28+
29+
</property>
30+
</bean>
31+
</property>
32+
</bean>
33+
34+
<bean id="itemProcessor"
35+
class="org.baeldung.spring_batch_intro.service.CustomItemProcessor" />
36+
37+
<bean id="itemWriter" class="org.springframework.batch.item.xml.StaxEventItemWriter">
38+
<property name="resource" value="file:xml/output.xml" />
39+
<property name="marshaller" ref="recordMarshaller" />
40+
<property name="rootTagName" value="transactionRecord" />
41+
</bean>
42+
43+
<bean id="recordMarshaller" class="org.springframework.oxm.jaxb.Jaxb2Marshaller">
44+
<property name="classesToBeBound">
45+
<list>
46+
<value>org.baeldung.spring_batch_intro.model.Transaction</value>
47+
</list>
48+
</property>
49+
</bean>
50+
51+
52+
<batch:job id="firstBatchJob">
53+
<batch:step id="step1">
54+
<batch:tasklet>
55+
<batch:chunk reader="itemReader" writer="itemWriter"
56+
processor="itemProcessor" commit-interval="10">
57+
</batch:chunk>
58+
</batch:tasklet>
59+
</batch:step>
60+
</batch:job>
61+
</beans>
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<beans xmlns="http://www.springframework.org/schema/beans"
2+
xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="
4+
http://www.springframework.org/schema/beans
5+
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
6+
http://www.springframework.org/schema/jdbc
7+
http://www.springframework.org/schema/jdbc/spring-jdbc-4.0.xsd">
8+
9+
<!-- connect to SQLite database -->
10+
<bean id="dataSource"
11+
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
12+
<property name="driverClassName" value="org.sqlite.JDBC" />
13+
<property name="url" value="jdbc:sqlite:repository.sqlite" />
14+
<property name="username" value="" />
15+
<property name="password" value="" />
16+
</bean>
17+
18+
<!-- create job-meta tables automatically -->
19+
<jdbc:initialize-database data-source="dataSource">
20+
<jdbc:script location="org/springframework/batch/core/schema-drop-sqlite.sql" />
21+
<jdbc:script location="org/springframework/batch/core/schema-sqlite.sql" />
22+
</jdbc:initialize-database>
23+
24+
<!-- stored job-meta in memory -->
25+
<!-- <bean id="jobRepository" class="org.springframework.batch.core.repository.support.MapJobRepositoryFactoryBean">
26+
<property name="transactionManager" ref="transactionManager" /> </bean> -->
27+
28+
<!-- stored job-meta in database -->
29+
<bean id="jobRepository"
30+
class="org.springframework.batch.core.repository.support.JobRepositoryFactoryBean">
31+
<property name="dataSource" ref="dataSource" />
32+
<property name="transactionManager" ref="transactionManager" />
33+
<property name="databaseType" value="sqlite" />
34+
</bean>
35+
36+
<bean id="transactionManager"
37+
class="org.springframework.batch.support.transaction.ResourcelessTransactionManager" />
38+
39+
<bean id="jobLauncher"
40+
class="org.springframework.batch.core.launch.support.SimpleJobLauncher">
41+
<property name="jobRepository" ref="jobRepository" />
42+
</bean>
43+
44+
</beans>

0 commit comments

Comments
 (0)