Skip to content

Commit 30ce8f9

Browse files
committed
Simplify monitoring of job executions
1 parent a298679 commit 30ce8f9

File tree

2 files changed

+37
-49
lines changed

2 files changed

+37
-49
lines changed

spring-cloud-task-batch/src/main/java/org/springframework/cloud/task/batch/handler/TaskJobLauncherApplicationRunner.java

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2020-2020 the original author or authors.
2+
* Copyright 2020-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -70,17 +70,17 @@ public class TaskJobLauncherApplicationRunner extends JobLauncherApplicationRunn
7070

7171
private static final Log logger = LogFactory.getLog(TaskJobLauncherApplicationRunner.class);
7272

73-
private JobLauncher taskJobLauncher;
73+
private final JobLauncher taskJobLauncher;
7474

75-
private JobExplorer taskJobExplorer;
75+
private final JobExplorer taskJobExplorer;
7676

77-
private JobRepository taskJobRepository;
77+
private final JobRepository taskJobRepository;
7878

79-
private List<JobExecution> jobExecutionList = new ArrayList<>();
79+
private final List<JobExecution> jobExecutionList = new ArrayList<>();
8080

8181
private ApplicationEventPublisher taskApplicationEventPublisher;
8282

83-
private TaskBatchProperties taskBatchProperties;
83+
private final TaskBatchProperties taskBatchProperties;
8484

8585
/**
8686
* Create a new {@link TaskJobLauncherApplicationRunner}.
@@ -100,6 +100,7 @@ public TaskJobLauncherApplicationRunner(JobLauncher jobLauncher, JobExplorer job
100100
this.taskBatchProperties = taskBatchProperties;
101101
}
102102

103+
@Override
103104
public void setApplicationEventPublisher(ApplicationEventPublisher publisher) {
104105
super.setApplicationEventPublisher(publisher);
105106
this.taskApplicationEventPublisher = publisher;
@@ -112,6 +113,7 @@ public void run(String... args) throws JobExecutionException {
112113
monitorJobExecutions();
113114
}
114115

116+
@Override
115117
protected void execute(Job job, JobParameters jobParameters) throws JobExecutionAlreadyRunningException,
116118
JobRestartException, JobInstanceAlreadyCompleteException, JobParametersInvalidException {
117119
String jobName = job.getName();
@@ -158,26 +160,31 @@ private void monitorJobExecutions() {
158160
template.iterate(context -> {
159161

160162
List<JobExecution> failedJobExecutions = new ArrayList<>();
161-
RepeatStatus repeatStatus = RepeatStatus.FINISHED;
162163
for (JobExecution jobExecution : this.jobExecutionList) {
163-
JobExecution currentJobExecution = this.taskJobExplorer.getJobExecution(jobExecution.getId());
164-
BatchStatus batchStatus = currentJobExecution.getStatus();
164+
BatchStatus batchStatus = getCurrentBatchStatus(jobExecution);
165165
if (batchStatus.isRunning()) {
166-
repeatStatus = RepeatStatus.CONTINUABLE;
166+
Thread.sleep(this.taskBatchProperties.getFailOnJobFailurePollInterval());
167+
return RepeatStatus.CONTINUABLE;
167168
}
168169
if (batchStatus.equals(BatchStatus.FAILED)) {
169170
failedJobExecutions.add(jobExecution);
170171
}
171172
}
172-
Thread.sleep(this.taskBatchProperties.getFailOnJobFailurePollInterval());
173173

174-
if (repeatStatus.equals(RepeatStatus.FINISHED) && failedJobExecutions.size() > 0) {
174+
if (failedJobExecutions.size() > 0) {
175175
throwJobFailedException(failedJobExecutions);
176176
}
177-
return repeatStatus;
177+
return RepeatStatus.FINISHED;
178178
});
179179
}
180180

181+
private BatchStatus getCurrentBatchStatus(JobExecution jobExecution) {
182+
if (jobExecution.getStatus().isRunning()) {
183+
return this.taskJobExplorer.getJobExecution(jobExecution.getId()).getStatus();
184+
}
185+
return jobExecution.getStatus();
186+
}
187+
181188
private void throwJobFailedException(List<JobExecution> failedJobExecutions) {
182189
StringBuilder message = new StringBuilder("The following Jobs have failed: \n");
183190
for (JobExecution failedJobExecution : failedJobExecutions) {

spring-cloud-task-batch/src/test/java/org/springframework/cloud/task/batch/handler/TaskJobLauncherApplicationRunnerTests.java

Lines changed: 17 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2018-2019 the original author or authors.
2+
* Copyright 2018-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -21,20 +21,16 @@
2121

2222
import javax.sql.DataSource;
2323

24-
import org.assertj.core.api.Condition;
2524
import org.junit.jupiter.api.AfterEach;
2625
import org.junit.jupiter.api.Test;
2726
import org.junit.jupiter.api.function.Executable;
2827

2928
import org.springframework.batch.core.Job;
30-
import org.springframework.batch.core.StepContribution;
3129
import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
3230
import org.springframework.batch.core.explore.JobExplorer;
3331
import org.springframework.batch.core.job.builder.JobBuilder;
3432
import org.springframework.batch.core.repository.JobRepository;
35-
import org.springframework.batch.core.scope.context.ChunkContext;
3633
import org.springframework.batch.core.step.builder.StepBuilder;
37-
import org.springframework.batch.core.step.tasklet.Tasklet;
3834
import org.springframework.batch.repeat.RepeatStatus;
3935
import org.springframework.batch.support.transaction.ResourcelessTransactionManager;
4036
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
@@ -165,17 +161,12 @@ private void validateContext() {
165161
assertThat(taskExplorer.getTaskExecution(jobExecutionIds.iterator().next()).getExecutionId()).isEqualTo(1);
166162
}
167163

168-
private void validateForFail(String errorMessage, Class clazz, String[] enabledArgs) {
164+
private void validateForFail(String errorMessage, Class<?> clazz, String[] enabledArgs) {
169165
Executable executable = () -> this.applicationContext = SpringApplication
170166
.run(new Class[] { clazz, PropertyPlaceholderAutoConfiguration.class }, enabledArgs);
171167

172-
assertThatExceptionOfType(IllegalStateException.class).isThrownBy(executable::execute)
173-
.has(new Condition<Throwable>() {
174-
@Override
175-
public boolean matches(Throwable value) {
176-
return errorMessage.equals(value.getCause().getMessage());
177-
}
178-
});
168+
assertThatExceptionOfType(IllegalStateException.class).isThrownBy(executable::execute).havingCause()
169+
.withMessage(errorMessage);
179170
}
180171

181172
@TaskBatchTest
@@ -186,12 +177,9 @@ public static class JobConfiguration {
186177
@Bean
187178
public Job job(JobRepository jobRepository, PlatformTransactionManager transactionManager) {
188179
return new JobBuilder("job", jobRepository)
189-
.start(new StepBuilder("step1", jobRepository).tasklet(new Tasklet() {
190-
@Override
191-
public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) {
192-
System.out.println("Executed");
193-
return RepeatStatus.FINISHED;
194-
}
180+
.start(new StepBuilder("step1", jobRepository).tasklet((contribution, chunkContext) -> {
181+
System.out.println("Executed");
182+
return RepeatStatus.FINISHED;
195183
}, transactionManager).build()).build();
196184
}
197185

@@ -240,27 +228,20 @@ public static class JobWithFailureConfiguration {
240228

241229
@Bean
242230
public Job jobFail() {
243-
return new JobBuilder("jobA").repository(this.jobRepository)
244-
.start(new StepBuilder("step1").repository(this.jobRepository).tasklet(new Tasklet() {
245-
@Override
246-
public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext)
247-
throws Exception {
248-
System.out.println("Executed");
249-
throw new IllegalStateException("WHOOPS");
250-
}
251-
}).transactionManager(transactionManager).build()).build();
231+
return new JobBuilder("jobA", this.jobRepository)
232+
.start(new StepBuilder("step1", this.jobRepository).tasklet((contribution, chunkContext) -> {
233+
System.out.println("Executed");
234+
throw new IllegalStateException("WHOOPS");
235+
}, transactionManager).build()).build();
252236
}
253237

254238
@Bean
255239
public Job jobFun() {
256-
return new JobBuilder("jobSucceed").repository(this.jobRepository)
257-
.start(new StepBuilder("step1Succeed").repository(this.jobRepository).tasklet(new Tasklet() {
258-
@Override
259-
public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) {
260-
System.out.println("Executed");
261-
return RepeatStatus.FINISHED;
262-
}
263-
}).transactionManager(transactionManager).build()).build();
240+
return new JobBuilder("jobSucceed", this.jobRepository)
241+
.start(new StepBuilder("step1Succeed", this.jobRepository).tasklet((contribution, chunkContext) -> {
242+
System.out.println("Executed");
243+
return RepeatStatus.FINISHED;
244+
}, transactionManager).build()).build();
264245
}
265246

266247
}

0 commit comments

Comments
 (0)