Skip to content

Commit 97c0a44

Browse files
timis1timis1
andauthored
JAVA-18609 GitHub Issue: Spring Batch - JobBuilderFactory and StepBui… (eugenp#13618)
* JAVA-18609 GitHub Issue: Spring Batch - JobBuilderFactory and StepBuilderFactory are deprecated --------- Co-authored-by: timis1 <[email protected]>
1 parent fcacb80 commit 97c0a44

File tree

53 files changed

+545
-739
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

53 files changed

+545
-739
lines changed

pom.xml

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -759,6 +759,7 @@
759759

760760
<module>javafx</module>
761761
<module>spring-batch</module>
762+
<module>spring-batch-2</module>
762763
<module>spring-boot-rest</module>
763764
<module>spring-drools</module>
764765
<module>spring-exceptions</module>
@@ -781,7 +782,6 @@
781782
<module>server-modules</module>
782783
<module>apache-cxf-modules</module>
783784

784-
785785
<module>spring-aop</module>
786786
<module>jmeter</module>
787787
<module>spring-aop-2</module>
@@ -928,7 +928,6 @@
928928
<module>spring-5-webflux</module>
929929
<module>spring-5-webflux-2</module>
930930
<module>spring-activiti</module>
931-
<module>spring-batch-2</module>
932931
<module>spring-core-2</module>
933932
<module>spring-core-3</module>
934933
<module>spring-core-5</module>
@@ -1012,6 +1011,7 @@
10121011

10131012
<module>javafx</module>
10141013
<module>spring-batch</module>
1014+
<module>spring-batch-2</module>
10151015
<module>spring-boot-rest</module>
10161016
<module>spring-drools</module>
10171017
<module>spring-exceptions</module>
@@ -1180,7 +1180,6 @@
11801180
<module>spring-5-webflux</module>
11811181
<module>spring-5-webflux-2</module>
11821182
<module>spring-activiti</module>
1183-
<module>spring-batch-2</module>
11841183
<module>spring-core-2</module>
11851184
<module>spring-core-3</module>
11861185
<module>spring-core-5</module>

spring-batch-2/pom.xml

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@
1111

1212
<parent>
1313
<groupId>com.baeldung</groupId>
14-
<artifactId>parent-boot-2</artifactId>
14+
<artifactId>parent-boot-3</artifactId>
1515
<version>0.0.1-SNAPSHOT</version>
16-
<relativePath>../parent-boot-2</relativePath>
16+
<relativePath>../parent-boot-3</relativePath>
1717
</parent>
1818

1919
<dependencies>
@@ -51,8 +51,9 @@
5151
</dependencies>
5252

5353
<properties>
54-
<spring.batch.version>4.3.0</spring.batch.version>
55-
<awaitility.version>3.1.1</awaitility.version>
54+
<spring.batch.version>5.0.0</spring.batch.version>
55+
<awaitility.version>4.2.0</awaitility.version>
56+
<start-class>com.baeldung.batch.SpringBootBatchProcessingApplication</start-class>
5657
</properties>
5758

5859
</project>

spring-batch-2/src/main/java/com/baeldung/batch/BatchConfiguration.java

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,31 +4,24 @@
44

55
import org.springframework.batch.core.Job;
66
import org.springframework.batch.core.Step;
7-
import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
8-
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
9-
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
7+
import org.springframework.batch.core.job.builder.JobBuilder;
108
import org.springframework.batch.core.launch.support.RunIdIncrementer;
9+
import org.springframework.batch.core.repository.JobRepository;
10+
import org.springframework.batch.core.step.builder.StepBuilder;
1111
import org.springframework.batch.item.database.BeanPropertyItemSqlParameterSourceProvider;
1212
import org.springframework.batch.item.database.JdbcBatchItemWriter;
1313
import org.springframework.batch.item.database.builder.JdbcBatchItemWriterBuilder;
1414
import org.springframework.batch.item.file.FlatFileItemReader;
1515
import org.springframework.batch.item.file.builder.FlatFileItemReaderBuilder;
1616
import org.springframework.batch.item.file.mapping.BeanWrapperFieldSetMapper;
17-
import org.springframework.beans.factory.annotation.Autowired;
1817
import org.springframework.beans.factory.annotation.Value;
1918
import org.springframework.context.annotation.Bean;
2019
import org.springframework.context.annotation.Configuration;
2120
import org.springframework.core.io.ClassPathResource;
21+
import org.springframework.transaction.PlatformTransactionManager;
2222

2323
@Configuration
24-
@EnableBatchProcessing
2524
public class BatchConfiguration {
26-
27-
@Autowired
28-
public JobBuilderFactory jobBuilderFactory;
29-
30-
@Autowired
31-
public StepBuilderFactory stepBuilderFactory;
3225

3326
@Value("${file.input}")
3427
private String fileInput;
@@ -59,8 +52,8 @@ public JdbcBatchItemWriter<Coffee> writer(DataSource dataSource) {
5952
}
6053

6154
@Bean
62-
public Job importUserJob(JobCompletionNotificationListener listener, Step step1) {
63-
return jobBuilderFactory.get("importUserJob")
55+
public Job importUserJob(JobRepository jobRepository, JobCompletionNotificationListener listener, Step step1) {
56+
return new JobBuilder("importUserJob", jobRepository)
6457
.incrementer(new RunIdIncrementer())
6558
.listener(listener)
6659
.flow(step1)
@@ -69,9 +62,9 @@ public Job importUserJob(JobCompletionNotificationListener listener, Step step1)
6962
}
7063

7164
@Bean
72-
public Step step1(JdbcBatchItemWriter<Coffee> writer) {
73-
return stepBuilderFactory.get("step1")
74-
.<Coffee, Coffee> chunk(10)
65+
public Step step1(JobRepository jobRepository, PlatformTransactionManager transactionManager, JdbcBatchItemWriter<Coffee> writer) {
66+
return new StepBuilder("step1", jobRepository)
67+
.<Coffee, Coffee> chunk(10, transactionManager)
7568
.reader(reader())
7669
.processor(processor())
7770
.writer(writer)

spring-batch-2/src/main/java/com/baeldung/batch/CoffeeItemProcessor.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ public class CoffeeItemProcessor implements ItemProcessor<Coffee, Coffee> {
1010
private static final Logger LOGGER = LoggerFactory.getLogger(CoffeeItemProcessor.class);
1111

1212
@Override
13-
public Coffee process(final Coffee coffee) throws Exception {
13+
public Coffee process(final Coffee coffee) {
1414
String brand = coffee.getBrand().toUpperCase();
1515
String origin = coffee.getOrigin().toUpperCase();
1616
String chracteristics = coffee.getCharacteristics().toUpperCase();

spring-batch-2/src/main/java/com/baeldung/batch/JobCompletionNotificationListener.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@
44
import org.slf4j.LoggerFactory;
55
import org.springframework.batch.core.BatchStatus;
66
import org.springframework.batch.core.JobExecution;
7-
import org.springframework.batch.core.listener.JobExecutionListenerSupport;
7+
import org.springframework.batch.core.JobExecutionListener;
88
import org.springframework.beans.factory.annotation.Autowired;
99
import org.springframework.jdbc.core.JdbcTemplate;
1010
import org.springframework.stereotype.Component;
1111

1212
@Component
13-
public class JobCompletionNotificationListener extends JobExecutionListenerSupport {
13+
public class JobCompletionNotificationListener implements JobExecutionListener {
1414

1515
private static final Logger LOGGER = LoggerFactory.getLogger(JobCompletionNotificationListener.class);
1616

spring-batch-2/src/main/java/com/baeldung/batchscheduler/SpringBatchScheduler.java

Lines changed: 20 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,16 @@
77
import org.springframework.batch.core.JobExecution;
88
import org.springframework.batch.core.JobParametersBuilder;
99
import org.springframework.batch.core.Step;
10-
import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
11-
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
12-
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
10+
import org.springframework.batch.core.job.builder.JobBuilder;
1311
import org.springframework.batch.core.launch.JobLauncher;
12+
import org.springframework.batch.core.repository.JobRepository;
13+
import org.springframework.batch.core.step.builder.StepBuilder;
1414
import org.springframework.batch.item.ItemWriter;
1515
import org.springframework.batch.item.file.FlatFileItemReader;
1616
import org.springframework.batch.item.file.builder.FlatFileItemReaderBuilder;
1717
import org.springframework.batch.item.file.mapping.BeanWrapperFieldSetMapper;
1818
import org.springframework.beans.factory.annotation.Autowired;
19+
import org.springframework.context.ApplicationContext;
1920
import org.springframework.context.annotation.Bean;
2021
import org.springframework.context.annotation.Configuration;
2122
import org.springframework.core.io.ClassPathResource;
@@ -24,17 +25,16 @@
2425
import org.springframework.scheduling.annotation.Scheduled;
2526
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
2627
import org.springframework.scheduling.support.ScheduledMethodRunnable;
28+
import org.springframework.transaction.PlatformTransactionManager;
2729

2830
import java.util.Date;
2931
import java.util.IdentityHashMap;
30-
import java.util.List;
3132
import java.util.Map;
3233
import java.util.concurrent.ScheduledFuture;
3334
import java.util.concurrent.atomic.AtomicBoolean;
3435
import java.util.concurrent.atomic.AtomicInteger;
3536

3637
@Configuration
37-
@EnableBatchProcessing
3838
@EnableScheduling
3939
public class SpringBatchScheduler {
4040

@@ -47,20 +47,20 @@ public class SpringBatchScheduler {
4747
private final Map<Object, ScheduledFuture<?>> scheduledTasks = new IdentityHashMap<>();
4848

4949
@Autowired
50-
private JobBuilderFactory jobBuilderFactory;
50+
private JobLauncher jobLauncher;
5151

5252
@Autowired
53-
private StepBuilderFactory stepBuilderFactory;
53+
private JobRepository jobRepository;
5454

5555
@Autowired
56-
private JobLauncher jobLauncher;
56+
private PlatformTransactionManager transactionManager;
5757

5858
@Scheduled(fixedRate = 2000)
5959
public void launchJob() throws Exception {
6060
Date date = new Date();
6161
logger.debug("scheduler starts at " + date);
6262
if (enabled.get()) {
63-
JobExecution jobExecution = jobLauncher.run(job(), new JobParametersBuilder().addDate("launchDate", date)
63+
JobExecution jobExecution = jobLauncher.run(job(jobRepository, transactionManager), new JobParametersBuilder().addDate("launchDate", date)
6464
.toJobParameters());
6565
batchRunCounter.incrementAndGet();
6666
logger.debug("Batch job ends with status as " + jobExecution.getStatus());
@@ -106,17 +106,16 @@ public void cancelFutureSchedulerTasks() {
106106
}
107107

108108
@Bean
109-
public Job job() {
110-
return jobBuilderFactory
111-
.get("job")
112-
.start(readBooks())
109+
public Job job(JobRepository jobRepository, PlatformTransactionManager transactionManager) {
110+
return new JobBuilder("job", jobRepository)
111+
.start(readBooks(jobRepository, transactionManager))
113112
.build();
114113
}
115114

116115
@Bean
117-
protected Step readBooks() {
118-
return stepBuilderFactory.get("readBooks")
119-
.<Book, Book> chunk(2)
116+
protected Step readBooks(JobRepository jobRepository, PlatformTransactionManager transactionManager) {
117+
return new StepBuilder("readBooks", jobRepository)
118+
.<Book, Book> chunk(2, transactionManager)
120119
.reader(reader())
121120
.writer(writer())
122121
.build();
@@ -128,7 +127,7 @@ public FlatFileItemReader<Book> reader() {
128127
.resource(new ClassPathResource("books.csv"))
129128
.delimited()
130129
.names(new String[] { "id", "name" })
131-
.fieldSetMapper(new BeanWrapperFieldSetMapper<Book>() {
130+
.fieldSetMapper(new BeanWrapperFieldSetMapper<>() {
132131
{
133132
setTargetType(Book.class);
134133
}
@@ -138,15 +137,10 @@ public FlatFileItemReader<Book> reader() {
138137

139138
@Bean
140139
public ItemWriter<Book> writer() {
141-
return new ItemWriter<Book>() {
142-
143-
@Override
144-
public void write(List<? extends Book> items) throws Exception {
145-
logger.debug("writer..." + items.size());
146-
for (Book item : items) {
147-
logger.debug(item.toString());
148-
}
149-
140+
return items -> {
141+
logger.debug("writer..." + items.size());
142+
for (Book item : items) {
143+
logger.debug(item.toString());
150144
}
151145
};
152146
}
Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,28 @@
11
package com.baeldung.batch;
22

3-
import static org.hamcrest.Matchers.is;
4-
import static org.junit.Assert.assertThat;
53

6-
import org.junit.After;
7-
import org.junit.Test;
8-
import org.junit.runner.RunWith;
4+
import static org.junit.jupiter.api.Assertions.assertEquals;
5+
6+
import org.junit.jupiter.api.AfterEach;
7+
import org.junit.jupiter.api.Test;
98
import org.springframework.batch.core.ExitStatus;
109
import org.springframework.batch.core.JobExecution;
1110
import org.springframework.batch.core.JobInstance;
1211
import org.springframework.batch.test.JobLauncherTestUtils;
1312
import org.springframework.batch.test.JobRepositoryTestUtils;
1413
import org.springframework.batch.test.context.SpringBatchTest;
1514
import org.springframework.beans.factory.annotation.Autowired;
16-
import org.springframework.boot.test.context.SpringBootTest;
15+
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
16+
import org.springframework.boot.test.mock.mockito.MockBean;
1717
import org.springframework.context.annotation.PropertySource;
1818
import org.springframework.test.annotation.DirtiesContext;
19-
import org.springframework.test.context.junit4.SpringRunner;
19+
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
2020

2121
@SpringBatchTest
22-
@SpringBootTest
2322
@DirtiesContext
23+
@SpringJUnitConfig(BatchConfiguration.class)
2424
@PropertySource("classpath:application.properties")
25-
@RunWith(SpringRunner.class)
25+
@EnableAutoConfiguration
2626
public class SpringBootBatchIntegrationTest {
2727

2828
@Autowired
@@ -31,7 +31,10 @@ public class SpringBootBatchIntegrationTest {
3131
@Autowired
3232
private JobRepositoryTestUtils jobRepositoryTestUtils;
3333

34-
@After
34+
@MockBean
35+
private JobCompletionNotificationListener jobCompletionNotificationListener;
36+
37+
@AfterEach
3538
public void cleanUp() {
3639
jobRepositoryTestUtils.removeJobExecutions();
3740
}
@@ -42,8 +45,8 @@ public void givenCoffeeList_whenJobExecuted_thenSuccess() throws Exception {
4245
JobInstance jobInstance = jobExecution.getJobInstance();
4346
ExitStatus jobExitStatus = jobExecution.getExitStatus();
4447

45-
assertThat(jobInstance.getJobName(), is("importUserJob"));
46-
assertThat(jobExitStatus.getExitCode(), is("COMPLETED"));
48+
assertEquals("importUserJob", jobInstance.getJobName());
49+
assertEquals("COMPLETED", jobExitStatus.getExitCode());
4750
}
4851

4952
}

0 commit comments

Comments
 (0)