BAEL-2168 Java EE 7 Batch Processing (#5645)
* jberet batch * Batch Understanding * partition * exception * some more changes
This commit is contained in:
committed by
Grzegorz Piwowarek
parent
2f9fee0391
commit
f784c3bb79
@@ -0,0 +1,79 @@
|
||||
package com.baeldung.batch.understanding;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import javax.batch.runtime.BatchRuntime;
|
||||
import javax.batch.runtime.BatchStatus;
|
||||
import javax.batch.runtime.JobExecution;
|
||||
import javax.batch.runtime.Metric;
|
||||
|
||||
public class BatchTestHelper {
|
||||
private static final int MAX_TRIES = 40;
|
||||
private static final int THREAD_SLEEP = 1000;
|
||||
|
||||
private BatchTestHelper() {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
public static JobExecution keepTestAlive(JobExecution jobExecution) throws InterruptedException {
|
||||
int maxTries = 0;
|
||||
while (!jobExecution.getBatchStatus()
|
||||
.equals(BatchStatus.COMPLETED)) {
|
||||
if (maxTries < MAX_TRIES) {
|
||||
maxTries++;
|
||||
Thread.sleep(THREAD_SLEEP);
|
||||
jobExecution = BatchRuntime.getJobOperator()
|
||||
.getJobExecution(jobExecution.getExecutionId());
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
Thread.sleep(THREAD_SLEEP);
|
||||
return jobExecution;
|
||||
}
|
||||
|
||||
public static JobExecution keepTestFailed(JobExecution jobExecution) throws InterruptedException {
|
||||
int maxTries = 0;
|
||||
while (!jobExecution.getBatchStatus()
|
||||
.equals(BatchStatus.FAILED)) {
|
||||
if (maxTries < MAX_TRIES) {
|
||||
maxTries++;
|
||||
Thread.sleep(THREAD_SLEEP);
|
||||
jobExecution = BatchRuntime.getJobOperator()
|
||||
.getJobExecution(jobExecution.getExecutionId());
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
Thread.sleep(THREAD_SLEEP);
|
||||
|
||||
return jobExecution;
|
||||
}
|
||||
|
||||
public static JobExecution keepTestStopped(JobExecution jobExecution) throws InterruptedException {
|
||||
int maxTries = 0;
|
||||
while (!jobExecution.getBatchStatus()
|
||||
.equals(BatchStatus.STOPPED)) {
|
||||
if (maxTries < MAX_TRIES) {
|
||||
maxTries++;
|
||||
Thread.sleep(THREAD_SLEEP);
|
||||
jobExecution = BatchRuntime.getJobOperator()
|
||||
.getJobExecution(jobExecution.getExecutionId());
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
Thread.sleep(THREAD_SLEEP);
|
||||
return jobExecution;
|
||||
}
|
||||
|
||||
public static Map<Metric.MetricType, Long> getMetricsMap(Metric[] metrics) {
|
||||
Map<Metric.MetricType, Long> metricsMap = new HashMap<>();
|
||||
for (Metric metric : metrics) {
|
||||
metricsMap.put(metric.getType(), metric.getValue());
|
||||
}
|
||||
return metricsMap;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
package com.baeldung.batch.understanding;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
import java.util.Map;
|
||||
import java.util.Properties;
|
||||
import javax.batch.operations.JobOperator;
|
||||
import javax.batch.runtime.BatchRuntime;
|
||||
import javax.batch.runtime.BatchStatus;
|
||||
import javax.batch.runtime.JobExecution;
|
||||
import javax.batch.runtime.Metric;
|
||||
import javax.batch.runtime.StepExecution;
|
||||
import com.baeldung.batch.understanding.BatchTestHelper;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
class CustomCheckPointUnitTest {
|
||||
@Test
|
||||
public void givenChunk_whenCustomCheckPoint_thenCommitCount_3() 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")) {
|
||||
Map<Metric.MetricType, Long> metricsMap = BatchTestHelper.getMetricsMap(stepExecution.getMetrics());
|
||||
assertEquals(3L, metricsMap.get(Metric.MetricType.COMMIT_COUNT)
|
||||
.longValue());
|
||||
}
|
||||
}
|
||||
assertEquals(jobExecution.getBatchStatus(), BatchStatus.COMPLETED);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,74 @@
|
||||
package com.baeldung.batch.understanding;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Properties;
|
||||
|
||||
import javax.batch.operations.JobOperator;
|
||||
import javax.batch.runtime.BatchRuntime;
|
||||
import javax.batch.runtime.BatchStatus;
|
||||
import javax.batch.runtime.JobExecution;
|
||||
import javax.batch.runtime.StepExecution;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
class JobSequenceUnitTest {
|
||||
@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);
|
||||
}
|
||||
|
||||
@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);
|
||||
}
|
||||
|
||||
@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);
|
||||
}
|
||||
|
||||
@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);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,68 @@
|
||||
package com.baeldung.batch.understanding;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Properties;
|
||||
|
||||
import javax.batch.operations.JobOperator;
|
||||
import javax.batch.runtime.BatchRuntime;
|
||||
import javax.batch.runtime.BatchStatus;
|
||||
import javax.batch.runtime.JobExecution;
|
||||
import javax.batch.runtime.Metric;
|
||||
import javax.batch.runtime.StepExecution;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
class SimpleBatchLetUnitTest {
|
||||
@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);
|
||||
}
|
||||
|
||||
@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);
|
||||
}
|
||||
|
||||
@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);
|
||||
}
|
||||
|
||||
@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);
|
||||
}
|
||||
|
||||
@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);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,67 @@
|
||||
package com.baeldung.batch.understanding;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Properties;
|
||||
|
||||
import javax.batch.operations.JobOperator;
|
||||
import javax.batch.runtime.BatchRuntime;
|
||||
import javax.batch.runtime.BatchStatus;
|
||||
import javax.batch.runtime.JobExecution;
|
||||
import javax.batch.runtime.Metric;
|
||||
import javax.batch.runtime.StepExecution;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
class SimpleChunkUnitTest {
|
||||
@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);
|
||||
}
|
||||
|
||||
@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);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
package com.baeldung.batch.understanding;
|
||||
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Properties;
|
||||
import javax.batch.operations.JobOperator;
|
||||
import javax.batch.runtime.BatchRuntime;
|
||||
import javax.batch.runtime.BatchStatus;
|
||||
import javax.batch.runtime.JobExecution;
|
||||
import javax.batch.runtime.Metric.MetricType;
|
||||
import javax.batch.runtime.StepExecution;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
class SimpleErrorChunkUnitTest {
|
||||
|
||||
@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);
|
||||
System.out.println(jobExecution.getBatchStatus());
|
||||
assertEquals(jobExecution.getBatchStatus(), BatchStatus.FAILED);
|
||||
}
|
||||
|
||||
@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")) {
|
||||
Map<MetricType, Long> metricsMap = BatchTestHelper.getMetricsMap(stepExecution.getMetrics());
|
||||
long skipCount = metricsMap.get(MetricType.PROCESS_SKIP_COUNT)
|
||||
.longValue();
|
||||
assertTrue("Skip count=" + skipCount, skipCount == 1l || skipCount == 2l);
|
||||
}
|
||||
}
|
||||
assertEquals(jobExecution.getBatchStatus(), BatchStatus.COMPLETED);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user