use of javax.batch.operations.JobOperator 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 = BatchRuntime.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(BatchStatus.COMPLETED, jobExecution.getBatchStatus());
}
use of javax.batch.operations.JobOperator 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 = BatchRuntime.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(BatchStatus.COMPLETED, jobExecution.getBatchStatus());
}
use of javax.batch.operations.JobOperator in project javaee7-samples by javaee-samples.
the class MyBatchletTest method testBatchletProcess.
/**
* In the test, we're just going to invoke the batch execution and wait for completion. To validate the test
* expected behaviour we just need to check the Batch Status in the +javax.batch.runtime.JobExecution+ object. We
* should get a +javax.batch.runtime.BatchStatus.COMPLETED+.
*
* @throws Exception an exception if the batch could not complete successfully.
*/
@Test
public void testBatchletProcess() throws Exception {
JobOperator jobOperator = BatchRuntime.getJobOperator();
Long executionId = jobOperator.start("myJob", new Properties());
JobExecution jobExecution = jobOperator.getJobExecution(executionId);
jobExecution = BatchTestHelper.keepTestAlive(jobExecution);
// <1> Job should be completed.
assertEquals(jobExecution.getBatchStatus(), BatchStatus.COMPLETED);
}
use of javax.batch.operations.JobOperator in project javaee7-samples by javaee-samples.
the class BatchCSVDatabaseTest method testBatchCSVDatabase.
/**
* 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 write 7 elements of type +Person+. Commits are executed after 3 elements
* are read.
*
* @throws Exception an exception if the batch could not complete successfully.
*/
@SuppressWarnings("unchecked")
@Test
public void testBatchCSVDatabase() throws Exception {
JobOperator jobOperator = BatchRuntime.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 7 elements. Check +MyItemReader+.
assertEquals(7L, metricsMap.get(Metric.MetricType.READ_COUNT).longValue());
// <2> The write count should be the same 7 read elements.
assertEquals(7L, 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(3L, metricsMap.get(Metric.MetricType.COMMIT_COUNT).longValue());
}
}
Query query = entityManager.createNamedQuery("Person.findAll");
List<Person> persons = query.getResultList();
// <4> Confirm that the elements were actually persisted into the database.
assertEquals(7L, persons.size());
// <5> Job should be completed.
assertEquals(jobExecution.getBatchStatus(), BatchStatus.COMPLETED);
}
use of javax.batch.operations.JobOperator in project javaee7-samples by javaee-samples.
the class BatchChunkMapperTest method testBatchChunkMapper.
/**
* 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 testBatchChunkMapper() throws Exception {
JobOperator jobOperator = BatchRuntime.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> Make sure that all the partitions were created.
assertEquals(2L, MyItemReader.totalReaders);
// <5> Job should be completed.
assertEquals(BatchStatus.COMPLETED, jobExecution.getBatchStatus());
}
Aggregations