Skip to content

[BATCH-2760] Failed JobExecution due to unavailable TaskExecutor leaves End Time unpopulated #643

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@
import org.springframework.core.task.TaskRejectedException;
import org.springframework.util.Assert;

import java.util.Date;

/**
* Simple implementation of the {@link JobLauncher} interface. The Spring Core
* {@link TaskExecutor} interface is used to launch a {@link Job}. This means
Expand Down Expand Up @@ -163,6 +165,8 @@ else if (t instanceof Error) {
}
catch (TaskRejectedException e) {
jobExecution.upgradeStatus(BatchStatus.FAILED);
jobExecution.setStartTime(new Date(System.currentTimeMillis()));
jobExecution.setEndTime(new Date(System.currentTimeMillis()));
if (jobExecution.getExitStatus().equals(ExitStatus.UNKNOWN)) {
jobExecution.setExitStatus(ExitStatus.FAILED.addExitDescription(e));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,16 @@
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

import org.junit.Before;
Expand All @@ -46,6 +51,7 @@
import org.springframework.batch.core.repository.JobRestartException;
import org.springframework.core.task.TaskExecutor;
import org.springframework.core.task.TaskRejectedException;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

/**
* @author Lucas Ward
Expand Down Expand Up @@ -265,6 +271,29 @@ private void run(ExitStatus exitStatus) throws Exception {
}
}

//BATCH-2760
@Test
public void testEndTimePopulatedWhenTaskExecutorExecuteFails() throws Exception {
//Given
ThreadPoolTaskExecutor mockThreadPoolTaskExecutor = mock(ThreadPoolTaskExecutor.class);
doThrow(new TaskRejectedException("submit rejected")).when(mockThreadPoolTaskExecutor).execute(any(Runnable.class));

JobExecution mockJobExecution = mock(JobExecution.class);
when(mockJobExecution.getExitStatus()).thenReturn(ExitStatus.UNKNOWN);

when(jobRepository.createJobExecution(anyString(), any())).thenReturn(mockJobExecution);

jobLauncher.setTaskExecutor(mockThreadPoolTaskExecutor);

//When
JobExecution jobExecution = jobLauncher.run(job, jobParameters);

//Then
verify(jobExecution).setStartTime(any(Date.class));
verify(jobExecution).setEndTime(any(Date.class));

}

private boolean contains(String str, String searchStr) {
return str.indexOf(searchStr) != -1;
}
Expand Down