org.springframework.batch.core.UnexpectedJobExecutionException Java Examples

The following examples show how to use org.springframework.batch.core.UnexpectedJobExecutionException. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: FileDeletingTasklet.java    From spring-batch with MIT License 5 votes vote down vote up
@Override
public RepeatStatus execute(StepContribution stepContribution,
    ChunkContext chunkContext) {
  try (Stream<Path> walk =
      Files.walk(Paths.get(directory.getFile().getPath()))) {
    walk.filter(Files::isRegularFile).map(Path::toFile)
        .forEach(File::delete);
  } catch (IOException e) {
    LOGGER.error("error deleting files", e);
    throw new UnexpectedJobExecutionException(
        "unable to delete files");
  }

  return RepeatStatus.FINISHED;
}
 
Example #2
Source File: TaskLauncherTaskletTests.java    From composed-task-runner with Apache License 2.0 5 votes vote down vote up
@Test
@DirtiesContext
public void testTaskLauncherTaskletFailure() {
	mockReturnValForTaskExecution(1L);
	TaskLauncherTasklet taskLauncherTasklet = getTaskExecutionTasklet();
	ChunkContext chunkContext = chunkContext();
	createCompleteTaskExecution(1);
	Throwable exception = assertThrows(UnexpectedJobExecutionException.class,
			() -> execute(taskLauncherTasklet, null, chunkContext));
	Assertions.assertThat(exception.getMessage()).isEqualTo("Task returned a non zero exit code.");
}
 
Example #3
Source File: TaskLauncherTaskletTests.java    From composed-task-runner with Apache License 2.0 5 votes vote down vote up
@Test
@DirtiesContext
public void testTaskLauncherTaskletNullResult() throws Exception {
	boolean isException = false;
	mockReturnValForTaskExecution(1L);
	TaskLauncherTasklet taskLauncherTasklet = getTaskExecutionTasklet();
	ChunkContext chunkContext = chunkContext();
	getCompleteTaskExecutionWithNull();
	Throwable exception = assertThrows(UnexpectedJobExecutionException.class,
			() -> execute(taskLauncherTasklet, null, chunkContext));
	Assertions.assertThat(exception.getMessage()).isEqualTo("Task returned a null exit code.");
}
 
Example #4
Source File: TaskLauncherTaskletTests.java    From spring-cloud-dataflow with Apache License 2.0 5 votes vote down vote up
@Test
@DirtiesContext
public void testTaskLauncherTaskletFailure() {
	mockReturnValForTaskExecution(1L);
	TaskLauncherTasklet taskLauncherTasklet = getTaskExecutionTasklet();
	ChunkContext chunkContext = chunkContext();
	createCompleteTaskExecution(1);
	Throwable exception = assertThrows(UnexpectedJobExecutionException.class,
			() -> execute(taskLauncherTasklet, null, chunkContext));
	Assertions.assertThat(exception.getMessage()).isEqualTo("Task returned a non zero exit code.");
}
 
Example #5
Source File: TaskLauncherTaskletTests.java    From spring-cloud-dataflow with Apache License 2.0 5 votes vote down vote up
@Test
@DirtiesContext
public void testTaskLauncherTaskletNullResult() throws Exception {
	boolean isException = false;
	mockReturnValForTaskExecution(1L);
	TaskLauncherTasklet taskLauncherTasklet = getTaskExecutionTasklet();
	ChunkContext chunkContext = chunkContext();
	getCompleteTaskExecutionWithNull();
	Throwable exception = assertThrows(UnexpectedJobExecutionException.class,
			() -> execute(taskLauncherTasklet, null, chunkContext));
	Assertions.assertThat(exception.getMessage()).isEqualTo("Task returned a null exit code.");
}
 
Example #6
Source File: JobOperationsController.java    From spring-boot-starter-batch-web with Apache License 2.0 5 votes vote down vote up
@ResponseStatus(HttpStatus.CONFLICT)
@ExceptionHandler({ UnexpectedJobExecutionException.class, JobInstanceAlreadyExistsException.class,
		JobInstanceAlreadyCompleteException.class })
public String handleAlreadyExists(Exception ex) {
	LOG.warn("JobInstance or JobExecution already exists.", ex);
	return ex.getMessage();
}
 
Example #7
Source File: TaskLauncherTasklet.java    From composed-task-runner with Apache License 2.0 4 votes vote down vote up
/**
 * Executes the task as specified by the taskName with the associated
 * properties and arguments.
 *
 * @param contribution mutable state to be passed back to update the current step execution
 * @param chunkContext contains the task-execution-id used by the listener.
 * @return Repeat status of FINISHED.
 */
@Override
public RepeatStatus execute(StepContribution contribution,
		ChunkContext chunkContext) {
	if (this.executionId == null) {
		this.timeout = System.currentTimeMillis() +
				this.composedTaskProperties.getMaxWaitTime();
		logger.debug("Wait time for this task to complete is " +
				this.composedTaskProperties.getMaxWaitTime());
		logger.debug("Interval check time for this task to complete is " +
				this.composedTaskProperties.getIntervalTimeBetweenChecks());

		String tmpTaskName = this.taskName.substring(0,
				this.taskName.lastIndexOf('_'));

		List<String> args = this.arguments;

		ExecutionContext stepExecutionContext = chunkContext.getStepContext().getStepExecution().
				getExecutionContext();
		if (stepExecutionContext.containsKey("task-arguments")) {
			args = (List<String>) stepExecutionContext.get("task-arguments");
		}
		if(this.taskProperties.getExecutionid() != null) {
			args.add("--spring.cloud.task.parent-execution-id=" + this.taskProperties.getExecutionid());
		}
		this.executionId = this.taskOperations.launch(tmpTaskName,
				this.properties, args, null);

		stepExecutionContext.put("task-execution-id", executionId);
		stepExecutionContext.put("task-arguments", args);
	}
	else {
		try {
			Thread.sleep(this.composedTaskProperties.getIntervalTimeBetweenChecks());
		}
		catch (InterruptedException e) {
			Thread.currentThread().interrupt();
			throw new IllegalStateException(e.getMessage(), e);
		}

		TaskExecution taskExecution =
				this.taskExplorer.getTaskExecution(this.executionId);
		if (taskExecution != null && taskExecution.getEndTime() != null) {
			if (taskExecution.getExitCode() == null) {
				throw new UnexpectedJobExecutionException("Task returned a null exit code.");
			}
			else if (taskExecution.getExitCode() != 0) {
				throw new UnexpectedJobExecutionException("Task returned a non zero exit code.");
			}
			else {
				return RepeatStatus.FINISHED;
			}
		}
		if (this.composedTaskProperties.getMaxWaitTime() > 0 &&
				System.currentTimeMillis() > timeout) {
			throw new TaskExecutionTimeoutException(String.format(
					"Timeout occurred while processing task with Execution Id %s",
					this.executionId));
		}
	}
	return RepeatStatus.CONTINUABLE;
}
 
Example #8
Source File: TaskLauncherTasklet.java    From spring-cloud-dataflow with Apache License 2.0 4 votes vote down vote up
/**
 * Executes the task as specified by the taskName with the associated
 * properties and arguments.
 *
 * @param contribution mutable state to be passed back to update the current step execution
 * @param chunkContext contains the task-execution-id used by the listener.
 * @return Repeat status of FINISHED.
 */
@Override
public RepeatStatus execute(StepContribution contribution,
		ChunkContext chunkContext) {
	if (this.executionId == null) {
		this.timeout = System.currentTimeMillis() +
				this.composedTaskProperties.getMaxWaitTime();
		logger.debug("Wait time for this task to complete is " +
				this.composedTaskProperties.getMaxWaitTime());
		logger.debug("Interval check time for this task to complete is " +
				this.composedTaskProperties.getIntervalTimeBetweenChecks());

		String tmpTaskName = this.taskName.substring(0,
				this.taskName.lastIndexOf('_'));

		List<String> args = this.arguments;

		ExecutionContext stepExecutionContext = chunkContext.getStepContext().getStepExecution().
				getExecutionContext();
		if (stepExecutionContext.containsKey("task-arguments")) {
			args = (List<String>) stepExecutionContext.get("task-arguments");
		}
		List<String> cleansedArgs = new ArrayList<>();
		if(args != null) {
			for(String argument : args) {
				if(!argument.startsWith("--spring.cloud.task.parent-execution-id=")) {
					cleansedArgs.add(argument);
				}
			}
			args = cleansedArgs;
		}
		if(this.taskProperties.getExecutionid() != null) {
			args.add("--spring.cloud.task.parent-execution-id=" + this.taskProperties.getExecutionid());
		}
		if(StringUtils.hasText(this.composedTaskProperties.getPlatformName())) {
			properties.put("spring.cloud.dataflow.task.platformName", this.composedTaskProperties.getPlatformName());
		}
		this.executionId = this.taskOperations.launch(tmpTaskName,
				this.properties, args, null);

		stepExecutionContext.put("task-execution-id", executionId);
		stepExecutionContext.put("task-arguments", args);
	}
	else {
		try {
			Thread.sleep(this.composedTaskProperties.getIntervalTimeBetweenChecks());
		}
		catch (InterruptedException e) {
			Thread.currentThread().interrupt();
			throw new IllegalStateException(e.getMessage(), e);
		}

		TaskExecution taskExecution =
				this.taskExplorer.getTaskExecution(this.executionId);
		if (taskExecution != null && taskExecution.getEndTime() != null) {
			if (taskExecution.getExitCode() == null) {
				throw new UnexpectedJobExecutionException("Task returned a null exit code.");
			}
			else if (taskExecution.getExitCode() != 0) {
				throw new UnexpectedJobExecutionException("Task returned a non zero exit code.");
			}
			else {
				return RepeatStatus.FINISHED;
			}
		}
		if (this.composedTaskProperties.getMaxWaitTime() > 0 &&
				System.currentTimeMillis() > timeout) {
			throw new TaskExecutionTimeoutException(String.format(
					"Timeout occurred while processing task with Execution Id %s",
					this.executionId));
		}
	}
	return RepeatStatus.CONTINUABLE;
}