Java Code Examples for javax.batch.operations.JobOperator#getJobExecution()

The following examples show how to use javax.batch.operations.JobOperator#getJobExecution() . 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: BatchTestHelper.java    From wow-auctions with GNU General Public License v3.0 6 votes vote down vote up
/**
 * We need to keep the test running because JobOperator runs the batch job in an asynchronous way, so the
 * JobExecution can be properly updated with the running job status.
 *
 * @param jobOperator the JobOperator of the job that is being executed.
 * @throws java.util.concurrent.TimeoutException if the job takes a long time to complete.
 */
public static JobExecution keepTestAlive(JobOperator jobOperator, Long executionId) throws TimeoutException {
    JobExecution jobExecution = jobOperator.getJobExecution(executionId);

    Date curDate = new Date();
    BatchStatus curBatchStatus = jobExecution.getBatchStatus();

    while (true) {
        if (curBatchStatus == BatchStatus.STOPPED ||
            curBatchStatus == BatchStatus.COMPLETED ||
            curBatchStatus == BatchStatus.FAILED) {
            break;
        }

        if (new Date().getTime() - curDate.getTime() > 1000000) {
            throw new TimeoutException("Job processing did not complete in time");
        }

        jobExecution = jobOperator.getJobExecution(executionId);
        curBatchStatus = jobExecution.getBatchStatus();
    }
    return jobExecution;
}
 
Example 2
Source File: CustomCheckPointUnitTest.java    From tutorials with MIT License 6 votes vote down vote up
@Test
public void givenChunk_whenCustomCheckPoint_thenCommitCountIsThree() throws Exception {
    JobOperator jobOperator = BatchRuntime.getJobOperator();
    Long executionId = jobOperator.start("customCheckPoint", new Properties());
    JobExecution jobExecution = jobOperator.getJobExecution(executionId);
    jobExecution = BatchTestHelper.keepTestAlive(jobExecution);
    for (StepExecution stepExecution : jobOperator.getStepExecutions(executionId)) {
        if (stepExecution.getStepName()
            .equals("firstChunkStep")) {
            jobOperator.getStepExecutions(executionId)
                .stream()
                .map(BatchTestHelper::getCommitCount)
                .forEach(count -> assertEquals(3L, count.longValue()));
        }
    }
    assertEquals(jobExecution.getBatchStatus(), BatchStatus.COMPLETED);
}
 
Example 3
Source File: SimpleChunkUnitTest.java    From tutorials with MIT License 6 votes vote down vote up
@Test
public void givenChunk_thenBatch_CompletesWithSucess() throws Exception {
    JobOperator jobOperator = BatchRuntime.getJobOperator();
    Long executionId = jobOperator.start("simpleChunk", new Properties());
    JobExecution jobExecution = jobOperator.getJobExecution(executionId);
    jobExecution = BatchTestHelper.keepTestAlive(jobExecution);
    List<StepExecution> stepExecutions = jobOperator.getStepExecutions(executionId);
    for (StepExecution stepExecution : stepExecutions) {
        if (stepExecution.getStepName()
            .equals("firstChunkStep")) {
            Map<Metric.MetricType, Long> metricsMap = BatchTestHelper.getMetricsMap(stepExecution.getMetrics());
            assertEquals(10L, metricsMap.get(Metric.MetricType.READ_COUNT)
                .longValue());
            assertEquals(10L / 2L, metricsMap.get(Metric.MetricType.WRITE_COUNT)
                .longValue());
            assertEquals(10L / 3 + (10L % 3 > 0 ? 1 : 0), metricsMap.get(Metric.MetricType.COMMIT_COUNT)
                .longValue());
        }
    }
    assertEquals(jobExecution.getBatchStatus(), BatchStatus.COMPLETED);
}
 
Example 4
Source File: SimpleChunkUnitTest.java    From tutorials with MIT License 6 votes vote down vote up
@Test
public void givenChunk__thenBatch_fetchInformation() throws Exception {
    JobOperator jobOperator = BatchRuntime.getJobOperator();
    Long executionId = jobOperator.start("simpleChunk", new Properties());
    JobExecution jobExecution = jobOperator.getJobExecution(executionId);
    jobExecution = BatchTestHelper.keepTestAlive(jobExecution);
    // job name contains simpleBatchLet which is the name of the file
    assertTrue(jobOperator.getJobNames().contains("simpleChunk"));
    // job parameters are empty
    assertTrue(jobOperator.getParameters(executionId).isEmpty());
    // step execution information
    List<StepExecution> stepExecutions = jobOperator.getStepExecutions(executionId);
    assertEquals("firstChunkStep", stepExecutions.get(0).getStepName());
    // finding out batch status
    assertEquals(BatchStatus.COMPLETED, stepExecutions.get(0).getBatchStatus());
    Map<Metric.MetricType, Long> metricTest = BatchTestHelper.getMetricsMap(stepExecutions.get(0).getMetrics());
    assertEquals(10L, metricTest.get(Metric.MetricType.READ_COUNT).longValue());
    assertEquals(5L, metricTest.get(Metric.MetricType.FILTER_COUNT).longValue());
    assertEquals(4L, metricTest.get(Metric.MetricType.COMMIT_COUNT).longValue());
    assertEquals(5L, metricTest.get(Metric.MetricType.WRITE_COUNT).longValue());
    assertEquals(0L, metricTest.get(Metric.MetricType.READ_SKIP_COUNT).longValue());
    assertEquals(0L, metricTest.get(Metric.MetricType.WRITE_SKIP_COUNT).longValue());
    assertEquals(0L, metricTest.get(Metric.MetricType.PROCESS_SKIP_COUNT).longValue());
    assertEquals(0L, metricTest.get(Metric.MetricType.ROLLBACK_COUNT).longValue());
    assertEquals(jobExecution.getBatchStatus(), BatchStatus.COMPLETED);
}
 
Example 5
Source File: SimpleErrorChunkUnitTest.java    From tutorials with MIT License 6 votes vote down vote up
@Test
public void givenChunkError_thenErrorSkipped_CompletesWithSuccess() throws Exception {
    JobOperator jobOperator = BatchRuntime.getJobOperator();
    Long executionId = jobOperator.start("simpleErrorSkipChunk", new Properties());
    JobExecution jobExecution = jobOperator.getJobExecution(executionId);
    jobExecution = BatchTestHelper.keepTestAlive(jobExecution);
    List<StepExecution> stepExecutions = jobOperator.getStepExecutions(executionId);
    for (StepExecution stepExecution : stepExecutions) {
        if (stepExecution.getStepName()
            .equals("errorStep")) {
            jobOperator.getStepExecutions(executionId)
            .stream()
            .map(BatchTestHelper::getProcessSkipCount)
            .forEach(skipCount -> assertEquals(1L, skipCount.longValue()));
        }
    }
    assertEquals(jobExecution.getBatchStatus(), BatchStatus.COMPLETED);
}
 
Example 6
Source File: JobSequenceUnitTest.java    From tutorials with MIT License 6 votes vote down vote up
@Test
public void givenSplit_thenBatch_CompletesWithSuccess() throws Exception {
    JobOperator jobOperator = BatchRuntime.getJobOperator();
    Long executionId = jobOperator.start("splitJobSequence", new Properties());
    JobExecution jobExecution = jobOperator.getJobExecution(executionId);
    jobExecution = BatchTestHelper.keepTestAlive(jobExecution);
    List<StepExecution> stepExecutions = jobOperator.getStepExecutions(executionId);
    List<String> executedSteps = new ArrayList<>();
    for (StepExecution stepExecution : stepExecutions) {
        executedSteps.add(stepExecution.getStepName());
    }
    assertEquals(3, stepExecutions.size());
    assertTrue(executedSteps.contains("splitJobSequenceStep1"));
    assertTrue(executedSteps.contains("splitJobSequenceStep2"));
    assertTrue(executedSteps.contains("splitJobSequenceStep3"));
    assertTrue(executedSteps.get(0).equals("splitJobSequenceStep1") || executedSteps.get(0).equals("splitJobSequenceStep2"));
    assertTrue(executedSteps.get(1).equals("splitJobSequenceStep1") || executedSteps.get(1).equals("splitJobSequenceStep2"));
    assertTrue(executedSteps.get(2).equals("splitJobSequenceStep3"));
    assertEquals(jobExecution.getBatchStatus(), BatchStatus.COMPLETED);
}
 
Example 7
Source File: SimpleBatchLetUnitTest.java    From tutorials with MIT License 5 votes vote down vote up
@Test
public void givenBatchLet_thenBatch_CompleteWithSuccess() throws Exception {
    JobOperator jobOperator = BatchRuntime.getJobOperator();
    Long executionId = jobOperator.start("simpleBatchLet", new Properties());
    JobExecution jobExecution = jobOperator.getJobExecution(executionId);
    jobExecution = BatchTestHelper.keepTestAlive(jobExecution);
    assertEquals(jobExecution.getBatchStatus(), BatchStatus.COMPLETED);
}
 
Example 8
Source File: SimpleBatchLetUnitTest.java    From tutorials with MIT License 5 votes vote down vote up
@Test
public void givenBatchLetProperty_thenBatch_CompleteWithSuccess() throws Exception {
    JobOperator jobOperator = BatchRuntime.getJobOperator();
    Long executionId = jobOperator.start("injectionSimpleBatchLet", new Properties());
    JobExecution jobExecution = jobOperator.getJobExecution(executionId);
    jobExecution = BatchTestHelper.keepTestAlive(jobExecution);
    assertEquals(jobExecution.getBatchStatus(), BatchStatus.COMPLETED);
}
 
Example 9
Source File: SimpleBatchLetUnitTest.java    From tutorials with MIT License 5 votes vote down vote up
@Test
public void givenBatchLetPartition_thenBatch_CompleteWithSuccess() throws Exception {
    JobOperator jobOperator = BatchRuntime.getJobOperator();
    Long executionId = jobOperator.start("partitionSimpleBatchLet", new Properties());
    JobExecution jobExecution = jobOperator.getJobExecution(executionId);
    jobExecution = BatchTestHelper.keepTestAlive(jobExecution);
    assertEquals(jobExecution.getBatchStatus(), BatchStatus.COMPLETED);
}
 
Example 10
Source File: SimpleBatchLetUnitTest.java    From tutorials with MIT License 5 votes vote down vote up
@Test
public void givenBatchLetStarted_whenStopped_thenBatchStopped() throws Exception {
    JobOperator jobOperator = BatchRuntime.getJobOperator();
    Long executionId = jobOperator.start("simpleBatchLet", new Properties());
    JobExecution jobExecution = jobOperator.getJobExecution(executionId);
    jobOperator.stop(executionId);
    jobExecution = BatchTestHelper.keepTestStopped(jobExecution);
    assertEquals(jobExecution.getBatchStatus(), BatchStatus.STOPPED);
}
 
Example 11
Source File: SimpleBatchLetUnitTest.java    From tutorials with MIT License 5 votes vote down vote up
@Test
public void givenBatchLetStopped_whenRestarted_thenBatchCompletesSuccess() throws Exception {
    JobOperator jobOperator = BatchRuntime.getJobOperator();
    Long executionId = jobOperator.start("simpleBatchLet", new Properties());
    JobExecution jobExecution = jobOperator.getJobExecution(executionId);
    jobOperator.stop(executionId);
    jobExecution = BatchTestHelper.keepTestStopped(jobExecution);
    assertEquals(jobExecution.getBatchStatus(), BatchStatus.STOPPED);
    executionId = jobOperator.restart(jobExecution.getExecutionId(), new Properties());
    jobExecution = BatchTestHelper.keepTestAlive(jobOperator.getJobExecution(executionId));
    assertEquals(jobExecution.getBatchStatus(), BatchStatus.COMPLETED);
}
 
Example 12
Source File: SimpleErrorChunkUnitTest.java    From tutorials with MIT License 5 votes vote down vote up
@Test
public void givenChunkError_thenBatch_CompletesWithFailed() throws Exception {
    JobOperator jobOperator = BatchRuntime.getJobOperator();
    Long executionId = jobOperator.start("simpleErrorChunk", new Properties());
    JobExecution jobExecution = jobOperator.getJobExecution(executionId);
    jobExecution = BatchTestHelper.keepTestFailed(jobExecution);
    assertEquals(jobExecution.getBatchStatus(), BatchStatus.FAILED);
}
 
Example 13
Source File: JobSequenceUnitTest.java    From tutorials with MIT License 5 votes vote down vote up
@Test
public void givenTwoSteps_thenBatch_CompleteWithSuccess() throws Exception {
    JobOperator jobOperator = BatchRuntime.getJobOperator();
    Long executionId = jobOperator.start("simpleJobSequence", new Properties());
    JobExecution jobExecution = jobOperator.getJobExecution(executionId);
    jobExecution = BatchTestHelper.keepTestAlive(jobExecution);
    assertEquals(2 , jobOperator.getStepExecutions(executionId).size());
    assertEquals(jobExecution.getBatchStatus(), BatchStatus.COMPLETED);
}
 
Example 14
Source File: JobSequenceUnitTest.java    From tutorials with MIT License 5 votes vote down vote up
@Test
public void givenFlow_thenBatch_CompleteWithSuccess() throws Exception {
    JobOperator jobOperator = BatchRuntime.getJobOperator();
    Long executionId = jobOperator.start("flowJobSequence", new Properties());
    JobExecution jobExecution = jobOperator.getJobExecution(executionId);
    jobExecution = BatchTestHelper.keepTestAlive(jobExecution);
    assertEquals(3 , jobOperator.getStepExecutions(executionId).size());
    assertEquals(jobExecution.getBatchStatus(), BatchStatus.COMPLETED);
}
 
Example 15
Source File: JobSequenceUnitTest.java    From tutorials with MIT License 5 votes vote down vote up
@Test
public void givenDecider_thenBatch_CompleteWithSuccess() throws Exception {
    JobOperator jobOperator = BatchRuntime.getJobOperator();
    Long executionId = jobOperator.start("decideJobSequence", new Properties());
    JobExecution jobExecution = jobOperator.getJobExecution(executionId);
    jobExecution = BatchTestHelper.keepTestAlive(jobExecution);
    List<StepExecution> stepExecutions = jobOperator.getStepExecutions(executionId);
    List<String> executedSteps = new ArrayList<>();
    for (StepExecution stepExecution : stepExecutions) {
        executedSteps.add(stepExecution.getStepName());
    }
    assertEquals(2, jobOperator.getStepExecutions(executionId).size());
    assertArrayEquals(new String[] { "firstBatchStepStep1", "firstBatchStepStep3" }, executedSteps.toArray());
    assertEquals(jobExecution.getBatchStatus(), BatchStatus.COMPLETED);
}
 
Example 16
Source File: StartableCommand.java    From incubator-batchee with Apache License 2.0 5 votes vote down vote up
private JobExecution report(final JobOperator operator, final long id) {
    final JobExecution execution = operator.getJobExecution(id);

    info("");
    info(LINE);

    info("Batch status: " + statusToString(execution.getBatchStatus()));
    info("Exit status:  " + execution.getExitStatus());
    if (execution.getEndTime() != null && execution.getStartTime() != null) {
        info("Duration:     " + TimeUnit.MILLISECONDS.toSeconds(execution.getEndTime().getTime() - execution.getStartTime().getTime()) + "s");
    }

    if (BatchStatus.FAILED.equals(execution.getBatchStatus())) {
        final Collection<StepExecution> stepExecutions = operator.getStepExecutions(id);
        for (final StepExecution stepExecution : stepExecutions) {
            if (BatchStatus.FAILED.equals(stepExecution.getBatchStatus())) {
                info("");
                info("Step name       : " + stepExecution.getStepName());
                info("Step status     : " + statusToString(stepExecution.getBatchStatus()));
                info("Step exit status: " + stepExecution.getExitStatus());
                break;
            }
        }
    }

    info(LINE);
    return execution;
}