JAVA-18609 GitHub Issue: Spring Batch - JobBuilderFactory and StepBui… (#13618)

* JAVA-18609 GitHub Issue: Spring Batch - JobBuilderFactory and StepBuilderFactory are deprecated

---------

Co-authored-by: timis1 <noreplay@yahoo.com>
This commit is contained in:
timis1
2023-04-05 12:46:50 +03:00
committed by GitHub
parent d0d58509e2
commit d1249befd1
53 changed files with 548 additions and 742 deletions
+5 -4
View File
@@ -11,9 +11,9 @@
<parent>
<groupId>com.baeldung</groupId>
<artifactId>parent-boot-2</artifactId>
<artifactId>parent-boot-3</artifactId>
<version>0.0.1-SNAPSHOT</version>
<relativePath>../parent-boot-2</relativePath>
<relativePath>../parent-boot-3</relativePath>
</parent>
<dependencies>
@@ -51,8 +51,9 @@
</dependencies>
<properties>
<spring.batch.version>4.3.0</spring.batch.version>
<awaitility.version>3.1.1</awaitility.version>
<spring.batch.version>5.0.0</spring.batch.version>
<awaitility.version>4.2.0</awaitility.version>
<start-class>com.baeldung.batch.SpringBootBatchProcessingApplication</start-class>
</properties>
</project>
@@ -4,31 +4,24 @@ import javax.sql.DataSource;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.core.job.builder.JobBuilder;
import org.springframework.batch.core.launch.support.RunIdIncrementer;
import org.springframework.batch.core.repository.JobRepository;
import org.springframework.batch.core.step.builder.StepBuilder;
import org.springframework.batch.item.database.BeanPropertyItemSqlParameterSourceProvider;
import org.springframework.batch.item.database.JdbcBatchItemWriter;
import org.springframework.batch.item.database.builder.JdbcBatchItemWriterBuilder;
import org.springframework.batch.item.file.FlatFileItemReader;
import org.springframework.batch.item.file.builder.FlatFileItemReaderBuilder;
import org.springframework.batch.item.file.mapping.BeanWrapperFieldSetMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;
import org.springframework.transaction.PlatformTransactionManager;
@Configuration
@EnableBatchProcessing
public class BatchConfiguration {
@Autowired
public JobBuilderFactory jobBuilderFactory;
@Autowired
public StepBuilderFactory stepBuilderFactory;
@Value("${file.input}")
private String fileInput;
@@ -59,8 +52,8 @@ public class BatchConfiguration {
}
@Bean
public Job importUserJob(JobCompletionNotificationListener listener, Step step1) {
return jobBuilderFactory.get("importUserJob")
public Job importUserJob(JobRepository jobRepository, JobCompletionNotificationListener listener, Step step1) {
return new JobBuilder("importUserJob", jobRepository)
.incrementer(new RunIdIncrementer())
.listener(listener)
.flow(step1)
@@ -69,9 +62,9 @@ public class BatchConfiguration {
}
@Bean
public Step step1(JdbcBatchItemWriter<Coffee> writer) {
return stepBuilderFactory.get("step1")
.<Coffee, Coffee> chunk(10)
public Step step1(JobRepository jobRepository, PlatformTransactionManager transactionManager, JdbcBatchItemWriter<Coffee> writer) {
return new StepBuilder("step1", jobRepository)
.<Coffee, Coffee> chunk(10, transactionManager)
.reader(reader())
.processor(processor())
.writer(writer)
@@ -10,7 +10,7 @@ public class CoffeeItemProcessor implements ItemProcessor<Coffee, Coffee> {
private static final Logger LOGGER = LoggerFactory.getLogger(CoffeeItemProcessor.class);
@Override
public Coffee process(final Coffee coffee) throws Exception {
public Coffee process(final Coffee coffee) {
String brand = coffee.getBrand().toUpperCase();
String origin = coffee.getOrigin().toUpperCase();
String chracteristics = coffee.getCharacteristics().toUpperCase();
@@ -4,13 +4,13 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.batch.core.BatchStatus;
import org.springframework.batch.core.JobExecution;
import org.springframework.batch.core.listener.JobExecutionListenerSupport;
import org.springframework.batch.core.JobExecutionListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;
@Component
public class JobCompletionNotificationListener extends JobExecutionListenerSupport {
public class JobCompletionNotificationListener implements JobExecutionListener {
private static final Logger LOGGER = LoggerFactory.getLogger(JobCompletionNotificationListener.class);
@@ -7,15 +7,16 @@ import org.springframework.batch.core.Job;
import org.springframework.batch.core.JobExecution;
import org.springframework.batch.core.JobParametersBuilder;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.core.job.builder.JobBuilder;
import org.springframework.batch.core.launch.JobLauncher;
import org.springframework.batch.core.repository.JobRepository;
import org.springframework.batch.core.step.builder.StepBuilder;
import org.springframework.batch.item.ItemWriter;
import org.springframework.batch.item.file.FlatFileItemReader;
import org.springframework.batch.item.file.builder.FlatFileItemReaderBuilder;
import org.springframework.batch.item.file.mapping.BeanWrapperFieldSetMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;
@@ -24,17 +25,16 @@ import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
import org.springframework.scheduling.support.ScheduledMethodRunnable;
import org.springframework.transaction.PlatformTransactionManager;
import java.util.Date;
import java.util.IdentityHashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;
@Configuration
@EnableBatchProcessing
@EnableScheduling
public class SpringBatchScheduler {
@@ -46,21 +46,21 @@ public class SpringBatchScheduler {
private final Map<Object, ScheduledFuture<?>> scheduledTasks = new IdentityHashMap<>();
@Autowired
private JobBuilderFactory jobBuilderFactory;
@Autowired
private StepBuilderFactory stepBuilderFactory;
@Autowired
private JobLauncher jobLauncher;
@Autowired
private JobRepository jobRepository;
@Autowired
private PlatformTransactionManager transactionManager;
@Scheduled(fixedRate = 2000)
public void launchJob() throws Exception {
Date date = new Date();
logger.debug("scheduler starts at " + date);
if (enabled.get()) {
JobExecution jobExecution = jobLauncher.run(job(), new JobParametersBuilder().addDate("launchDate", date)
JobExecution jobExecution = jobLauncher.run(job(jobRepository, transactionManager), new JobParametersBuilder().addDate("launchDate", date)
.toJobParameters());
batchRunCounter.incrementAndGet();
logger.debug("Batch job ends with status as " + jobExecution.getStatus());
@@ -106,17 +106,16 @@ public class SpringBatchScheduler {
}
@Bean
public Job job() {
return jobBuilderFactory
.get("job")
.start(readBooks())
public Job job(JobRepository jobRepository, PlatformTransactionManager transactionManager) {
return new JobBuilder("job", jobRepository)
.start(readBooks(jobRepository, transactionManager))
.build();
}
@Bean
protected Step readBooks() {
return stepBuilderFactory.get("readBooks")
.<Book, Book> chunk(2)
protected Step readBooks(JobRepository jobRepository, PlatformTransactionManager transactionManager) {
return new StepBuilder("readBooks", jobRepository)
.<Book, Book> chunk(2, transactionManager)
.reader(reader())
.writer(writer())
.build();
@@ -128,7 +127,7 @@ public class SpringBatchScheduler {
.resource(new ClassPathResource("books.csv"))
.delimited()
.names(new String[] { "id", "name" })
.fieldSetMapper(new BeanWrapperFieldSetMapper<Book>() {
.fieldSetMapper(new BeanWrapperFieldSetMapper<>() {
{
setTargetType(Book.class);
}
@@ -138,15 +137,10 @@ public class SpringBatchScheduler {
@Bean
public ItemWriter<Book> writer() {
return new ItemWriter<Book>() {
@Override
public void write(List<? extends Book> items) throws Exception {
logger.debug("writer..." + items.size());
for (Book item : items) {
logger.debug(item.toString());
}
return items -> {
logger.debug("writer..." + items.size());
for (Book item : items) {
logger.debug(item.toString());
}
};
}
@@ -1,11 +1,10 @@
package com.baeldung.batch;
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertThat;
import org.junit.After;
import org.junit.Test;
import org.junit.runner.RunWith;
import static org.junit.jupiter.api.Assertions.assertEquals;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Test;
import org.springframework.batch.core.ExitStatus;
import org.springframework.batch.core.JobExecution;
import org.springframework.batch.core.JobInstance;
@@ -13,16 +12,17 @@ import org.springframework.batch.test.JobLauncherTestUtils;
import org.springframework.batch.test.JobRepositoryTestUtils;
import org.springframework.batch.test.context.SpringBatchTest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.context.annotation.PropertySource;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
@SpringBatchTest
@SpringBootTest
@DirtiesContext
@SpringJUnitConfig(BatchConfiguration.class)
@PropertySource("classpath:application.properties")
@RunWith(SpringRunner.class)
@EnableAutoConfiguration
public class SpringBootBatchIntegrationTest {
@Autowired
@@ -31,7 +31,10 @@ public class SpringBootBatchIntegrationTest {
@Autowired
private JobRepositoryTestUtils jobRepositoryTestUtils;
@After
@MockBean
private JobCompletionNotificationListener jobCompletionNotificationListener;
@AfterEach
public void cleanUp() {
jobRepositoryTestUtils.removeJobExecutions();
}
@@ -42,8 +45,8 @@ public class SpringBootBatchIntegrationTest {
JobInstance jobInstance = jobExecution.getJobInstance();
ExitStatus jobExitStatus = jobExecution.getExitStatus();
assertThat(jobInstance.getJobName(), is("importUserJob"));
assertThat(jobExitStatus.getExitCode(), is("COMPLETED"));
assertEquals("importUserJob", jobInstance.getJobName());
assertEquals("COMPLETED", jobExitStatus.getExitCode());
}
}
@@ -1,28 +1,20 @@
package com.baeldung.batchscheduler;
import com.baeldung.batchscheduler.SpringBatchScheduler;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.batch.test.context.SpringBatchTest;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.PropertySource;
import org.springframework.scheduling.annotation.ScheduledAnnotationBeanPostProcessor;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.junit4.SpringRunner;
import static org.awaitility.Awaitility.await;
import static java.util.concurrent.TimeUnit.*;
import static org.junit.jupiter.api.Assertions.assertEquals;
@SpringBatchTest
@SpringBootTest
@DirtiesContext
@PropertySource("classpath:application.properties")
@RunWith(SpringRunner.class)
public class SpringBatchSchedulerIntegrationTest {
@Autowired
@@ -31,37 +23,36 @@ public class SpringBatchSchedulerIntegrationTest {
@Test
public void stopJobsWhenSchedulerDisabled() {
SpringBatchScheduler schedulerBean = context.getBean(SpringBatchScheduler.class);
await().untilAsserted(() -> Assert.assertEquals(2, schedulerBean.getBatchRunCounter()
await().untilAsserted(() -> assertEquals(2, schedulerBean.getBatchRunCounter()
.get()));
schedulerBean.stop();
await().atLeast(3, SECONDS);
Assert.assertEquals(2, schedulerBean.getBatchRunCounter()
.get());
assertEquals(2, schedulerBean.getBatchRunCounter().get());
}
@Test
public void stopJobSchedulerWhenSchedulerDestroyed() throws Exception {
public void stopJobSchedulerWhenSchedulerDestroyed() {
ScheduledAnnotationBeanPostProcessor bean = context.getBean(ScheduledAnnotationBeanPostProcessor.class);
SpringBatchScheduler schedulerBean = context.getBean(SpringBatchScheduler.class);
await().untilAsserted(() -> Assert.assertEquals(2, schedulerBean.getBatchRunCounter()
await().untilAsserted(() -> assertEquals(2, schedulerBean.getBatchRunCounter()
.get()));
bean.postProcessBeforeDestruction(schedulerBean, "SpringBatchScheduler");
await().atLeast(3, SECONDS);
Assert.assertEquals(2, schedulerBean.getBatchRunCounter()
assertEquals(2, schedulerBean.getBatchRunCounter()
.get());
}
@Test
public void stopJobSchedulerWhenFutureTasksCancelled() throws Exception {
public void stopJobSchedulerWhenFutureTasksCancelled() {
SpringBatchScheduler schedulerBean = context.getBean(SpringBatchScheduler.class);
await().untilAsserted(() -> Assert.assertEquals(2, schedulerBean.getBatchRunCounter()
await().untilAsserted(() -> assertEquals(2, schedulerBean.getBatchRunCounter()
.get()));
schedulerBean.cancelFutureSchedulerTasks();
await().atLeast(3, SECONDS);
Assert.assertEquals(2, schedulerBean.getBatchRunCounter()
assertEquals(2, schedulerBean.getBatchRunCounter()
.get());
}