Search in sources :

Example 1 with StepExecution

use of javax.batch.runtime.StepExecution in project javaee7-samples by javaee-samples.

the class BatchChunkOptionalProcessorTest method testBatchChunkOptionalProcessor.

/**
 * In the test, we're just going to invoke the batch execution and wait for completion. To validate the test
 * expected behaviour we need to query the +javax.batch.runtime.Metric+ object available in the step execution.
 *
 * The batch process itself will read 10 elements from numbers  1 to 10, and write the same elements. Commits are
 * executed after 3 elements are read.
 *
 * @throws Exception an exception if the batch could not complete successfully.
 */
@Test
public void testBatchChunkOptionalProcessor() throws Exception {
    JobOperator jobOperator = getJobOperator();
    Long executionId = jobOperator.start("myJob", 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("myStep")) {
            Map<Metric.MetricType, Long> metricsMap = BatchTestHelper.getMetricsMap(stepExecution.getMetrics());
            // <1> The read count should be 10 elements. Check +MyItemReader+.
            assertEquals(10L, metricsMap.get(Metric.MetricType.READ_COUNT).longValue());
            // <2> The write count should be same 10 read elements.
            assertEquals(10L, metricsMap.get(Metric.MetricType.WRITE_COUNT).longValue());
            // <3> The commit count should be 4. Checkpoint is on every 3rd read, 4 commits for read elements.
            assertEquals(10L / 3 + (10L % 3 > 0 ? 1 : 0), metricsMap.get(Metric.MetricType.COMMIT_COUNT).longValue());
        }
    }
    // <4> Job should be completed.
    assertEquals(COMPLETED, jobExecution.getBatchStatus());
}
Also used : JobExecution(javax.batch.runtime.JobExecution) JobOperator(javax.batch.operations.JobOperator) BatchRuntime.getJobOperator(javax.batch.runtime.BatchRuntime.getJobOperator) Properties(java.util.Properties) StepExecution(javax.batch.runtime.StepExecution) Test(org.junit.Test)

Example 2 with StepExecution

use of javax.batch.runtime.StepExecution in project javaee7-samples by javaee-samples.

the class BatchSplitTest method testBatchSplit.

/**
 * In the test, we're just going to invoke the batch execution and wait for completion. To validate the test
 * expected behaviour we need to query +javax.batch.operations.JobOperator#getStepExecutions+.
 *
 * @throws Exception an exception if the batch could not complete successfully.
 */
@Test
public void testBatchSplit() throws Exception {
    JobOperator jobOperator = getJobOperator();
    Long executionId = jobOperator.start("myJob", 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());
    }
    // <1> Make sure all the steps were executed.
    assertEquals(3, stepExecutions.size());
    assertTrue(executedSteps.contains("step1"));
    assertTrue(executedSteps.contains("step2"));
    assertTrue(executedSteps.contains("step3"));
    // <2> Steps 'step1' and 'step2' can appear in any order, since they were executed in parallel.
    assertTrue(executedSteps.get(0).equals("step1") || executedSteps.get(0).equals("step2"));
    assertTrue(executedSteps.get(1).equals("step1") || executedSteps.get(1).equals("step2"));
    // <3> Step 'step3' is always the last to be executed.
    assertTrue(executedSteps.get(2).equals("step3"));
    // <4> Job should be completed.
    assertEquals(COMPLETED, jobExecution.getBatchStatus());
}
Also used : JobExecution(javax.batch.runtime.JobExecution) ArrayList(java.util.ArrayList) JobOperator(javax.batch.operations.JobOperator) BatchRuntime.getJobOperator(javax.batch.runtime.BatchRuntime.getJobOperator) Properties(java.util.Properties) StepExecution(javax.batch.runtime.StepExecution) Test(org.junit.Test)

Example 3 with StepExecution

use of javax.batch.runtime.StepExecution in project javaee7-samples by javaee-samples.

the class BatchChunkPartitionTest method testBatchChunkPartition.

/**
 * In the test, we're just going to invoke the batch execution and wait for completion. To validate the test
 * expected behaviour we need to query the +javax.batch.runtime.Metric+ object available in the step execution.
 *
 * The batch process itself will read and process 20 elements from numbers 1 to 20, but only write the odd
 * elements. Elements from 1 to 10 will be processed in one partition and elements from 11 to 20 in another
 * partition. Commits are executed after 3 elements are read by partition.
 *
 * @throws Exception an exception if the batch could not complete successfully.
 */
@Test
public void testBatchChunkPartition() throws Exception {
    JobOperator jobOperator = getJobOperator();
    Long executionId = jobOperator.start("myJob", 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("myStep")) {
            Map<Metric.MetricType, Long> metricsMap = BatchTestHelper.getMetricsMap(stepExecution.getMetrics());
            // <1> The read count should be 20 elements. Check +MyItemReader+.
            assertEquals(20L, metricsMap.get(Metric.MetricType.READ_COUNT).longValue());
            // <2> The write count should be 10. Only half of the elements read are processed to be written.
            assertEquals(10L, metricsMap.get(Metric.MetricType.WRITE_COUNT).longValue());
            // Number of elements by the item count value on myJob.xml, plus an additional transaction for the
            // remaining elements by each partition.
            long commitCount = (10L / 3 + (10 % 3 > 0 ? 1 : 0)) * 2;
            // <3> The commit count should be 8. Checkpoint is on every 3rd read, 4 commits for read elements and 2 partitions.
            assertEquals(commitCount, metricsMap.get(Metric.MetricType.COMMIT_COUNT).longValue());
        }
    }
    // <4> Job should be completed.
    assertEquals(COMPLETED, jobExecution.getBatchStatus());
}
Also used : JobExecution(javax.batch.runtime.JobExecution) JobOperator(javax.batch.operations.JobOperator) BatchRuntime.getJobOperator(javax.batch.runtime.BatchRuntime.getJobOperator) Properties(java.util.Properties) StepExecution(javax.batch.runtime.StepExecution) Test(org.junit.Test)

Example 4 with StepExecution

use of javax.batch.runtime.StepExecution in project javaee7-samples by javaee-samples.

the class BatchChunkExceptionTest method testBatchChunkException.

/**
 * In the test, we're just going to invoke the batch execution and wait for completion. To validate the test
 * expected behaviour we need to query the +javax.batch.runtime.Metric+ object available in the step execution.
 *
 * @throws Exception an exception if the batch could not complete successfully.
 */
@Test
public void testBatchChunkException() throws Exception {
    JobOperator jobOperator = getJobOperator();
    Long executionId = jobOperator.start("myJob", new Properties());
    JobExecution jobExecution = jobOperator.getJobExecution(executionId);
    jobExecution = keepTestAlive(jobExecution);
    List<StepExecution> stepExecutions = jobOperator.getStepExecutions(executionId);
    for (StepExecution stepExecution : stepExecutions) {
        if (stepExecution.getStepName().equals("myStep")) {
            Map<MetricType, Long> metricsMap = getMetricsMap(stepExecution.getMetrics());
            // TODO: Both WildFLy and Payara have a 2 here, but the test originally tested
            // for 1. Needs investigation.
            long skipCount = metricsMap.get(PROCESS_SKIP_COUNT).longValue();
            assertTrue("Skip count=" + skipCount, skipCount == 1l || skipCount == 2l);
            // There are a few differences between Glassfish and Wildfly. Needs investigation.
            // assertEquals(1L, metricsMap.get(Metric.MetricType.WRITE_SKIP_COUNT).longValue());
            // assertEquals(1L, retryReadExecutions);
            assertTrue("retryReadExecutions=" + retryReadExecutions, retryReadExecutions == 1l || retryReadExecutions == 2l);
        }
    }
    assertTrue(chunkExceptionsCountDownLatch.await(0, SECONDS));
    assertEquals(COMPLETED, jobExecution.getBatchStatus());
}
Also used : JobExecution(javax.batch.runtime.JobExecution) MetricType(javax.batch.runtime.Metric.MetricType) JobOperator(javax.batch.operations.JobOperator) BatchRuntime.getJobOperator(javax.batch.runtime.BatchRuntime.getJobOperator) Properties(java.util.Properties) StepExecution(javax.batch.runtime.StepExecution) Test(org.junit.Test)

Example 5 with StepExecution

use of javax.batch.runtime.StepExecution in project javaee7-samples by javaee-samples.

the class BatchMultipleStepsTest method testBatchMultipleSteps.

/**
 * In the test, we're just going to invoke the batch execution and wait for completion. To validate the test
 * expected behaviour we need to query +javax.batch.operations.JobOperator#getStepExecutions+ and the
 * +javax.batch.runtime.Metric+ object available in the step execution.
 *
 * @throws Exception an exception if the batch could not complete successfully.
 */
@Test
public void testBatchMultipleSteps() throws Exception {
    JobOperator jobOperator = getJobOperator();
    Long executionId = jobOperator.start("myJob", 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());
        if (stepExecution.getStepName().equals("step1")) {
            Map<Metric.MetricType, Long> metricsMap = BatchTestHelper.getMetricsMap(stepExecution.getMetrics());
            assertEquals(10L, metricsMap.get(Metric.MetricType.READ_COUNT).longValue());
            assertEquals(10L / 2, metricsMap.get(Metric.MetricType.WRITE_COUNT).longValue());
            assertEquals(10L / 3 + (10L % 3 > 0 ? 1 : 0), metricsMap.get(Metric.MetricType.COMMIT_COUNT).longValue());
        }
    }
    // <1> Make sure all the steps were executed.
    assertEquals(2, stepExecutions.size());
    // <2> Make sure all the steps were executed in order of declaration.
    assertArrayEquals(new String[] { "step1", "step2" }, executedSteps.toArray());
    // <3> Job should be completed.
    assertEquals(COMPLETED, jobExecution.getBatchStatus());
}
Also used : JobExecution(javax.batch.runtime.JobExecution) ArrayList(java.util.ArrayList) JobOperator(javax.batch.operations.JobOperator) BatchRuntime.getJobOperator(javax.batch.runtime.BatchRuntime.getJobOperator) Properties(java.util.Properties) StepExecution(javax.batch.runtime.StepExecution) Test(org.junit.Test)

Aggregations

StepExecution (javax.batch.runtime.StepExecution)19 JobOperator (javax.batch.operations.JobOperator)16 Properties (java.util.Properties)14 JobExecution (javax.batch.runtime.JobExecution)14 BatchRuntime.getJobOperator (javax.batch.runtime.BatchRuntime.getJobOperator)13 Test (org.junit.Test)13 ArrayList (java.util.ArrayList)5 PersistenceException (com.ibm.jbatch.container.exception.PersistenceException)2 StepExecutionImpl (com.ibm.jbatch.container.jobinstance.StepExecutionImpl)2 TCCLObjectInputStream (com.ibm.jbatch.container.util.TCCLObjectInputStream)2 ByteArrayInputStream (java.io.ByteArrayInputStream)2 IOException (java.io.IOException)2 ObjectInputStream (java.io.ObjectInputStream)2 Serializable (java.io.Serializable)2 Connection (java.sql.Connection)2 PreparedStatement (java.sql.PreparedStatement)2 ResultSet (java.sql.ResultSet)2 SQLException (java.sql.SQLException)2 Timestamp (java.sql.Timestamp)2 HashMap (java.util.HashMap)2