Skip to content

Commit 57b27f6

Browse files
adding java based spring batch configuration
1 parent 9b2b856 commit 57b27f6

9 files changed

Lines changed: 392 additions & 211 deletions

File tree

‎spring-batch-intro/pom.xml‎

Lines changed: 37 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,44 @@
11
<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>
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>
44

5-
<groupId>org.baeldung</groupId>
6-
<artifactId>spring-batch-intro</artifactId>
7-
<version>0.1-SNAPSHOT</version>
8-
<packaging>jar</packaging>
5+
<groupId>org.baeldung</groupId>
6+
<artifactId>spring-batch-intro</artifactId>
7+
<version>0.1-SNAPSHOT</version>
8+
<packaging>jar</packaging>
99

10-
<name>spring-batch-intro</name>
11-
<url>http://maven.apache.org</url>
10+
<name>spring-batch-intro</name>
11+
<url>http://maven.apache.org</url>
1212

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>
13+
<properties>
14+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
15+
<spring.version>4.2.0.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>
1919

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>
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+
<dependency>
38+
<groupId>org.springframework.batch</groupId>
39+
<artifactId>spring-batch-core</artifactId>
40+
<version>${spring.batch.version}</version>
41+
</dependency>
42+
</dependencies>
4443

4544
</project>
Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,38 @@
11
package org.baeldung.spring_batch_intro;
22

3+
import javax.swing.Spring;
4+
35
import org.springframework.batch.core.Job;
46
import org.springframework.batch.core.JobExecution;
57
import org.springframework.batch.core.JobParameters;
68
import org.springframework.batch.core.launch.JobLauncher;
79
import org.springframework.context.ApplicationContext;
10+
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
811
import org.springframework.context.support.ClassPathXmlApplicationContext;
912

1013
public class App {
11-
public static void main(String[] args) {
14+
public static void main(String[] args) {
15+
// Spring Java config
16+
AnnotationConfigApplicationContext context = new
17+
AnnotationConfigApplicationContext();
18+
context.register(SpringConfig.class);
19+
context.register(SpringBatchConfig.class);
20+
context.refresh();
21+
22+
// Spring xml config
23+
// ApplicationContext context = new ClassPathXmlApplicationContext(
24+
// "spring-batch-intro.xml");
1225

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-
}
26+
JobLauncher jobLauncher = (JobLauncher) context.getBean("jobLauncher");
27+
Job job = (Job) context.getBean("firstBatchJob");
28+
System.out.println("Starting the batch job");
29+
try {
30+
JobExecution execution = jobLauncher.run(job, new JobParameters());
31+
System.out.println("Job Status : " + execution.getStatus());
32+
System.out.println("Job completed");
33+
} catch (Exception e) {
34+
e.printStackTrace();
35+
System.out.println("Job failed");
36+
}
37+
}
2838
}
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
package org.baeldung.spring_batch_intro;
2+
3+
import java.net.MalformedURLException;
4+
import java.text.ParseException;
5+
6+
import org.baeldung.spring_batch_intro.model.Transaction;
7+
import org.baeldung.spring_batch_intro.service.CustomItemProcessor;
8+
import org.baeldung.spring_batch_intro.service.RecordFieldSetMapper;
9+
import org.springframework.batch.core.Job;
10+
import org.springframework.batch.core.Step;
11+
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
12+
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
13+
import org.springframework.batch.item.ItemProcessor;
14+
import org.springframework.batch.item.ItemReader;
15+
import org.springframework.batch.item.ItemWriter;
16+
import org.springframework.batch.item.UnexpectedInputException;
17+
import org.springframework.batch.item.file.FlatFileItemReader;
18+
import org.springframework.batch.item.file.mapping.DefaultLineMapper;
19+
import org.springframework.batch.item.file.transform.DelimitedLineTokenizer;
20+
import org.springframework.batch.item.xml.StaxEventItemWriter;
21+
import org.springframework.beans.factory.annotation.Autowired;
22+
import org.springframework.beans.factory.annotation.Qualifier;
23+
import org.springframework.beans.factory.annotation.Value;
24+
import org.springframework.context.annotation.Bean;
25+
import org.springframework.core.io.Resource;
26+
import org.springframework.oxm.Marshaller;
27+
import org.springframework.oxm.jaxb.Jaxb2Marshaller;
28+
29+
public class SpringBatchConfig {
30+
@Autowired
31+
private JobBuilderFactory jobs;
32+
33+
@Autowired
34+
private StepBuilderFactory steps;
35+
36+
@Value("input/record.csv")
37+
private Resource inputCsv;
38+
39+
@Value("file:xml/output.xml")
40+
private Resource outputXml;
41+
42+
@Bean
43+
public ItemReader<Transaction> itemReader()
44+
throws UnexpectedInputException, ParseException, Exception {
45+
FlatFileItemReader<Transaction> reader = new FlatFileItemReader<Transaction>();
46+
DelimitedLineTokenizer tokenizer = new DelimitedLineTokenizer();
47+
String[] tokens = { "username", "userid", "transactiondate", "amount" };
48+
tokenizer.setNames(tokens);
49+
reader.setResource(inputCsv);
50+
DefaultLineMapper<Transaction> lineMapper = new DefaultLineMapper<Transaction>();
51+
lineMapper.setLineTokenizer(tokenizer);
52+
lineMapper.setFieldSetMapper(new RecordFieldSetMapper());
53+
reader.setLineMapper(lineMapper);
54+
return reader;
55+
}
56+
57+
@Bean
58+
public ItemProcessor<Transaction, Transaction> itemProcessor() {
59+
return new CustomItemProcessor();
60+
}
61+
62+
@Bean
63+
public ItemWriter<Transaction> itemWriter(Marshaller marshaller)
64+
throws MalformedURLException {
65+
StaxEventItemWriter<Transaction> itemWriter = new StaxEventItemWriter<Transaction>();
66+
itemWriter.setMarshaller(marshaller);
67+
itemWriter.setRootTagName("transactionRecord");
68+
itemWriter.setResource(outputXml);
69+
return itemWriter;
70+
}
71+
72+
@Bean
73+
public Marshaller marshaller() {
74+
Jaxb2Marshaller marshaller = new Jaxb2Marshaller();
75+
marshaller.setClassesToBeBound(new Class[] { Transaction.class });
76+
return marshaller;
77+
}
78+
79+
@Bean
80+
protected Step step1(ItemReader<Transaction> reader,
81+
ItemProcessor<Transaction, Transaction> processor,
82+
ItemWriter<Transaction> writer) {
83+
return steps.get("step1").<Transaction, Transaction> chunk(10)
84+
.reader(reader).processor(processor).writer(writer).build();
85+
}
86+
87+
@Bean(name = "firstBatchJob")
88+
public Job job(@Qualifier("step1") Step step1) {
89+
return jobs.get("firstBatchJob").start(step1).build();
90+
}
91+
92+
}
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
package org.baeldung.spring_batch_intro;
2+
3+
import java.net.MalformedURLException;
4+
5+
import javax.sql.DataSource;
6+
7+
import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
8+
import org.springframework.batch.core.launch.JobLauncher;
9+
import org.springframework.batch.core.launch.support.SimpleJobLauncher;
10+
import org.springframework.batch.core.repository.JobRepository;
11+
import org.springframework.batch.core.repository.support.JobRepositoryFactoryBean;
12+
import org.springframework.batch.support.transaction.ResourcelessTransactionManager;
13+
import org.springframework.beans.factory.annotation.Value;
14+
import org.springframework.context.annotation.Bean;
15+
import org.springframework.context.annotation.Configuration;
16+
import org.springframework.core.io.Resource;
17+
import org.springframework.jdbc.datasource.DriverManagerDataSource;
18+
import org.springframework.jdbc.datasource.init.DataSourceInitializer;
19+
import org.springframework.jdbc.datasource.init.ResourceDatabasePopulator;
20+
import org.springframework.transaction.PlatformTransactionManager;
21+
22+
@Configuration
23+
@EnableBatchProcessing
24+
public class SpringConfig {
25+
26+
@Value("org/springframework/batch/core/schema-drop-sqlite.sql")
27+
private Resource dropReopsitoryTables;
28+
29+
@Value("org/springframework/batch/core/schema-sqlite.sql")
30+
private Resource dataReopsitorySchema;
31+
32+
@Bean
33+
public DataSource dataSource() {
34+
DriverManagerDataSource dataSource = new DriverManagerDataSource();
35+
dataSource.setDriverClassName("org.sqlite.JDBC");
36+
dataSource.setUrl("jdbc:sqlite:repository.sqlite");
37+
return dataSource;
38+
}
39+
40+
@Bean
41+
public DataSourceInitializer dataSourceInitializer(DataSource dataSource)
42+
throws MalformedURLException {
43+
ResourceDatabasePopulator databasePopulator = new ResourceDatabasePopulator();
44+
45+
databasePopulator.addScript(dropReopsitoryTables);
46+
databasePopulator.addScript(dataReopsitorySchema);
47+
databasePopulator.setIgnoreFailedDrops(true);
48+
49+
DataSourceInitializer initializer = new DataSourceInitializer();
50+
initializer.setDataSource(dataSource);
51+
initializer.setDatabasePopulator(databasePopulator);
52+
53+
return initializer;
54+
}
55+
56+
private JobRepository getJobRepository() throws Exception {
57+
JobRepositoryFactoryBean factory = new JobRepositoryFactoryBean();
58+
factory.setDataSource(dataSource());
59+
factory.setTransactionManager(getTransactionManager());
60+
factory.afterPropertiesSet();
61+
return (JobRepository) factory.getObject();
62+
}
63+
64+
private PlatformTransactionManager getTransactionManager() {
65+
return new ResourcelessTransactionManager();
66+
}
67+
68+
public JobLauncher getJobLauncher() throws Exception {
69+
SimpleJobLauncher jobLauncher = new SimpleJobLauncher();
70+
jobLauncher.setJobRepository(getJobRepository());
71+
jobLauncher.afterPropertiesSet();
72+
return jobLauncher;
73+
}
74+
75+
}

0 commit comments

Comments
 (0)