Updated project to use Spring Boot 3.0

Resolves issues:
TASK-810
TASK-812
TASK-813
This commit is contained in:
Glenn Renfro
2022-01-07 17:38:50 -05:00
parent a95b420315
commit 02b44227d4
30 changed files with 184 additions and 143 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2016-2019 the original author or authors.
* Copyright 2016-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -154,22 +154,7 @@ public class DeployerPartitionHandler
* @param jobExplorer used to acquire the status of the job.
* @param resource the url to the app to be launched.
* @param stepName the name of the step.
* @deprecated Use the constructor that accepts {@link TaskRepository} as well as the
* this constructor's set of parameters.
*/
@Deprecated
public DeployerPartitionHandler(TaskLauncher taskLauncher, JobExplorer jobExplorer,
Resource resource, String stepName) {
Assert.notNull(taskLauncher, "A taskLauncher is required");
Assert.notNull(jobExplorer, "A jobExplorer is required");
Assert.notNull(resource, "A resource is required");
Assert.hasText(stepName, "A step name is required");
this.taskLauncher = taskLauncher;
this.jobExplorer = jobExplorer;
this.resource = resource;
this.stepName = stepName;
}
public DeployerPartitionHandler(TaskLauncher taskLauncher, JobExplorer jobExplorer,
Resource resource, String stepName, TaskRepository taskRepository) {

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2018-2019 the original author or authors.
* Copyright 2018-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -16,6 +16,10 @@
package org.springframework.cloud.task.batch.handler;
import java.util.List;
import javax.sql.DataSource;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
@@ -32,21 +36,28 @@ import org.springframework.batch.core.configuration.annotation.EnableBatchProces
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.core.explore.JobExplorer;
import org.springframework.batch.core.explore.support.MapJobExplorerFactoryBean;
import org.springframework.batch.core.explore.support.JobExplorerFactoryBean;
import org.springframework.batch.core.launch.JobLauncher;
import org.springframework.batch.core.launch.support.RunIdIncrementer;
import org.springframework.batch.core.launch.support.SimpleJobLauncher;
import org.springframework.batch.core.repository.JobRepository;
import org.springframework.batch.core.repository.JobRestartException;
import org.springframework.batch.core.repository.support.MapJobRepositoryFactoryBean;
import org.springframework.batch.core.repository.dao.DefaultExecutionContextSerializer;
import org.springframework.batch.core.repository.support.JobRepositoryFactoryBean;
import org.springframework.batch.core.step.tasklet.Tasklet;
import org.springframework.batch.repeat.RepeatStatus;
import org.springframework.batch.support.transaction.ResourcelessTransactionManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.jdbc.EmbeddedDataSourceConfiguration;
import org.springframework.cloud.task.batch.configuration.TaskBatchProperties;
import org.springframework.cloud.task.listener.TaskException;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.task.SyncTaskExecutor;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.init.DataSourceInitializer;
import org.springframework.jdbc.datasource.init.ResourceDatabasePopulator;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit.jupiter.SpringExtension;
@@ -195,16 +206,25 @@ public class TaskJobLauncherApplicationRunnerCoreTests {
runFailedJob(new JobParametersBuilder().addLong("run.id", 1L)
.addLong("id", 2L, false).addLong("foo", 3L, false).toJobParameters());
assertThat(this.jobExplorer.getJobInstances("job", 0, 100)).hasSize(1);
JobInstance jobInstance = this.jobExplorer.getJobInstance(0L);
assertThat(this.jobExplorer.getJobExecutions(jobInstance)).hasSize(2);
JobInstance jobInstance = jobExplorer.getLastJobInstance("job");
List<JobExecution> executions = this.jobExplorer.getJobExecutions(jobInstance);
assertThat(executions).hasSize(2);
JobExecution firstJobExecution = executions.get(0);
JobExecution secondJobExecution = executions.get(1);
if ((executions.get(0).getId() > executions.get(1).getId())) {
firstJobExecution = executions.get(1);
secondJobExecution = executions.get(0);
}
// first execution
JobExecution firstJobExecution = this.jobExplorer.getJobExecution(0L);
JobParameters parameters = firstJobExecution.getJobParameters();
assertThat(parameters.getLong("run.id")).isEqualTo(1L);
assertThat(parameters.getLong("id")).isEqualTo(1L);
assertThat(parameters.getLong("foo")).isEqualTo(2L);
assertThat(parameters.getLong("id")).isEqualTo(2L);
assertThat(parameters.getLong("foo")).isEqualTo(3L);
// second execution
JobExecution secondJobExecution = this.jobExplorer.getJobExecution(1L);
parameters = secondJobExecution.getJobParameters();
// identifying parameters should be the same as previous execution
assertThat(parameters.getLong("run.id")).isEqualTo(1L);
@@ -232,21 +252,34 @@ public class TaskJobLauncherApplicationRunnerCoreTests {
@Configuration
@EnableBatchProcessing
@Import(EmbeddedDataSourceConfiguration.class)
protected static class BatchConfiguration implements BatchConfigurer {
private ResourcelessTransactionManager transactionManager = new ResourcelessTransactionManager();
private JobRepository jobRepository;
private MapJobRepositoryFactoryBean jobRepositoryFactory = new MapJobRepositoryFactoryBean(
this.transactionManager);
@Autowired
private DataSource dataSource;
public BatchConfiguration() throws Exception {
this.jobRepository = this.jobRepositoryFactory.getObject();
}
private JobRepository jobRepositoryNew() throws Exception {
JobRepositoryFactoryBean jobRepositoryFactoryBean = new JobRepositoryFactoryBean();
jobRepositoryFactoryBean.setDataSource(dataSource);
jobRepositoryFactoryBean.setTransactionManager(transactionManager);
jobRepositoryFactoryBean.setSerializer(new DefaultExecutionContextSerializer());
this.jobRepository = jobRepositoryFactoryBean.getObject();
return this.jobRepository;
}
@Override
public JobRepository getJobRepository() {
public JobRepository getJobRepository() throws Exception {
if (this.jobRepository == null) {
this.jobRepository = jobRepositoryNew();
}
return this.jobRepository;
}
@@ -256,18 +289,36 @@ public class TaskJobLauncherApplicationRunnerCoreTests {
}
@Override
public JobLauncher getJobLauncher() {
public JobLauncher getJobLauncher() throws Exception {
SimpleJobLauncher launcher = new SimpleJobLauncher();
launcher.setJobRepository(this.jobRepository);
launcher.setJobRepository(getJobRepository());
launcher.setTaskExecutor(new SyncTaskExecutor());
return launcher;
}
@Override
public JobExplorer getJobExplorer() throws Exception {
return new MapJobExplorerFactoryBean(this.jobRepositoryFactory).getObject();
dataSourceInitializer().afterPropertiesSet();
JobExplorerFactoryBean factoryBean = new JobExplorerFactoryBean();
factoryBean.setDataSource(dataSource);
factoryBean.setJdbcOperations(new JdbcTemplate(dataSource));
factoryBean.setSerializer(new DefaultExecutionContextSerializer());
return factoryBean.getObject();
}
public DataSourceInitializer dataSourceInitializer() {
DataSourceInitializer dataSourceInitializer = new DataSourceInitializer();
dataSourceInitializer.setDataSource(dataSource);
ResourceDatabasePopulator databasePopulator = new ResourceDatabasePopulator();
databasePopulator.addScript(new ClassPathResource("org/springframework/batch/core/schema-h2.sql"));
dataSourceInitializer.setDatabasePopulator(databasePopulator);
databasePopulator = new ResourceDatabasePopulator();
databasePopulator.addScript(new ClassPathResource("org/springframework/batch/core/schema-drop-h2.sql"));
dataSourceInitializer.setDatabaseCleaner(databasePopulator);
return dataSourceInitializer;
}
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2016-2019 the original author or authors.
* Copyright 2016-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -88,7 +88,7 @@ public class DeployerPartitionHandlerTests {
@BeforeEach
public void setUp() {
MockitoAnnotations.initMocks(this);
MockitoAnnotations.openMocks(this);
this.environment = new MockEnvironment();
TaskExecution taskExecution = new TaskExecution(2, 0, "name", new Date(),
new Date(), "", Collections.emptyList(), null, null, null);
@@ -108,7 +108,7 @@ public class DeployerPartitionHandlerTests {
this.resource, null, "A step name is required");
new DeployerPartitionHandler(this.taskLauncher, this.jobExplorer, this.resource,
"step-name");
"step-name", this.taskRepository);
}
@Test
@@ -132,7 +132,7 @@ public class DeployerPartitionHandlerTests {
@Test
public void testNoPartitions() throws Exception {
DeployerPartitionHandler handler = new DeployerPartitionHandler(this.taskLauncher,
this.jobExplorer, this.resource, "step1");
this.jobExplorer, this.resource, "step1", this.taskRepository);
handler.setEnvironment(this.environment);
StepExecution stepExecution = new StepExecution("step1", new JobExecution(1L));

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2016-2019 the original author or authors.
* Copyright 2016-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -69,7 +69,7 @@ public class DeployerStepExecutionHandlerTests {
@BeforeEach
public void setUp() {
MockitoAnnotations.initMocks(this);
MockitoAnnotations.openMocks(this);
this.handler = new DeployerStepExecutionHandler(this.beanFactory,
this.jobExplorer, this.jobRepository);