Search in sources :

Example 16 with StepExecution

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

the class BatchChunkSimpleNoBeansTest method testBatchChunkSimpleNoBeans.

/**
 * 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 10 elements from numbers  1 to 10, but only write the odd
 * elements. Commits are executed after 3 elements are read.
 *
 * @throws Exception an exception if the batch could not complete successfully.
 */
@Test
public void testBatchChunkSimpleNoBeans() 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 5. Only half of the elements read are processed to be written.
            assertEquals(10L / 2L, 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 17 with StepExecution

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

the class ChunkSimpleTest method testChunkSimple.

/**
 * 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 10 elements from numbers  1 to 10, but only write the odd
 * elements. Commits are executed after 3 elements are read.
 *
 * @throws Exception an exception if the batch could not complete successfully.
 */
@Test
public void testChunkSimple() 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 5. Only half of the elements read are processed to be written.
            assertEquals(10L / 2L, 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(jobExecution.getBatchStatus(), COMPLETED);
}
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 18 with StepExecution

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

the class BatchFlowTest method testBatchFlow.

/**
 * 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 testBatchFlow() 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("step2")) {
            Map<Metric.MetricType, Long> metricsMap = BatchTestHelper.getMetricsMap(stepExecution.getMetrics());
            System.out.println(metricsMap);
            assertEquals(5L, metricsMap.get(Metric.MetricType.READ_COUNT).longValue());
            assertEquals(5L, metricsMap.get(Metric.MetricType.WRITE_COUNT).longValue());
            assertEquals(5L / 3 + (5 % 3 > 0 ? 1 : 0), metricsMap.get(Metric.MetricType.COMMIT_COUNT).longValue());
        }
    }
    // <1> Make sure all the steps were executed.
    assertEquals(3, stepExecutions.size());
    // <2> Make sure all the steps were executed in order of declaration.
    assertArrayEquals(new String[] { "step1", "step2", "step3" }, 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)

Example 19 with StepExecution

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

the class BatchDecisionTest method testBatchDecision.

/**
 * 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 testBatchDecision() 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 that only two steps were executed.
    assertEquals(2, stepExecutions.size());
    // <2> Make sure that only the expected steps were executed an in order.
    assertArrayEquals(new String[] { "step1", "step3" }, executedSteps.toArray());
    // <3> Make sure that this step was never executed.
    assertFalse(executedSteps.contains("step2"));
    // <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)

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