Skip to content

Commit 40e23a7

Browse files
committed
BAEL-1543: Moving from XML to Java based Spring config.
1 parent 8af29e8 commit 40e23a7

6 files changed

Lines changed: 197 additions & 88 deletions

File tree

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
package org.baeldung.taskletsvschunks.config;
2+
3+
import org.baeldung.taskletsvschunks.chunks.LineProcessor;
4+
import org.baeldung.taskletsvschunks.chunks.LineReader;
5+
import org.baeldung.taskletsvschunks.chunks.LinesWriter;
6+
import org.baeldung.taskletsvschunks.model.Line;
7+
import org.springframework.batch.core.Job;
8+
import org.springframework.batch.core.Step;
9+
import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
10+
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
11+
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
12+
import org.springframework.batch.core.launch.JobLauncher;
13+
import org.springframework.batch.core.launch.support.SimpleJobLauncher;
14+
import org.springframework.batch.core.repository.JobRepository;
15+
import org.springframework.batch.core.repository.support.MapJobRepositoryFactoryBean;
16+
import org.springframework.batch.item.ItemProcessor;
17+
import org.springframework.batch.item.ItemReader;
18+
import org.springframework.batch.item.ItemWriter;
19+
import org.springframework.batch.support.transaction.ResourcelessTransactionManager;
20+
import org.springframework.batch.test.JobLauncherTestUtils;
21+
import org.springframework.beans.factory.annotation.Autowired;
22+
import org.springframework.context.annotation.Bean;
23+
import org.springframework.context.annotation.Configuration;
24+
import org.springframework.transaction.PlatformTransactionManager;
25+
26+
@Configuration
27+
@EnableBatchProcessing
28+
public class ChunksConfig {
29+
30+
@Autowired private JobBuilderFactory jobs;
31+
32+
@Autowired private StepBuilderFactory steps;
33+
34+
@Bean
35+
public JobLauncherTestUtils jobLauncherTestUtils() {
36+
return new JobLauncherTestUtils();
37+
}
38+
39+
@Bean
40+
public JobRepository jobRepository() throws Exception {
41+
MapJobRepositoryFactoryBean factory = new MapJobRepositoryFactoryBean();
42+
factory.setTransactionManager(transactionManager());
43+
return (JobRepository) factory.getObject();
44+
}
45+
46+
@Bean
47+
public PlatformTransactionManager transactionManager() {
48+
return new ResourcelessTransactionManager();
49+
}
50+
51+
@Bean
52+
public JobLauncher jobLauncher() throws Exception {
53+
SimpleJobLauncher jobLauncher = new SimpleJobLauncher();
54+
jobLauncher.setJobRepository(jobRepository());
55+
return jobLauncher;
56+
}
57+
58+
@Bean
59+
public ItemReader<Line> itemReader() {
60+
return new LineReader();
61+
}
62+
63+
@Bean
64+
public ItemProcessor<Line, Line> itemProcessor() {
65+
return new LineProcessor();
66+
}
67+
68+
@Bean
69+
public ItemWriter<Line> itemWriter() {
70+
return new LinesWriter();
71+
}
72+
73+
@Bean
74+
protected Step processLines(ItemReader<Line> reader, ItemProcessor<Line, Line> processor, ItemWriter<Line> writer) {
75+
return steps.get("processLines").<Line, Line> chunk(2)
76+
.reader(reader)
77+
.processor(processor)
78+
.writer(writer)
79+
.build();
80+
}
81+
82+
@Bean
83+
public Job job() {
84+
return jobs
85+
.get("chunksJob")
86+
.start(processLines(itemReader(), itemProcessor(), itemWriter()))
87+
.build();
88+
}
89+
90+
}
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
package org.baeldung.taskletsvschunks.config;
2+
3+
import org.baeldung.taskletsvschunks.tasklets.LinesProcessor;
4+
import org.baeldung.taskletsvschunks.tasklets.LinesReader;
5+
import org.baeldung.taskletsvschunks.tasklets.LinesWriter;
6+
import org.springframework.batch.core.Job;
7+
import org.springframework.batch.core.Step;
8+
import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
9+
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
10+
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
11+
import org.springframework.batch.core.launch.JobLauncher;
12+
import org.springframework.batch.core.launch.support.SimpleJobLauncher;
13+
import org.springframework.batch.core.repository.JobRepository;
14+
import org.springframework.batch.core.repository.support.MapJobRepositoryFactoryBean;
15+
import org.springframework.batch.support.transaction.ResourcelessTransactionManager;
16+
import org.springframework.batch.test.JobLauncherTestUtils;
17+
import org.springframework.beans.factory.annotation.Autowired;
18+
import org.springframework.context.annotation.Bean;
19+
import org.springframework.context.annotation.Configuration;
20+
import org.springframework.transaction.PlatformTransactionManager;
21+
22+
@Configuration
23+
@EnableBatchProcessing
24+
public class TaskletsConfig {
25+
26+
@Autowired private JobBuilderFactory jobs;
27+
28+
@Autowired private StepBuilderFactory steps;
29+
30+
@Bean
31+
public JobLauncherTestUtils jobLauncherTestUtils() {
32+
return new JobLauncherTestUtils();
33+
}
34+
35+
@Bean
36+
public JobRepository jobRepository() throws Exception {
37+
MapJobRepositoryFactoryBean factory = new MapJobRepositoryFactoryBean();
38+
factory.setTransactionManager(transactionManager());
39+
return (JobRepository) factory.getObject();
40+
}
41+
42+
@Bean
43+
public PlatformTransactionManager transactionManager() {
44+
return new ResourcelessTransactionManager();
45+
}
46+
47+
@Bean
48+
public JobLauncher jobLauncher() throws Exception {
49+
SimpleJobLauncher jobLauncher = new SimpleJobLauncher();
50+
jobLauncher.setJobRepository(jobRepository());
51+
return jobLauncher;
52+
}
53+
54+
@Bean
55+
public LinesReader linesReader() {
56+
return new LinesReader();
57+
}
58+
59+
@Bean
60+
public LinesProcessor linesProcessor() {
61+
return new LinesProcessor();
62+
}
63+
64+
@Bean
65+
public LinesWriter linesWriter() {
66+
return new LinesWriter();
67+
}
68+
69+
@Bean
70+
protected Step readLines() {
71+
return steps
72+
.get("readLines")
73+
.tasklet(linesReader())
74+
.build();
75+
}
76+
77+
@Bean
78+
protected Step processLines() {
79+
return steps
80+
.get("processLines")
81+
.tasklet(linesProcessor())
82+
.build();
83+
}
84+
85+
@Bean
86+
protected Step writeLines() {
87+
return steps
88+
.get("writeLines")
89+
.tasklet(linesWriter())
90+
.build();
91+
}
92+
93+
@Bean
94+
public Job job() {
95+
return jobs
96+
.get("taskletsJob")
97+
.start(readLines())
98+
.next(processLines())
99+
.next(writeLines())
100+
.build();
101+
}
102+
103+
}

‎spring-batch/src/main/resources/taskletsvschunks/chunks.xml‎

Lines changed: 0 additions & 39 deletions
This file was deleted.

‎spring-batch/src/main/resources/taskletsvschunks/tasklets.xml‎

Lines changed: 0 additions & 47 deletions
This file was deleted.

‎spring-batch/src/test/java/org/baeldung/taskletsvschunks/chunks/ChunksTest.java‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package org.baeldung.taskletsvschunks.chunks;
22

3+
import org.baeldung.taskletsvschunks.config.ChunksConfig;
34
import org.junit.Assert;
45
import org.junit.Test;
56
import org.junit.runner.RunWith;
@@ -11,7 +12,7 @@
1112
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
1213

1314
@RunWith(SpringJUnit4ClassRunner.class)
14-
@ContextConfiguration(locations = "classpath:/taskletsvschunks/chunks.xml")
15+
@ContextConfiguration(classes = ChunksConfig.class)
1516
public class ChunksTest {
1617

1718
@Autowired private JobLauncherTestUtils jobLauncherTestUtils;

‎spring-batch/src/test/java/org/baeldung/taskletsvschunks/tasklets/TaskletsTest.java‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package org.baeldung.taskletsvschunks.tasklets;
22

3+
import org.baeldung.taskletsvschunks.config.TaskletsConfig;
34
import org.junit.Assert;
45
import org.junit.Test;
56
import org.junit.runner.RunWith;
@@ -11,7 +12,7 @@
1112
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
1213

1314
@RunWith(SpringJUnit4ClassRunner.class)
14-
@ContextConfiguration(locations = "classpath:/taskletsvschunks/tasklets.xml")
15+
@ContextConfiguration(classes = TaskletsConfig.class)
1516
public class TaskletsTest {
1617

1718
@Autowired private JobLauncherTestUtils jobLauncherTestUtils;

0 commit comments

Comments
 (0)