Java Code Examples for org.springframework.batch.core.JobExecution#getJobInstance()

The following examples show how to use org.springframework.batch.core.JobExecution#getJobInstance() . 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: SpringBatchRetryIntegrationTest.java    From tutorials with MIT License 6 votes vote down vote up
@Test
public void whenEndpointFailsTwicePasses3rdTime_thenSuccess() throws Exception {
    FileSystemResource expectedResult = new FileSystemResource(EXPECTED_OUTPUT);
    FileSystemResource actualResult = new FileSystemResource(TEST_OUTPUT);

    //fails for first two calls and passes third time onwards
    when(httpResponse.getEntity())
      .thenReturn(new StringEntity("{ \"age\":10, \"postCode\":\"430222\" }"));
    when(closeableHttpClient.execute(any()))
      .thenThrow(new ConnectTimeoutException("Timeout count 1"))
      .thenThrow(new ConnectTimeoutException("Timeout count 2"))
      .thenReturn(httpResponse);

    JobExecution jobExecution = jobLauncherTestUtils.launchJob(defaultJobParameters());
    JobInstance actualJobInstance = jobExecution.getJobInstance();
    ExitStatus actualJobExitStatus = jobExecution.getExitStatus();

    assertThat(actualJobInstance.getJobName(), is("retryBatchJob"));
    assertThat(actualJobExitStatus.getExitCode(), is("COMPLETED"));
    AssertFile.assertFileEquals(expectedResult, actualResult);
}
 
Example 2
Source File: SpringBatchIntegrationTest.java    From tutorials with MIT License 6 votes vote down vote up
@Test
public void givenReferenceOutput_whenJobExecuted_thenSuccess() throws Exception {
    // given
    FileSystemResource expectedResult = new FileSystemResource(EXPECTED_OUTPUT);
    FileSystemResource actualResult = new FileSystemResource(TEST_OUTPUT);

    // when
    JobExecution jobExecution = jobLauncherTestUtils.launchJob(defaultJobParameters());
    JobInstance actualJobInstance = jobExecution.getJobInstance();
    ExitStatus actualJobExitStatus = jobExecution.getExitStatus();

    // then
    assertThat(actualJobInstance.getJobName(), is("transformBooksRecords"));
    assertThat(actualJobExitStatus.getExitCode(), is("COMPLETED"));
    AssertFile.assertFileEquals(expectedResult, actualResult);
}
 
Example 3
Source File: JobExecutionThinResource.java    From spring-cloud-dataflow with Apache License 2.0 5 votes vote down vote up
public JobExecutionThinResource(TaskJobExecution taskJobExecution, TimeZone timeZone) {
	Assert.notNull(taskJobExecution, "taskJobExecution must not be null");
	this.taskExecutionId = taskJobExecution.getTaskId();
	JobExecution jobExecution = taskJobExecution.getJobExecution();
	this.timeZone = timeZone;
	this.executionId = jobExecution.getId();
	this.jobId = jobExecution.getJobId();
	this.stepExecutionCount = taskJobExecution.getStepExecutionCount();
	this.jobParameters =converter.getProperties(jobExecution.getJobParameters());
	this.jobParametersString = fromJobParameters(
			this.argumentSanitizer.sanitizeJobParameters(jobExecution.getJobParameters()));
	this.defined = taskJobExecution.isTaskDefined();
	JobInstance jobInstance = jobExecution.getJobInstance();
	this.status = taskJobExecution.getJobExecution().getStatus();
	if (jobInstance != null) {
		this.name = jobInstance.getJobName();
		this.restartable = JobUtils.isJobExecutionRestartable(jobExecution);
		this.abandonable = JobUtils.isJobExecutionAbandonable(jobExecution);
		this.stoppable = JobUtils.isJobExecutionStoppable(jobExecution);
		this.instanceId = jobExecution.getJobInstance().getInstanceId();
	}
	else {
		this.name = "?";
	}

	// Duration is always in GMT
	durationFormat.setTimeZone(TimeUtils.getDefaultTimeZone());
	// The others can be localized
	timeFormat.setTimeZone(timeZone);
	dateFormat.setTimeZone(timeZone);
	if (jobExecution.getStartTime() != null) {
		this.startDate = dateFormat.format(jobExecution.getStartTime());
		this.startTime = timeFormat.format(jobExecution.getStartTime());
		Date endTime = jobExecution.getEndTime() != null ? jobExecution.getEndTime() : new Date();
		this.duration = durationFormat.format(new Date(endTime.getTime() - jobExecution.getStartTime().getTime()));
		this.startDateTime = jobExecution.getStartTime();
	}

}
 
Example 4
Source File: SimpleJobService.java    From spring-cloud-dataflow with Apache License 2.0 5 votes vote down vote up
@Override
public JobExecution abandon(Long jobExecutionId) throws NoSuchJobExecutionException,
		JobExecutionAlreadyRunningException {

	JobExecution jobExecution = getJobExecution(jobExecutionId);
	if (jobExecution.getStatus().isLessThan(BatchStatus.STOPPING)) {
		throw new JobExecutionAlreadyRunningException(
				"JobExecution is running or complete and therefore cannot be aborted");
	}

	logger.info("Aborting job execution: " + jobExecution);

	Collection<String> jsrJobNames = getJsrJobNames();

	JobInstance jobInstance = jobExecution.getJobInstance();
	if (jsrJobOperator != null && jsrJobNames.contains(jobInstance.getJobName())) {
		jsrJobOperator.abandon(jobExecutionId);
		jobExecution = getJobExecution(jobExecutionId);
	}
	else {
		jobExecution.upgradeStatus(BatchStatus.ABANDONED);
		jobExecution.setEndTime(new Date());
		jobRepository.update(jobExecution);
	}

	return jobExecution;

}
 
Example 5
Source File: DefaultJobService.java    From spring-batch-lightmin with Apache License 2.0 5 votes vote down vote up
@Override
public void attachJobInstance(final JobExecution jobExecution) {
    if (jobExecution != null) {
        if (jobExecution.getJobInstance() != null) {
            final JobInstance jobInstance = this.jobExplorer.getJobInstance(jobExecution.getJobInstance().getId());
            jobExecution.setJobInstance(jobInstance);
        } else {
            throw new SpringBatchLightminApplicationException("JobInstance of JobExecution with id:" + jobExecution.getJobId() + "is null, cannot provide information");
        }
    } else {
        throw new SpringBatchLightminApplicationException("jobExecution is null, cannot provide information");
    }
}
 
Example 6
Source File: SpringBatchRetryIntegrationTest.java    From tutorials with MIT License 5 votes vote down vote up
@Test
public void whenEndpointAlwaysFail_thenJobFails() throws Exception {
    when(closeableHttpClient.execute(any()))
      .thenThrow(new ConnectTimeoutException("Endpoint is down"));

    JobExecution jobExecution = jobLauncherTestUtils.launchJob(defaultJobParameters());
    JobInstance actualJobInstance = jobExecution.getJobInstance();
    ExitStatus actualJobExitStatus = jobExecution.getExitStatus();

    assertThat(actualJobInstance.getJobName(), is("retryBatchJob"));
    assertThat(actualJobExitStatus.getExitCode(), is("FAILED"));
    assertThat(actualJobExitStatus.getExitDescription(), containsString("org.apache.http.conn.ConnectTimeoutException"));
}
 
Example 7
Source File: BaseJobExecutionListener.java    From spring-boot-doma2-sample with Apache License 2.0 4 votes vote down vote up
@Override
public void afterJob(JobExecution jobExecution) {
    // コンテキストを取り出す
    val context = BatchContextHolder.getContext();

    // 機能別の終了処理を呼び出す
    try {
        after(jobExecution, context);
    } catch (Throwable t) {
        log.error("exception occurred. ", t);
        throw new IllegalStateException(t);
    } finally {
        // 共通の終了処理
        try {
            val batchId = context.getBatchId();
            val batchName = context.getBatchName();
            val jobStatus = jobExecution.getStatus();
            val endTime = jobExecution.getEndTime();

            if (log.isDebugEnabled()) {
                val jobId = jobExecution.getJobId();
                val jobInstance = jobExecution.getJobInstance();
                val jobInstanceId = jobInstance.getInstanceId();
                log.debug("job executed. [job={}(JobInstanceId:{} status:{})] in {}ms", jobId, jobInstanceId,
                        jobStatus, took(jobExecution));
                jobExecution.getStepExecutions()
                        .forEach(s -> log.debug("step executed. [step={}] in {}ms", s.getStepName(), took(s)));
            }

            if (!jobStatus.isRunning()) {
                log.info("*********************************************");
                log.info("* バッチID   : {}", batchId);
                log.info("* バッチ名   : {}", batchName);
                log.info("* ステータス : {}", jobStatus.getBatchStatus().toString());
                log.info("* 対象件数   : {}", context.getTotalCount());
                log.info("* 処理件数   : {}", context.getProcessCount());
                log.info("* エラー件数 : {}", context.getErrorCount());
                log.info("* 終了時刻   : {}", DateUtils.format(endTime, YYYY_MM_DD_HHmmss));
                log.info("*********************************************");
            }
        } finally {
            MDC.remove(MDC_BATCH_ID);

            // 監査情報をクリアする
            AuditInfoHolder.clear();

            // ジョブコンテキストをクリアする
            context.clear();
        }
    }
}