Skip to content

Commit f4c6580

Browse files
committed
spring-batch
1 parent c64116f commit f4c6580

File tree

12 files changed

+409
-1
lines changed

12 files changed

+409
-1
lines changed

.gitignore

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,5 @@
44
/spring-capgemini-jaxb/target/
55
/ws-client/target/
66
/spring-jms/target/
7-
/spring-jmx/target/
7+
/spring-jmx/target/
8+
/spring-batch-sample/target/

pom.xml

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
<module>ws-client</module>
1515
<module>spring-jms</module>
1616
<module>spring-jmx</module>
17+
<module>spring-batch-sample</module>
1718
</modules>
1819
<properties>
1920
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project-shared-configuration>
3+
<!--
4+
This file contains additional configuration written by modules in the NetBeans IDE.
5+
The configuration is intended to be shared among all the users of project and
6+
therefore it is assumed to be part of version control checkout.
7+
Without this configuration present, some functionality in the IDE may be limited or fail altogether.
8+
-->
9+
<spring-data xmlns="http://www.netbeans.org/ns/spring-data/1">
10+
<config-files>
11+
<config-file>src/main/resources/database.xml</config-file>
12+
<config-file>src/main/resources/batch-context.xml</config-file>
13+
</config-files>
14+
<config-file-groups/>
15+
</spring-data>
16+
</project-shared-configuration>

spring-batch-sample/pom.xml

+71
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 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+
<groupId>pl.altkom.spring</groupId>
5+
<artifactId>spring-batch</artifactId>
6+
7+
<parent>
8+
<groupId>pl.altkom.spring</groupId>
9+
<artifactId>spring-capgemini</artifactId>
10+
<version>1.0-SNAPSHOT</version>
11+
</parent>
12+
13+
<packaging>jar</packaging>
14+
<properties>
15+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
16+
<maven.compiler.source>1.7</maven.compiler.source>
17+
<maven.compiler.target>1.7</maven.compiler.target>
18+
<spring.batch.version>3.0.1.RELEASE</spring.batch.version>
19+
<spring.version>4.0.6.RELEASE</spring.version>
20+
</properties>
21+
22+
<dependencies>
23+
24+
<dependency>
25+
<groupId>org.springframework</groupId>
26+
<artifactId>spring-context</artifactId>
27+
<version>${spring.version}</version>
28+
</dependency>
29+
30+
31+
<dependency>
32+
<groupId>org.springframework.batch</groupId>
33+
<artifactId>spring-batch-core</artifactId>
34+
<version>${spring.batch.version}</version>
35+
</dependency>
36+
37+
<!-- Spring Batch unit test -->
38+
<dependency>
39+
<groupId>org.springframework.batch</groupId>
40+
<artifactId>spring-batch-test</artifactId>
41+
<version>${spring.batch.version}</version>
42+
</dependency>
43+
44+
<dependency>
45+
<artifactId>spring-tx</artifactId>
46+
<groupId>org.springframework</groupId>
47+
<version>${spring.version}</version>
48+
</dependency>
49+
50+
<dependency>
51+
<groupId>org.springframework</groupId>
52+
<artifactId>spring-jdbc</artifactId>
53+
<version>${spring.version}</version>
54+
</dependency>
55+
56+
<dependency>
57+
<groupId>org.apache.derby</groupId>
58+
<artifactId>derbyclient</artifactId>
59+
<version>10.10.2.0</version>
60+
</dependency>
61+
62+
<!-- Spring XML to/back object -->
63+
<dependency>
64+
<groupId>org.springframework</groupId>
65+
<artifactId>spring-oxm</artifactId>
66+
<version>${spring.version}</version>
67+
</dependency>
68+
69+
</dependencies>
70+
71+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*
2+
* Copyright 2014-09-16 the original author or authors.
3+
*/
4+
5+
package pl.altkom.sping.batch.sample;
6+
7+
8+
import javax.xml.bind.Marshaller;
9+
import org.springframework.batch.core.Job;
10+
import org.springframework.batch.core.JobExecution;
11+
import org.springframework.batch.core.JobParameters;
12+
import org.springframework.batch.core.launch.JobLauncher;
13+
import org.springframework.context.ApplicationContext;
14+
import org.springframework.context.support.ClassPathXmlApplicationContext;
15+
16+
/**
17+
*
18+
* @author Adrian Lapierre <[email protected]>
19+
*/
20+
public class Main {
21+
22+
public static void main(String[] args) {
23+
24+
System.out.println(Marshaller.JAXB_FORMATTED_OUTPUT);
25+
26+
String[] springConfig =
27+
{
28+
"batch-task.xml"
29+
};
30+
31+
ApplicationContext context =
32+
new ClassPathXmlApplicationContext(springConfig);
33+
34+
JobLauncher jobLauncher = (JobLauncher) context.getBean("jobLauncher");
35+
Job job = (Job) context.getBean("helloWorldJob");
36+
37+
try {
38+
39+
JobExecution execution = jobLauncher.run(job, new JobParameters());
40+
System.out.println("Exit Status : " + execution.getStatus());
41+
42+
} catch (Exception e) {
43+
e.printStackTrace();
44+
}
45+
46+
System.out.println("Done");
47+
48+
49+
}
50+
51+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
* Copyright 2014-09-18 the original author or authors.
3+
*/
4+
package pl.altkom.sping.batch.sample.mapper;
5+
6+
import java.text.ParseException;
7+
import java.text.SimpleDateFormat;
8+
import org.springframework.batch.item.file.mapping.FieldSetMapper;
9+
import org.springframework.batch.item.file.transform.FieldSet;
10+
import org.springframework.validation.BindException;
11+
import pl.altkom.sping.batch.sample.model.Report;
12+
13+
/**
14+
*
15+
* @author Adrian Lapierre <[email protected]>
16+
*/
17+
public class ReportFieldSetMapper implements FieldSetMapper<Report> {
18+
19+
private SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
20+
21+
@Override
22+
public Report mapFieldSet(FieldSet fieldSet) throws BindException {
23+
24+
Report report = new Report();
25+
report.setId(fieldSet.readInt(0));
26+
report.setSales(fieldSet.readBigDecimal(1));
27+
report.setQty(fieldSet.readInt(2));
28+
report.setStaffName(fieldSet.readString(3));
29+
30+
//default format yyyy-MM-dd
31+
//fieldSet.readDate(4);
32+
String date = fieldSet.readString(4);
33+
try {
34+
report.setDate(dateFormat.parse(date));
35+
} catch (ParseException e) {
36+
e.printStackTrace();
37+
}
38+
39+
return report;
40+
41+
}
42+
43+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/*
2+
* Copyright 2014-09-18 the original author or authors.
3+
*/
4+
package pl.altkom.sping.batch.sample.model;
5+
6+
import java.math.BigDecimal;
7+
import java.util.Date;
8+
import javax.xml.bind.annotation.XmlAttribute;
9+
import javax.xml.bind.annotation.XmlElement;
10+
import javax.xml.bind.annotation.XmlRootElement;
11+
12+
/**
13+
*
14+
* @author Adrian Lapierre <[email protected]>
15+
*/
16+
@XmlRootElement(name = "record")
17+
public class Report {
18+
19+
private int id;
20+
private BigDecimal sales;
21+
private int qty;
22+
private String staffName;
23+
private Date date;
24+
25+
@XmlAttribute(name = "id")
26+
public int getId() {
27+
return id;
28+
}
29+
30+
public void setId(int id) {
31+
this.id = id;
32+
}
33+
34+
@XmlElement(name = "sales")
35+
public BigDecimal getSales() {
36+
return sales;
37+
}
38+
39+
public void setSales(BigDecimal sales) {
40+
this.sales = sales;
41+
}
42+
43+
@XmlElement(name = "qty")
44+
public int getQty() {
45+
return qty;
46+
}
47+
48+
public void setQty(int qty) {
49+
this.qty = qty;
50+
}
51+
52+
@XmlElement(name = "staffName")
53+
public String getStaffName() {
54+
return staffName;
55+
}
56+
57+
public void setStaffName(String staffName) {
58+
this.staffName = staffName;
59+
}
60+
61+
public Date getDate() {
62+
return date;
63+
}
64+
65+
public void setDate(Date date) {
66+
this.date = date;
67+
}
68+
69+
@Override
70+
public String toString() {
71+
return "Report [id=" + id + ", sales=" + sales
72+
+ ", qty=" + qty + ", staffName=" + staffName + "]";
73+
}
74+
75+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/*
2+
* Copyright 2014-09-18 the original author or authors.
3+
*/
4+
package pl.altkom.sping.batch.sample.processor;
5+
6+
import org.springframework.batch.item.ItemProcessor;
7+
import pl.altkom.sping.batch.sample.model.Report;
8+
9+
10+
11+
/**
12+
*
13+
* @author Adrian Lapierre <[email protected]>
14+
*/
15+
public class CustomItemProcessor implements ItemProcessor<Report, Report> {
16+
17+
@Override
18+
public Report process(Report item) throws Exception {
19+
System.out.println("Processing..." + item);
20+
return item;
21+
}
22+
23+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<beans xmlns="http://www.springframework.org/schema/beans"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xmlns:c="http://www.springframework.org/schema/c"
5+
xmlns:context="http://www.springframework.org/schema/context"
6+
xmlns:lang="http://www.springframework.org/schema/lang"
7+
xmlns:p="http://www.springframework.org/schema/p"
8+
xmlns:util="http://www.springframework.org/schema/util"
9+
xmlns:batch="http://www.springframework.org/schema/batch"
10+
11+
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
12+
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
13+
http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang-4.0.xsd
14+
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd
15+
http://www.springframework.org/schema/batch http://www.springframework.org/schema/batch/spring-batch.xsd
16+
">
17+
18+
<import resource="classpath:database.xml" />
19+
20+
<!-- stored job-meta in database -->
21+
<bean id="jobRepository"
22+
class="org.springframework.batch.core.repository.support.JobRepositoryFactoryBean">
23+
<property name="dataSource" ref="dataSource" />
24+
<property name="transactionManager" ref="transactionManager" />
25+
<property name="databaseType" value="derby" />
26+
</bean>
27+
28+
<bean id="jobLauncher"
29+
class="org.springframework.batch.core.launch.support.SimpleJobLauncher">
30+
<property name="jobRepository" ref="jobRepository" />
31+
</bean>
32+
33+
<bean id="report" class="pl.altkom.sping.batch.sample.model.Report" scope="prototype" />
34+
<bean id="itemProcessor" class="pl.altkom.sping.batch.sample.processor.CustomItemProcessor" />
35+
36+
<batch:job id="helloWorldJob">
37+
<batch:step id="step1">
38+
<batch:tasklet>
39+
<batch:chunk reader="cvsFileItemReader" writer="xmlItemWriter"
40+
processor="itemProcessor" commit-interval="10">
41+
</batch:chunk>
42+
</batch:tasklet>
43+
</batch:step>
44+
</batch:job>
45+
46+
<bean id="cvsFileItemReader" class="org.springframework.batch.item.file.FlatFileItemReader">
47+
48+
<property name="resource" value="classpath:csv-source.csv" />
49+
50+
<property name="lineMapper">
51+
<bean class="org.springframework.batch.item.file.mapping.DefaultLineMapper">
52+
<property name="lineTokenizer">
53+
<bean
54+
class="org.springframework.batch.item.file.transform.DelimitedLineTokenizer">
55+
<property name="names" value="id,sales,qty,staffName,date" />
56+
</bean>
57+
</property>
58+
<property name="fieldSetMapper">
59+
<bean class="pl.altkom.sping.batch.sample.mapper.ReportFieldSetMapper" />
60+
61+
<!-- if no data type conversion, use BeanWrapperFieldSetMapper to map by name
62+
<bean
63+
class="org.springframework.batch.item.file.mapping.BeanWrapperFieldSetMapper">
64+
<property name="prototypeBeanName" value="report" />
65+
</bean>
66+
-->
67+
</property>
68+
</bean>
69+
</property>
70+
71+
</bean>
72+
73+
<bean id="xmlItemWriter" class="org.springframework.batch.item.xml.StaxEventItemWriter">
74+
<property name="resource" value="file:xml/outputs/report.xml" />
75+
<property name="marshaller" ref="reportMarshaller" />
76+
<property name="rootTagName" value="report" />
77+
</bean>
78+
79+
<bean id="reportMarshaller" class="org.springframework.oxm.jaxb.Jaxb2Marshaller">
80+
<property name="marshallerProperties">
81+
<map>
82+
<entry key="jaxb.formatted.output">
83+
<value type="boolean">true</value>
84+
</entry>
85+
</map>
86+
</property>
87+
<property name="classesToBeBound">
88+
<list>
89+
<value>pl.altkom.sping.batch.sample.model.Report</value>
90+
</list>
91+
</property>
92+
93+
</bean>
94+
95+
</beans>

0 commit comments

Comments
 (0)