use of javax.batch.operations.JobOperator in project javaee7-samples by javaee-samples.
the class BatchListenersTest method testBatchListeners.
/**
* 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 and
* also verify if the listeners were executed correctly via a +CountDownLatch+ wait.
*
* The batch process itself will read and process 10 elements from numbers 1 to 10, but only write the odd
* elements.
*
* * Each listener will decrement the total value of the +CountDownLatch+, until all the predicted events are
* executed. The number of predicted events is 60:
*
* - +MyJobListener+ executes 2 times, 1 for +MyJobListener#beforeJob+ and 1 more for +MyJobListener#afterJob+.
*
* - +MyStepListener+ executes 2 times, 1 for +MyStepListener#beforeStep+ and 1 more for +MyStepListener#afterStep+.
*
* - +MyChunkListener+ executes 8 times, 4 for +MyChunkListener#beforeChunk+ and 4 more
* for +MyChunkListener#afterChunk+. Chunk size is set to 3 and the total elements is 10, so 10/3 = 3 and 1 more
* for the last element, means 4 for each chunk listener event.
*
* - +MyItemReader+ executes 22 times, 10 elements in total plus an empty read, so +MyItemReadListener#beforeRead+
* executes 11 times and +MyItemReadListener#afterRead+ the other 11 times.
*
* - +MyItemProcessorListener+ executes 20 times, 10 elements read in total,
* so +MyItemProcessorLister#beforeProcess+ executes 10 times
* and +MyItemProcessorLister#afterProcess+ the other 10 times.
*
* - +MyItemWriterListener+ executed 6 times, 3 times for +MyItemWriterListener#beforeWrite+ and another 3 times
* for +MyItemWriterListener#afterWrite+. This one is a bit more tricky, since not every element needs to be
* written. Looking at +MyItemProcessor+, only even records are going to be written. We also need to take into
* account the elements read per chunk, so: Chunk[1] read and process [1,2,3] and wrote [2,6], Chunk[2] read and
* process [4,5,6] and wrote [10], Chunk[3] read and process [7,8,9] and wrote [14,18], Chunk[4] read and process
* [10] and did not wrote anything, so only 3 writes for the full processing.
*
* - Total: 2 + 2 + 8 + 22 + 20 + 6 = 60
*
* @throws Exception an exception if the batch could not complete successfully.
*/
@Test
public void testBatchListeners() 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());
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());
}
}
assertTrue(BatchListenerRecorder.batchListenersCountDownLatch.await(0, TimeUnit.SECONDS));
assertEquals(jobExecution.getBatchStatus(), BatchStatus.COMPLETED);
}
use of javax.batch.operations.JobOperator in project javaee7-samples by javaee-samples.
the class BatchChunkCheckpointTest method testBatchChunkCheckpoint.
/**
* 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 5 elements are read by the custom checkpoint algorithm.
*
* @throws Exception an exception if the batch could not complete successfully.
*/
@Test
public void testBatchChunkCheckpoint() 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 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 3. Checkpoint is on every 5th read, plus one final read-commit.
assertEquals(10L / 5L + 1, metricsMap.get(Metric.MetricType.COMMIT_COUNT).longValue());
}
}
// <4> The checkpoint algorithm should be checked 10 times. One for each element read.
assertTrue(MyCheckpointAlgorithm.checkpointCountDownLatch.await(0, TimeUnit.SECONDS));
// <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 JmsItemReaderTest method runJob.
private void runJob() throws InterruptedException {
JobOperator jobOperator = BatchRuntime.getJobOperator();
Long executionId = jobOperator.start("jms-job", new Properties());
JobExecution jobExecution = jobOperator.getJobExecution(executionId);
BatchTestHelper.keepTestAlive(jobExecution);
}
use of javax.batch.operations.JobOperator in project wildfly by wildfly.
the class NoCdiTestCase method testJobOperatorIsAvailable.
@Test
public void testJobOperatorIsAvailable() throws Exception {
final JobOperator operator = BatchRuntime.getJobOperator();
assertNotNull(operator);
}
use of javax.batch.operations.JobOperator in project wildfly by wildfly.
the class ChunkPartitionTestCase method testSuspend.
@Test
public void testSuspend() throws Exception {
try {
final Properties jobProperties = new Properties();
jobProperties.setProperty("reader.end", "10");
final JobOperator jobOperator = BatchRuntime.getJobOperator();
// Start the first job
long executionId = jobOperator.start("chunk-suspend", jobProperties);
JobExecution jobExecution = jobOperator.getJobExecution(executionId);
// Wait until the job is complete for a maximum of 5 seconds
waitForTermination(jobExecution, 5);
Assert.assertEquals(BatchStatus.COMPLETED, jobExecution.getBatchStatus());
// Check that count
Assert.assertEquals(10, countingItemWriter.getWrittenItemSize());
// Suspend the server
managementClient.getControllerClient().execute(Operations.createOperation("suspend"));
// Submit another job which should be queued, should be safe with an InMemoryJobRepository (the default)
executionId = jobOperator.start("chunk-suspend", jobProperties);
// The job should not exist yet as the server is suspended
try {
jobOperator.getJobExecution(executionId);
} catch (NoSuchJobExecutionException expected) {
Assert.fail("Job should not exist as the server is suspended: " + executionId);
}
// Resume the server which should kick of queued jobs
managementClient.getControllerClient().execute(Operations.createOperation("resume"));
// Get the execution
jobExecution = jobOperator.getJobExecution(executionId);
// Wait until the job is complete for a maximum of 5 seconds
waitForTermination(jobExecution, 5);
Assert.assertEquals(BatchStatus.COMPLETED, jobExecution.getBatchStatus());
// Check that count
Assert.assertEquals(20, countingItemWriter.getWrittenItemSize());
} finally {
managementClient.getControllerClient().execute(Operations.createOperation("resume"));
}
}
Aggregations