Added checkstyle

This commit is contained in:
Marcin Grzejszczak
2019-02-03 19:27:07 +01:00
parent 4d0acf120c
commit 60f1e21d03
249 changed files with 7450 additions and 5857 deletions

View File

@@ -1,5 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
@@ -12,7 +14,8 @@
<artifactId>spring-cloud-task-batch</artifactId>
<packaging>jar</packaging>
<name>Spring Cloud Task Batch</name>
<description>Module for use when combining Spring Cloud Task with Spring Batch</description>
<description>Module for use when combining Spring Cloud Task with Spring Batch
</description>
<dependencies>
<dependency>
@@ -79,7 +82,7 @@
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<scope>test</scope>
</dependency>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2016-2018 the original author or authors.
* Copyright 2015-2019 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.
@@ -13,6 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.cloud.task.batch.configuration;
import org.springframework.batch.core.Job;
@@ -38,8 +39,9 @@ import org.springframework.context.annotation.Configuration;
* @author Michael Minella
*/
@Configuration
@ConditionalOnBean({Job.class})
@ConditionalOnProperty(name = {"spring.cloud.task.batch.listener.enable", "spring.cloud.task.batch.listener.enabled"}, havingValue = "true", matchIfMissing = true)
@ConditionalOnBean({ Job.class })
@ConditionalOnProperty(name = { "spring.cloud.task.batch.listener.enable",
"spring.cloud.task.batch.listener.enabled" }, havingValue = "true", matchIfMissing = true)
public class TaskBatchAutoConfiguration {
@Bean
@@ -48,6 +50,9 @@ public class TaskBatchAutoConfiguration {
return new TaskBatchExecutionListenerBeanPostProcessor();
}
/**
* Auto configuration for Task Batch Execution Listener.
*/
@Configuration
@ConditionalOnMissingBean(name = "taskBatchExecutionListener")
@EnableConfigurationProperties(TaskProperties.class)
@@ -60,20 +65,23 @@ public class TaskBatchAutoConfiguration {
private TaskProperties taskProperties;
@Bean
public TaskBatchExecutionListenerFactoryBean taskBatchExecutionListener(TaskExplorer taskExplorer) {
public TaskBatchExecutionListenerFactoryBean taskBatchExecutionListener(
TaskExplorer taskExplorer) {
TaskConfigurer taskConfigurer = null;
if(!this.context.getBeansOfType(TaskConfigurer.class).isEmpty()) {
if (!this.context.getBeansOfType(TaskConfigurer.class).isEmpty()) {
taskConfigurer = this.context.getBean(TaskConfigurer.class);
}
if(taskConfigurer != null && taskConfigurer.getTaskDataSource() != null) {
if (taskConfigurer != null && taskConfigurer.getTaskDataSource() != null) {
return new TaskBatchExecutionListenerFactoryBean(
taskConfigurer.getTaskDataSource(),
taskExplorer, taskProperties.getTablePrefix());
taskConfigurer.getTaskDataSource(), taskExplorer,
this.taskProperties.getTablePrefix());
}
else {
return new TaskBatchExecutionListenerFactoryBean(null,
taskExplorer, taskProperties.getTablePrefix());
return new TaskBatchExecutionListenerFactoryBean(null, taskExplorer,
this.taskProperties.getTablePrefix());
}
}
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2016 the original author or authors.
* Copyright 2015-2019 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.
@@ -13,6 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.cloud.task.batch.configuration;
import java.util.ArrayList;
@@ -28,7 +29,7 @@ import org.springframework.util.Assert;
/**
* Injects a configured {@link TaskBatchExecutionListener} into any batch jobs (beans
* assignable to {@link AbstractJob}) that are executed within the scope of a task. The
* assignable to {@link AbstractJob}) that are executed within the scope of a task. The
* context this is used within is expected to have only one bean of type
* {@link TaskBatchExecutionListener}.
*
@@ -50,17 +51,17 @@ public class TaskBatchExecutionListenerBeanPostProcessor implements BeanPostProc
@Override
public Object postProcessAfterInitialization(Object bean, String beanName)
throws BeansException {
if(jobNames.size() > 0 && !jobNames.contains(beanName)) {
return bean;
if (this.jobNames.size() > 0 && !this.jobNames.contains(beanName)) {
return bean;
}
int length = this.applicationContext
.getBeanNamesForType(TaskBatchExecutionListener.class).length;
if(bean instanceof AbstractJob) {
if(length != 1) {
throw new IllegalStateException("The application context is required to " +
"have exactly 1 instance of the TaskBatchExecutionListener but has " +
length);
if (bean instanceof AbstractJob) {
if (length != 1) {
throw new IllegalStateException("The application context is required to "
+ "have exactly 1 instance of the TaskBatchExecutionListener but has "
+ length);
}
((AbstractJob) bean).registerJobExecutionListener(
this.applicationContext.getBean(TaskBatchExecutionListener.class));
@@ -73,4 +74,5 @@ public class TaskBatchExecutionListenerBeanPostProcessor implements BeanPostProc
this.jobNames = jobNames;
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2016 the original author or authors.
* Copyright 2015-2019 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.
@@ -13,9 +13,11 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.cloud.task.batch.configuration;
import java.lang.reflect.Field;
import javax.sql.DataSource;
import org.springframework.aop.framework.Advised;
@@ -32,13 +34,14 @@ import org.springframework.util.Assert;
import org.springframework.util.ReflectionUtils;
/**
* {@link FactoryBean} for a {@link TaskBatchExecutionListener}. Provides a jdbc based
* listener if there is a {@link DataSource} available. Otherwise, builds a listener that
* {@link FactoryBean} for a {@link TaskBatchExecutionListener}. Provides a jdbc based
* listener if there is a {@link DataSource} available. Otherwise, builds a listener that
* uses the map based implementation.
*
* @author Michael Minella
*/
public class TaskBatchExecutionListenerFactoryBean implements FactoryBean<TaskBatchExecutionListener> {
public class TaskBatchExecutionListenerFactoryBean
implements FactoryBean<TaskBatchExecutionListener> {
private TaskBatchExecutionListener listener;
@@ -49,45 +52,45 @@ public class TaskBatchExecutionListenerFactoryBean implements FactoryBean<TaskBa
private String tablePrefix = TaskProperties.DEFAULT_TABLE_PREFIX;
/**
* Initializes the TaskBatchExecutionListenerFactoryBean and defaults the
* tablePrefix to {@link TaskProperties#DEFAULT_TABLE_PREFIX}.
*
* Initializes the TaskBatchExecutionListenerFactoryBean and defaults the tablePrefix
* to {@link TaskProperties#DEFAULT_TABLE_PREFIX}.
* @param dataSource the dataSource to use for the TaskBatchExecutionListener.
* @param taskExplorer the taskExplorer to use for the TaskBatchExecutionListener.
*/
public TaskBatchExecutionListenerFactoryBean(DataSource dataSource, TaskExplorer taskExplorer) {
public TaskBatchExecutionListenerFactoryBean(DataSource dataSource,
TaskExplorer taskExplorer) {
this.dataSource = dataSource;
this.taskExplorer = taskExplorer;
}
/**
* Initializes the TaskBatchExecutionListenerFactoryBean.
*
* @param dataSource the dataSource to use for the TaskBatchExecutionListener.
* @param taskExplorer the taskExplorer to use for the TaskBatchExecutionListener.
* @param tablePrefix the prefix for the task tables accessed by the
* TaskBatchExecutionListener.
*/
public TaskBatchExecutionListenerFactoryBean(DataSource dataSource, TaskExplorer taskExplorer, String tablePrefix) {
this(dataSource,taskExplorer);
public TaskBatchExecutionListenerFactoryBean(DataSource dataSource,
TaskExplorer taskExplorer, String tablePrefix) {
this(dataSource, taskExplorer);
Assert.hasText(tablePrefix, "tablePrefix must not be null nor empty.");
this.tablePrefix = tablePrefix;
}
@Override
public TaskBatchExecutionListener getObject() throws Exception {
if(listener != null){
return listener;
if (this.listener != null) {
return this.listener;
}
if(this.dataSource == null) {
if (this.dataSource == null) {
this.listener = new TaskBatchExecutionListener(getMapTaskBatchDao());
}
else {
this.listener = new TaskBatchExecutionListener(
new JdbcTaskBatchDao(this.dataSource, tablePrefix));
new JdbcTaskBatchDao(this.dataSource, this.tablePrefix));
}
return listener;
return this.listener;
}
@Override
@@ -101,22 +104,25 @@ public class TaskBatchExecutionListenerFactoryBean implements FactoryBean<TaskBa
}
private MapTaskBatchDao getMapTaskBatchDao() throws Exception {
Field taskExecutionDaoField = ReflectionUtils.findField(SimpleTaskExplorer.class, "taskExecutionDao");
Field taskExecutionDaoField = ReflectionUtils.findField(SimpleTaskExplorer.class,
"taskExecutionDao");
taskExecutionDaoField.setAccessible(true);
MapTaskExecutionDao taskExecutionDao;
if(AopUtils.isJdkDynamicProxy(this.taskExplorer)) {
SimpleTaskExplorer dereferencedTaskRepository = (SimpleTaskExplorer) ((Advised) this.taskExplorer).getTargetSource().getTarget();
if (AopUtils.isJdkDynamicProxy(this.taskExplorer)) {
SimpleTaskExplorer dereferencedTaskRepository = (SimpleTaskExplorer) ((Advised) this.taskExplorer)
.getTargetSource().getTarget();
taskExecutionDao =
(MapTaskExecutionDao) ReflectionUtils.getField(taskExecutionDaoField, dereferencedTaskRepository);
taskExecutionDao = (MapTaskExecutionDao) ReflectionUtils
.getField(taskExecutionDaoField, dereferencedTaskRepository);
}
else {
taskExecutionDao =
(MapTaskExecutionDao) ReflectionUtils.getField(taskExecutionDaoField, this.taskExplorer);
taskExecutionDao = (MapTaskExecutionDao) ReflectionUtils
.getField(taskExecutionDaoField, this.taskExplorer);
}
return new MapTaskBatchDao(taskExecutionDao.getBatchJobAssociations());
}
}

View File

@@ -1,17 +1,17 @@
/*
* Copyright 2018 the original author or authors.
* Copyright 2015-2019 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.
* You may obtain a copy of the License at
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.cloud.task.batch.configuration;
@@ -19,18 +19,17 @@ package org.springframework.cloud.task.batch.configuration;
import org.springframework.boot.context.properties.ConfigurationProperties;
/**
* Establish properties to be used for how Tasks work with
* Spring Batch.
* Establish properties to be used for how Tasks work with Spring Batch.
*
* @author Glenn Renfro
* @author Michael Minella
*
* @since 2.0.0
*/
@ConfigurationProperties(prefix = "spring.cloud.task.batch")
public class TaskBatchProperties {
private static final long DEFAULT_POLL_INTERVAL = 5000L;
/**
* Comma-separated list of job names to execute on startup (for instance,
* `job1,job2`). By default, all Jobs found in the context are executed.
@@ -39,16 +38,16 @@ public class TaskBatchProperties {
/**
* The order for the {@code CommandLineRunner} used to run batch jobs when
* {@code spring.cloud.task.batch.fail-on-job-failure=true}. Defaults to 0 (same as the
* {@code spring.cloud.task.batch.fail-on-job-failure=true}. Defaults to 0 (same as
* the
* {@link org.springframework.boot.autoconfigure.batch.JobLauncherCommandLineRunner}).
*/
private int commandLineRunnerOrder = 0;
/**
* Fixed delay in milliseconds that Spring Cloud Task will wait when checking if
* {@link org.springframework.batch.core.JobExecution}s have completed,
* when spring.cloud.task.batch.failOnJobFailure is set to true. Defaults
* to 5000.
* {@link org.springframework.batch.core.JobExecution}s have completed, when
* spring.cloud.task.batch.failOnJobFailure is set to true. Defaults to 5000.
*/
private long failOnJobFailurePollInterval = DEFAULT_POLL_INTERVAL;
@@ -61,7 +60,7 @@ public class TaskBatchProperties {
}
public int getCommandLineRunnerOrder() {
return commandLineRunnerOrder;
return this.commandLineRunnerOrder;
}
public void setCommandLineRunnerOrder(int commandLineRunnerOrder) {
@@ -69,10 +68,11 @@ public class TaskBatchProperties {
}
public long getFailOnJobFailurePollInterval() {
return failOnJobFailurePollInterval;
return this.failOnJobFailurePollInterval;
}
public void setFailOnJobFailurePollInterval(long failOnJobFailurePollInterval) {
this.failOnJobFailurePollInterval = failOnJobFailurePollInterval;
}
}

View File

@@ -1,17 +1,17 @@
/*
* Copyright 2018 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
* Copyright 2015-2019 the original author or authors.
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.cloud.task.batch.configuration;
@@ -47,16 +47,15 @@ public class TaskJobLauncherAutoConfiguration {
private TaskBatchProperties properties;
@Bean
public TaskJobLauncherCommandLineRunnerFactoryBean jobLauncherCommandLineRunner(JobLauncher jobLauncher,
JobExplorer jobExplorer, List<Job> jobs, JobRegistry jobRegistry, JobRepository jobRepository) {
TaskJobLauncherCommandLineRunnerFactoryBean taskJobLauncherCommandLineRunnerFactoryBean =
new TaskJobLauncherCommandLineRunnerFactoryBean(jobLauncher,
jobExplorer,
jobs,
this.properties,
jobRegistry,
jobRepository);
public TaskJobLauncherCommandLineRunnerFactoryBean jobLauncherCommandLineRunner(
JobLauncher jobLauncher, JobExplorer jobExplorer, List<Job> jobs,
JobRegistry jobRegistry, JobRepository jobRepository) {
TaskJobLauncherCommandLineRunnerFactoryBean taskJobLauncherCommandLineRunnerFactoryBean;
taskJobLauncherCommandLineRunnerFactoryBean = new TaskJobLauncherCommandLineRunnerFactoryBean(
jobLauncher, jobExplorer, jobs, this.properties, jobRegistry,
jobRepository);
return taskJobLauncherCommandLineRunnerFactoryBean;
}
}

View File

@@ -1,17 +1,17 @@
/*
* Copyright 2018 the original author or authors.
* Copyright 2015-2019 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.
* You may obtain a copy of the License at
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.cloud.task.batch.configuration;
@@ -33,7 +33,8 @@ import org.springframework.util.StringUtils;
*
* @author Glenn Renfro
*/
public class TaskJobLauncherCommandLineRunnerFactoryBean implements FactoryBean<TaskJobLauncherCommandLineRunner> {
public class TaskJobLauncherCommandLineRunnerFactoryBean
implements FactoryBean<TaskJobLauncherCommandLineRunner> {
private JobLauncher jobLauncher;
@@ -52,8 +53,9 @@ public class TaskJobLauncherCommandLineRunnerFactoryBean implements FactoryBean<
private JobRepository jobRepository;
public TaskJobLauncherCommandLineRunnerFactoryBean(JobLauncher jobLauncher,
JobExplorer jobExplorer, List<Job> jobs, TaskBatchProperties taskBatchProperties,
JobRegistry jobRegistry, JobRepository jobRepository) {
JobExplorer jobExplorer, List<Job> jobs,
TaskBatchProperties taskBatchProperties, JobRegistry jobRegistry,
JobRepository jobRepository) {
Assert.notNull(taskBatchProperties, "properties must not be null");
this.jobLauncher = jobLauncher;
this.jobExplorer = jobExplorer;
@@ -72,15 +74,16 @@ public class TaskJobLauncherCommandLineRunnerFactoryBean implements FactoryBean<
@Override
public TaskJobLauncherCommandLineRunner getObject() {
TaskJobLauncherCommandLineRunner taskJobLauncherCommandLineRunner =
new TaskJobLauncherCommandLineRunner(this.jobLauncher, this.jobExplorer, this.jobRepository, this.taskBatchProperties);
TaskJobLauncherCommandLineRunner taskJobLauncherCommandLineRunner = new TaskJobLauncherCommandLineRunner(
this.jobLauncher, this.jobExplorer, this.jobRepository,
this.taskBatchProperties);
taskJobLauncherCommandLineRunner.setJobs(this.jobs);
if(StringUtils.hasText(this.jobNames)) {
if (StringUtils.hasText(this.jobNames)) {
taskJobLauncherCommandLineRunner.setJobNames(this.jobNames);
}
taskJobLauncherCommandLineRunner.setJobRegistry(this.jobRegistry);
if(this.order != null) {
if (this.order != null) {
taskJobLauncherCommandLineRunner.setOrder(this.order);
}
return taskJobLauncherCommandLineRunner;

View File

@@ -1,17 +1,17 @@
/*
* Copyright 2018 the original author or authors.
* Copyright 2015-2019 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.
* You may obtain a copy of the License at
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.cloud.task.batch.handler;
@@ -57,11 +57,11 @@ import org.springframework.util.StringUtils;
* {@link CommandLineRunner} to {@link JobLauncher launch} Spring Batch jobs. Runs all
* jobs in the surrounding context by default and throws an exception upon the first job
* that returns an {@link BatchStatus} of FAILED if a {@link TaskExecutor} in the
* {@link JobLauncher} is not specified. If a {@link TaskExecutor} is specified
* in the {@link JobLauncher} then all Jobs are launched and an
* exception is thrown if one or more of the jobs has an {@link BatchStatus} of FAILED.
* TaskJobLauncherCommandLineRunner can also be used to launch a specific job by
* providing a jobName. The TaskJobLaunchercommandLineRunner takes the place of the
* {@link JobLauncher} is not specified. If a {@link TaskExecutor} is specified in the
* {@link JobLauncher} then all Jobs are launched and an exception is thrown if one or
* more of the jobs has an {@link BatchStatus} of FAILED. TaskJobLauncherCommandLineRunner
* can also be used to launch a specific job by providing a jobName. The
* TaskJobLaunchercommandLineRunner takes the place of the
* {@link org.springframework.boot.autoconfigure.batch.JobLauncherCommandLineRunner} when
* it is in use.
*
@@ -70,15 +70,15 @@ import org.springframework.util.StringUtils;
*/
public class TaskJobLauncherCommandLineRunner extends JobLauncherCommandLineRunner {
private static final Log logger = LogFactory
.getLog(TaskJobLauncherCommandLineRunner.class);
private JobLauncher taskJobLauncher;
private JobExplorer taskJobExplorer;
private JobRepository taskJobRepository;
private static final Log logger = LogFactory
.getLog(TaskJobLauncherCommandLineRunner.class);
private List<JobExecution> jobExecutionList = new ArrayList<>();
private ApplicationEventPublisher taskApplicationEventPublisher;
@@ -91,10 +91,12 @@ public class TaskJobLauncherCommandLineRunner extends JobLauncherCommandLineRunn
* @param jobExplorer to check the job repository for previous executions
* @param jobRepository to check if a job instance exists with the given parameters
* when running a job
* @param taskBatchProperties the properties used to configure the taskBatchProperties.
* @param taskBatchProperties the properties used to configure the
* taskBatchProperties.
*/
public TaskJobLauncherCommandLineRunner(JobLauncher jobLauncher, JobExplorer jobExplorer,
JobRepository jobRepository, TaskBatchProperties taskBatchProperties) {
public TaskJobLauncherCommandLineRunner(JobLauncher jobLauncher,
JobExplorer jobExplorer, JobRepository jobRepository,
TaskBatchProperties taskBatchProperties) {
super(jobLauncher, jobExplorer, jobRepository);
this.taskJobLauncher = jobLauncher;
this.taskJobExplorer = jobExplorer;
@@ -151,7 +153,8 @@ public class TaskJobLauncherCommandLineRunner extends JobLauncherCommandLineRunn
}
JobExecution execution = this.taskJobLauncher.run(job, parameters);
if (this.taskApplicationEventPublisher != null) {
this.taskApplicationEventPublisher.publishEvent(new JobExecutionEvent(execution));
this.taskApplicationEventPublisher
.publishEvent(new JobExecutionEvent(execution));
}
this.jobExecutionList.add(execution);
if (execution.getStatus().equals(BatchStatus.FAILED)) {
@@ -168,8 +171,9 @@ public class TaskJobLauncherCommandLineRunner extends JobLauncherCommandLineRunn
List<JobExecution> failedJobExecutions = new ArrayList<>();
RepeatStatus repeatStatus = RepeatStatus.FINISHED;
for (JobExecution jobExecution : jobExecutionList) {
JobExecution currentJobExecution = taskJobExplorer.getJobExecution(jobExecution.getId());
for (JobExecution jobExecution : this.jobExecutionList) {
JobExecution currentJobExecution = this.taskJobExplorer
.getJobExecution(jobExecution.getId());
BatchStatus batchStatus = currentJobExecution.getStatus();
if (batchStatus.isRunning()) {
repeatStatus = RepeatStatus.CONTINUABLE;
@@ -178,9 +182,10 @@ public class TaskJobLauncherCommandLineRunner extends JobLauncherCommandLineRunn
failedJobExecutions.add(jobExecution);
}
}
Thread.sleep(taskBatchProperties.getFailOnJobFailurePollInterval());
Thread.sleep(this.taskBatchProperties.getFailOnJobFailurePollInterval());
if (repeatStatus.equals(RepeatStatus.FINISHED) && failedJobExecutions.size() > 0) {
if (repeatStatus.equals(RepeatStatus.FINISHED)
&& failedJobExecutions.size() > 0) {
throwJobFailedException(failedJobExecutions);
}
return repeatStatus;
@@ -190,8 +195,8 @@ public class TaskJobLauncherCommandLineRunner extends JobLauncherCommandLineRunn
private void throwJobFailedException(List<JobExecution> failedJobExecutions) {
StringBuilder message = new StringBuilder("The following Jobs have failed: \n");
for (JobExecution failedJobExecution : failedJobExecutions) {
message.append(String.format("Job %s failed during " +
"execution for job instance id %s with jobExecutionId of %s \n",
message.append(String.format("Job %s failed during "
+ "execution for job instance id %s with jobExecutionId of %s \n",
failedJobExecution.getJobInstance().getJobName(),
failedJobExecution.getJobId(), failedJobExecution.getId()));
}
@@ -201,6 +206,7 @@ public class TaskJobLauncherCommandLineRunner extends JobLauncherCommandLineRunn
throw new TaskException(message.toString());
}
private JobParameters removeNonIdentifying(JobParameters parameters) {
Map<String, JobParameter> parameterMap = parameters.getParameters();
HashMap<String, JobParameter> copy = new HashMap<>(parameterMap);
@@ -225,4 +231,5 @@ public class TaskJobLauncherCommandLineRunner extends JobLauncherCommandLineRunn
merged.putAll(additionals.getParameters());
return new JobParameters(merged);
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2016 the original author or authors.
* Copyright 2015-2019 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.
@@ -13,6 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.cloud.task.batch.listener;
import org.springframework.batch.core.JobExecution;
@@ -28,9 +29,9 @@ public interface TaskBatchDao {
/**
* Saves the relationship between a task execution and a job execution.
*
* @param taskExecution task execution
* @param jobExecution job execution
*/
void saveRelationship(TaskExecution taskExecution, JobExecution jobExecution);
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2016 the original author or authors.
* Copyright 2015-2019 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.
@@ -13,10 +13,12 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.cloud.task.batch.listener;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.batch.core.JobExecution;
import org.springframework.batch.core.listener.JobExecutionListenerSupport;
import org.springframework.cloud.task.listener.annotation.BeforeTask;
@@ -31,14 +33,14 @@ import org.springframework.util.Assert;
*/
public class TaskBatchExecutionListener extends JobExecutionListenerSupport {
private static final Log logger = LogFactory.getLog(TaskBatchExecutionListener.class);
private TaskExecution taskExecution;
private TaskBatchDao taskBatchDao;
private static final Log logger = LogFactory.getLog(TaskBatchExecutionListener.class);
/**
* @param taskBatchDao dao used to persist the relationship. Must not be null
* @param taskBatchDao dao used to persist the relationship. Must not be null
*/
public TaskBatchExecutionListener(TaskBatchDao taskBatchDao) {
Assert.notNull(taskBatchDao, "A TaskBatchDao is required");
@@ -53,14 +55,16 @@ public class TaskBatchExecutionListener extends JobExecutionListenerSupport {
@Override
public void beforeJob(JobExecution jobExecution) {
if(this.taskExecution == null) {
logger.warn("This job was executed outside the scope of a task but still used the task listener.");
if (this.taskExecution == null) {
logger.warn(
"This job was executed outside the scope of a task but still used the task listener.");
}
else {
logger.info(String.format("The job execution id %s was run within the task execution %s",
jobExecution.getId(),
this.taskExecution.getExecutionId()));
taskBatchDao.saveRelationship(taskExecution, jobExecution);
logger.info(String.format(
"The job execution id %s was run within the task execution %s",
jobExecution.getId(), this.taskExecution.getExecutionId()));
this.taskBatchDao.saveRelationship(this.taskExecution, jobExecution);
}
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2016-2018 the original author or authors.
* Copyright 2015-2019 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.
@@ -13,6 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.cloud.task.batch.listener.support;
import javax.sql.DataSource;
@@ -27,7 +28,7 @@ import org.springframework.util.Assert;
import org.springframework.util.StringUtils;
/**
* JDBC based implementation of the {@link TaskBatchDao}. Intended to be used in
* JDBC based implementation of the {@link TaskBatchDao}. Intended to be used in
* conjunction with the JDBC based
* {@link org.springframework.cloud.task.repository.TaskRepository}
*
@@ -36,10 +37,10 @@ import org.springframework.util.StringUtils;
*/
public class JdbcTaskBatchDao implements TaskBatchDao {
private String tablePrefix = TaskProperties.DEFAULT_TABLE_PREFIX;
private static final String INSERT_STATEMENT = "INSERT INTO %PREFIX%TASK_BATCH VALUES(?, ?)";
private String tablePrefix = TaskProperties.DEFAULT_TABLE_PREFIX;
private JdbcOperations jdbcTemplate;
/**
@@ -68,10 +69,12 @@ public class JdbcTaskBatchDao implements TaskBatchDao {
public void saveRelationship(TaskExecution taskExecution, JobExecution jobExecution) {
Assert.notNull(taskExecution, "A taskExecution is required");
Assert.notNull(jobExecution, "A jobExecution is required");
jdbcTemplate.update(getQuery(INSERT_STATEMENT), taskExecution.getExecutionId(), jobExecution.getId());
this.jdbcTemplate.update(getQuery(INSERT_STATEMENT),
taskExecution.getExecutionId(), jobExecution.getId());
}
private String getQuery(String base) {
return StringUtils.replace(base, "%PREFIX%", tablePrefix);
return StringUtils.replace(base, "%PREFIX%", this.tablePrefix);
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2016 the original author or authors.
* Copyright 2015-2019 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.
@@ -13,6 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.cloud.task.batch.listener.support;
import java.util.Map;
@@ -25,8 +26,10 @@ import org.springframework.cloud.task.repository.TaskExecution;
import org.springframework.util.Assert;
/**
* Map implementation of the {@link TaskBatchDao}. <p> This is intended for
* testing purposes only!</p>
* Map implementation of the {@link TaskBatchDao}.
* <p>
* This is intended for testing purposes only!
* </p>
*
* @author Michael Minella
*/
@@ -44,8 +47,9 @@ public class MapTaskBatchDao implements TaskBatchDao {
Assert.notNull(taskExecution, "A taskExecution is required");
Assert.notNull(jobExecution, "A jobExecution is required");
if(this.relationships.containsKey(taskExecution.getExecutionId())) {
this.relationships.get(taskExecution.getExecutionId()).add(jobExecution.getId());
if (this.relationships.containsKey(taskExecution.getExecutionId())) {
this.relationships.get(taskExecution.getExecutionId())
.add(jobExecution.getId());
}
else {
TreeSet<Long> jobExecutionIds = new TreeSet<>();
@@ -54,4 +58,5 @@ public class MapTaskBatchDao implements TaskBatchDao {
this.relationships.put(taskExecution.getExecutionId(), jobExecutionIds);
}
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2016 the original author or authors.
* Copyright 2015-2019 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.
@@ -13,6 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.cloud.task.batch.partition;
import java.util.List;
@@ -33,10 +34,10 @@ public interface CommandLineArgsProvider {
* worker for the specified {@link ExecutionContext}.
*
* Note: This method is called once per partition.
*
* @param executionContext the unique state for the step to be executed.
* @return a list of formatted command line arguments to be passed to the worker (the
* list will be joined via spaces).
* list will be joined via spaces).
*/
List<String> getCommandLineArgs(ExecutionContext executionContext);
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2016-2018 the original author or authors.
* Copyright 2015-2019 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.
@@ -13,6 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.cloud.task.batch.partition;
import java.util.ArrayList;
@@ -51,39 +52,57 @@ import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
/**
* <p>A {@link PartitionHandler} implementation that delegates to a {@link TaskLauncher} for
* each of the workers. The id of the worker's StepExecution is passed as an environment
* variable to the worker. The worker, bootstrapped by the
* <p>
* A {@link PartitionHandler} implementation that delegates to a {@link TaskLauncher} for
* each of the workers. The id of the worker's StepExecution is passed as an environment
* variable to the worker. The worker, bootstrapped by the
* {@link DeployerStepExecutionHandler}, looks up the StepExecution in the JobRepository
* and executes it. This PartitionHandler polls the JobRepository for the results.</p>
* and executes it. This PartitionHandler polls the JobRepository for the results.
* </p>
*
* <p>If the job fails, the partitions will be re-executed per normal batch rules (steps that
* <p>
* If the job fails, the partitions will be re-executed per normal batch rules (steps that
* are complete should do nothing, failed steps should restart based on their
* configurations).</p>
* configurations).
* </p>
*
* <p>This PartitionHandler and all of the worker processes must share the same JobRepository
* data store (aka point the same database).</p>
* <p>
* This PartitionHandler and all of the worker processes must share the same JobRepository
* data store (aka point the same database).
* </p>
*
* @author Michael Minella
*/
public class DeployerPartitionHandler implements PartitionHandler, EnvironmentAware, InitializingBean {
public class DeployerPartitionHandler
implements PartitionHandler, EnvironmentAware, InitializingBean {
/**
* ID of Spring Cloud Task job execution.
*/
public static final String SPRING_CLOUD_TASK_JOB_EXECUTION_ID = "spring.cloud.task.job-execution-id";
/**
* ID of Spring Cloud Task step execution.
*/
public static final String SPRING_CLOUD_TASK_STEP_EXECUTION_ID = "spring.cloud.task.step-execution-id";
/**
* Name of Spring Cloud Task step.
*/
public static final String SPRING_CLOUD_TASK_STEP_NAME = "spring.cloud.task.step-name";
/**
* ID of Spring Cloud Task parent execution.
*/
public static final String SPRING_CLOUD_TASK_PARENT_EXECUTION_ID = "spring.cloud.task.parentExecutionId";
/**
* Spring Cloud Task property name.
*/
public static final String SPRING_CLOUD_TASK_NAME = "spring.cloud.task.name";
private static final long DEFAULT_POLL_INTERVAL = 10000;
public static final String SPRING_CLOUD_TASK_JOB_EXECUTION_ID =
"spring.cloud.task.job-execution-id";
public static final String SPRING_CLOUD_TASK_STEP_EXECUTION_ID =
"spring.cloud.task.step-execution-id";
public static final String SPRING_CLOUD_TASK_STEP_NAME =
"spring.cloud.task.step-name";
public static final String SPRING_CLOUD_TASK_PARENT_EXECUTION_ID =
"spring.cloud.task.parentExecutionId";
public static final String SPRING_CLOUD_TASK_NAME = "spring.cloud.task.name";
private int maxWorkers = -1;
private int gridSize = 1;
@@ -118,10 +137,8 @@ public class DeployerPartitionHandler implements PartitionHandler, EnvironmentAw
private boolean defaultArgsAsEnvironmentVars = false;
public DeployerPartitionHandler(TaskLauncher taskLauncher,
JobExplorer jobExplorer,
Resource resource,
String stepName) {
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");
@@ -135,17 +152,16 @@ public class DeployerPartitionHandler implements PartitionHandler, EnvironmentAw
/**
* Used to provide any environment variables to be set on each worker launched.
*
* @param environmentVariablesProvider an {@link EnvironmentVariablesProvider}
*/
public void setEnvironmentVariablesProvider(EnvironmentVariablesProvider environmentVariablesProvider) {
public void setEnvironmentVariablesProvider(
EnvironmentVariablesProvider environmentVariablesProvider) {
this.environmentVariablesProvider = environmentVariablesProvider;
}
/**
* If set to true, the default args that are used internally by Spring Cloud Task and
* Spring Batch are passed as environment variables instead of command line arguments.
*
* @param defaultArgsAsEnvironmentVars defaults to false
*/
public void setDefaultArgsAsEnvironmentVars(boolean defaultArgsAsEnvironmentVars) {
@@ -154,17 +170,16 @@ public class DeployerPartitionHandler implements PartitionHandler, EnvironmentAw
/**
* Used to provide any command line arguements to be passed to each worker launched.
*
* @param commandLineArgsProvider {@link CommandLineArgsProvider}
*/
public void setCommandLineArgsProvider(CommandLineArgsProvider commandLineArgsProvider) {
public void setCommandLineArgsProvider(
CommandLineArgsProvider commandLineArgsProvider) {
this.commandLineArgsProvider = commandLineArgsProvider;
}
/**
* The maximum number of workers to be executing at once.
*
* @param maxWorkers number of workers. Defaults to -1 (unlimited)
* @param maxWorkers number of workers. Defaults to -1 (unlimited)
*/
public void setMaxWorkers(int maxWorkers) {
Assert.isTrue(maxWorkers != 0, "maxWorkers cannot be 0");
@@ -172,11 +187,11 @@ public class DeployerPartitionHandler implements PartitionHandler, EnvironmentAw
}
/**
* Approximate size of the pool of worker JVMs available. May be used by the
* Approximate size of the pool of worker JVMs available. May be used by the
* {@link StepExecutionSplitter} to determine how many partitions to create (at the
* discretion of the {@link org.springframework.batch.core.partition.support.Partitioner}).
*
* @param gridSize size of grid. Defaults to 1
* discretion of the
* {@link org.springframework.batch.core.partition.support.Partitioner}).
* @param gridSize size of grid. Defaults to 1
*/
public void setGridSize(int gridSize) {
this.gridSize = gridSize;
@@ -184,25 +199,22 @@ public class DeployerPartitionHandler implements PartitionHandler, EnvironmentAw
/**
* The interval to check the job repository for completed steps.
*
* @param pollInterval interval. Defaults to 10 seconds
* @param pollInterval interval. Defaults to 10 seconds
*/
public void setPollInterval(long pollInterval) {
this.pollInterval = pollInterval;
}
/**
* Timeout for the master step. This is a timeout for all workers to complete.
*
* @param timeout timeout. Defaults to none (-1).
* Timeout for the master step. This is a timeout for all workers to complete.
* @param timeout timeout. Defaults to none (-1).
*/
public void setTimeout(long timeout) {
this.timeout = timeout;
}
/**
* Map of deployment properties to be used by the {@link TaskLauncher}
*
* Map of deployment properties to be used by the {@link TaskLauncher}.
* @param deploymentProperties properties to be used by the {@link TaskLauncher}
*/
public void setDeploymentProperties(Map<String, String> deploymentProperties) {
@@ -210,9 +222,8 @@ public class DeployerPartitionHandler implements PartitionHandler, EnvironmentAw
}
/**
* The name of the application to be launched. Useful in environments where
* The name of the application to be launched. Useful in environments where
* application deployments are reused (such as CloudFoundry).
*
* @param applicationName The name of the application to be launched
*/
public void setApplicationName(String applicationName) {
@@ -223,9 +234,9 @@ public class DeployerPartitionHandler implements PartitionHandler, EnvironmentAw
public void beforeTask(TaskExecution taskExecution) {
this.taskExecution = taskExecution;
if(this.commandLineArgsProvider == null) {
SimpleCommandLineArgsProvider provider = new
SimpleCommandLineArgsProvider(taskExecution);
if (this.commandLineArgsProvider == null) {
SimpleCommandLineArgsProvider provider = new SimpleCommandLineArgsProvider(
taskExecution);
this.commandLineArgsProvider = provider;
}
@@ -235,8 +246,8 @@ public class DeployerPartitionHandler implements PartitionHandler, EnvironmentAw
public Collection<StepExecution> handle(StepExecutionSplitter stepSplitter,
StepExecution stepExecution) throws Exception {
final Set<StepExecution> tempCandidates =
stepSplitter.split(stepExecution, this.gridSize);
final Set<StepExecution> tempCandidates = stepSplitter.split(stepExecution,
this.gridSize);
// Following two lines due to https://jira.spring.io/browse/BATCH-2490
final Set<StepExecution> candidates = new HashSet<>(tempCandidates.size());
@@ -244,7 +255,7 @@ public class DeployerPartitionHandler implements PartitionHandler, EnvironmentAw
int partitions = candidates.size();
logger.debug(String.format("%s partitions were returned", partitions));
this.logger.debug(String.format("%s partitions were returned", partitions));
final Set<StepExecution> executed = new HashSet<>(candidates.size());
@@ -259,7 +270,8 @@ public class DeployerPartitionHandler implements PartitionHandler, EnvironmentAw
return pollReplies(stepExecution, executed, candidates, partitions);
}
private void launchWorkers(Set<StepExecution> candidates, Set<StepExecution> executed) {
private void launchWorkers(Set<StepExecution> candidates,
Set<StepExecution> executed) {
for (StepExecution execution : candidates) {
if (this.currentWorkers < this.maxWorkers || this.maxWorkers < 0) {
launchWorker(execution);
@@ -273,59 +285,59 @@ public class DeployerPartitionHandler implements PartitionHandler, EnvironmentAw
private void launchWorker(StepExecution workerStepExecution) {
List<String> arguments = new ArrayList<>();
ExecutionContext copyContext = new ExecutionContext(workerStepExecution.getExecutionContext());
ExecutionContext copyContext = new ExecutionContext(
workerStepExecution.getExecutionContext());
arguments.addAll(
this.commandLineArgsProvider
.getCommandLineArgs(copyContext));
arguments.addAll(this.commandLineArgsProvider.getCommandLineArgs(copyContext));
if(!this.defaultArgsAsEnvironmentVars) {
if (!this.defaultArgsAsEnvironmentVars) {
arguments.add(formatArgument(SPRING_CLOUD_TASK_JOB_EXECUTION_ID,
String.valueOf(workerStepExecution.getJobExecution().getId())));
arguments.add(formatArgument(SPRING_CLOUD_TASK_STEP_EXECUTION_ID,
String.valueOf(workerStepExecution.getId())));
arguments.add(formatArgument(SPRING_CLOUD_TASK_STEP_NAME, this.stepName));
arguments.add(formatArgument(SPRING_CLOUD_TASK_NAME, String.format("%s_%s_%s",
taskExecution.getTaskName(),
workerStepExecution.getJobExecution().getJobInstance().getJobName(),
workerStepExecution.getStepName())));
arguments
.add(formatArgument(SPRING_CLOUD_TASK_NAME,
String.format("%s_%s_%s", this.taskExecution.getTaskName(),
workerStepExecution.getJobExecution().getJobInstance()
.getJobName(),
workerStepExecution.getStepName())));
arguments.add(formatArgument(SPRING_CLOUD_TASK_PARENT_EXECUTION_ID,
String.valueOf(taskExecution.getExecutionId())));
String.valueOf(this.taskExecution.getExecutionId())));
}
copyContext = new ExecutionContext(workerStepExecution.getExecutionContext());
Map<String, String> environmentVariables = this.environmentVariablesProvider.getEnvironmentVariables(copyContext);
Map<String, String> environmentVariables = this.environmentVariablesProvider
.getEnvironmentVariables(copyContext);
if(this.defaultArgsAsEnvironmentVars) {
if (this.defaultArgsAsEnvironmentVars) {
environmentVariables.put(SPRING_CLOUD_TASK_JOB_EXECUTION_ID,
String.valueOf(workerStepExecution.getJobExecution().getId()));
environmentVariables.put(SPRING_CLOUD_TASK_STEP_EXECUTION_ID,
String.valueOf(workerStepExecution.getId()));
environmentVariables.put(SPRING_CLOUD_TASK_STEP_NAME, this.stepName);
environmentVariables.put(SPRING_CLOUD_TASK_NAME, String.format("%s_%s_%s",
taskExecution.getTaskName(),
workerStepExecution.getJobExecution().getJobInstance().getJobName(),
workerStepExecution.getStepName()));
environmentVariables
.put(SPRING_CLOUD_TASK_NAME,
String.format("%s_%s_%s", this.taskExecution.getTaskName(),
workerStepExecution.getJobExecution().getJobInstance()
.getJobName(),
workerStepExecution.getStepName()));
environmentVariables.put(SPRING_CLOUD_TASK_PARENT_EXECUTION_ID,
String.valueOf(taskExecution.getExecutionId()));
String.valueOf(this.taskExecution.getExecutionId()));
}
AppDefinition definition =
new AppDefinition(resolveApplicationName(),
environmentVariables);
AppDefinition definition = new AppDefinition(resolveApplicationName(),
environmentVariables);
AppDeploymentRequest request =
new AppDeploymentRequest(definition,
this.resource,
this.deploymentProperties,
arguments);
AppDeploymentRequest request = new AppDeploymentRequest(definition, this.resource,
this.deploymentProperties, arguments);
taskLauncher.launch(request);
this.taskLauncher.launch(request);
}
private String resolveApplicationName() {
if(StringUtils.hasText(this.applicationName)) {
if (StringUtils.hasText(this.applicationName)) {
return this.applicationName;
}
else {
@@ -338,8 +350,7 @@ public class DeployerPartitionHandler implements PartitionHandler, EnvironmentAw
}
private Collection<StepExecution> pollReplies(final StepExecution masterStepExecution,
final Set<StepExecution> executed,
final Set<StepExecution> candidates,
final Set<StepExecution> executed, final Set<StepExecution> candidates,
final int size) throws Exception {
final Collection<StepExecution> result = new ArrayList<>(executed.size());
@@ -351,13 +362,14 @@ public class DeployerPartitionHandler implements PartitionHandler, EnvironmentAw
for (StepExecution curStepExecution : executed) {
if (!result.contains(curStepExecution)) {
StepExecution partitionStepExecution =
jobExplorer.getStepExecution(masterStepExecution.getJobExecutionId(), curStepExecution.getId());
StepExecution partitionStepExecution = DeployerPartitionHandler.this.jobExplorer
.getStepExecution(masterStepExecution.getJobExecutionId(),
curStepExecution.getId());
BatchStatus batchStatus = partitionStepExecution.getStatus();
if (batchStatus != null && isComplete(batchStatus)) {
result.add(partitionStepExecution);
currentWorkers--;
DeployerPartitionHandler.this.currentWorkers--;
if (!candidates.isEmpty()) {
@@ -382,8 +394,8 @@ public class DeployerPartitionHandler implements PartitionHandler, EnvironmentAw
Poller<Collection<StepExecution>> poller = new DirectPoller<>(this.pollInterval);
Future<Collection<StepExecution>> resultsFuture = poller.poll(callback);
if (timeout >= 0) {
return resultsFuture.get(timeout, TimeUnit.MILLISECONDS);
if (this.timeout >= 0) {
return resultsFuture.get(this.timeout, TimeUnit.MILLISECONDS);
}
else {
return resultsFuture.get();
@@ -391,7 +403,8 @@ public class DeployerPartitionHandler implements PartitionHandler, EnvironmentAw
}
private boolean isComplete(BatchStatus status) {
return status.equals(BatchStatus.COMPLETED) || status.isGreaterThan(BatchStatus.STARTED);
return status.equals(BatchStatus.COMPLETED)
|| status.isGreaterThan(BatchStatus.STARTED);
}
@Override
@@ -401,10 +414,11 @@ public class DeployerPartitionHandler implements PartitionHandler, EnvironmentAw
@Override
public void afterPropertiesSet() throws Exception {
if(this.environmentVariablesProvider == null) {
this.environmentVariablesProvider =
new SimpleEnvironmentVariablesProvider(this.environment);
if (this.environmentVariablesProvider == null) {
this.environmentVariablesProvider = new SimpleEnvironmentVariablesProvider(
this.environment);
}
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2016 the original author or authors.
* Copyright 2015-2019 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.
@@ -13,6 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.cloud.task.batch.partition;
import org.apache.commons.logging.Log;
@@ -34,20 +35,24 @@ import org.springframework.core.env.Environment;
import org.springframework.util.Assert;
/**
* <p>A {@link CommandLineRunner} used to execute a {@link Step}. No result is provided
* <p>
* A {@link CommandLineRunner} used to execute a {@link Step}. No result is provided
* directly to the associated {@link DeployerPartitionHandler} as it will obtain the step
* results directly from the shared job repository.</p>
* results directly from the shared job repository.
* </p>
*
* <p>The {@link StepExecution} is rehydrated based on the environment variables provided.
* Specifically, the following variables are required:</p>
* <p>
* The {@link StepExecution} is rehydrated based on the environment variables provided.
* Specifically, the following variables are required:
* </p>
* <ul>
* <li>{@link DeployerPartitionHandler#SPRING_CLOUD_TASK_JOB_EXECUTION_ID}: The id of
* the JobExecution.</li>
* <li>{@link DeployerPartitionHandler#SPRING_CLOUD_TASK_STEP_EXECUTION_ID}: The id of
* the StepExecution.</li>
* <li>{@link DeployerPartitionHandler#SPRING_CLOUD_TASK_STEP_NAME}: The id of the
* bean definition for the Step to execute. The id must be found within the provided
* {@link BeanFactory}</li>
* <li>{@link DeployerPartitionHandler#SPRING_CLOUD_TASK_JOB_EXECUTION_ID}: The id of the
* JobExecution.</li>
* <li>{@link DeployerPartitionHandler#SPRING_CLOUD_TASK_STEP_EXECUTION_ID}: The id of the
* StepExecution.</li>
* <li>{@link DeployerPartitionHandler#SPRING_CLOUD_TASK_STEP_NAME}: The id of the bean
* definition for the Step to execute. The id must be found within the provided
* {@link BeanFactory}</li>
* </ul>
*
* @author Michael Minella
@@ -65,7 +70,8 @@ public class DeployerStepExecutionHandler implements CommandLineRunner {
private StepLocator stepLocator;
public DeployerStepExecutionHandler(BeanFactory beanFactory, JobExplorer jobExplorer, JobRepository jobRepository) {
public DeployerStepExecutionHandler(BeanFactory beanFactory, JobExplorer jobExplorer,
JobRepository jobRepository) {
Assert.notNull(beanFactory, "A beanFactory is required");
Assert.notNull(jobExplorer, "A jobExplorer is required");
Assert.notNull(jobRepository, "A jobRepository is required");
@@ -82,38 +88,60 @@ public class DeployerStepExecutionHandler implements CommandLineRunner {
validateRequest();
Long jobExecutionId = Long.parseLong(environment.getProperty(DeployerPartitionHandler.SPRING_CLOUD_TASK_JOB_EXECUTION_ID));
Long stepExecutionId = Long.parseLong(environment.getProperty(DeployerPartitionHandler.SPRING_CLOUD_TASK_STEP_EXECUTION_ID));
StepExecution stepExecution = jobExplorer.getStepExecution(jobExecutionId, stepExecutionId);
Long jobExecutionId = Long.parseLong(this.environment.getProperty(
DeployerPartitionHandler.SPRING_CLOUD_TASK_JOB_EXECUTION_ID));
Long stepExecutionId = Long.parseLong(this.environment.getProperty(
DeployerPartitionHandler.SPRING_CLOUD_TASK_STEP_EXECUTION_ID));
StepExecution stepExecution = this.jobExplorer.getStepExecution(jobExecutionId,
stepExecutionId);
if (stepExecution == null) {
throw new NoSuchStepException(String.format("No StepExecution could be located for step execution id %s within job execution %s", stepExecutionId, jobExecutionId));
throw new NoSuchStepException(String.format(
"No StepExecution could be located for step execution id %s within job execution %s",
stepExecutionId, jobExecutionId));
}
String stepName = environment.getProperty(DeployerPartitionHandler.SPRING_CLOUD_TASK_STEP_NAME);
Step step = stepLocator.getStep(stepName);
String stepName = this.environment
.getProperty(DeployerPartitionHandler.SPRING_CLOUD_TASK_STEP_NAME);
Step step = this.stepLocator.getStep(stepName);
try {
logger.debug(String.format("Executing step %s with step execution id %s and job execution id %s", stepExecution.getStepName(), stepExecutionId, jobExecutionId));
this.logger.debug(String.format(
"Executing step %s with step execution id %s and job execution id %s",
stepExecution.getStepName(), stepExecutionId, jobExecutionId));
step.execute(stepExecution);
}
catch (JobInterruptedException e) {
stepExecution.setStatus(BatchStatus.STOPPED);
jobRepository.update(stepExecution);
this.jobRepository.update(stepExecution);
}
catch (Throwable e) {
stepExecution.addFailureException(e);
stepExecution.setStatus(BatchStatus.FAILED);
jobRepository.update(stepExecution);
this.jobRepository.update(stepExecution);
}
}
private void validateRequest() {
Assert.isTrue(environment.containsProperty(DeployerPartitionHandler.SPRING_CLOUD_TASK_JOB_EXECUTION_ID), "A job execution id is required");
Assert.isTrue(environment.containsProperty(DeployerPartitionHandler.SPRING_CLOUD_TASK_STEP_EXECUTION_ID), "A step execution id is required");
Assert.isTrue(environment.containsProperty(DeployerPartitionHandler.SPRING_CLOUD_TASK_STEP_NAME), "A step name is required");
Assert.isTrue(
this.environment.containsProperty(
DeployerPartitionHandler.SPRING_CLOUD_TASK_JOB_EXECUTION_ID),
"A job execution id is required");
Assert.isTrue(
this.environment.containsProperty(
DeployerPartitionHandler.SPRING_CLOUD_TASK_STEP_EXECUTION_ID),
"A step execution id is required");
Assert.isTrue(
this.environment.containsProperty(
DeployerPartitionHandler.SPRING_CLOUD_TASK_STEP_NAME),
"A step name is required");
Assert.isTrue(this.stepLocator.getStepNames().contains(environment.getProperty(DeployerPartitionHandler.SPRING_CLOUD_TASK_STEP_NAME)), "The step requested cannot be found in the provided BeanFactory");
Assert.isTrue(
this.stepLocator.getStepNames()
.contains(this.environment.getProperty(
DeployerPartitionHandler.SPRING_CLOUD_TASK_STEP_NAME)),
"The step requested cannot be found in the provided BeanFactory");
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2016 the original author or authors.
* Copyright 2015-2019 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.
@@ -13,6 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.cloud.task.batch.partition;
import java.util.Map;
@@ -24,19 +25,18 @@ import org.springframework.batch.item.ExecutionContext;
* each worker in a partitioned job.
*
* @author Michael Minella
*
* @since 1.0.2
*/
public interface EnvironmentVariablesProvider {
/**
* Provides a {@link Map} of Strings to be used as environment variables. This method
* will be called for each worker step. For example, if there are 5 partitions, this
* Provides a {@link Map} of Strings to be used as environment variables. This method
* will be called for each worker step. For example, if there are 5 partitions, this
* method will be called 5 times.
*
* @param executionContext the {@link ExecutionContext} associated with the worker's
* step
* step
* @return A {@link Map} of values to be used as environment variables
*/
Map<String, String> getEnvironmentVariables(ExecutionContext executionContext);
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2016 the original author or authors.
* Copyright 2015-2019 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.
@@ -13,6 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.cloud.task.batch.partition;
import java.util.Collections;
@@ -21,23 +22,23 @@ import java.util.Map;
import org.springframework.batch.item.ExecutionContext;
/**
* A simple no-op implementation of the {@link EnvironmentVariablesProvider}. It returns
* A simple no-op implementation of the {@link EnvironmentVariablesProvider}. It returns
* an empty {@link Map}.
*
* @author Michael Minella
*
* @since 1.0.2
*/
public class NoOpEnvironmentVariablesProvider implements EnvironmentVariablesProvider {
/**
*
* @param executionContext the {@link ExecutionContext} associated with the worker's
* step
* step
* @return an empty {@link Map}
*/
@Override
public Map<String, String> getEnvironmentVariables(ExecutionContext executionContext) {
public Map<String, String> getEnvironmentVariables(
ExecutionContext executionContext) {
return Collections.emptyMap();
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2016 the original author or authors.
* Copyright 2015-2019 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.
@@ -13,6 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.cloud.task.batch.partition;
import java.util.List;
@@ -38,6 +39,7 @@ public class PassThroughCommandLineArgsProvider implements CommandLineArgsProvid
@Override
public List<String> getCommandLineArgs(ExecutionContext executionContext) {
return commandLineArgs;
return this.commandLineArgs;
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2016-2018 the original author or authors.
* Copyright 2015-2019 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.
@@ -13,6 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.cloud.task.batch.partition;
import java.util.ArrayList;
@@ -31,7 +32,8 @@ import org.springframework.util.Assert;
* @author Glenn Renfro
* @since 1.1.0
*/
public class SimpleCommandLineArgsProvider extends TaskExecutionListenerSupport implements CommandLineArgsProvider {
public class SimpleCommandLineArgsProvider extends TaskExecutionListenerSupport
implements CommandLineArgsProvider {
private TaskExecution taskExecution;
@@ -56,7 +58,6 @@ public class SimpleCommandLineArgsProvider extends TaskExecutionListenerSupport
/**
* Additional command line args to be appended.
*
* @param appendedArgs list of arguments
* @since 1.2
*/
@@ -67,17 +68,18 @@ public class SimpleCommandLineArgsProvider extends TaskExecutionListenerSupport
@Override
public List<String> getCommandLineArgs(ExecutionContext executionContext) {
int listSize = this.taskExecution.getArguments().size() +
(this.appendedArgs != null ? this.appendedArgs.size() : 0);
int listSize = this.taskExecution.getArguments().size()
+ (this.appendedArgs != null ? this.appendedArgs.size() : 0);
List<String> args = new ArrayList<>(listSize);
args.addAll(this.taskExecution.getArguments());
if(this.appendedArgs != null) {
if (this.appendedArgs != null) {
args.addAll(this.appendedArgs);
}
return args;
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2016 the original author or authors.
* Copyright 2015-2019 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.
@@ -13,6 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.cloud.task.batch.partition;
import java.util.Arrays;
@@ -29,12 +30,11 @@ import org.springframework.core.env.PropertySource;
/**
* Copies all existing environment variables as made available in the {@link Environment}
* only if includeCurrentEnvironment is set to true (default).
* The <code>environmentProperties</code> option provides the ability to override any
* specific values on an as needed basis.
* only if includeCurrentEnvironment is set to true (default). The
* <code>environmentProperties</code> option provides the ability to override any specific
* values on an as needed basis.
*
* @author Michael Minella
*
* @since 1.0.2
*/
public class SimpleEnvironmentVariablesProvider implements EnvironmentVariablesProvider {
@@ -53,28 +53,31 @@ public class SimpleEnvironmentVariablesProvider implements EnvironmentVariablesP
}
/**
* @param environmentProperties a {@link Map} of properties used to override any values
* configured in the current {@link Environment}
* @param environmentProperties a {@link Map} of properties used to override any
* values configured in the current {@link Environment}
*/
public void setEnvironmentProperties(Map<String, String> environmentProperties) {
this.environmentProperties = environmentProperties;
}
/**
* Establishes if current environment variables will be included as a part of the provider.
* @param includeCurrentEnvironment true(default) include local environment properties. False do not include
* current environment properties.
* Establishes if current environment variables will be included as a part of the
* provider.
* @param includeCurrentEnvironment true(default) include local environment
* properties. False do not include current environment properties.
*/
public void setIncludeCurrentEnvironment(boolean includeCurrentEnvironment) {
this.includeCurrentEnvironment = includeCurrentEnvironment;
}
@Override
public Map<String, String> getEnvironmentVariables(ExecutionContext executionContext) {
public Map<String, String> getEnvironmentVariables(
ExecutionContext executionContext) {
Map<String, String> environmentProperties = new HashMap<>(this.environmentProperties.size());
Map<String, String> environmentProperties = new HashMap<>(
this.environmentProperties.size());
if(includeCurrentEnvironment) {
if (this.includeCurrentEnvironment) {
environmentProperties.putAll(getCurrentEnvironmentProperties());
}
@@ -88,9 +91,11 @@ public class SimpleEnvironmentVariablesProvider implements EnvironmentVariablesP
Set<String> keys = new HashSet<>();
for (PropertySource<?> propertySource : ((AbstractEnvironment) this.environment).getPropertySources()) {
for (PropertySource<?> propertySource : ((AbstractEnvironment) this.environment)
.getPropertySources()) {
if (propertySource instanceof MapPropertySource) {
keys.addAll(Arrays.asList(((MapPropertySource) propertySource).getPropertyNames()));
keys.addAll(Arrays
.asList(((MapPropertySource) propertySource).getPropertyNames()));
}
}
@@ -100,4 +105,5 @@ public class SimpleEnvironmentVariablesProvider implements EnvironmentVariablesP
return currentEnvironment;
}
}

View File

@@ -1,64 +1,64 @@
{
"properties": [
{
"defaultValue": true,
"name": "spring.cloud.task.batch.listener.enabled",
"description": "This property is used to determine if a task will be linked to the batch jobs that are run.",
"type": "java.lang.Boolean"
},
{
"defaultValue": false,
"name": "spring.cloud.task.batch.fail-on-job-failure",
"description": "This property is used to determine if a task app should return with a non zero exit code if a batch job fails.",
"type": "java.lang.Boolean"
},
{
"defaultValue": true,
"name": "spring.cloud.task.batch.events.enabled",
"description": "This property is used to determine if a task should listen for batch events.",
"type": "java.lang.Boolean"
},
{
"defaultValue": true,
"name": "spring.cloud.task.batch.events.chunk.enabled",
"description": "This property is used to determine if a task should listen for batch chunk events.",
"type": "java.lang.Boolean"
},
{
"defaultValue": true,
"name": "spring.cloud.task.batch.events.item-process.enabled",
"description": "This property is used to determine if a task should listen for batch item processed events.",
"type": "java.lang.Boolean"
},
{
"defaultValue": true,
"name": "spring.cloud.task.batch.events.item-read.enabled",
"description": "This property is used to determine if a task should listen for batch item read events.",
"type": "java.lang.Boolean"
},
{
"defaultValue": true,
"name": "spring.cloud.task.batch.events.item-write.enabled",
"description": "This property is used to determine if a task should listen for batch item write events.",
"type": "java.lang.Boolean"
},
{
"defaultValue": true,
"name": "spring.cloud.task.batch.events.job-execution.enabled",
"description": "This property is used to determine if a task should listen for batch job execution events.",
"type": "java.lang.Boolean"
},
{
"defaultValue": true,
"name": "spring.cloud.task.batch.events.skip.enabled",
"description": "This property is used to determine if a task should listen for batch skip events.",
"type": "java.lang.Boolean"
},
{
"defaultValue": true,
"name": "spring.cloud.task.batch.events.step-execution.enabled",
"description": "This property is used to determine if a task should listen for batch step execution events.",
"type": "java.lang.Boolean"
}
]
"properties": [
{
"defaultValue": true,
"name": "spring.cloud.task.batch.listener.enabled",
"description": "This property is used to determine if a task will be linked to the batch jobs that are run.",
"type": "java.lang.Boolean"
},
{
"defaultValue": false,
"name": "spring.cloud.task.batch.fail-on-job-failure",
"description": "This property is used to determine if a task app should return with a non zero exit code if a batch job fails.",
"type": "java.lang.Boolean"
},
{
"defaultValue": true,
"name": "spring.cloud.task.batch.events.enabled",
"description": "This property is used to determine if a task should listen for batch events.",
"type": "java.lang.Boolean"
},
{
"defaultValue": true,
"name": "spring.cloud.task.batch.events.chunk.enabled",
"description": "This property is used to determine if a task should listen for batch chunk events.",
"type": "java.lang.Boolean"
},
{
"defaultValue": true,
"name": "spring.cloud.task.batch.events.item-process.enabled",
"description": "This property is used to determine if a task should listen for batch item processed events.",
"type": "java.lang.Boolean"
},
{
"defaultValue": true,
"name": "spring.cloud.task.batch.events.item-read.enabled",
"description": "This property is used to determine if a task should listen for batch item read events.",
"type": "java.lang.Boolean"
},
{
"defaultValue": true,
"name": "spring.cloud.task.batch.events.item-write.enabled",
"description": "This property is used to determine if a task should listen for batch item write events.",
"type": "java.lang.Boolean"
},
{
"defaultValue": true,
"name": "spring.cloud.task.batch.events.job-execution.enabled",
"description": "This property is used to determine if a task should listen for batch job execution events.",
"type": "java.lang.Boolean"
},
{
"defaultValue": true,
"name": "spring.cloud.task.batch.events.skip.enabled",
"description": "This property is used to determine if a task should listen for batch skip events.",
"type": "java.lang.Boolean"
},
{
"defaultValue": true,
"name": "spring.cloud.task.batch.events.step-execution.enabled",
"description": "This property is used to determine if a task should listen for batch step execution events.",
"type": "java.lang.Boolean"
}
]
}

View File

@@ -1,17 +1,17 @@
/*
* Copyright 2018 the original author or authors.
* Copyright 2015-2019 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.
* You may obtain a copy of the License at
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.cloud.task.batch.configuration;
@@ -35,4 +35,5 @@ import org.springframework.boot.autoconfigure.ImportAutoConfiguration;
@Documented
@ImportAutoConfiguration
public @interface TaskBatchTest {
}

View File

@@ -1,17 +1,17 @@
/*
* Copyright 2018 the original author or authors.
* Copyright 2015-2019 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.
* You may obtain a copy of the License at
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.cloud.task.batch.configuration;
@@ -33,33 +33,33 @@ import static org.assertj.core.api.Assertions.assertThat;
*/
public class TaskJobLauncherAutoConfigurationTests {
private final ApplicationContextRunner contextRunner = new ApplicationContextRunner().
withUserConfiguration(TaskBatchExecutionListenerTests.JobConfiguration.class,
private final ApplicationContextRunner contextRunner = new ApplicationContextRunner()
.withUserConfiguration(TaskBatchExecutionListenerTests.JobConfiguration.class,
PropertyPlaceholderAutoConfiguration.class,
EmbeddedDataSourceConfiguration.class,
BatchAutoConfiguration.class,
EmbeddedDataSourceConfiguration.class, BatchAutoConfiguration.class,
TaskJobLauncherAutoConfiguration.class);
@Test
public void testAutoBuiltDataSourceWithTaskJobLauncherCLR() {
this.contextRunner.withPropertyValues("spring.cloud.task.batch.fail-on-job-failure=true").run(context -> {
assertThat(context).hasSingleBean(TaskJobLauncherCommandLineRunner.class);
assertThat(context.getBean(TaskJobLauncherCommandLineRunner.class)
.getOrder())
.isEqualTo(0);
});
this.contextRunner
.withPropertyValues("spring.cloud.task.batch.fail-on-job-failure=true")
.run(context -> {
assertThat(context)
.hasSingleBean(TaskJobLauncherCommandLineRunner.class);
assertThat(context.getBean(TaskJobLauncherCommandLineRunner.class)
.getOrder()).isEqualTo(0);
});
}
@Test
public void testAutoBuiltDataSourceWithTaskJobLauncherCLROrder() {
this.contextRunner.
withPropertyValues("spring.cloud.task.batch.fail-on-job-failure=true",
"spring.cloud.task.batch.commandLineRunnerOrder=100").
run(context -> {
this.contextRunner
.withPropertyValues("spring.cloud.task.batch.fail-on-job-failure=true",
"spring.cloud.task.batch.commandLineRunnerOrder=100")
.run(context -> {
assertThat(context.getBean(TaskJobLauncherCommandLineRunner.class)
.getOrder())
.isEqualTo(100);
});
.getOrder()).isEqualTo(100);
});
}
@Test
@@ -69,5 +69,5 @@ public class TaskJobLauncherAutoConfigurationTests {
assertThat(context).doesNotHaveBean(TaskJobLauncherCommandLineRunner.class);
});
}
}
}

View File

@@ -1,23 +1,21 @@
/*
* Copyright 2018 the original author or authors.
* Copyright 2015-2019 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.
* You may obtain a copy of the License at
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.cloud.task.batch.handler;
import org.assertj.core.api.AssertionsForClassTypes;
import org.junit.Before;
import org.junit.Test;
import org.junit.jupiter.api.function.Executable;
@@ -55,13 +53,14 @@ import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.transaction.PlatformTransactionManager;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
/**
* @author Glenn Renfro
*/
@RunWith(SpringRunner.class)
@ContextConfiguration(classes = {TaskJobLauncherCommandLineRunnerCoreTests.BatchConfiguration.class})
@ContextConfiguration(classes = {
TaskJobLauncherCommandLineRunnerCoreTests.BatchConfiguration.class })
public class TaskJobLauncherCommandLineRunnerCoreTests {
@Autowired
@@ -93,11 +92,11 @@ public class TaskJobLauncherCommandLineRunnerCoreTests {
Tasklet tasklet = (contribution, chunkContext) -> RepeatStatus.FINISHED;
this.step = this.steps.get("step").tasklet(tasklet).build();
this.job = this.jobs.get("job").start(this.step).build();
this.runner = new TaskJobLauncherCommandLineRunner(this.jobLauncher, this.jobExplorer, jobRepository, new TaskBatchProperties());
this.runner = new TaskJobLauncherCommandLineRunner(this.jobLauncher,
this.jobExplorer, this.jobRepository, new TaskBatchProperties());
}
@DirtiesContext
@Test
public void basicExecution() throws Exception {
@@ -146,7 +145,6 @@ public class TaskJobLauncherCommandLineRunnerCoreTests {
assertThat(this.jobExplorer.getJobInstances("job", 0, 100)).hasSize(2);
}
@DirtiesContext
@Test
public void retryFailedExecutionOnNonRestartableJob() throws Exception {
@@ -162,9 +160,9 @@ public class TaskJobLauncherCommandLineRunnerCoreTests {
// try to re-run a failed execution
Executable executable = () -> this.runner.execute(this.job,
new JobParametersBuilder().addLong("run.id", 1L).toJobParameters());
Throwable exception = assertThrows(JobRestartException.class, executable);
AssertionsForClassTypes.assertThat(exception.getMessage())
.isEqualTo("JobInstance already exists and is not restartable");
assertThatExceptionOfType(JobRestartException.class)
.isThrownBy(executable::execute)
.withMessage("JobInstance already exists and is not restartable");
}
@DirtiesContext
@@ -177,12 +175,11 @@ public class TaskJobLauncherCommandLineRunnerCoreTests {
.addLong("foo", 2L, false).toJobParameters();
runFailedJob(jobParameters);
assertThat(this.jobExplorer.getJobInstances("job", 0, 100)).hasSize(1);
runFailedJob(new JobParametersBuilder(jobParameters)
.addLong("run.id", 1L).toJobParameters());
runFailedJob(new JobParametersBuilder(jobParameters).addLong("run.id", 1L)
.toJobParameters());
assertThat(this.jobExplorer.getJobInstances("job", 0, 100)).hasSize(1);
}
@DirtiesContext
@Test
public void retryFailedExecutionWithDifferentNonIdentifyingParametersFromPreviousExecution()
@@ -195,7 +192,7 @@ public class TaskJobLauncherCommandLineRunnerCoreTests {
runFailedJob(jobParameters);
assertThat(this.jobExplorer.getJobInstances("job", 0, 100)).hasSize(1);
// try to re-run a failed execution with non identifying parameters
runFailedJob( new JobParametersBuilder().addLong("run.id", 1L)
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);
@@ -216,7 +213,6 @@ public class TaskJobLauncherCommandLineRunnerCoreTests {
assertThat(parameters.getLong("foo")).isEqualTo(3L);
}
private Tasklet throwingTasklet() {
return (contribution, chunkContext) -> {
throw new RuntimeException("Planned");
@@ -238,13 +234,11 @@ public class TaskJobLauncherCommandLineRunnerCoreTests {
@EnableBatchProcessing
protected static class BatchConfiguration implements BatchConfigurer {
private ResourcelessTransactionManager transactionManager =
new ResourcelessTransactionManager();
private ResourcelessTransactionManager transactionManager = new ResourcelessTransactionManager();
private JobRepository jobRepository;
private MapJobRepositoryFactoryBean jobRepositoryFactory =
new MapJobRepositoryFactoryBean(
private MapJobRepositoryFactoryBean jobRepositoryFactory = new MapJobRepositoryFactoryBean(
this.transactionManager);
public BatchConfiguration() throws Exception {
@@ -275,4 +269,5 @@ public class TaskJobLauncherCommandLineRunnerCoreTests {
}
}
}

View File

@@ -1,17 +1,17 @@
/*
* Copyright 2018 the original author or authors.
* Copyright 2015-2019 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.
* You may obtain a copy of the License at
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.cloud.task.batch.handler;
@@ -20,6 +20,7 @@ import java.util.Set;
import javax.sql.DataSource;
import org.assertj.core.api.Condition;
import org.junit.After;
import org.junit.Test;
import org.junit.jupiter.api.function.Executable;
@@ -59,18 +60,18 @@ import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.scheduling.concurrent.ConcurrentTaskExecutor;
import static org.assertj.core.api.AssertionsForInterfaceTypes.assertThat;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
/**
* @author Glenn Renfro
*/
public class TaskJobLauncherCommandLineRunnerTests {
private ConfigurableApplicationContext applicationContext;
private static final String DEFAULT_ERROR_MESSAGE = "The following Jobs have failed: \n"
+ "Job jobA failed during execution for job instance id 1 with jobExecutionId of 1 \n";
private static final String DEFAULT_ERROR_MESSAGE = "The following Jobs have failed: \n" +
"Job jobA failed during execution for job instance id 1 with jobExecutionId of 1 \n";
private ConfigurableApplicationContext applicationContext;
@After
public void tearDown() {
@@ -82,20 +83,22 @@ public class TaskJobLauncherCommandLineRunnerTests {
@Test
public void testTaskJobLauncherCLRSuccessFail() {
String[] enabledArgs = new String[] {
"--spring.cloud.task.batch.failOnJobFailure=true"};
validateForFail(DEFAULT_ERROR_MESSAGE, TaskJobLauncherCommandLineRunnerTests.JobWithFailureConfiguration.class,
"--spring.cloud.task.batch.failOnJobFailure=true" };
validateForFail(DEFAULT_ERROR_MESSAGE,
TaskJobLauncherCommandLineRunnerTests.JobWithFailureConfiguration.class,
enabledArgs);
}
/**
* Verifies that the task will return an exit code other than zero if the
* job fails with the deprecated EnableTask annotation.
* Verifies that the task will return an exit code other than zero if the job fails
* with the deprecated EnableTask annotation.
*/
@Test
public void testTaskJobLauncherCLRSuccessFailWithAnnotation() {
String[] enabledArgs = new String[] {
"--spring.cloud.task.batch.failOnJobFailure=true"};
validateForFail(DEFAULT_ERROR_MESSAGE, TaskJobLauncherCommandLineRunnerTests.JobWithFailureAnnotatedConfiguration.class,
"--spring.cloud.task.batch.failOnJobFailure=true" };
validateForFail(DEFAULT_ERROR_MESSAGE,
TaskJobLauncherCommandLineRunnerTests.JobWithFailureAnnotatedConfiguration.class,
enabledArgs);
}
@@ -103,8 +106,9 @@ public class TaskJobLauncherCommandLineRunnerTests {
public void testTaskJobLauncherCLRSuccessFailWithTaskExecutor() {
String[] enabledArgs = new String[] {
"--spring.cloud.task.batch.failOnJobFailure=true",
"--spring.cloud.task.batch.failOnJobFailurePollInterval=500"};
validateForFail(DEFAULT_ERROR_MESSAGE, TaskJobLauncherCommandLineRunnerTests.JobWithFailureTaskExecutorConfiguration.class,
"--spring.cloud.task.batch.failOnJobFailurePollInterval=500" };
validateForFail(DEFAULT_ERROR_MESSAGE,
TaskJobLauncherCommandLineRunnerTests.JobWithFailureTaskExecutorConfiguration.class,
enabledArgs);
}
@@ -112,11 +116,12 @@ public class TaskJobLauncherCommandLineRunnerTests {
public void testTaskJobLauncherPickOneJob() {
String[] enabledArgs = new String[] {
"--spring.cloud.task.batch.fail-on-job-failure=true",
"--spring.cloud.task.batch.jobNames=jobSucceed"};
"--spring.cloud.task.batch.jobNames=jobSucceed" };
boolean isExceptionThrown = false;
try {
this.applicationContext = SpringApplication
.run(new Class[] { TaskJobLauncherCommandLineRunnerTests.JobWithFailureConfiguration.class }, enabledArgs);
this.applicationContext = SpringApplication.run(new Class[] {
TaskJobLauncherCommandLineRunnerTests.JobWithFailureConfiguration.class },
enabledArgs);
}
catch (IllegalStateException exception) {
isExceptionThrown = true;
@@ -128,40 +133,51 @@ public class TaskJobLauncherCommandLineRunnerTests {
@Test
public void testCommandLineRunnerSetToFalse() {
String[] enabledArgs = new String[] {};
this.applicationContext = SpringApplication
.run(new Class[] { TaskJobLauncherCommandLineRunnerTests.JobConfiguration.class }, enabledArgs);
this.applicationContext = SpringApplication.run(
new Class[] {
TaskJobLauncherCommandLineRunnerTests.JobConfiguration.class },
enabledArgs);
validateContext();
assertThat(applicationContext.getBean(JobLauncherCommandLineRunner.class)).isNotNull();
assertThat(this.applicationContext.getBean(JobLauncherCommandLineRunner.class))
.isNotNull();
Executable executable = () -> applicationContext.getBean(TaskJobLauncherCommandLineRunner.class);
Executable executable = () -> this.applicationContext
.getBean(TaskJobLauncherCommandLineRunner.class);
Throwable exception = assertThrows(NoSuchBeanDefinitionException.class, executable);
assertThat(exception.getMessage()).isEqualTo("No qualifying bean of type " +
"'org.springframework.cloud.task.batch.handler.TaskJobLauncherCommandLineRunner' available");
assertThatExceptionOfType(NoSuchBeanDefinitionException.class)
.isThrownBy(executable::execute).withMessage("No qualifying bean of type "
+ "'org.springframework.cloud.task.batch.handler.TaskJobLauncherCommandLineRunner' available");
validateContext();
}
private void validateContext() {
TaskExplorer taskExplorer = this.applicationContext.getBean(TaskExplorer.class);
Page<TaskExecution> page = taskExplorer.findTaskExecutionsByName("application", PageRequest.of(0, 1));
Page<TaskExecution> page = taskExplorer.findTaskExecutionsByName("application",
PageRequest.of(0, 1));
Set<Long> jobExecutionIds = taskExplorer
.getJobExecutionIdsByTaskExecutionId(page.iterator().next().getExecutionId());
Set<Long> jobExecutionIds = taskExplorer.getJobExecutionIdsByTaskExecutionId(
page.iterator().next().getExecutionId());
assertThat(jobExecutionIds.size()).isEqualTo(1);
assertThat(taskExplorer.getTaskExecution(jobExecutionIds.iterator().next()).getExecutionId()).isEqualTo(1);
assertThat(taskExplorer.getTaskExecution(jobExecutionIds.iterator().next())
.getExecutionId()).isEqualTo(1);
}
private void validateForFail(String errorMessage, Class clazz, String [] enabledArgs) {
Executable executable = () -> this.applicationContext = SpringApplication
.run(new Class[] { clazz,PropertyPlaceholderAutoConfiguration.class}, enabledArgs);
private void validateForFail(String errorMessage, Class clazz, String[] enabledArgs) {
Executable executable = () -> this.applicationContext = SpringApplication.run(
new Class[] { clazz, PropertyPlaceholderAutoConfiguration.class },
enabledArgs);
Throwable exception = assertThrows(IllegalStateException.class, executable);
assertThat(exception.getCause().getMessage()).isEqualTo(errorMessage);
assertThatExceptionOfType(IllegalStateException.class)
.isThrownBy(executable::execute).has(new Condition<Throwable>() {
@Override
public boolean matches(Throwable value) {
return errorMessage.equals(value.getCause().getMessage());
}
});
}
@EnableBatchProcessing
@TaskBatchTest
@Import(EmbeddedDataSourceConfiguration.class)
@@ -176,25 +192,23 @@ public class TaskJobLauncherCommandLineRunnerTests {
@Bean
public Job job() {
return jobBuilderFactory.get("job")
.start(stepBuilderFactory.get("step1").tasklet(new Tasklet() {
return this.jobBuilderFactory.get("job")
.start(this.stepBuilderFactory.get("step1").tasklet(new Tasklet() {
@Override
public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) {
public RepeatStatus execute(StepContribution contribution,
ChunkContext chunkContext) {
System.out.println("Executed");
return RepeatStatus.FINISHED;
}
}).build())
.build();
}).build()).build();
}
}
@EnableBatchProcessing
@ImportAutoConfiguration({
PropertyPlaceholderAutoConfiguration.class,
BatchAutoConfiguration.class,
TaskBatchAutoConfiguration.class,
TaskJobLauncherAutoConfiguration.class,
SingleTaskConfiguration.class,
@ImportAutoConfiguration({ PropertyPlaceholderAutoConfiguration.class,
BatchAutoConfiguration.class, TaskBatchAutoConfiguration.class,
TaskJobLauncherAutoConfiguration.class, SingleTaskConfiguration.class,
SimpleTaskAutoConfiguration.class })
@Import(EmbeddedDataSourceConfiguration.class)
@EnableTask
@@ -208,50 +222,54 @@ public class TaskJobLauncherCommandLineRunnerTests {
@Bean
public Job jobFail() {
return jobBuilderFactory.get("jobA")
.start(stepBuilderFactory.get("step1").tasklet(new Tasklet() {
return this.jobBuilderFactory.get("jobA")
.start(this.stepBuilderFactory.get("step1").tasklet(new Tasklet() {
@Override
public RepeatStatus execute(StepContribution contribution,
ChunkContext chunkContext)
throws Exception {
ChunkContext chunkContext) throws Exception {
System.out.println("Executed");
throw new IllegalStateException("WHOOPS");
throw new IllegalStateException("WHOOPS");
}
}).build())
.build();
}).build()).build();
}
@Bean
public Job jobFun() {
return jobBuilderFactory.get("jobSucceed")
.start(stepBuilderFactory.get("step1Succeed").tasklet(new Tasklet() {
return this.jobBuilderFactory.get("jobSucceed").start(
this.stepBuilderFactory.get("step1Succeed").tasklet(new Tasklet() {
@Override
public RepeatStatus execute(StepContribution contribution,
ChunkContext chunkContext) {
System.out.println("Executed");
return RepeatStatus.FINISHED;
}
}).build())
.build();
}).build()).build();
}
}
@EnableTask
public static class JobWithFailureAnnotatedConfiguration extends JobWithFailureConfiguration{
public static class JobWithFailureAnnotatedConfiguration
extends JobWithFailureConfiguration {
}
public static class JobWithFailureTaskExecutorConfiguration extends JobWithFailureConfiguration{
public static class JobWithFailureTaskExecutorConfiguration
extends JobWithFailureConfiguration {
@Bean
public BatchConfigurer batchConfigurer(DataSource dataSource) {
return new TestBatchConfigurer(dataSource);
}
}
private static class TestBatchConfigurer extends DefaultBatchConfigurer{
public TestBatchConfigurer(DataSource dataSource) {
private static class TestBatchConfigurer extends DefaultBatchConfigurer {
TestBatchConfigurer(DataSource dataSource) {
super(dataSource);
}
protected JobLauncher createJobLauncher() throws Exception {
SimpleJobLauncher jobLauncher = new SimpleJobLauncher();
jobLauncher.setJobRepository(getJobRepository());
@@ -259,5 +277,7 @@ public class TaskJobLauncherCommandLineRunnerTests {
jobLauncher.afterPropertiesSet();
return jobLauncher;
}
}
}

View File

@@ -1,22 +1,23 @@
/*
* Copyright 2018 the original author or authors.
* Copyright 2015-2019 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.
* You may obtain a copy of the License at
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.cloud.task.batch.listener;
import java.util.Set;
import javax.sql.DataSource;
import org.junit.After;
@@ -56,8 +57,8 @@ public class PrefixTests {
@Test
public void testPrefix() {
this.applicationContext = SpringApplication.run(
JobConfiguration.class, "--spring.cloud.task.tablePrefix=FOO_");
this.applicationContext = SpringApplication.run(JobConfiguration.class,
"--spring.cloud.task.tablePrefix=FOO_");
TaskExplorer taskExplorer = this.applicationContext.getBean(TaskExplorer.class);
@@ -80,22 +81,19 @@ public class PrefixTests {
@Bean
public Job job() {
return jobBuilderFactory.get("job")
.start(stepBuilderFactory.get("step1")
.tasklet((contribution, chunkContext) -> {
return this.jobBuilderFactory.get("job").start(this.stepBuilderFactory
.get("step1").tasklet((contribution, chunkContext) -> {
System.out.println("Executed");
return RepeatStatus.FINISHED;
}).build())
.build();
}).build()).build();
}
@Bean
public DataSource dataSource() {
return new EmbeddedDatabaseBuilder()
.addScript("classpath:schema-h2.sql")
.setType(EmbeddedDatabaseType.H2)
.build();
return new EmbeddedDatabaseBuilder().addScript("classpath:schema-h2.sql")
.setType(EmbeddedDatabaseType.H2).build();
}
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2016-2018 the original author or authors.
* Copyright 2015-2019 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.
@@ -13,6 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.cloud.task.batch.listener;
import java.util.ArrayList;
@@ -61,7 +62,7 @@ import org.springframework.data.domain.PageRequest;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType;
import static org.junit.Assert.assertEquals;
import static org.assertj.core.api.Assertions.assertThat;
/**
* @author Michael Minella
@@ -75,15 +76,14 @@ public class TaskBatchExecutionListenerTests {
@After
public void tearDown() {
if(this.applicationContext != null && this.applicationContext.isActive()) {
if (this.applicationContext != null && this.applicationContext.isActive()) {
this.applicationContext.close();
}
}
@Test
public void testAutobuiltDataSource() {
this.applicationContext = SpringApplication.run(JobConfiguration.class ,
ARGS);
this.applicationContext = SpringApplication.run(JobConfiguration.class, ARGS);
validateContext();
}
@@ -96,28 +96,28 @@ public class TaskBatchExecutionListenerTests {
@Test(expected = AssertionError.class)
public void testNoAutoConfigurationEnable() {
this.applicationContext = SpringApplication.run(JobConfiguration.class ,
this.applicationContext = SpringApplication.run(JobConfiguration.class,
"--spring.cloud.task.batch.listener.enable=false");
validateContext();
}
@Test(expected = AssertionError.class)
public void testNoAutoConfigurationBothDisabled() {
this.applicationContext = SpringApplication.run(JobConfiguration.class ,
this.applicationContext = SpringApplication.run(JobConfiguration.class,
"--spring.cloud.task.batch.listener.enable=false --spring.cloud.task.batch.listener.enabled=false");
validateContext();
}
@Test
public void testAutoConfigurationEnable() {
this.applicationContext = SpringApplication.run(JobConfiguration.class ,
this.applicationContext = SpringApplication.run(JobConfiguration.class,
"--spring.cloud.task.batch.listener.enable=true");
validateContext();
}
@Test
public void testAutoConfigurationEnabled() {
this.applicationContext = SpringApplication.run(JobConfiguration.class ,
this.applicationContext = SpringApplication.run(JobConfiguration.class,
"--spring.cloud.task.batch.listener.enabled=true");
validateContext();
}
@@ -132,26 +132,34 @@ public class TaskBatchExecutionListenerTests {
private void validateContext() {
TaskExplorer taskExplorer = this.applicationContext.getBean(TaskExplorer.class);
Page<TaskExecution> page = taskExplorer.findTaskExecutionsByName("application", PageRequest.of(0, 1));
Page<TaskExecution> page = taskExplorer.findTaskExecutionsByName("application",
PageRequest.of(0, 1));
Set<Long> jobExecutionIds = taskExplorer.getJobExecutionIdsByTaskExecutionId(page.iterator().next().getExecutionId());
Set<Long> jobExecutionIds = taskExplorer.getJobExecutionIdsByTaskExecutionId(
page.iterator().next().getExecutionId());
assertEquals(1, jobExecutionIds.size());
assertEquals(1, taskExplorer.getTaskExecution(jobExecutionIds.iterator().next()).getExecutionId());
assertThat(jobExecutionIds.size()).isEqualTo(1);
assertThat(taskExplorer.getTaskExecution(jobExecutionIds.iterator().next())
.getExecutionId()).isEqualTo(1);
}
@Test
public void testMultipleDataSources() {
this.applicationContext = SpringApplication.run(JobConfigurationMultipleDataSources.class, ARGS);
this.applicationContext = SpringApplication
.run(JobConfigurationMultipleDataSources.class, ARGS);
TaskExplorer taskExplorer = this.applicationContext.getBean(TaskExplorer.class);
Page<TaskExecution> page = taskExplorer.findTaskExecutionsByName("application", PageRequest.of(0, 1));
Page<TaskExecution> page = taskExplorer.findTaskExecutionsByName("application",
PageRequest.of(0, 1));
Set<Long> jobExecutionIds = taskExplorer.getJobExecutionIdsByTaskExecutionId(page.iterator().next().getExecutionId());
Set<Long> jobExecutionIds = taskExplorer.getJobExecutionIdsByTaskExecutionId(
page.iterator().next().getExecutionId());
assertEquals(1, jobExecutionIds.size());
assertEquals(1, taskExplorer.getTaskExecution(jobExecutionIds.iterator().next()).getExecutionId());
assertThat(jobExecutionIds.size()).isEqualTo(1);
assertThat(taskExplorer.getTaskExecution(jobExecutionIds.iterator().next())
.getExecutionId()).isEqualTo(1);
}
@Test
@@ -160,11 +168,13 @@ public class TaskBatchExecutionListenerTests {
TaskExplorer taskExplorer = this.applicationContext.getBean(TaskExplorer.class);
Page<TaskExecution> page = taskExplorer.findTaskExecutionsByName("application", PageRequest.of(0, 1));
Page<TaskExecution> page = taskExplorer.findTaskExecutionsByName("application",
PageRequest.of(0, 1));
Set<Long> jobExecutionIds = taskExplorer.getJobExecutionIdsByTaskExecutionId(page.iterator().next().getExecutionId());
Set<Long> jobExecutionIds = taskExplorer.getJobExecutionIdsByTaskExecutionId(
page.iterator().next().getExecutionId());
assertEquals(0, jobExecutionIds.size());
assertThat(jobExecutionIds.size()).isEqualTo(0);
}
@Test
@@ -173,28 +183,39 @@ public class TaskBatchExecutionListenerTests {
TaskExplorer taskExplorer = this.applicationContext.getBean(TaskExplorer.class);
Page<TaskExecution> page = taskExplorer.findTaskExecutionsByName("application", PageRequest.of(0, 1));
Page<TaskExecution> page = taskExplorer.findTaskExecutionsByName("application",
PageRequest.of(0, 1));
Set<Long> jobExecutionIds = taskExplorer.getJobExecutionIdsByTaskExecutionId(page.iterator().next().getExecutionId());
Set<Long> jobExecutionIds = taskExplorer.getJobExecutionIdsByTaskExecutionId(
page.iterator().next().getExecutionId());
assertEquals(1, jobExecutionIds.size());
assertEquals(1, (long) taskExplorer.getTaskExecutionIdByJobExecutionId(jobExecutionIds.iterator().next()));
assertThat(jobExecutionIds.size()).isEqualTo(1);
assertThat((long) taskExplorer
.getTaskExecutionIdByJobExecutionId(jobExecutionIds.iterator().next()))
.isEqualTo(1);
}
@Test
public void testMultipleJobs() {
this.applicationContext = SpringApplication.run(MultipleJobConfiguration.class, ARGS);
this.applicationContext = SpringApplication.run(MultipleJobConfiguration.class,
ARGS);
TaskExplorer taskExplorer = this.applicationContext.getBean(TaskExplorer.class);
Page<TaskExecution> page = taskExplorer.findTaskExecutionsByName("application", PageRequest.of(0, 1));
Page<TaskExecution> page = taskExplorer.findTaskExecutionsByName("application",
PageRequest.of(0, 1));
Set<Long> jobExecutionIds = taskExplorer.getJobExecutionIdsByTaskExecutionId(page.iterator().next().getExecutionId());
Set<Long> jobExecutionIds = taskExplorer.getJobExecutionIdsByTaskExecutionId(
page.iterator().next().getExecutionId());
assertEquals(2, jobExecutionIds.size());
assertThat(jobExecutionIds.size()).isEqualTo(2);
Iterator<Long> jobExecutionIdsIterator = jobExecutionIds.iterator();
assertEquals(1, (long) taskExplorer.getTaskExecutionIdByJobExecutionId(jobExecutionIdsIterator.next()));
assertEquals(1, (long) taskExplorer.getTaskExecutionIdByJobExecutionId(jobExecutionIdsIterator.next()));
assertThat((long) taskExplorer
.getTaskExecutionIdByJobExecutionId(jobExecutionIdsIterator.next()))
.isEqualTo(1);
assertThat((long) taskExplorer
.getTaskExecutionIdByJobExecutionId(jobExecutionIdsIterator.next()))
.isEqualTo(1);
}
@Test
@@ -204,27 +225,26 @@ public class TaskBatchExecutionListenerTests {
jobNames.add("job2");
jobNames.add("TESTOBJECT");
TaskBatchExecutionListenerBeanPostProcessor beanPostProcessor =
beanPostProcessor(jobNames);
TaskBatchExecutionListenerBeanPostProcessor beanPostProcessor = beanPostProcessor(
jobNames);
SimpleJob testObject = new SimpleJob();
SimpleJob bean = (SimpleJob) beanPostProcessor.
postProcessBeforeInitialization(testObject,"TESTOBJECT");
assertEquals(testObject,bean);
SimpleJob bean = (SimpleJob) beanPostProcessor
.postProcessBeforeInitialization(testObject, "TESTOBJECT");
assertThat(bean).isEqualTo(testObject);
}
@Test
public void testBatchExecutionListenerBeanPostProcessorWithEmptyJobNames() {
TaskBatchExecutionListenerBeanPostProcessor beanPostProcessor =
beanPostProcessor(Collections.emptyList());
TaskBatchExecutionListenerBeanPostProcessor beanPostProcessor = beanPostProcessor(
Collections.emptyList());
SimpleJob testObject = new SimpleJob();
SimpleJob bean = (SimpleJob) beanPostProcessor.
postProcessBeforeInitialization(testObject,"TESTOBJECT");
assertEquals(testObject,bean);
SimpleJob bean = (SimpleJob) beanPostProcessor
.postProcessBeforeInitialization(testObject, "TESTOBJECT");
assertThat(bean).isEqualTo(testObject);
}
@Test(expected = IllegalArgumentException.class)
public void testBatchExecutionListenerBeanPostProcessorNullJobNames() {
beanPostProcessor(null);
@@ -232,16 +252,14 @@ public class TaskBatchExecutionListenerTests {
private TaskBatchExecutionListenerBeanPostProcessor beanPostProcessor(
List<String> jobNames) {
this.applicationContext = SpringApplication.run(new Class[] {JobConfiguration.class,
PropertyPlaceholderAutoConfiguration.class, EmbeddedDataSourceConfiguration.class,
BatchAutoConfiguration.class,
TaskBatchAutoConfiguration.class,
SimpleTaskAutoConfiguration.class,
this.applicationContext = SpringApplication.run(new Class[] {
JobConfiguration.class, PropertyPlaceholderAutoConfiguration.class,
EmbeddedDataSourceConfiguration.class, BatchAutoConfiguration.class,
TaskBatchAutoConfiguration.class, SimpleTaskAutoConfiguration.class,
SingleTaskConfiguration.class }, ARGS);
TaskBatchExecutionListenerBeanPostProcessor beanPostProcessor =
this.applicationContext.getBean(
TaskBatchExecutionListenerBeanPostProcessor.class);
TaskBatchExecutionListenerBeanPostProcessor beanPostProcessor = this.applicationContext
.getBean(TaskBatchExecutionListenerBeanPostProcessor.class);
beanPostProcessor.setJobNames(jobNames);
return beanPostProcessor;
@@ -269,16 +287,17 @@ public class TaskBatchExecutionListenerTests {
@Bean
public Job job() {
return jobBuilderFactory.get("job")
.start(stepBuilderFactory.get("step1").tasklet(new Tasklet() {
return this.jobBuilderFactory.get("job")
.start(this.stepBuilderFactory.get("step1").tasklet(new Tasklet() {
@Override
public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws Exception {
public RepeatStatus execute(StepContribution contribution,
ChunkContext chunkContext) throws Exception {
System.out.println("Executed");
return RepeatStatus.FINISHED;
}
}).build())
.build();
}).build()).build();
}
}
@EnableBatchProcessing
@@ -298,14 +317,18 @@ public class TaskBatchExecutionListenerTests {
return new FactoryBean<Job>() {
@Override
public Job getObject() throws Exception {
return jobBuilderFactory.get("job")
.start(stepBuilderFactory.get("step1").tasklet(new Tasklet() {
@Override
public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws Exception {
System.out.println("Executed");
return RepeatStatus.FINISHED;
}
}).build())
return JobFactoryBeanConfiguration.this.jobBuilderFactory.get("job")
.start(JobFactoryBeanConfiguration.this.stepBuilderFactory
.get("step1").tasklet(new Tasklet() {
@Override
public RepeatStatus execute(
StepContribution contribution,
ChunkContext chunkContext)
throws Exception {
System.out.println("Executed");
return RepeatStatus.FINISHED;
}
}).build())
.build();
}
@@ -320,6 +343,7 @@ public class TaskBatchExecutionListenerTests {
}
};
}
}
@EnableBatchProcessing
@@ -336,34 +360,31 @@ public class TaskBatchExecutionListenerTests {
@Bean
public Job job() {
return jobBuilderFactory.get("job")
.start(stepBuilderFactory.get("step1").tasklet(new Tasklet() {
return this.jobBuilderFactory.get("job")
.start(this.stepBuilderFactory.get("step1").tasklet(new Tasklet() {
@Override
public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws Exception {
public RepeatStatus execute(StepContribution contribution,
ChunkContext chunkContext) throws Exception {
System.out.println("Executed");
return RepeatStatus.FINISHED;
}
}).build())
.build();
}).build()).build();
}
@Bean
@Primary
public DataSource myDataSource() {
EmbeddedDatabaseBuilder builder = new EmbeddedDatabaseBuilder()
.setType(EmbeddedDatabaseType.H2)
.setName("myDataSource");
.setType(EmbeddedDatabaseType.H2).setName("myDataSource");
return builder.build();
}
@Bean
public DataSource incorrectDataSource() {
EmbeddedDatabaseBuilder builder = new EmbeddedDatabaseBuilder()
.setType(EmbeddedDatabaseType.H2)
.setName("incorrectDataSource");
.setType(EmbeddedDatabaseType.H2).setName("incorrectDataSource");
return builder.build();
}
@Bean
public TaskConfigurer taskConfigurer() {
@@ -374,6 +395,7 @@ public class TaskBatchExecutionListenerTests {
public DefaultBatchConfigurer batchConfigurer() {
return new DefaultBatchConfigurer(myDataSource());
}
}
@EnableBatchProcessing
@@ -390,28 +412,30 @@ public class TaskBatchExecutionListenerTests {
@Bean
public Job job1() {
return jobBuilderFactory.get("job1")
.start(stepBuilderFactory.get("job1step1").tasklet(new Tasklet() {
return this.jobBuilderFactory.get("job1").start(
this.stepBuilderFactory.get("job1step1").tasklet(new Tasklet() {
@Override
public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws Exception {
public RepeatStatus execute(StepContribution contribution,
ChunkContext chunkContext) throws Exception {
System.out.println("Executed job1");
return RepeatStatus.FINISHED;
}
}).build())
.build();
}).build()).build();
}
@Bean
public Job job2() {
return jobBuilderFactory.get("job2")
.start(stepBuilderFactory.get("job2step1").tasklet(new Tasklet() {
return this.jobBuilderFactory.get("job2").start(
this.stepBuilderFactory.get("job2step1").tasklet(new Tasklet() {
@Override
public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws Exception {
public RepeatStatus execute(StepContribution contribution,
ChunkContext chunkContext) throws Exception {
System.out.println("Executed job2");
return RepeatStatus.FINISHED;
}
}).build())
.build();
}).build()).build();
}
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2016-2017 the original author or authors.
* Copyright 2015-2019 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.
@@ -13,6 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.cloud.task.batch.partition;
import java.util.ArrayList;
@@ -50,9 +51,7 @@ import org.springframework.core.env.Environment;
import org.springframework.core.io.Resource;
import org.springframework.mock.env.MockEnvironment;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.any;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.times;
@@ -64,6 +63,9 @@ import static org.mockito.Mockito.when;
*/
public class DeployerPartitionHandlerTests {
@Captor
ArgumentCaptor<AppDeploymentRequest> appDeploymentRequestArgumentCaptor;
@Mock
private TaskLauncher taskLauncher;
@@ -81,8 +83,6 @@ public class DeployerPartitionHandlerTests {
private Environment environment;
@Captor ArgumentCaptor<AppDeploymentRequest> appDeploymentRequestArgumentCaptor;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
@@ -91,27 +91,33 @@ public class DeployerPartitionHandlerTests {
@Test
public void testConstructorValidation() {
validateConstructorValidation(null, null, null, null, "A taskLauncher is required");
validateConstructorValidation(this.taskLauncher, null, null, null, "A jobExplorer is required");
validateConstructorValidation(this.taskLauncher, this.jobExplorer, null, null, "A resource is required");
validateConstructorValidation(this.taskLauncher, this.jobExplorer, this.resource, null, "A step name is required");
validateConstructorValidation(null, null, null, null,
"A taskLauncher is required");
validateConstructorValidation(this.taskLauncher, null, null, null,
"A jobExplorer is required");
validateConstructorValidation(this.taskLauncher, this.jobExplorer, null, null,
"A resource is required");
validateConstructorValidation(this.taskLauncher, this.jobExplorer, this.resource,
null, "A step name is required");
new DeployerPartitionHandler(this.taskLauncher, this.jobExplorer, this.resource, "step-name");
new DeployerPartitionHandler(this.taskLauncher, this.jobExplorer, this.resource,
"step-name");
}
@Test
public void testNoPartitions() throws Exception {
DeployerPartitionHandler handler = new DeployerPartitionHandler(this.taskLauncher, this.jobExplorer, this.resource, "step1");
DeployerPartitionHandler handler = new DeployerPartitionHandler(this.taskLauncher,
this.jobExplorer, this.resource, "step1");
handler.setEnvironment(this.environment);
StepExecution stepExecution = new StepExecution("step1", new JobExecution(1L));
when(this.splitter.split(stepExecution, 1)).thenReturn(new HashSet<StepExecution>());
when(this.splitter.split(stepExecution, 1)).thenReturn(new HashSet<>());
Collection<StepExecution> results = handler.handle(this.splitter, stepExecution);
verify(this.taskLauncher, never()).launch((AppDeploymentRequest) any());
assertTrue(results.isEmpty());
assertThat(results.isEmpty()).isTrue();
}
@Test
@@ -121,9 +127,11 @@ public class DeployerPartitionHandlerTests {
JobExecution jobExecution = masterStepExecution.getJobExecution();
StepExecution workerStepExecutionStart = getStepExecutionStart(jobExecution, 4L);
StepExecution workerStepExecutionFinish = getStepExecutionFinish(workerStepExecutionStart, BatchStatus.COMPLETED);
StepExecution workerStepExecutionFinish = getStepExecutionFinish(
workerStepExecutionStart, BatchStatus.COMPLETED);
DeployerPartitionHandler handler = new DeployerPartitionHandler(this.taskLauncher, this.jobExplorer, this.resource, "step1");
DeployerPartitionHandler handler = new DeployerPartitionHandler(this.taskLauncher,
this.jobExplorer, this.resource, "step1");
handler.setEnvironment(this.environment);
TaskExecution taskExecution = new TaskExecution();
@@ -133,32 +141,40 @@ public class DeployerPartitionHandlerTests {
stepExecutions.add(workerStepExecutionStart);
when(this.splitter.split(masterStepExecution, 1)).thenReturn(stepExecutions);
when(this.jobExplorer.getStepExecution(1L, 4L)).thenReturn(workerStepExecutionFinish);
when(this.jobExplorer.getStepExecution(1L, 4L))
.thenReturn(workerStepExecutionFinish);
handler.afterPropertiesSet();
handler.beforeTask(taskExecution);
Collection<StepExecution> results = handler.handle(this.splitter, masterStepExecution);
Collection<StepExecution> results = handler.handle(this.splitter,
masterStepExecution);
verify(this.taskLauncher).launch(this.appDeploymentRequestArgumentCaptor.capture());
verify(this.taskLauncher)
.launch(this.appDeploymentRequestArgumentCaptor.capture());
AppDeploymentRequest request = this.appDeploymentRequestArgumentCaptor.getValue();
assertEquals(this.resource, request.getResource());
assertEquals(0, request.getDeploymentProperties().size());
assertThat(request.getResource()).isEqualTo(this.resource);
assertThat(request.getDeploymentProperties().size()).isEqualTo(0);
AppDefinition appDefinition = request.getDefinition();
assertEquals("partitionedJobTask", appDefinition.getName());
assertTrue(request.getCommandlineArguments().contains(formatArgs(DeployerPartitionHandler.SPRING_CLOUD_TASK_JOB_EXECUTION_ID, "1")));
assertTrue(request.getCommandlineArguments().contains(formatArgs(DeployerPartitionHandler.SPRING_CLOUD_TASK_STEP_EXECUTION_ID, "4")));
assertTrue(request.getCommandlineArguments().contains(formatArgs(DeployerPartitionHandler.SPRING_CLOUD_TASK_STEP_NAME, "step1")));
assertThat(appDefinition.getName()).isEqualTo("partitionedJobTask");
assertThat(request.getCommandlineArguments().contains(formatArgs(
DeployerPartitionHandler.SPRING_CLOUD_TASK_JOB_EXECUTION_ID, "1")))
.isTrue();
assertThat(request.getCommandlineArguments().contains(formatArgs(
DeployerPartitionHandler.SPRING_CLOUD_TASK_STEP_EXECUTION_ID, "4")))
.isTrue();
assertThat(request.getCommandlineArguments().contains(formatArgs(
DeployerPartitionHandler.SPRING_CLOUD_TASK_STEP_NAME, "step1"))).isTrue();
assertEquals(1, results.size());
assertThat(results.size()).isEqualTo(1);
StepExecution resultStepExecution = results.iterator().next();
assertEquals(BatchStatus.COMPLETED, resultStepExecution.getStatus());
assertEquals("step1:partition1", resultStepExecution.getStepName());
assertThat(resultStepExecution.getStatus()).isEqualTo(BatchStatus.COMPLETED);
assertThat(resultStepExecution.getStepName()).isEqualTo("step1:partition1");
}
@Test
@@ -168,49 +184,64 @@ public class DeployerPartitionHandlerTests {
JobExecution jobExecution = masterStepExecution.getJobExecution();
StepExecution workerStepExecutionStart = getStepExecutionStart(jobExecution, 4L);
StepExecution workerStepExecutionFinish = getStepExecutionFinish(workerStepExecutionStart, BatchStatus.COMPLETED);
StepExecution workerStepExecutionFinish = getStepExecutionFinish(
workerStepExecutionStart, BatchStatus.COMPLETED);
DeployerPartitionHandler handler = new DeployerPartitionHandler(this.taskLauncher, this.jobExplorer, this.resource, "step1");
DeployerPartitionHandler handler = new DeployerPartitionHandler(this.taskLauncher,
this.jobExplorer, this.resource, "step1");
handler.setEnvironment(this.environment);
handler.setDefaultArgsAsEnvironmentVars(true);
TaskExecution taskExecution = new TaskExecution(55, null, null, null,
null, null, new ArrayList<String>(), null, null);
TaskExecution taskExecution = new TaskExecution(55, null, null, null, null, null,
new ArrayList<>(), null, null);
taskExecution.setTaskName("partitionedJobTask");
Set<StepExecution> stepExecutions = new HashSet<>();
stepExecutions.add(workerStepExecutionStart);
when(this.splitter.split(masterStepExecution, 1)).thenReturn(stepExecutions);
when(this.jobExplorer.getStepExecution(1L, 4L)).thenReturn(workerStepExecutionFinish);
when(this.jobExplorer.getStepExecution(1L, 4L))
.thenReturn(workerStepExecutionFinish);
handler.afterPropertiesSet();
handler.beforeTask(taskExecution);
Collection<StepExecution> results = handler.handle(this.splitter, masterStepExecution);
Collection<StepExecution> results = handler.handle(this.splitter,
masterStepExecution);
verify(this.taskLauncher).launch(this.appDeploymentRequestArgumentCaptor.capture());
verify(this.taskLauncher)
.launch(this.appDeploymentRequestArgumentCaptor.capture());
AppDeploymentRequest request = this.appDeploymentRequestArgumentCaptor.getValue();
assertEquals(this.resource, request.getResource());
assertEquals(0, request.getDeploymentProperties().size());
assertThat(request.getResource()).isEqualTo(this.resource);
assertThat(request.getDeploymentProperties().size()).isEqualTo(0);
AppDefinition appDefinition = request.getDefinition();
assertEquals("partitionedJobTask", appDefinition.getName());
assertTrue(request.getCommandlineArguments().isEmpty());
assertEquals("1", request.getDefinition().getProperties().get(DeployerPartitionHandler.SPRING_CLOUD_TASK_JOB_EXECUTION_ID));
assertEquals("4", request.getDefinition().getProperties().get(DeployerPartitionHandler.SPRING_CLOUD_TASK_STEP_EXECUTION_ID));
assertEquals("step1", request.getDefinition().getProperties().get(DeployerPartitionHandler.SPRING_CLOUD_TASK_STEP_NAME));
assertEquals("partitionedJobTask_partitionedJob_step1:partition1", request.getDefinition().getProperties().get(DeployerPartitionHandler.SPRING_CLOUD_TASK_NAME));
assertEquals("55", request.getDefinition().getProperties().get(DeployerPartitionHandler.SPRING_CLOUD_TASK_PARENT_EXECUTION_ID));
assertThat(appDefinition.getName()).isEqualTo("partitionedJobTask");
assertThat(request.getCommandlineArguments().isEmpty()).isTrue();
assertThat(request.getDefinition().getProperties()
.get(DeployerPartitionHandler.SPRING_CLOUD_TASK_JOB_EXECUTION_ID))
.isEqualTo("1");
assertThat(request.getDefinition().getProperties()
.get(DeployerPartitionHandler.SPRING_CLOUD_TASK_STEP_EXECUTION_ID))
.isEqualTo("4");
assertThat(request.getDefinition().getProperties()
.get(DeployerPartitionHandler.SPRING_CLOUD_TASK_STEP_NAME))
.isEqualTo("step1");
assertThat(request.getDefinition().getProperties()
.get(DeployerPartitionHandler.SPRING_CLOUD_TASK_NAME))
.isEqualTo("partitionedJobTask_partitionedJob_step1:partition1");
assertThat(request.getDefinition().getProperties()
.get(DeployerPartitionHandler.SPRING_CLOUD_TASK_PARENT_EXECUTION_ID))
.isEqualTo("55");
assertEquals(1, results.size());
assertThat(results.size()).isEqualTo(1);
StepExecution resultStepExecution = results.iterator().next();
assertEquals(BatchStatus.COMPLETED, resultStepExecution.getStatus());
assertEquals("step1:partition1", resultStepExecution.getStepName());
assertThat(resultStepExecution.getStatus()).isEqualTo(BatchStatus.COMPLETED);
assertThat(resultStepExecution.getStepName()).isEqualTo("step1:partition1");
}
@Test
@@ -220,13 +251,15 @@ public class DeployerPartitionHandlerTests {
JobExecution jobExecution = masterStepExecution.getJobExecution();
StepExecution workerStepExecutionStart = getStepExecutionStart(jobExecution, 4L);
StepExecution workerStepExecutionFinish = getStepExecutionFinish(workerStepExecutionStart, BatchStatus.COMPLETED);
StepExecution workerStepExecutionFinish = getStepExecutionFinish(
workerStepExecutionStart, BatchStatus.COMPLETED);
DeployerPartitionHandler handler = new DeployerPartitionHandler(this.taskLauncher, this.jobExplorer, this.resource, "step1");
DeployerPartitionHandler handler = new DeployerPartitionHandler(this.taskLauncher,
this.jobExplorer, this.resource, "step1");
handler.setEnvironment(this.environment);
TaskExecution taskExecution = new TaskExecution(55, null, null, null,
null, null, new ArrayList<String>(), null, null);
TaskExecution taskExecution = new TaskExecution(55, null, null, null, null, null,
new ArrayList<>(), null, null);
taskExecution.setTaskName("partitionedJobTask");
@@ -234,7 +267,8 @@ public class DeployerPartitionHandlerTests {
stepExecutions.add(workerStepExecutionStart);
when(this.splitter.split(masterStepExecution, 1)).thenReturn(stepExecutions);
when(this.jobExplorer.getStepExecution(1L, 4L)).thenReturn(workerStepExecutionFinish);
when(this.jobExplorer.getStepExecution(1L, 4L))
.thenReturn(workerStepExecutionFinish);
handler.afterPropertiesSet();
@@ -242,10 +276,13 @@ public class DeployerPartitionHandlerTests {
handler.handle(this.splitter, masterStepExecution);
verify(this.taskLauncher).launch(this.appDeploymentRequestArgumentCaptor.capture());
verify(this.taskLauncher)
.launch(this.appDeploymentRequestArgumentCaptor.capture());
AppDeploymentRequest request = this.appDeploymentRequestArgumentCaptor.getValue();
assertTrue(request.getCommandlineArguments().contains(formatArgs(DeployerPartitionHandler.SPRING_CLOUD_TASK_PARENT_EXECUTION_ID, "55")));
assertThat(request.getCommandlineArguments().contains(formatArgs(
DeployerPartitionHandler.SPRING_CLOUD_TASK_PARENT_EXECUTION_ID, "55")))
.isTrue();
}
@Test
@@ -255,15 +292,19 @@ public class DeployerPartitionHandlerTests {
JobExecution jobExecution = masterStepExecution.getJobExecution();
StepExecution workerStepExecutionStart1 = getStepExecutionStart(jobExecution, 4L);
StepExecution workerStepExecutionFinish1 = getStepExecutionFinish(workerStepExecutionStart1, BatchStatus.COMPLETED);
StepExecution workerStepExecutionFinish1 = getStepExecutionFinish(
workerStepExecutionStart1, BatchStatus.COMPLETED);
StepExecution workerStepExecutionStart2 = getStepExecutionStart(jobExecution, 5L);
StepExecution workerStepExecutionFinish2 = getStepExecutionFinish(workerStepExecutionStart2, BatchStatus.COMPLETED);
StepExecution workerStepExecutionFinish2 = getStepExecutionFinish(
workerStepExecutionStart2, BatchStatus.COMPLETED);
StepExecution workerStepExecutionStart3 = getStepExecutionStart(jobExecution, 6L);
StepExecution workerStepExecutionFinish3 = getStepExecutionFinish(workerStepExecutionStart3, BatchStatus.COMPLETED);
StepExecution workerStepExecutionFinish3 = getStepExecutionFinish(
workerStepExecutionStart3, BatchStatus.COMPLETED);
DeployerPartitionHandler handler = new DeployerPartitionHandler(this.taskLauncher, this.jobExplorer, this.resource, "step1");
DeployerPartitionHandler handler = new DeployerPartitionHandler(this.taskLauncher,
this.jobExplorer, this.resource, "step1");
handler.setEnvironment(this.environment);
TaskExecution taskExecution = new TaskExecution();
@@ -277,18 +318,24 @@ public class DeployerPartitionHandlerTests {
when(this.splitter.split(masterStepExecution, 1)).thenReturn(stepExecutions);
when(this.jobExplorer.getStepExecution(1L, 4L)).thenReturn(workerStepExecutionFinish1);
when(this.jobExplorer.getStepExecution(1L, 5L)).thenReturn(workerStepExecutionFinish2);
when(this.jobExplorer.getStepExecution(1L, 6L)).thenReturn(workerStepExecutionFinish3);
when(this.jobExplorer.getStepExecution(1L, 4L))
.thenReturn(workerStepExecutionFinish1);
when(this.jobExplorer.getStepExecution(1L, 5L))
.thenReturn(workerStepExecutionFinish2);
when(this.jobExplorer.getStepExecution(1L, 6L))
.thenReturn(workerStepExecutionFinish3);
handler.afterPropertiesSet();
handler.beforeTask(taskExecution);
Collection<StepExecution> results = handler.handle(this.splitter, masterStepExecution);
Collection<StepExecution> results = handler.handle(this.splitter,
masterStepExecution);
verify(this.taskLauncher, times(3)).launch(this.appDeploymentRequestArgumentCaptor.capture());
verify(this.taskLauncher, times(3))
.launch(this.appDeploymentRequestArgumentCaptor.capture());
List<AppDeploymentRequest> allValues = this.appDeploymentRequestArgumentCaptor.getAllValues();
List<AppDeploymentRequest> allValues = this.appDeploymentRequestArgumentCaptor
.getAllValues();
validateAppDeploymentRequests(allValues, 3);
@@ -302,15 +349,19 @@ public class DeployerPartitionHandlerTests {
JobExecution jobExecution = masterStepExecution.getJobExecution();
StepExecution workerStepExecutionStart1 = getStepExecutionStart(jobExecution, 4L);
StepExecution workerStepExecutionFinish1 = getStepExecutionFinish(workerStepExecutionStart1, BatchStatus.COMPLETED);
StepExecution workerStepExecutionFinish1 = getStepExecutionFinish(
workerStepExecutionStart1, BatchStatus.COMPLETED);
StepExecution workerStepExecutionStart2 = getStepExecutionStart(jobExecution, 5L);
StepExecution workerStepExecutionFinish2 = getStepExecutionFinish(workerStepExecutionStart2, BatchStatus.COMPLETED);
StepExecution workerStepExecutionFinish2 = getStepExecutionFinish(
workerStepExecutionStart2, BatchStatus.COMPLETED);
StepExecution workerStepExecutionStart3 = getStepExecutionStart(jobExecution, 6L);
StepExecution workerStepExecutionFinish3 = getStepExecutionFinish(workerStepExecutionStart3, BatchStatus.COMPLETED);
StepExecution workerStepExecutionFinish3 = getStepExecutionFinish(
workerStepExecutionStart3, BatchStatus.COMPLETED);
DeployerPartitionHandler handler = new DeployerPartitionHandler(this.taskLauncher, this.jobExplorer, this.resource, "step1");
DeployerPartitionHandler handler = new DeployerPartitionHandler(this.taskLauncher,
this.jobExplorer, this.resource, "step1");
handler.setEnvironment(this.environment);
handler.setMaxWorkers(2);
@@ -325,18 +376,24 @@ public class DeployerPartitionHandlerTests {
when(this.splitter.split(masterStepExecution, 1)).thenReturn(stepExecutions);
when(this.jobExplorer.getStepExecution(1L, 4L)).thenReturn(workerStepExecutionFinish1);
when(this.jobExplorer.getStepExecution(1L, 5L)).thenReturn(workerStepExecutionFinish2);
when(this.jobExplorer.getStepExecution(1L, 6L)).thenReturn(workerStepExecutionFinish3);
when(this.jobExplorer.getStepExecution(1L, 4L))
.thenReturn(workerStepExecutionFinish1);
when(this.jobExplorer.getStepExecution(1L, 5L))
.thenReturn(workerStepExecutionFinish2);
when(this.jobExplorer.getStepExecution(1L, 6L))
.thenReturn(workerStepExecutionFinish3);
handler.afterPropertiesSet();
handler.beforeTask(taskExecution);
Collection<StepExecution> results = handler.handle(this.splitter, masterStepExecution);
Collection<StepExecution> results = handler.handle(this.splitter,
masterStepExecution);
verify(this.taskLauncher, times(3)).launch(this.appDeploymentRequestArgumentCaptor.capture());
verify(this.taskLauncher, times(3))
.launch(this.appDeploymentRequestArgumentCaptor.capture());
List<AppDeploymentRequest> allValues = this.appDeploymentRequestArgumentCaptor.getAllValues();
List<AppDeploymentRequest> allValues = this.appDeploymentRequestArgumentCaptor
.getAllValues();
validateAppDeploymentRequests(allValues, 3);
@@ -350,15 +407,19 @@ public class DeployerPartitionHandlerTests {
JobExecution jobExecution = masterStepExecution.getJobExecution();
StepExecution workerStepExecutionStart1 = getStepExecutionStart(jobExecution, 4L);
StepExecution workerStepExecutionFinish1 = getStepExecutionFinish(workerStepExecutionStart1, BatchStatus.COMPLETED);
StepExecution workerStepExecutionFinish1 = getStepExecutionFinish(
workerStepExecutionStart1, BatchStatus.COMPLETED);
StepExecution workerStepExecutionStart2 = getStepExecutionStart(jobExecution, 5L);
StepExecution workerStepExecutionFinish2 = getStepExecutionFinish(workerStepExecutionStart2, BatchStatus.FAILED);
StepExecution workerStepExecutionFinish2 = getStepExecutionFinish(
workerStepExecutionStart2, BatchStatus.FAILED);
StepExecution workerStepExecutionStart3 = getStepExecutionStart(jobExecution, 6L);
StepExecution workerStepExecutionFinish3 = getStepExecutionFinish(workerStepExecutionStart3, BatchStatus.COMPLETED);
StepExecution workerStepExecutionFinish3 = getStepExecutionFinish(
workerStepExecutionStart3, BatchStatus.COMPLETED);
DeployerPartitionHandler handler = new DeployerPartitionHandler(this.taskLauncher, this.jobExplorer, this.resource, "step1");
DeployerPartitionHandler handler = new DeployerPartitionHandler(this.taskLauncher,
this.jobExplorer, this.resource, "step1");
handler.setEnvironment(this.environment);
handler.setMaxWorkers(2);
@@ -373,18 +434,24 @@ public class DeployerPartitionHandlerTests {
when(this.splitter.split(masterStepExecution, 1)).thenReturn(stepExecutions);
when(this.jobExplorer.getStepExecution(1L, 4L)).thenReturn(workerStepExecutionFinish1);
when(this.jobExplorer.getStepExecution(1L, 5L)).thenReturn(workerStepExecutionFinish2);
when(this.jobExplorer.getStepExecution(1L, 6L)).thenReturn(workerStepExecutionFinish3);
when(this.jobExplorer.getStepExecution(1L, 4L))
.thenReturn(workerStepExecutionFinish1);
when(this.jobExplorer.getStepExecution(1L, 5L))
.thenReturn(workerStepExecutionFinish2);
when(this.jobExplorer.getStepExecution(1L, 6L))
.thenReturn(workerStepExecutionFinish3);
handler.afterPropertiesSet();
handler.beforeTask(taskExecution);
Collection<StepExecution> results = handler.handle(this.splitter, masterStepExecution);
Collection<StepExecution> results = handler.handle(this.splitter,
masterStepExecution);
verify(this.taskLauncher, times(3)).launch(this.appDeploymentRequestArgumentCaptor.capture());
verify(this.taskLauncher, times(3))
.launch(this.appDeploymentRequestArgumentCaptor.capture());
List<AppDeploymentRequest> allValues = this.appDeploymentRequestArgumentCaptor.getAllValues();
List<AppDeploymentRequest> allValues = this.appDeploymentRequestArgumentCaptor
.getAllValues();
validateAppDeploymentRequests(allValues, 3);
@@ -395,13 +462,13 @@ public class DeployerPartitionHandlerTests {
StepExecution curResult = resultsIterator.next();
if (curResult.getStepName().equals("step1:partition2")) {
assertEquals(BatchStatus.FAILED, curResult.getStatus());
assertThat(curResult.getStatus()).isEqualTo(BatchStatus.FAILED);
}
else {
assertEquals(BatchStatus.COMPLETED, curResult.getStatus());
assertThat(curResult.getStatus()).isEqualTo(BatchStatus.COMPLETED);
}
assertTrue(!names.contains(curResult.getStepName()));
assertThat(!names.contains(curResult.getStepName())).isTrue();
names.add(curResult.getStepName());
}
}
@@ -413,15 +480,18 @@ public class DeployerPartitionHandlerTests {
JobExecution jobExecution = masterStepExecution.getJobExecution();
StepExecution workerStepExecutionStart = getStepExecutionStart(jobExecution, 4L);
StepExecution workerStepExecutionFinish = getStepExecutionFinish(workerStepExecutionStart, BatchStatus.COMPLETED);
StepExecution workerStepExecutionFinish = getStepExecutionFinish(
workerStepExecutionStart, BatchStatus.COMPLETED);
DeployerPartitionHandler handler = new DeployerPartitionHandler(this.taskLauncher, this.jobExplorer, this.resource, "step1");
DeployerPartitionHandler handler = new DeployerPartitionHandler(this.taskLauncher,
this.jobExplorer, this.resource, "step1");
Map<String, String> environmentParameters = new HashMap<>(2);
environmentParameters.put("foo", "bar");
environmentParameters.put("baz", "qux");
SimpleEnvironmentVariablesProvider environmentVariablesProvider = new SimpleEnvironmentVariablesProvider(this.environment);
SimpleEnvironmentVariablesProvider environmentVariablesProvider = new SimpleEnvironmentVariablesProvider(
this.environment);
environmentVariablesProvider.setEnvironmentProperties(environmentParameters);
handler.setEnvironmentVariablesProvider(environmentVariablesProvider);
@@ -432,33 +502,41 @@ public class DeployerPartitionHandlerTests {
stepExecutions.add(workerStepExecutionStart);
when(this.splitter.split(masterStepExecution, 1)).thenReturn(stepExecutions);
when(this.jobExplorer.getStepExecution(1L, 4L)).thenReturn(workerStepExecutionFinish);
when(this.jobExplorer.getStepExecution(1L, 4L))
.thenReturn(workerStepExecutionFinish);
handler.afterPropertiesSet();
handler.beforeTask(taskExecution);
Collection<StepExecution> results = handler.handle(this.splitter, masterStepExecution);
Collection<StepExecution> results = handler.handle(this.splitter,
masterStepExecution);
verify(this.taskLauncher).launch(this.appDeploymentRequestArgumentCaptor.capture());
verify(this.taskLauncher)
.launch(this.appDeploymentRequestArgumentCaptor.capture());
AppDeploymentRequest request = this.appDeploymentRequestArgumentCaptor.getValue();
assertEquals(this.resource, request.getResource());
assertEquals(2, request.getDefinition().getProperties().size());
assertEquals("bar", request.getDefinition().getProperties().get("foo"));
assertEquals("qux", request.getDefinition().getProperties().get("baz"));
assertThat(request.getResource()).isEqualTo(this.resource);
assertThat(request.getDefinition().getProperties().size()).isEqualTo(2);
assertThat(request.getDefinition().getProperties().get("foo")).isEqualTo("bar");
assertThat(request.getDefinition().getProperties().get("baz")).isEqualTo("qux");
AppDefinition appDefinition = request.getDefinition();
assertEquals("partitionedJobTask", appDefinition.getName());
assertTrue(request.getCommandlineArguments().contains(formatArgs(DeployerPartitionHandler.SPRING_CLOUD_TASK_JOB_EXECUTION_ID, "1")));
assertTrue(request.getCommandlineArguments().contains(formatArgs(DeployerPartitionHandler.SPRING_CLOUD_TASK_STEP_EXECUTION_ID, "4")));
assertTrue(request.getCommandlineArguments().contains(formatArgs(DeployerPartitionHandler.SPRING_CLOUD_TASK_STEP_NAME, "step1")));
assertThat(appDefinition.getName()).isEqualTo("partitionedJobTask");
assertThat(request.getCommandlineArguments().contains(formatArgs(
DeployerPartitionHandler.SPRING_CLOUD_TASK_JOB_EXECUTION_ID, "1")))
.isTrue();
assertThat(request.getCommandlineArguments().contains(formatArgs(
DeployerPartitionHandler.SPRING_CLOUD_TASK_STEP_EXECUTION_ID, "4")))
.isTrue();
assertThat(request.getCommandlineArguments().contains(formatArgs(
DeployerPartitionHandler.SPRING_CLOUD_TASK_STEP_NAME, "step1"))).isTrue();
assertEquals(1, results.size());
assertThat(results.size()).isEqualTo(1);
StepExecution resultStepExecution = results.iterator().next();
assertEquals(BatchStatus.COMPLETED, resultStepExecution.getStatus());
assertEquals("step1:partition1", resultStepExecution.getStepName());
assertThat(resultStepExecution.getStatus()).isEqualTo(BatchStatus.COMPLETED);
assertThat(resultStepExecution.getStepName()).isEqualTo("step1:partition1");
}
@Test
@@ -471,16 +549,19 @@ public class DeployerPartitionHandlerTests {
JobExecution jobExecution = masterStepExecution.getJobExecution();
StepExecution workerStepExecutionStart = getStepExecutionStart(jobExecution, 4L);
StepExecution workerStepExecutionFinish = getStepExecutionFinish(workerStepExecutionStart, BatchStatus.COMPLETED);
StepExecution workerStepExecutionFinish = getStepExecutionFinish(
workerStepExecutionStart, BatchStatus.COMPLETED);
DeployerPartitionHandler handler = new DeployerPartitionHandler(this.taskLauncher, this.jobExplorer, this.resource, "step1");
DeployerPartitionHandler handler = new DeployerPartitionHandler(this.taskLauncher,
this.jobExplorer, this.resource, "step1");
handler.setEnvironment(this.environment);
Map<String, String> environmentParameters = new HashMap<>(2);
environmentParameters.put("foo", "bar");
environmentParameters.put("baz", "qux");
SimpleEnvironmentVariablesProvider environmentVariablesProvider = new SimpleEnvironmentVariablesProvider(this.environment);
SimpleEnvironmentVariablesProvider environmentVariablesProvider = new SimpleEnvironmentVariablesProvider(
this.environment);
environmentVariablesProvider.setEnvironmentProperties(environmentParameters);
handler.setEnvironmentVariablesProvider(environmentVariablesProvider);
@@ -491,34 +572,43 @@ public class DeployerPartitionHandlerTests {
stepExecutions.add(workerStepExecutionStart);
when(this.splitter.split(masterStepExecution, 1)).thenReturn(stepExecutions);
when(this.jobExplorer.getStepExecution(1L, 4L)).thenReturn(workerStepExecutionFinish);
when(this.jobExplorer.getStepExecution(1L, 4L))
.thenReturn(workerStepExecutionFinish);
handler.afterPropertiesSet();
handler.beforeTask(taskExecution);
Collection<StepExecution> results = handler.handle(this.splitter, masterStepExecution);
Collection<StepExecution> results = handler.handle(this.splitter,
masterStepExecution);
verify(this.taskLauncher).launch(this.appDeploymentRequestArgumentCaptor.capture());
verify(this.taskLauncher)
.launch(this.appDeploymentRequestArgumentCaptor.capture());
AppDeploymentRequest request = this.appDeploymentRequestArgumentCaptor.getValue();
assertEquals(this.resource, request.getResource());
assertEquals(3, request.getDefinition().getProperties().size());
assertEquals("bar", request.getDefinition().getProperties().get("foo"));
assertEquals("qux", request.getDefinition().getProperties().get("baz"));
assertEquals("batch", request.getDefinition().getProperties().get("task"));
assertThat(request.getResource()).isEqualTo(this.resource);
assertThat(request.getDefinition().getProperties().size()).isEqualTo(3);
assertThat(request.getDefinition().getProperties().get("foo")).isEqualTo("bar");
assertThat(request.getDefinition().getProperties().get("baz")).isEqualTo("qux");
assertThat(request.getDefinition().getProperties().get("task"))
.isEqualTo("batch");
AppDefinition appDefinition = request.getDefinition();
assertEquals("partitionedJobTask", appDefinition.getName());
assertTrue(request.getCommandlineArguments().contains(formatArgs(DeployerPartitionHandler.SPRING_CLOUD_TASK_JOB_EXECUTION_ID, "1")));
assertTrue(request.getCommandlineArguments().contains(formatArgs(DeployerPartitionHandler.SPRING_CLOUD_TASK_STEP_EXECUTION_ID, "4")));
assertTrue(request.getCommandlineArguments().contains(formatArgs(DeployerPartitionHandler.SPRING_CLOUD_TASK_STEP_NAME, "step1")));
assertThat(appDefinition.getName()).isEqualTo("partitionedJobTask");
assertThat(request.getCommandlineArguments().contains(formatArgs(
DeployerPartitionHandler.SPRING_CLOUD_TASK_JOB_EXECUTION_ID, "1")))
.isTrue();
assertThat(request.getCommandlineArguments().contains(formatArgs(
DeployerPartitionHandler.SPRING_CLOUD_TASK_STEP_EXECUTION_ID, "4")))
.isTrue();
assertThat(request.getCommandlineArguments().contains(formatArgs(
DeployerPartitionHandler.SPRING_CLOUD_TASK_STEP_NAME, "step1"))).isTrue();
assertEquals(1, results.size());
assertThat(results.size()).isEqualTo(1);
StepExecution resultStepExecution = results.iterator().next();
assertEquals(BatchStatus.COMPLETED, resultStepExecution.getStatus());
assertEquals("step1:partition1", resultStepExecution.getStepName());
assertThat(resultStepExecution.getStatus()).isEqualTo(BatchStatus.COMPLETED);
assertThat(resultStepExecution.getStepName()).isEqualTo("step1:partition1");
}
@Test
@@ -528,12 +618,15 @@ public class DeployerPartitionHandlerTests {
JobExecution jobExecution = masterStepExecution.getJobExecution();
StepExecution workerStepExecutionStart1 = getStepExecutionStart(jobExecution, 4L);
StepExecution workerStepExecutionFinish1 = getStepExecutionFinish(workerStepExecutionStart1, BatchStatus.COMPLETED);
StepExecution workerStepExecutionFinish1 = getStepExecutionFinish(
workerStepExecutionStart1, BatchStatus.COMPLETED);
StepExecution workerStepExecutionStart2 = getStepExecutionStart(jobExecution, 5L);
StepExecution workerStepExecutionFinish2 = getStepExecutionFinish(workerStepExecutionStart2, BatchStatus.COMPLETED);
StepExecution workerStepExecutionFinish2 = getStepExecutionFinish(
workerStepExecutionStart2, BatchStatus.COMPLETED);
DeployerPartitionHandler handler = new DeployerPartitionHandler(this.taskLauncher, this.jobExplorer, this.resource, "step1");
DeployerPartitionHandler handler = new DeployerPartitionHandler(this.taskLauncher,
this.jobExplorer, this.resource, "step1");
handler.setEnvironment(this.environment);
handler.setPollInterval(20000L);
@@ -547,26 +640,33 @@ public class DeployerPartitionHandlerTests {
stepExecutions.add(workerStepExecutionStart2);
when(this.splitter.split(masterStepExecution, 1)).thenReturn(stepExecutions);
when(this.jobExplorer.getStepExecution(1L, 4L)).thenReturn(workerStepExecutionFinish1);
when(this.jobExplorer.getStepExecution(1L, 5L)).thenReturn(workerStepExecutionFinish2);
when(this.jobExplorer.getStepExecution(1L, 4L))
.thenReturn(workerStepExecutionFinish1);
when(this.jobExplorer.getStepExecution(1L, 5L))
.thenReturn(workerStepExecutionFinish2);
handler.afterPropertiesSet();
handler.beforeTask(taskExecution);
Date startTime = new Date();
Collection<StepExecution> results = handler.handle(this.splitter, masterStepExecution);
Collection<StepExecution> results = handler.handle(this.splitter,
masterStepExecution);
Date endTime = new Date();
verify(this.taskLauncher, times(2)).launch(this.appDeploymentRequestArgumentCaptor.capture());
verify(this.taskLauncher, times(2))
.launch(this.appDeploymentRequestArgumentCaptor.capture());
List<AppDeploymentRequest> allRequests = this.appDeploymentRequestArgumentCaptor.getAllValues();
List<AppDeploymentRequest> allRequests = this.appDeploymentRequestArgumentCaptor
.getAllValues();
validateAppDeploymentRequests(allRequests, 2);
validateStepExecutionResults(results);
assertTrue("Time difference was too small: " + (endTime.getTime() - startTime.getTime()),
endTime.getTime() - startTime.getTime() >= 19999);
assertThat(endTime.getTime() - startTime.getTime() >= 19999)
.as("Time difference was too small: "
+ (endTime.getTime() - startTime.getTime()))
.isTrue();
}
@Test(expected = TimeoutException.class)
@@ -576,12 +676,15 @@ public class DeployerPartitionHandlerTests {
JobExecution jobExecution = masterStepExecution.getJobExecution();
StepExecution workerStepExecutionStart1 = getStepExecutionStart(jobExecution, 4L);
StepExecution workerStepExecutionFinish1 = getStepExecutionFinish(workerStepExecutionStart1, BatchStatus.COMPLETED);
StepExecution workerStepExecutionFinish1 = getStepExecutionFinish(
workerStepExecutionStart1, BatchStatus.COMPLETED);
StepExecution workerStepExecutionStart2 = getStepExecutionStart(jobExecution, 5L);
StepExecution workerStepExecutionFinish2 = getStepExecutionFinish(workerStepExecutionStart2, BatchStatus.COMPLETED);
StepExecution workerStepExecutionFinish2 = getStepExecutionFinish(
workerStepExecutionStart2, BatchStatus.COMPLETED);
DeployerPartitionHandler handler = new DeployerPartitionHandler(this.taskLauncher, this.jobExplorer, this.resource, "step1");
DeployerPartitionHandler handler = new DeployerPartitionHandler(this.taskLauncher,
this.jobExplorer, this.resource, "step1");
handler.setEnvironment(this.environment);
handler.setPollInterval(20000L);
@@ -596,8 +699,10 @@ public class DeployerPartitionHandlerTests {
stepExecutions.add(workerStepExecutionStart2);
when(this.splitter.split(masterStepExecution, 1)).thenReturn(stepExecutions);
when(this.jobExplorer.getStepExecution(1L, 4L)).thenReturn(workerStepExecutionFinish1);
when(this.jobExplorer.getStepExecution(1L, 5L)).thenReturn(workerStepExecutionFinish2);
when(this.jobExplorer.getStepExecution(1L, 4L))
.thenReturn(workerStepExecutionFinish1);
when(this.jobExplorer.getStepExecution(1L, 5L))
.thenReturn(workerStepExecutionFinish2);
handler.afterPropertiesSet();
@@ -613,12 +718,15 @@ public class DeployerPartitionHandlerTests {
JobExecution jobExecution = masterStepExecution.getJobExecution();
StepExecution workerStepExecutionStart1 = getStepExecutionStart(jobExecution, 4L);
StepExecution workerStepExecutionFinish1 = getStepExecutionFinish(workerStepExecutionStart1, BatchStatus.COMPLETED);
StepExecution workerStepExecutionFinish1 = getStepExecutionFinish(
workerStepExecutionStart1, BatchStatus.COMPLETED);
StepExecution workerStepExecutionStart2 = getStepExecutionStart(jobExecution, 5L);
StepExecution workerStepExecutionFinish2 = getStepExecutionFinish(workerStepExecutionStart2, BatchStatus.COMPLETED);
StepExecution workerStepExecutionFinish2 = getStepExecutionFinish(
workerStepExecutionStart2, BatchStatus.COMPLETED);
DeployerPartitionHandler handler = new DeployerPartitionHandler(this.taskLauncher, this.jobExplorer, this.resource, "step1");
DeployerPartitionHandler handler = new DeployerPartitionHandler(this.taskLauncher,
this.jobExplorer, this.resource, "step1");
handler.setEnvironment(this.environment);
handler.setGridSize(2);
@@ -631,18 +739,23 @@ public class DeployerPartitionHandlerTests {
stepExecutions.add(workerStepExecutionStart2);
when(this.splitter.split(masterStepExecution, 2)).thenReturn(stepExecutions);
when(this.jobExplorer.getStepExecution(1L, 4L)).thenReturn(workerStepExecutionFinish1);
when(this.jobExplorer.getStepExecution(1L, 5L)).thenReturn(workerStepExecutionFinish2);
when(this.jobExplorer.getStepExecution(1L, 4L))
.thenReturn(workerStepExecutionFinish1);
when(this.jobExplorer.getStepExecution(1L, 5L))
.thenReturn(workerStepExecutionFinish2);
handler.afterPropertiesSet();
handler.beforeTask(taskExecution);
Collection<StepExecution> results = handler.handle(this.splitter, masterStepExecution);
Collection<StepExecution> results = handler.handle(this.splitter,
masterStepExecution);
verify(this.taskLauncher, times(2)).launch(this.appDeploymentRequestArgumentCaptor.capture());
verify(this.taskLauncher, times(2))
.launch(this.appDeploymentRequestArgumentCaptor.capture());
List<AppDeploymentRequest> allRequests = this.appDeploymentRequestArgumentCaptor.getAllValues();
List<AppDeploymentRequest> allRequests = this.appDeploymentRequestArgumentCaptor
.getAllValues();
validateAppDeploymentRequests(allRequests, 2);
@@ -656,9 +769,11 @@ public class DeployerPartitionHandlerTests {
JobExecution jobExecution = masterStepExecution.getJobExecution();
StepExecution workerStepExecutionStart = getStepExecutionStart(jobExecution, 4L);
StepExecution workerStepExecutionFinish = getStepExecutionFinish(workerStepExecutionStart, BatchStatus.COMPLETED);
StepExecution workerStepExecutionFinish = getStepExecutionFinish(
workerStepExecutionStart, BatchStatus.COMPLETED);
DeployerPartitionHandler handler = new DeployerPartitionHandler(this.taskLauncher, this.jobExplorer, this.resource, "step1");
DeployerPartitionHandler handler = new DeployerPartitionHandler(this.taskLauncher,
this.jobExplorer, this.resource, "step1");
handler.setEnvironment(this.environment);
Map<String, String> deploymentProperties = new HashMap<>(2);
@@ -674,48 +789,59 @@ public class DeployerPartitionHandlerTests {
stepExecutions.add(workerStepExecutionStart);
when(this.splitter.split(masterStepExecution, 1)).thenReturn(stepExecutions);
when(this.jobExplorer.getStepExecution(1L, 4L)).thenReturn(workerStepExecutionFinish);
when(this.jobExplorer.getStepExecution(1L, 4L))
.thenReturn(workerStepExecutionFinish);
handler.afterPropertiesSet();
handler.beforeTask(taskExecution);
Collection<StepExecution> results = handler.handle(this.splitter, masterStepExecution);
Collection<StepExecution> results = handler.handle(this.splitter,
masterStepExecution);
verify(this.taskLauncher).launch(this.appDeploymentRequestArgumentCaptor.capture());
verify(this.taskLauncher)
.launch(this.appDeploymentRequestArgumentCaptor.capture());
AppDeploymentRequest request = this.appDeploymentRequestArgumentCaptor.getValue();
assertEquals(this.resource, request.getResource());
assertEquals(2, request.getDeploymentProperties().size());
assertEquals("bar", request.getDeploymentProperties().get("foo"));
assertEquals("qux", request.getDeploymentProperties().get("baz"));
assertThat(request.getResource()).isEqualTo(this.resource);
assertThat(request.getDeploymentProperties().size()).isEqualTo(2);
assertThat(request.getDeploymentProperties().get("foo")).isEqualTo("bar");
assertThat(request.getDeploymentProperties().get("baz")).isEqualTo("qux");
AppDefinition appDefinition = request.getDefinition();
assertEquals("partitionedJobTask", appDefinition.getName());
assertTrue(request.getCommandlineArguments().contains(formatArgs(DeployerPartitionHandler.SPRING_CLOUD_TASK_JOB_EXECUTION_ID, "1")));
assertTrue(request.getCommandlineArguments().contains(formatArgs(DeployerPartitionHandler.SPRING_CLOUD_TASK_STEP_EXECUTION_ID, "4")));
assertTrue(request.getCommandlineArguments().contains(formatArgs(DeployerPartitionHandler.SPRING_CLOUD_TASK_STEP_NAME, "step1")));
assertThat(appDefinition.getName()).isEqualTo("partitionedJobTask");
assertThat(request.getCommandlineArguments().contains(formatArgs(
DeployerPartitionHandler.SPRING_CLOUD_TASK_JOB_EXECUTION_ID, "1")))
.isTrue();
assertThat(request.getCommandlineArguments().contains(formatArgs(
DeployerPartitionHandler.SPRING_CLOUD_TASK_STEP_EXECUTION_ID, "4")))
.isTrue();
assertThat(request.getCommandlineArguments().contains(formatArgs(
DeployerPartitionHandler.SPRING_CLOUD_TASK_STEP_NAME, "step1"))).isTrue();
assertEquals(1, results.size());
assertThat(results.size()).isEqualTo(1);
StepExecution resultStepExecution = results.iterator().next();
assertEquals(BatchStatus.COMPLETED, resultStepExecution.getStatus());
assertEquals("step1:partition1", resultStepExecution.getStepName());
assertThat(resultStepExecution.getStatus()).isEqualTo(BatchStatus.COMPLETED);
assertThat(resultStepExecution.getStepName()).isEqualTo("step1:partition1");
}
private String formatArgs(String key, String value) {
return String.format("--%s=%s", key, value);
}
private StepExecution getStepExecutionFinish(StepExecution stepExecutionStart, BatchStatus status) {
StepExecution workerStepExecutionFinish = new StepExecution(stepExecutionStart.getStepName(), stepExecutionStart.getJobExecution());
private StepExecution getStepExecutionFinish(StepExecution stepExecutionStart,
BatchStatus status) {
StepExecution workerStepExecutionFinish = new StepExecution(
stepExecutionStart.getStepName(), stepExecutionStart.getJobExecution());
workerStepExecutionFinish.setId(stepExecutionStart.getId());
workerStepExecutionFinish.setStatus(status);
return workerStepExecutionFinish;
}
private StepExecution getStepExecutionStart(JobExecution jobExecution, long id) {
StepExecution workerStepExecutionStart = new StepExecution("step1:partition" + (id - 3), jobExecution);
StepExecution workerStepExecutionStart = new StepExecution(
"step1:partition" + (id - 3), jobExecution);
workerStepExecutionStart.setId(id);
return workerStepExecutionStart;
}
@@ -738,14 +864,15 @@ public class DeployerPartitionHandlerTests {
while (resultsIterator.hasNext()) {
StepExecution curResult = resultsIterator.next();
assertEquals(BatchStatus.COMPLETED, curResult.getStatus());
assertThat(curResult.getStatus()).isEqualTo(BatchStatus.COMPLETED);
assertTrue(!names.contains(curResult.getStepName()));
assertThat(!names.contains(curResult.getStepName())).isTrue();
names.add(curResult.getStepName());
}
}
private void validateAppDeploymentRequests(List<AppDeploymentRequest> allRequests, int numberOfPartitions) {
private void validateAppDeploymentRequests(List<AppDeploymentRequest> allRequests,
int numberOfPartitions) {
Collections.sort(allRequests, new Comparator<AppDeploymentRequest>() {
@Override
public int compare(AppDeploymentRequest o1, AppDeploymentRequest o2) {
@@ -753,7 +880,8 @@ public class DeployerPartitionHandlerTests {
String o1Command = "";
for (String commandlineArgument : commandlineArguments) {
if(commandlineArgument.contains(DeployerPartitionHandler.SPRING_CLOUD_TASK_STEP_EXECUTION_ID)) {
if (commandlineArgument.contains(
DeployerPartitionHandler.SPRING_CLOUD_TASK_STEP_EXECUTION_ID)) {
o1Command = commandlineArgument;
break;
}
@@ -763,7 +891,8 @@ public class DeployerPartitionHandlerTests {
String o2Command = "";
for (String commandlineArgument : commandlineArguments) {
if(commandlineArgument.contains(DeployerPartitionHandler.SPRING_CLOUD_TASK_STEP_EXECUTION_ID)) {
if (commandlineArgument.contains(
DeployerPartitionHandler.SPRING_CLOUD_TASK_STEP_EXECUTION_ID)) {
o2Command = commandlineArgument;
break;
}
@@ -775,23 +904,33 @@ public class DeployerPartitionHandlerTests {
for (int i = 4; i < (numberOfPartitions + 4); i++) {
AppDeploymentRequest request = allRequests.get(i - 4);
assertEquals(this.resource, request.getResource());
assertEquals(0, request.getDeploymentProperties().size());
assertThat(request.getResource()).isEqualTo(this.resource);
assertThat(request.getDeploymentProperties().size()).isEqualTo(0);
AppDefinition appDefinition = request.getDefinition();
assertEquals("partitionedJobTask", appDefinition.getName());
assertTrue(request.getCommandlineArguments().contains(formatArgs(DeployerPartitionHandler.SPRING_CLOUD_TASK_JOB_EXECUTION_ID, "1")));
assertTrue(request.getCommandlineArguments().contains(formatArgs(DeployerPartitionHandler.SPRING_CLOUD_TASK_STEP_EXECUTION_ID, String.valueOf(i))));
assertTrue(request.getCommandlineArguments().contains(formatArgs(DeployerPartitionHandler.SPRING_CLOUD_TASK_STEP_NAME, "step1")));
assertThat(appDefinition.getName()).isEqualTo("partitionedJobTask");
assertThat(request.getCommandlineArguments().contains(formatArgs(
DeployerPartitionHandler.SPRING_CLOUD_TASK_JOB_EXECUTION_ID, "1")))
.isTrue();
assertThat(request.getCommandlineArguments()
.contains(formatArgs(
DeployerPartitionHandler.SPRING_CLOUD_TASK_STEP_EXECUTION_ID,
String.valueOf(i)))).isTrue();
assertThat(request.getCommandlineArguments().contains(formatArgs(
DeployerPartitionHandler.SPRING_CLOUD_TASK_STEP_NAME, "step1")))
.isTrue();
}
}
private void validateConstructorValidation(TaskLauncher taskLauncher, JobExplorer jobExplorer, Resource resource, String stepName, String expectedMessage) {
private void validateConstructorValidation(TaskLauncher taskLauncher,
JobExplorer jobExplorer, Resource resource, String stepName,
String expectedMessage) {
try {
new DeployerPartitionHandler(taskLauncher, jobExplorer, resource, stepName);
}
catch (IllegalArgumentException iae) {
assertEquals(expectedMessage, iae.getMessage());
assertThat(iae.getMessage()).isEqualTo(expectedMessage);
}
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2016 the original author or authors.
* Copyright 2015-2019 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.
@@ -13,6 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.cloud.task.batch.partition;
import org.junit.Before;
@@ -35,7 +36,7 @@ import org.springframework.beans.factory.ListableBeanFactory;
import org.springframework.core.env.Environment;
import org.springframework.test.util.ReflectionTestUtils;
import static org.junit.Assert.assertEquals;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyZeroInteractions;
@@ -70,7 +71,8 @@ public class DeployerStepExecutionHandlerTests {
public void setUp() {
MockitoAnnotations.initMocks(this);
this.handler = new DeployerStepExecutionHandler(this.beanFactory, this.jobExplorer, this.jobRepository);
this.handler = new DeployerStepExecutionHandler(this.beanFactory,
this.jobExplorer, this.jobRepository);
ReflectionTestUtils.setField(this.handler, "environment", this.environment);
}
@@ -78,69 +80,116 @@ public class DeployerStepExecutionHandlerTests {
@Test
public void testConstructorValidation() {
validateConstructorValidation(null, null, null, "A beanFactory is required");
validateConstructorValidation(this.beanFactory, null, null, "A jobExplorer is required");
validateConstructorValidation(this.beanFactory, this.jobExplorer, null, "A jobRepository is required");
validateConstructorValidation(this.beanFactory, null, null,
"A jobExplorer is required");
validateConstructorValidation(this.beanFactory, this.jobExplorer, null,
"A jobRepository is required");
new DeployerStepExecutionHandler(this.beanFactory, this.jobExplorer, this.jobRepository);
new DeployerStepExecutionHandler(this.beanFactory, this.jobExplorer,
this.jobRepository);
}
@Test
public void testValidationOfRequestValuesExist() throws Exception {
validateEnvironmentConfiguration("A job execution id is required", new String[0]);
validateEnvironmentConfiguration("A step execution id is required", new String[] {DeployerPartitionHandler.SPRING_CLOUD_TASK_JOB_EXECUTION_ID});
validateEnvironmentConfiguration("A step name is required", new String[] {DeployerPartitionHandler.SPRING_CLOUD_TASK_JOB_EXECUTION_ID, DeployerPartitionHandler.SPRING_CLOUD_TASK_STEP_EXECUTION_ID});
validateEnvironmentConfiguration("A step execution id is required", new String[] {
DeployerPartitionHandler.SPRING_CLOUD_TASK_JOB_EXECUTION_ID });
validateEnvironmentConfiguration("A step name is required",
new String[] {
DeployerPartitionHandler.SPRING_CLOUD_TASK_JOB_EXECUTION_ID,
DeployerPartitionHandler.SPRING_CLOUD_TASK_STEP_EXECUTION_ID });
}
@Test
public void testValidationOfRequestStepFound() throws Exception {
when(this.environment.containsProperty(DeployerPartitionHandler.SPRING_CLOUD_TASK_JOB_EXECUTION_ID)).thenReturn(true);
when(this.environment.containsProperty(DeployerPartitionHandler.SPRING_CLOUD_TASK_STEP_EXECUTION_ID)).thenReturn(true);
when(this.environment.containsProperty(DeployerPartitionHandler.SPRING_CLOUD_TASK_STEP_NAME)).thenReturn(true);
when(this.environment.getProperty(DeployerPartitionHandler.SPRING_CLOUD_TASK_STEP_NAME)).thenReturn("foo");
when(this.beanFactory.getBeanNamesForType(Step.class)).thenReturn(new String[] {"bar", "baz"});
when(this.environment.containsProperty(
DeployerPartitionHandler.SPRING_CLOUD_TASK_JOB_EXECUTION_ID))
.thenReturn(true);
when(this.environment.containsProperty(
DeployerPartitionHandler.SPRING_CLOUD_TASK_STEP_EXECUTION_ID))
.thenReturn(true);
when(this.environment
.containsProperty(DeployerPartitionHandler.SPRING_CLOUD_TASK_STEP_NAME))
.thenReturn(true);
when(this.environment
.getProperty(DeployerPartitionHandler.SPRING_CLOUD_TASK_STEP_NAME))
.thenReturn("foo");
when(this.beanFactory.getBeanNamesForType(Step.class))
.thenReturn(new String[] { "bar", "baz" });
try {
this.handler.run();
}
catch (IllegalArgumentException iae) {
assertEquals("The step requested cannot be found in the provided BeanFactory", iae.getMessage());
assertThat(iae.getMessage()).isEqualTo(
"The step requested cannot be found in the provided BeanFactory");
}
}
@Test
public void testMissingStepExecution() throws Exception {
when(this.environment.containsProperty(DeployerPartitionHandler.SPRING_CLOUD_TASK_JOB_EXECUTION_ID)).thenReturn(true);
when(this.environment.containsProperty(DeployerPartitionHandler.SPRING_CLOUD_TASK_STEP_EXECUTION_ID)).thenReturn(true);
when(this.environment.containsProperty(DeployerPartitionHandler.SPRING_CLOUD_TASK_STEP_NAME)).thenReturn(true);
when(this.environment.getProperty(DeployerPartitionHandler.SPRING_CLOUD_TASK_STEP_NAME)).thenReturn("foo");
when(this.beanFactory.getBeanNamesForType(Step.class)).thenReturn(new String[] {"foo", "bar", "baz"});
when(this.environment.getProperty(DeployerPartitionHandler.SPRING_CLOUD_TASK_STEP_EXECUTION_ID)).thenReturn("2");
when(this.environment.getProperty(DeployerPartitionHandler.SPRING_CLOUD_TASK_JOB_EXECUTION_ID)).thenReturn("1");
when(this.environment.containsProperty(
DeployerPartitionHandler.SPRING_CLOUD_TASK_JOB_EXECUTION_ID))
.thenReturn(true);
when(this.environment.containsProperty(
DeployerPartitionHandler.SPRING_CLOUD_TASK_STEP_EXECUTION_ID))
.thenReturn(true);
when(this.environment
.containsProperty(DeployerPartitionHandler.SPRING_CLOUD_TASK_STEP_NAME))
.thenReturn(true);
when(this.environment
.getProperty(DeployerPartitionHandler.SPRING_CLOUD_TASK_STEP_NAME))
.thenReturn("foo");
when(this.beanFactory.getBeanNamesForType(Step.class))
.thenReturn(new String[] { "foo", "bar", "baz" });
when(this.environment.getProperty(
DeployerPartitionHandler.SPRING_CLOUD_TASK_STEP_EXECUTION_ID))
.thenReturn("2");
when(this.environment
.getProperty(DeployerPartitionHandler.SPRING_CLOUD_TASK_JOB_EXECUTION_ID))
.thenReturn("1");
try {
this.handler.run();
}
catch (NoSuchStepException nsse) {
assertEquals("No StepExecution could be located for step execution id 2 within job execution 1", nsse.getMessage());
assertThat(nsse.getMessage()).isEqualTo(
"No StepExecution could be located for step execution id 2 within job execution 1");
}
}
@Test
public void testRunSuccessful() throws Exception {
StepExecution workerStep = new StepExecution("workerStep", new JobExecution(1L), 2L);
StepExecution workerStep = new StepExecution("workerStep", new JobExecution(1L),
2L);
when(this.environment.containsProperty(DeployerPartitionHandler.SPRING_CLOUD_TASK_JOB_EXECUTION_ID)).thenReturn(true);
when(this.environment.containsProperty(DeployerPartitionHandler.SPRING_CLOUD_TASK_STEP_EXECUTION_ID)).thenReturn(true);
when(this.environment.containsProperty(DeployerPartitionHandler.SPRING_CLOUD_TASK_STEP_NAME)).thenReturn(true);
when(this.environment.getProperty(DeployerPartitionHandler.SPRING_CLOUD_TASK_STEP_NAME)).thenReturn("workerStep");
when(this.beanFactory.getBeanNamesForType(Step.class)).thenReturn(new String[] {"workerStep", "foo", "bar"});
when(this.environment.getProperty(DeployerPartitionHandler.SPRING_CLOUD_TASK_STEP_EXECUTION_ID)).thenReturn("2");
when(this.environment.getProperty(DeployerPartitionHandler.SPRING_CLOUD_TASK_JOB_EXECUTION_ID)).thenReturn("1");
when(this.environment.containsProperty(
DeployerPartitionHandler.SPRING_CLOUD_TASK_JOB_EXECUTION_ID))
.thenReturn(true);
when(this.environment.containsProperty(
DeployerPartitionHandler.SPRING_CLOUD_TASK_STEP_EXECUTION_ID))
.thenReturn(true);
when(this.environment
.containsProperty(DeployerPartitionHandler.SPRING_CLOUD_TASK_STEP_NAME))
.thenReturn(true);
when(this.environment
.getProperty(DeployerPartitionHandler.SPRING_CLOUD_TASK_STEP_NAME))
.thenReturn("workerStep");
when(this.beanFactory.getBeanNamesForType(Step.class))
.thenReturn(new String[] { "workerStep", "foo", "bar" });
when(this.environment.getProperty(
DeployerPartitionHandler.SPRING_CLOUD_TASK_STEP_EXECUTION_ID))
.thenReturn("2");
when(this.environment
.getProperty(DeployerPartitionHandler.SPRING_CLOUD_TASK_JOB_EXECUTION_ID))
.thenReturn("1");
when(this.jobExplorer.getStepExecution(1L, 2L)).thenReturn(workerStep);
when(this.environment.getProperty(DeployerPartitionHandler.SPRING_CLOUD_TASK_STEP_NAME)).thenReturn("workerStep");
when(this.environment
.getProperty(DeployerPartitionHandler.SPRING_CLOUD_TASK_STEP_NAME))
.thenReturn("workerStep");
when(this.beanFactory.getBean("workerStep", Step.class)).thenReturn(this.step);
handler.run();
this.handler.run();
verify(this.step).execute(workerStep);
verifyZeroInteractions(this.jobRepository);
@@ -148,51 +197,87 @@ public class DeployerStepExecutionHandlerTests {
@Test
public void testJobInterruptedException() throws Exception {
StepExecution workerStep = new StepExecution("workerStep", new JobExecution(1L), 2L);
StepExecution workerStep = new StepExecution("workerStep", new JobExecution(1L),
2L);
when(this.environment.containsProperty(DeployerPartitionHandler.SPRING_CLOUD_TASK_JOB_EXECUTION_ID)).thenReturn(true);
when(this.environment.containsProperty(DeployerPartitionHandler.SPRING_CLOUD_TASK_STEP_EXECUTION_ID)).thenReturn(true);
when(this.environment.containsProperty(DeployerPartitionHandler.SPRING_CLOUD_TASK_STEP_NAME)).thenReturn(true);
when(this.environment.getProperty(DeployerPartitionHandler.SPRING_CLOUD_TASK_STEP_NAME)).thenReturn("workerStep");
when(this.beanFactory.getBeanNamesForType(Step.class)).thenReturn(new String[] {"workerStep", "foo", "bar"});
when(this.environment.getProperty(DeployerPartitionHandler.SPRING_CLOUD_TASK_STEP_EXECUTION_ID)).thenReturn("2");
when(this.environment.getProperty(DeployerPartitionHandler.SPRING_CLOUD_TASK_JOB_EXECUTION_ID)).thenReturn("1");
when(this.environment.containsProperty(
DeployerPartitionHandler.SPRING_CLOUD_TASK_JOB_EXECUTION_ID))
.thenReturn(true);
when(this.environment.containsProperty(
DeployerPartitionHandler.SPRING_CLOUD_TASK_STEP_EXECUTION_ID))
.thenReturn(true);
when(this.environment
.containsProperty(DeployerPartitionHandler.SPRING_CLOUD_TASK_STEP_NAME))
.thenReturn(true);
when(this.environment
.getProperty(DeployerPartitionHandler.SPRING_CLOUD_TASK_STEP_NAME))
.thenReturn("workerStep");
when(this.beanFactory.getBeanNamesForType(Step.class))
.thenReturn(new String[] { "workerStep", "foo", "bar" });
when(this.environment.getProperty(
DeployerPartitionHandler.SPRING_CLOUD_TASK_STEP_EXECUTION_ID))
.thenReturn("2");
when(this.environment
.getProperty(DeployerPartitionHandler.SPRING_CLOUD_TASK_JOB_EXECUTION_ID))
.thenReturn("1");
when(this.jobExplorer.getStepExecution(1L, 2L)).thenReturn(workerStep);
when(this.environment.getProperty(DeployerPartitionHandler.SPRING_CLOUD_TASK_STEP_NAME)).thenReturn("workerStep");
when(this.environment
.getProperty(DeployerPartitionHandler.SPRING_CLOUD_TASK_STEP_NAME))
.thenReturn("workerStep");
when(this.beanFactory.getBean("workerStep", Step.class)).thenReturn(this.step);
doThrow(new JobInterruptedException("expected")).when(this.step).execute(workerStep);
doThrow(new JobInterruptedException("expected")).when(this.step)
.execute(workerStep);
handler.run();
this.handler.run();
verify(this.jobRepository).update(this.stepExecutionArgumentCaptor.capture());
assertEquals(BatchStatus.STOPPED, this.stepExecutionArgumentCaptor.getValue().getStatus());
assertThat(this.stepExecutionArgumentCaptor.getValue().getStatus())
.isEqualTo(BatchStatus.STOPPED);
}
@Test
public void testRuntimeException() throws Exception {
StepExecution workerStep = new StepExecution("workerStep", new JobExecution(1L), 2L);
StepExecution workerStep = new StepExecution("workerStep", new JobExecution(1L),
2L);
when(this.environment.containsProperty(DeployerPartitionHandler.SPRING_CLOUD_TASK_JOB_EXECUTION_ID)).thenReturn(true);
when(this.environment.containsProperty(DeployerPartitionHandler.SPRING_CLOUD_TASK_STEP_EXECUTION_ID)).thenReturn(true);
when(this.environment.containsProperty(DeployerPartitionHandler.SPRING_CLOUD_TASK_STEP_NAME)).thenReturn(true);
when(this.environment.getProperty(DeployerPartitionHandler.SPRING_CLOUD_TASK_STEP_NAME)).thenReturn("workerStep");
when(this.beanFactory.getBeanNamesForType(Step.class)).thenReturn(new String[] {"workerStep", "foo", "bar"});
when(this.environment.getProperty(DeployerPartitionHandler.SPRING_CLOUD_TASK_STEP_EXECUTION_ID)).thenReturn("2");
when(this.environment.getProperty(DeployerPartitionHandler.SPRING_CLOUD_TASK_JOB_EXECUTION_ID)).thenReturn("1");
when(this.environment.containsProperty(
DeployerPartitionHandler.SPRING_CLOUD_TASK_JOB_EXECUTION_ID))
.thenReturn(true);
when(this.environment.containsProperty(
DeployerPartitionHandler.SPRING_CLOUD_TASK_STEP_EXECUTION_ID))
.thenReturn(true);
when(this.environment
.containsProperty(DeployerPartitionHandler.SPRING_CLOUD_TASK_STEP_NAME))
.thenReturn(true);
when(this.environment
.getProperty(DeployerPartitionHandler.SPRING_CLOUD_TASK_STEP_NAME))
.thenReturn("workerStep");
when(this.beanFactory.getBeanNamesForType(Step.class))
.thenReturn(new String[] { "workerStep", "foo", "bar" });
when(this.environment.getProperty(
DeployerPartitionHandler.SPRING_CLOUD_TASK_STEP_EXECUTION_ID))
.thenReturn("2");
when(this.environment
.getProperty(DeployerPartitionHandler.SPRING_CLOUD_TASK_JOB_EXECUTION_ID))
.thenReturn("1");
when(this.jobExplorer.getStepExecution(1L, 2L)).thenReturn(workerStep);
when(this.environment.getProperty(DeployerPartitionHandler.SPRING_CLOUD_TASK_STEP_NAME)).thenReturn("workerStep");
when(this.environment
.getProperty(DeployerPartitionHandler.SPRING_CLOUD_TASK_STEP_NAME))
.thenReturn("workerStep");
when(this.beanFactory.getBean("workerStep", Step.class)).thenReturn(this.step);
doThrow(new RuntimeException("expected")).when(this.step).execute(workerStep);
handler.run();
this.handler.run();
verify(this.jobRepository).update(this.stepExecutionArgumentCaptor.capture());
assertEquals(BatchStatus.FAILED, this.stepExecutionArgumentCaptor.getValue().getStatus());
assertThat(this.stepExecutionArgumentCaptor.getValue().getStatus())
.isEqualTo(BatchStatus.FAILED);
}
private void validateEnvironmentConfiguration(String errorMessage, String[] properties) throws Exception {
private void validateEnvironmentConfiguration(String errorMessage,
String[] properties) throws Exception {
for (String property : properties) {
when(this.environment.containsProperty(property)).thenReturn(true);
@@ -202,17 +287,18 @@ public class DeployerStepExecutionHandlerTests {
this.handler.run();
}
catch (IllegalArgumentException iae) {
assertEquals(errorMessage, iae.getMessage());
assertThat(iae.getMessage()).isEqualTo(errorMessage);
}
}
private void validateConstructorValidation(BeanFactory beanFactory, JobExplorer jobExplorer, JobRepository jobRepository, String message) {
private void validateConstructorValidation(BeanFactory beanFactory,
JobExplorer jobExplorer, JobRepository jobRepository, String message) {
try {
new DeployerStepExecutionHandler(beanFactory, jobExplorer, jobRepository);
}
catch (IllegalArgumentException iae) {
assertEquals(message, iae.getMessage());
assertThat(iae.getMessage()).isEqualTo(message);
}
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2016 the original author or authors.
* Copyright 2015-2019 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.
@@ -13,13 +13,15 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.cloud.task.batch.partition;
import java.util.Map;
import org.junit.Before;
import org.junit.Test;
import static org.junit.Assert.*;
import static org.assertj.core.api.Assertions.assertThat;
/**
* @author Michael Minella
@@ -35,11 +37,14 @@ public class NoOpEnvironmentVariablesProviderTests {
@Test
public void test() {
Map<String, String> environmentVariables = this.provider.getEnvironmentVariables(null);
assertNotNull(environmentVariables);
assertTrue(environmentVariables.isEmpty());
Map<String, String> environmentVariables = this.provider
.getEnvironmentVariables(null);
assertThat(environmentVariables).isNotNull();
assertThat(environmentVariables.isEmpty()).isTrue();
Map<String, String> environmentVariables2 = this.provider.getEnvironmentVariables(null);
assertTrue(environmentVariables == environmentVariables2);
Map<String, String> environmentVariables2 = this.provider
.getEnvironmentVariables(null);
assertThat(environmentVariables == environmentVariables2).isTrue();
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2016 the original author or authors.
* Copyright 2015-2019 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.
@@ -13,13 +13,15 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.cloud.task.batch.partition;
import java.util.Arrays;
import java.util.List;
import org.junit.Test;
import static org.junit.Assert.*;
import static org.assertj.core.api.Assertions.assertThat;
/**
* @author Michael Minella
@@ -39,8 +41,9 @@ public class PassThroughCommandLineArgsProviderTests {
List<String> commandLineArgs = provider.getCommandLineArgs(null);
assertEquals("foo", commandLineArgs.get(0));
assertEquals("bar", commandLineArgs.get(1));
assertEquals("baz", commandLineArgs.get(2));
assertThat(commandLineArgs.get(0)).isEqualTo("foo");
assertThat(commandLineArgs.get(1)).isEqualTo("bar");
assertThat(commandLineArgs.get(2)).isEqualTo("baz");
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2016-2018 the original author or authors.
* Copyright 2015-2019 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.
@@ -13,6 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.cloud.task.batch.partition;
import java.util.ArrayList;
@@ -23,7 +24,7 @@ import org.junit.Test;
import org.springframework.cloud.task.repository.TaskExecution;
import static org.junit.Assert.assertEquals;
import static org.assertj.core.api.Assertions.assertThat;
/**
* @author Michael Minella
@@ -35,13 +36,14 @@ public class SimpleCommandLineArgsProviderTests {
TaskExecution taskExecution = new TaskExecution();
taskExecution.setArguments(Arrays.asList("foo", "bar", "baz"));
SimpleCommandLineArgsProvider provider = new SimpleCommandLineArgsProvider(taskExecution);
SimpleCommandLineArgsProvider provider = new SimpleCommandLineArgsProvider(
taskExecution);
List<String> commandLineArgs = provider.getCommandLineArgs(null);
assertEquals("foo", commandLineArgs.get(0));
assertEquals("bar", commandLineArgs.get(1));
assertEquals("baz", commandLineArgs.get(2));
assertThat(commandLineArgs.get(0)).isEqualTo("foo");
assertThat(commandLineArgs.get(1)).isEqualTo("bar");
assertThat(commandLineArgs.get(2)).isEqualTo("baz");
}
@Test
@@ -54,17 +56,18 @@ public class SimpleCommandLineArgsProviderTests {
TaskExecution taskExecution = new TaskExecution();
taskExecution.setArguments(Arrays.asList("foo", "bar", "baz"));
SimpleCommandLineArgsProvider provider = new SimpleCommandLineArgsProvider(taskExecution);
SimpleCommandLineArgsProvider provider = new SimpleCommandLineArgsProvider(
taskExecution);
provider.setAppendedArgs(appendedValues);
List<String> commandLineArgs = provider.getCommandLineArgs(null);
assertEquals("foo", commandLineArgs.get(0));
assertEquals("bar", commandLineArgs.get(1));
assertEquals("baz", commandLineArgs.get(2));
assertEquals("one", commandLineArgs.get(3));
assertEquals("two", commandLineArgs.get(4));
assertEquals("three", commandLineArgs.get(5));
assertThat(commandLineArgs.get(0)).isEqualTo("foo");
assertThat(commandLineArgs.get(1)).isEqualTo("bar");
assertThat(commandLineArgs.get(2)).isEqualTo("baz");
assertThat(commandLineArgs.get(3)).isEqualTo("one");
assertThat(commandLineArgs.get(4)).isEqualTo("two");
assertThat(commandLineArgs.get(5)).isEqualTo("three");
}
@Test
@@ -73,14 +76,16 @@ public class SimpleCommandLineArgsProviderTests {
TaskExecution taskExecution = new TaskExecution();
taskExecution.setArguments(Arrays.asList("foo", "bar", "baz"));
SimpleCommandLineArgsProvider provider = new SimpleCommandLineArgsProvider(taskExecution);
SimpleCommandLineArgsProvider provider = new SimpleCommandLineArgsProvider(
taskExecution);
provider.setAppendedArgs(null);
List<String> commandLineArgs = provider.getCommandLineArgs(null);
assertEquals(3, commandLineArgs.size());
assertEquals("foo", commandLineArgs.get(0));
assertEquals("bar", commandLineArgs.get(1));
assertEquals("baz", commandLineArgs.get(2));
assertThat(commandLineArgs.size()).isEqualTo(3);
assertThat(commandLineArgs.get(0)).isEqualTo("foo");
assertThat(commandLineArgs.get(1)).isEqualTo("bar");
assertThat(commandLineArgs.get(2)).isEqualTo("baz");
}
}