From f784c3bb793b6de50323700e771a5e001f3e4850 Mon Sep 17 00:00:00 2001 From: Amitabh Mandal Date: Sun, 25 Nov 2018 15:31:22 +0530 Subject: [PATCH] BAEL-2168 Java EE 7 Batch Processing (#5645) * jberet batch * Batch Understanding * partition * exception * some more changes --- jee-7/pom.xml | 935 ++++++++++-------- .../ChunkExceptionSkipReadListener.java | 11 + .../batch/understanding/CustomCheckPoint.java | 12 + .../understanding/DeciderJobSequence.java | 14 + .../understanding/InjectSimpleBatchLet.java | 20 + .../batch/understanding/SimpleBatchLet.java | 13 + .../SimpleChunkItemProcessor.java | 12 + .../understanding/SimpleChunkItemReader.java | 27 + .../SimpleChunkItemReaderError.java | 31 + .../understanding/SimpleChunkWriter.java | 13 + .../exception/MyInputRecord.java | 41 + .../exception/MyItemProcessor.java | 15 + .../understanding/exception/MyItemReader.java | 42 + .../understanding/exception/MyItemWriter.java | 19 + .../exception/MyOutputRecord.java | 39 + .../exception/MyRetryProcessorListener.java | 11 + .../exception/MyRetryReadListener.java | 11 + .../exception/MyRetryWriteListener.java | 12 + .../exception/MySkipProcessorListener.java | 11 + .../exception/MySkipReadListener.java | 11 + .../exception/MySkipWriteListener.java | 13 + .../META-INF/batch-jobs/customCheckPoint.xml | 12 + .../META-INF/batch-jobs/decideJobSequence.xml | 18 + .../META-INF/batch-jobs/flowJobSequence.xml | 21 + .../batch-jobs/injectionSimpleBatchLet.xml | 13 + .../batch-jobs/partitionSimpleBatchLet.xml | 24 + .../META-INF/batch-jobs/simpleBatchLet.xml | 9 + .../META-INF/batch-jobs/simpleChunk.xml | 11 + .../META-INF/batch-jobs/simpleErrorChunk.xml | 24 + .../batch-jobs/simpleErrorSkipChunk.xml | 26 + .../META-INF/batch-jobs/simpleJobSequence.xml | 17 + .../META-INF/batch-jobs/splitJobSequence.xml | 22 + jee-7/src/main/resources/META-INF/beans.xml | 8 + .../batch/understanding/BatchTestHelper.java | 79 ++ .../CustomCheckPointUnitTest.java | 33 + .../understanding/JobSequenceUnitTest.java | 74 ++ .../understanding/SimpleBatchLetUnitTest.java | 68 ++ .../understanding/SimpleChunkUnitTest.java | 67 ++ .../SimpleErrorChunkUnitTest.java | 48 + 39 files changed, 1473 insertions(+), 414 deletions(-) create mode 100644 jee-7/src/main/java/com/baeldung/batch/understanding/ChunkExceptionSkipReadListener.java create mode 100644 jee-7/src/main/java/com/baeldung/batch/understanding/CustomCheckPoint.java create mode 100644 jee-7/src/main/java/com/baeldung/batch/understanding/DeciderJobSequence.java create mode 100644 jee-7/src/main/java/com/baeldung/batch/understanding/InjectSimpleBatchLet.java create mode 100644 jee-7/src/main/java/com/baeldung/batch/understanding/SimpleBatchLet.java create mode 100644 jee-7/src/main/java/com/baeldung/batch/understanding/SimpleChunkItemProcessor.java create mode 100644 jee-7/src/main/java/com/baeldung/batch/understanding/SimpleChunkItemReader.java create mode 100644 jee-7/src/main/java/com/baeldung/batch/understanding/SimpleChunkItemReaderError.java create mode 100644 jee-7/src/main/java/com/baeldung/batch/understanding/SimpleChunkWriter.java create mode 100644 jee-7/src/main/java/com/baeldung/batch/understanding/exception/MyInputRecord.java create mode 100644 jee-7/src/main/java/com/baeldung/batch/understanding/exception/MyItemProcessor.java create mode 100644 jee-7/src/main/java/com/baeldung/batch/understanding/exception/MyItemReader.java create mode 100644 jee-7/src/main/java/com/baeldung/batch/understanding/exception/MyItemWriter.java create mode 100644 jee-7/src/main/java/com/baeldung/batch/understanding/exception/MyOutputRecord.java create mode 100644 jee-7/src/main/java/com/baeldung/batch/understanding/exception/MyRetryProcessorListener.java create mode 100644 jee-7/src/main/java/com/baeldung/batch/understanding/exception/MyRetryReadListener.java create mode 100644 jee-7/src/main/java/com/baeldung/batch/understanding/exception/MyRetryWriteListener.java create mode 100644 jee-7/src/main/java/com/baeldung/batch/understanding/exception/MySkipProcessorListener.java create mode 100644 jee-7/src/main/java/com/baeldung/batch/understanding/exception/MySkipReadListener.java create mode 100644 jee-7/src/main/java/com/baeldung/batch/understanding/exception/MySkipWriteListener.java create mode 100644 jee-7/src/main/resources/META-INF/batch-jobs/customCheckPoint.xml create mode 100644 jee-7/src/main/resources/META-INF/batch-jobs/decideJobSequence.xml create mode 100644 jee-7/src/main/resources/META-INF/batch-jobs/flowJobSequence.xml create mode 100644 jee-7/src/main/resources/META-INF/batch-jobs/injectionSimpleBatchLet.xml create mode 100644 jee-7/src/main/resources/META-INF/batch-jobs/partitionSimpleBatchLet.xml create mode 100644 jee-7/src/main/resources/META-INF/batch-jobs/simpleBatchLet.xml create mode 100644 jee-7/src/main/resources/META-INF/batch-jobs/simpleChunk.xml create mode 100644 jee-7/src/main/resources/META-INF/batch-jobs/simpleErrorChunk.xml create mode 100644 jee-7/src/main/resources/META-INF/batch-jobs/simpleErrorSkipChunk.xml create mode 100644 jee-7/src/main/resources/META-INF/batch-jobs/simpleJobSequence.xml create mode 100644 jee-7/src/main/resources/META-INF/batch-jobs/splitJobSequence.xml create mode 100644 jee-7/src/main/resources/META-INF/beans.xml create mode 100644 jee-7/src/test/java/com/baeldung/batch/understanding/BatchTestHelper.java create mode 100644 jee-7/src/test/java/com/baeldung/batch/understanding/CustomCheckPointUnitTest.java create mode 100644 jee-7/src/test/java/com/baeldung/batch/understanding/JobSequenceUnitTest.java create mode 100644 jee-7/src/test/java/com/baeldung/batch/understanding/SimpleBatchLetUnitTest.java create mode 100644 jee-7/src/test/java/com/baeldung/batch/understanding/SimpleChunkUnitTest.java create mode 100644 jee-7/src/test/java/com/baeldung/batch/understanding/SimpleErrorChunkUnitTest.java diff --git a/jee-7/pom.xml b/jee-7/pom.xml index 4f6e6a20fb..9011648d17 100644 --- a/jee-7/pom.xml +++ b/jee-7/pom.xml @@ -1,430 +1,537 @@ - - 4.0.0 - jee-7 - 1.0-SNAPSHOT - war - JavaEE 7 Arquillian Archetype Sample + + 4.0.0 + jee-7 + 1.0-SNAPSHOT + war + JavaEE 7 Arquillian Archetype Sample - - com.baeldung - parent-modules - 1.0.0-SNAPSHOT - + + com.baeldung + parent-modules + 1.0.0-SNAPSHOT + - - - javax - javaee-api - ${javaee_api.version} - provided - + + + javax + javaee-api + ${javaee_api.version} + provided + - - org.jboss.arquillian.junit - arquillian-junit-container - test - - - org.jboss.arquillian.graphene - graphene-webdriver - ${graphene-webdriver.version} - pom - test - - - com.jayway.awaitility - awaitility - ${awaitility.version} - test - + + org.jboss.arquillian.junit + arquillian-junit-container + test + + + org.jboss.arquillian.graphene + graphene-webdriver + ${graphene-webdriver.version} + pom + test + + + com.jayway.awaitility + awaitility + ${awaitility.version} + test + - - org.jboss.shrinkwrap.resolver - shrinkwrap-resolver-impl-maven - test - jar - + + org.jboss.shrinkwrap.resolver + shrinkwrap-resolver-impl-maven + test + jar + - - org.jboss.shrinkwrap.resolver - shrinkwrap-resolver-impl-maven-archive - test - - - org.apache.httpcomponents - httpclient - ${httpclient.version} - - - commons-io - commons-io - ${commons-io.version} - - - com.sun.faces - jsf-api - ${com.sun.faces.jsf.version} - - - com.sun.faces - jsf-impl - ${com.sun.faces.jsf.version} - - - javax.servlet - jstl - ${jstl.version} - - - javax.servlet - javax.servlet-api - ${javax.servlet-api.version} - - - javax.servlet.jsp - jsp-api - ${jsp-api.version} - provided - - - taglibs - standard - ${taglibs.standard.version} - + + org.jboss.shrinkwrap.resolver + shrinkwrap-resolver-impl-maven-archive + test + + + org.apache.httpcomponents + httpclient + ${httpclient.version} + + + commons-io + commons-io + ${commons-io.version} + + + com.sun.faces + jsf-api + ${com.sun.faces.jsf.version} + + + com.sun.faces + jsf-impl + ${com.sun.faces.jsf.version} + + + javax.servlet + jstl + ${jstl.version} + + + javax.servlet + javax.servlet-api + ${javax.servlet-api.version} + + + javax.servlet.jsp + jsp-api + ${jsp-api.version} + provided + + + taglibs + standard + ${taglibs.standard.version} + - - javax.mvc - javax.mvc-api - 20160715 - - - org.glassfish.ozark - ozark - ${ozark.version} - + + javax.mvc + javax.mvc-api + 20160715 + + + org.glassfish.ozark + ozark + ${ozark.version} + - - org.springframework.security - spring-security-web - ${org.springframework.security.version} - + + org.springframework.security + spring-security-web + ${org.springframework.security.version} + - - org.springframework.security - spring-security-config - ${org.springframework.security.version} - - - org.springframework.security - spring-security-taglibs - ${org.springframework.security.version} - - + + org.springframework.security + spring-security-config + ${org.springframework.security.version} + + + org.springframework.security + spring-security-taglibs + ${org.springframework.security.version} + + - - - - org.apache.maven.plugins - maven-war-plugin - ${maven-war-plugin.version} - - src/main/webapp - false - - - - + + org.jboss.spec.javax.batch + jboss-batch-api_1.0_spec + 1.0.0.Final + + + org.jberet + jberet-core + 1.0.2.Final + + + org.jberet + jberet-support + 1.0.2.Final + + + org.jboss.spec.javax.transaction + jboss-transaction-api_1.2_spec + 1.0.0.Final + + + org.jboss.marshalling + jboss-marshalling + 1.4.2.Final + + + org.jboss.weld + weld-core + 2.1.1.Final + + + org.jboss.weld.se + weld-se + 2.1.1.Final + + + org.jberet + jberet-se + 1.0.2.Final + + + com.h2database + h2 + 1.4.178 + + + org.glassfish.jersey.containers + jersey-container-jetty-servlet + 2.22.1 + + - - - - org.jboss.arquillian - arquillian-bom - ${arquillian_core.version} - import - pom - - - org.jboss.arquillian.extension - arquillian-drone-bom - ${arquillian-drone-bom.version} - pom - import - - - + + + + org.apache.maven.plugins + maven-war-plugin + ${maven-war-plugin.version} + + src/main/webapp + false + + + + + + + + org.eclipse.m2e + lifecycle-mapping + 1.0.0 + + + + + + + org.apache.maven.plugins + + + maven-pmd-plugin + + + [3.8,) + + + check + + + + + + + + + + + + + - - - wildfly-managed-arquillian - - true - - - standalone-full.xml - ${project.build.directory}/wildfly-${version.wildfly} - - - - io.undertow - undertow-websockets-jsr - ${undertow-websockets-jsr.version} - test - - - org.jboss.resteasy - resteasy-client - ${resteasy.version} - test - - - org.jboss.resteasy - resteasy-jaxb-provider - ${resteasy.version} - test - - - org.jboss.resteasy - resteasy-json-p-provider - ${resteasy.version} - test - - - org.wildfly - wildfly-arquillian-container-managed - ${wildfly.version} - test - - + + + + org.jboss.arquillian + arquillian-bom + ${arquillian_core.version} + import + pom + + + org.jboss.arquillian.extension + arquillian-drone-bom + ${arquillian-drone-bom.version} + pom + import + + + - - - - - maven-dependency-plugin - ${maven-dependency-plugin.version} - - ${maven.test.skip} - - - - unpack - process-test-classes - - unpack - - - - - org.wildfly - wildfly-dist - ${wildfly.version} - zip - false - ${project.build.directory} - - - - - - - - maven-surefire-plugin - ${maven-surefire-plugin.version} - - - ${project.build.directory}/wildfly-${wildfly.version} - - - - - - - - - wildfly-remote-arquillian - - - io.undertow - undertow-websockets-jsr - ${undertow-websockets-jsr.version} - test - - - org.jboss.resteasy - resteasy-client - ${resteasy.version} - test - - - org.jboss.resteasy - resteasy-jaxb-provider - ${resteasy.version} - test - - - org.jboss.resteasy - resteasy-json-p-provider - ${resteasy.version} - test - - - org.wildfly - wildfly-arquillian-container-remote - ${wildfly.version} - test - - - - - glassfish-embedded-arquillian - - - org.glassfish.main.extras - glassfish-embedded-all - ${glassfish-embedded-all.version} - test - - - org.glassfish - javax.json - ${javax.json.version} - test - - - org.glassfish.tyrus - tyrus-client - ${tyrus.version} - test - - - org.glassfish.tyrus - tyrus-container-grizzly-client - ${tyrus.version} - test - - - org.glassfish.jersey.core - jersey-client - ${jersey.version} - test - - - org.jboss.arquillian.container - arquillian-glassfish-embedded-3.1 - ${arquillian-glassfish.version} - test - - - - - glassfish-remote-arquillian - - - org.glassfish - javax.json - ${javax.json.version} - test - - - org.glassfish.tyrus - tyrus-client - ${tyrus.version} - test - - - org.glassfish.tyrus - tyrus-container-grizzly-client - ${tyrus.version} - test - - - org.glassfish.jersey.core - jersey-client - ${jersey.version} - test - - - org.glassfish.jersey.media - jersey-media-json-jackson - ${jersey.version} - test - - - org.glassfish.jersey.media - jersey-media-json-processing - ${jersey.version} - test - - - org.jboss.arquillian.container - arquillian-glassfish-remote-3.1 - ${arquillian-glassfish.version} - test - - - - - webdriver-chrome - - true - - - chrome - - - - webdriver-firefox - - firefox - - - + + + wildfly-managed-arquillian + + true + + + standalone-full.xml + ${project.build.directory}/wildfly-${version.wildfly} + + + + io.undertow + undertow-websockets-jsr + ${undertow-websockets-jsr.version} + test + + + org.jboss.resteasy + resteasy-client + ${resteasy.version} + test + + + org.jboss.resteasy + resteasy-jaxb-provider + ${resteasy.version} + test + + + org.jboss.resteasy + resteasy-json-p-provider + ${resteasy.version} + test + + + org.wildfly + wildfly-arquillian-container-managed + ${wildfly.version} + test + + + sun.jdk + jconsole + + + + - - - bintray-mvc-spec-maven - bintray - http://dl.bintray.com/mvc-spec/maven - - true - - - false - - - + + + + + maven-dependency-plugin + ${maven-dependency-plugin.version} + + ${maven.test.skip} + + + + unpack + process-test-classes + + unpack + + + + + org.wildfly + wildfly-dist + ${wildfly.version} + zip + false + ${project.build.directory} + + + sun.jdk + jconsole + + + + + + + + + + maven-surefire-plugin + ${maven-surefire-plugin.version} + + + ${project.build.directory}/wildfly-${wildfly.version} + + + + + + + + + wildfly-remote-arquillian + + + io.undertow + undertow-websockets-jsr + ${undertow-websockets-jsr.version} + test + + + org.jboss.resteasy + resteasy-client + ${resteasy.version} + test + + + org.jboss.resteasy + resteasy-jaxb-provider + ${resteasy.version} + test + + + org.jboss.resteasy + resteasy-json-p-provider + ${resteasy.version} + test + + + org.wildfly + wildfly-arquillian-container-remote + ${wildfly.version} + test + + + sun.jdk + jconsole + + + + + + + glassfish-embedded-arquillian + + + org.glassfish.main.extras + glassfish-embedded-all + ${glassfish-embedded-all.version} + test + + + org.glassfish + javax.json + ${javax.json.version} + test + + + org.glassfish.tyrus + tyrus-client + ${tyrus.version} + test + + + org.glassfish.tyrus + tyrus-container-grizzly-client + ${tyrus.version} + test + + + org.glassfish.jersey.core + jersey-client + ${jersey.version} + test + + + org.jboss.arquillian.container + arquillian-glassfish-embedded-3.1 + ${arquillian-glassfish.version} + test + + + + + glassfish-remote-arquillian + + + org.glassfish + javax.json + ${javax.json.version} + test + + + org.glassfish.tyrus + tyrus-client + ${tyrus.version} + test + + + org.glassfish.tyrus + tyrus-container-grizzly-client + ${tyrus.version} + test + + + org.glassfish.jersey.core + jersey-client + ${jersey.version} + test + + + org.glassfish.jersey.media + jersey-media-json-jackson + ${jersey.version} + test + + + org.glassfish.jersey.media + jersey-media-json-processing + ${jersey.version} + test + + + org.jboss.arquillian.container + arquillian-glassfish-remote-3.1 + ${arquillian-glassfish.version} + test + + + + + webdriver-chrome + + true + + + chrome + + + + webdriver-firefox + + firefox + + + - - 1.8 - 3.0.0 - 7.0 - 1.1.11.Final - 8.2.1.Final - 1.7.0 - 1.4.6.Final - 3.0.19.Final - 4.1.1 - 1.0.4 - 1.13 - 2.25 - 1.0.0.Final - 2.6 - 4.2.3.RELEASE - 2.21.0 - 1.1.2 - 2.4 - 2.2.14 - 4.5 - 2.0.1.Final - 3.1.0 - 2.1.0.Final - 2.8 - 1.2 - 2.2 - 20160715 - + + + bintray-mvc-spec-maven + bintray + http://dl.bintray.com/mvc-spec/maven + + true + + + false + + + - + + 1.8 + 3.0.0 + 7.0 + 1.1.11.Final + 8.2.1.Final + 1.7.0 + 1.4.6.Final + 3.0.19.Final + 4.1.1 + 1.0.4 + 1.13 + 2.25 + 1.0.0.Final + 2.6 + 4.2.3.RELEASE + 2.21.0 + 1.1.2 + 2.4 + 2.2.14 + 4.5 + 2.0.1.Final + 3.1.0 + 2.1.0.Final + 2.8 + 1.2 + 2.2 + 20160715 + + + \ No newline at end of file diff --git a/jee-7/src/main/java/com/baeldung/batch/understanding/ChunkExceptionSkipReadListener.java b/jee-7/src/main/java/com/baeldung/batch/understanding/ChunkExceptionSkipReadListener.java new file mode 100644 index 0000000000..ce47de66af --- /dev/null +++ b/jee-7/src/main/java/com/baeldung/batch/understanding/ChunkExceptionSkipReadListener.java @@ -0,0 +1,11 @@ +package com.baeldung.batch.understanding; + +import javax.batch.api.chunk.listener.SkipReadListener; +import javax.inject.Named; + +@Named +public class ChunkExceptionSkipReadListener implements SkipReadListener { + @Override + public void onSkipReadItem(Exception e) throws Exception { + } +} \ No newline at end of file diff --git a/jee-7/src/main/java/com/baeldung/batch/understanding/CustomCheckPoint.java b/jee-7/src/main/java/com/baeldung/batch/understanding/CustomCheckPoint.java new file mode 100644 index 0000000000..fe6759b365 --- /dev/null +++ b/jee-7/src/main/java/com/baeldung/batch/understanding/CustomCheckPoint.java @@ -0,0 +1,12 @@ +package com.baeldung.batch.understanding; + +import javax.batch.api.chunk.AbstractCheckpointAlgorithm; +import javax.inject.Named; + +@Named +public class CustomCheckPoint extends AbstractCheckpointAlgorithm { + @Override + public boolean isReadyToCheckpoint() throws Exception { + return SimpleChunkItemReader.COUNT % 5 == 0; + } +} diff --git a/jee-7/src/main/java/com/baeldung/batch/understanding/DeciderJobSequence.java b/jee-7/src/main/java/com/baeldung/batch/understanding/DeciderJobSequence.java new file mode 100644 index 0000000000..6cc2012f23 --- /dev/null +++ b/jee-7/src/main/java/com/baeldung/batch/understanding/DeciderJobSequence.java @@ -0,0 +1,14 @@ +package com.baeldung.batch.understanding; + +import javax.batch.api.Decider; +import javax.batch.runtime.StepExecution; +import javax.inject.Named; + +@Named +public class DeciderJobSequence implements Decider { + @Override + public String decide(StepExecution[] ses) throws Exception { + return "nothing"; + } + +} \ No newline at end of file diff --git a/jee-7/src/main/java/com/baeldung/batch/understanding/InjectSimpleBatchLet.java b/jee-7/src/main/java/com/baeldung/batch/understanding/InjectSimpleBatchLet.java new file mode 100644 index 0000000000..93eb20708d --- /dev/null +++ b/jee-7/src/main/java/com/baeldung/batch/understanding/InjectSimpleBatchLet.java @@ -0,0 +1,20 @@ +package com.baeldung.batch.understanding; + +import javax.batch.api.AbstractBatchlet; +import javax.batch.api.BatchProperty; +import javax.batch.runtime.BatchStatus; +import javax.inject.Inject; +import javax.inject.Named; + +@Named +public class InjectSimpleBatchLet extends AbstractBatchlet { + @Inject + @BatchProperty(name = "name") + private String nameString; + + @Override + public String process() throws Exception { + System.out.println("Value passed in = " + nameString); + return BatchStatus.COMPLETED.toString(); + } +} diff --git a/jee-7/src/main/java/com/baeldung/batch/understanding/SimpleBatchLet.java b/jee-7/src/main/java/com/baeldung/batch/understanding/SimpleBatchLet.java new file mode 100644 index 0000000000..6a367b064b --- /dev/null +++ b/jee-7/src/main/java/com/baeldung/batch/understanding/SimpleBatchLet.java @@ -0,0 +1,13 @@ +package com.baeldung.batch.understanding; + +import javax.batch.api.AbstractBatchlet; +import javax.batch.runtime.BatchStatus; +import javax.inject.Named; + +@Named +public class SimpleBatchLet extends AbstractBatchlet { + @Override + public String process() throws Exception { + return BatchStatus.FAILED.toString(); + } +} diff --git a/jee-7/src/main/java/com/baeldung/batch/understanding/SimpleChunkItemProcessor.java b/jee-7/src/main/java/com/baeldung/batch/understanding/SimpleChunkItemProcessor.java new file mode 100644 index 0000000000..3f8166b6d8 --- /dev/null +++ b/jee-7/src/main/java/com/baeldung/batch/understanding/SimpleChunkItemProcessor.java @@ -0,0 +1,12 @@ +package com.baeldung.batch.understanding; + +import javax.batch.api.chunk.ItemProcessor; +import javax.inject.Named; + +@Named +public class SimpleChunkItemProcessor implements ItemProcessor { + @Override + public Integer processItem(Object t) { + return ((Integer) t).intValue() % 2 == 0 ? null : ((Integer) t).intValue(); + } +} diff --git a/jee-7/src/main/java/com/baeldung/batch/understanding/SimpleChunkItemReader.java b/jee-7/src/main/java/com/baeldung/batch/understanding/SimpleChunkItemReader.java new file mode 100644 index 0000000000..10f81d95d0 --- /dev/null +++ b/jee-7/src/main/java/com/baeldung/batch/understanding/SimpleChunkItemReader.java @@ -0,0 +1,27 @@ +package com.baeldung.batch.understanding; + +import java.io.Serializable; +import java.util.StringTokenizer; +import javax.batch.api.chunk.AbstractItemReader; +import javax.inject.Named; + +@Named +public class SimpleChunkItemReader extends AbstractItemReader { + private StringTokenizer tokens; + public static int COUNT = 0; + + @Override + public Integer readItem() throws Exception { + if (tokens.hasMoreTokens()) { + COUNT++; + String tempTokenize = tokens.nextToken(); + return Integer.valueOf(tempTokenize); + } + return null; + } + + @Override + public void open(Serializable checkpoint) throws Exception { + tokens = new StringTokenizer("1,2,3,4,5,6,7,8,9,10", ","); + } +} diff --git a/jee-7/src/main/java/com/baeldung/batch/understanding/SimpleChunkItemReaderError.java b/jee-7/src/main/java/com/baeldung/batch/understanding/SimpleChunkItemReaderError.java new file mode 100644 index 0000000000..92096d0571 --- /dev/null +++ b/jee-7/src/main/java/com/baeldung/batch/understanding/SimpleChunkItemReaderError.java @@ -0,0 +1,31 @@ +package com.baeldung.batch.understanding; + +import java.io.Serializable; +import java.util.StringTokenizer; + +import javax.batch.api.chunk.AbstractItemReader; +import javax.inject.Named; + +@Named +public class SimpleChunkItemReaderError extends AbstractItemReader { + private StringTokenizer tokens; + public static int COUNT = 0; + + @Override + public Integer readItem() throws Exception { + if (tokens.hasMoreTokens()) { + COUNT++; + int token = Integer.valueOf(tokens.nextToken()); + if (token == 3) { + throw new RuntimeException("Something happened"); + } + return Integer.valueOf(token); + } + return null; + } + + @Override + public void open(Serializable checkpoint) throws Exception { + tokens = new StringTokenizer("1,2,3,4,5,6,7,8,9,10", ","); + } +} \ No newline at end of file diff --git a/jee-7/src/main/java/com/baeldung/batch/understanding/SimpleChunkWriter.java b/jee-7/src/main/java/com/baeldung/batch/understanding/SimpleChunkWriter.java new file mode 100644 index 0000000000..909596766d --- /dev/null +++ b/jee-7/src/main/java/com/baeldung/batch/understanding/SimpleChunkWriter.java @@ -0,0 +1,13 @@ +package com.baeldung.batch.understanding; + +import java.util.List; + +import javax.batch.api.chunk.AbstractItemWriter; +import javax.inject.Named; + +@Named +public class SimpleChunkWriter extends AbstractItemWriter { + @Override + public void writeItems(List items) throws Exception { + } +} diff --git a/jee-7/src/main/java/com/baeldung/batch/understanding/exception/MyInputRecord.java b/jee-7/src/main/java/com/baeldung/batch/understanding/exception/MyInputRecord.java new file mode 100644 index 0000000000..e813a699c2 --- /dev/null +++ b/jee-7/src/main/java/com/baeldung/batch/understanding/exception/MyInputRecord.java @@ -0,0 +1,41 @@ +package com.baeldung.batch.understanding.exception; + +import java.io.Serializable; + +public class MyInputRecord implements Serializable { + private int id; + + public MyInputRecord(int id) { + this.id = id; + } + + public int getId() { + return id; + } + + public void setId(int id) { + this.id = id; + } + + @Override + public boolean equals(Object o) { + if (this == o) + return true; + if (o == null || getClass() != o.getClass()) + return false; + + MyInputRecord that = (MyInputRecord) o; + + return id == that.id; + } + + @Override + public int hashCode() { + return id; + } + + @Override + public String toString() { + return "MyInputRecord: " + id; + } +} diff --git a/jee-7/src/main/java/com/baeldung/batch/understanding/exception/MyItemProcessor.java b/jee-7/src/main/java/com/baeldung/batch/understanding/exception/MyItemProcessor.java new file mode 100644 index 0000000000..4427a1f548 --- /dev/null +++ b/jee-7/src/main/java/com/baeldung/batch/understanding/exception/MyItemProcessor.java @@ -0,0 +1,15 @@ +package com.baeldung.batch.understanding.exception; + +import javax.batch.api.chunk.ItemProcessor; +import javax.inject.Named; + +@Named +public class MyItemProcessor implements ItemProcessor { + @Override + public Object processItem(Object t) { + if (((MyInputRecord) t).getId() == 6) { + throw new NullPointerException(); + } + return new MyOutputRecord(((MyInputRecord) t).getId()); + } +} diff --git a/jee-7/src/main/java/com/baeldung/batch/understanding/exception/MyItemReader.java b/jee-7/src/main/java/com/baeldung/batch/understanding/exception/MyItemReader.java new file mode 100644 index 0000000000..dd1876ab7d --- /dev/null +++ b/jee-7/src/main/java/com/baeldung/batch/understanding/exception/MyItemReader.java @@ -0,0 +1,42 @@ +package com.baeldung.batch.understanding.exception; + +import javax.batch.api.chunk.AbstractItemReader; +import javax.inject.Named; +import java.io.Serializable; +import java.util.StringTokenizer; + +@Named +public class MyItemReader extends AbstractItemReader { + private StringTokenizer tokens; + private MyInputRecord lastElement; + private boolean alreadyFailed; + + @Override + public void open(Serializable checkpoint) { + tokens = new StringTokenizer("1,2,3,4,5,6,7,8,9,10", ","); + if (checkpoint != null) { + while (!Integer.valueOf(tokens.nextToken()) + .equals(((MyInputRecord) checkpoint).getId())) { + } + } + } + + @Override + public Object readItem() { + if (tokens.hasMoreTokens()) { + int token = Integer.valueOf(tokens.nextToken()); + if (token == 5 && !alreadyFailed) { + alreadyFailed = true; + throw new IllegalArgumentException("Could not read record"); + } + lastElement = new MyInputRecord(token); + return lastElement; + } + return null; + } + + @Override + public Serializable checkpointInfo() throws Exception { + return lastElement; + } +} diff --git a/jee-7/src/main/java/com/baeldung/batch/understanding/exception/MyItemWriter.java b/jee-7/src/main/java/com/baeldung/batch/understanding/exception/MyItemWriter.java new file mode 100644 index 0000000000..4e80d86d44 --- /dev/null +++ b/jee-7/src/main/java/com/baeldung/batch/understanding/exception/MyItemWriter.java @@ -0,0 +1,19 @@ + +package com.baeldung.batch.understanding.exception; + +import javax.batch.api.chunk.AbstractItemWriter; +import javax.inject.Named; +import java.util.List; + +@Named +public class MyItemWriter extends AbstractItemWriter { + private static int retries = 0; + + @Override + public void writeItems(List list) { + if (retries <= 3 && list.contains(new MyOutputRecord(8))) { + retries++; + throw new UnsupportedOperationException(); + } + } +} diff --git a/jee-7/src/main/java/com/baeldung/batch/understanding/exception/MyOutputRecord.java b/jee-7/src/main/java/com/baeldung/batch/understanding/exception/MyOutputRecord.java new file mode 100644 index 0000000000..8f18fca7ba --- /dev/null +++ b/jee-7/src/main/java/com/baeldung/batch/understanding/exception/MyOutputRecord.java @@ -0,0 +1,39 @@ +package com.baeldung.batch.understanding.exception; + +import java.io.Serializable; + +public class MyOutputRecord implements Serializable { + private int id; + + public MyOutputRecord(int id) { + this.id = id; + } + + public int getId() { + return id; + } + + public void setId(int id) { + this.id = id; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + + MyOutputRecord that = (MyOutputRecord) o; + + return id == that.id; + } + + @Override + public int hashCode() { + return id; + } + + @Override + public String toString() { + return "MyOutputRecord: " + id; + } +} diff --git a/jee-7/src/main/java/com/baeldung/batch/understanding/exception/MyRetryProcessorListener.java b/jee-7/src/main/java/com/baeldung/batch/understanding/exception/MyRetryProcessorListener.java new file mode 100644 index 0000000000..3bd35820c3 --- /dev/null +++ b/jee-7/src/main/java/com/baeldung/batch/understanding/exception/MyRetryProcessorListener.java @@ -0,0 +1,11 @@ +package com.baeldung.batch.understanding.exception; + +import javax.batch.api.chunk.listener.RetryProcessListener; +import javax.inject.Named; + +@Named +public class MyRetryProcessorListener implements RetryProcessListener { + @Override + public void onRetryProcessException(Object item, Exception ex) throws Exception { + } +} diff --git a/jee-7/src/main/java/com/baeldung/batch/understanding/exception/MyRetryReadListener.java b/jee-7/src/main/java/com/baeldung/batch/understanding/exception/MyRetryReadListener.java new file mode 100644 index 0000000000..5c4f56b75d --- /dev/null +++ b/jee-7/src/main/java/com/baeldung/batch/understanding/exception/MyRetryReadListener.java @@ -0,0 +1,11 @@ +package com.baeldung.batch.understanding.exception; + +import javax.batch.api.chunk.listener.RetryReadListener; +import javax.inject.Named; + +@Named +public class MyRetryReadListener implements RetryReadListener { + @Override + public void onRetryReadException(Exception ex) throws Exception { + } +} diff --git a/jee-7/src/main/java/com/baeldung/batch/understanding/exception/MyRetryWriteListener.java b/jee-7/src/main/java/com/baeldung/batch/understanding/exception/MyRetryWriteListener.java new file mode 100644 index 0000000000..a8683d86a9 --- /dev/null +++ b/jee-7/src/main/java/com/baeldung/batch/understanding/exception/MyRetryWriteListener.java @@ -0,0 +1,12 @@ +package com.baeldung.batch.understanding.exception; + +import javax.batch.api.chunk.listener.RetryWriteListener; +import javax.inject.Named; +import java.util.List; + +@Named +public class MyRetryWriteListener implements RetryWriteListener { + @Override + public void onRetryWriteException(List items, Exception ex) throws Exception { + } +} diff --git a/jee-7/src/main/java/com/baeldung/batch/understanding/exception/MySkipProcessorListener.java b/jee-7/src/main/java/com/baeldung/batch/understanding/exception/MySkipProcessorListener.java new file mode 100644 index 0000000000..74f7565cf7 --- /dev/null +++ b/jee-7/src/main/java/com/baeldung/batch/understanding/exception/MySkipProcessorListener.java @@ -0,0 +1,11 @@ +package com.baeldung.batch.understanding.exception; + +import javax.batch.api.chunk.listener.SkipProcessListener; +import javax.inject.Named; + +@Named +public class MySkipProcessorListener implements SkipProcessListener { + @Override + public void onSkipProcessItem(Object t, Exception e) throws Exception { + } +} diff --git a/jee-7/src/main/java/com/baeldung/batch/understanding/exception/MySkipReadListener.java b/jee-7/src/main/java/com/baeldung/batch/understanding/exception/MySkipReadListener.java new file mode 100644 index 0000000000..aecaf93e75 --- /dev/null +++ b/jee-7/src/main/java/com/baeldung/batch/understanding/exception/MySkipReadListener.java @@ -0,0 +1,11 @@ +package com.baeldung.batch.understanding.exception; + +import javax.batch.api.chunk.listener.SkipReadListener; +import javax.inject.Named; + +@Named +public class MySkipReadListener implements SkipReadListener { + @Override + public void onSkipReadItem(Exception e) throws Exception { + } +} diff --git a/jee-7/src/main/java/com/baeldung/batch/understanding/exception/MySkipWriteListener.java b/jee-7/src/main/java/com/baeldung/batch/understanding/exception/MySkipWriteListener.java new file mode 100644 index 0000000000..928773b61d --- /dev/null +++ b/jee-7/src/main/java/com/baeldung/batch/understanding/exception/MySkipWriteListener.java @@ -0,0 +1,13 @@ +package com.baeldung.batch.understanding.exception; + +import javax.batch.api.chunk.listener.SkipWriteListener; +import javax.inject.Named; +import java.util.List; + +@Named +public class MySkipWriteListener implements SkipWriteListener { + @Override + public void onSkipWriteItem(List list, Exception e) throws Exception { + list.remove(new MyOutputRecord(2)); + } +} diff --git a/jee-7/src/main/resources/META-INF/batch-jobs/customCheckPoint.xml b/jee-7/src/main/resources/META-INF/batch-jobs/customCheckPoint.xml new file mode 100644 index 0000000000..b0f327dd39 --- /dev/null +++ b/jee-7/src/main/resources/META-INF/batch-jobs/customCheckPoint.xml @@ -0,0 +1,12 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/jee-7/src/main/resources/META-INF/batch-jobs/decideJobSequence.xml b/jee-7/src/main/resources/META-INF/batch-jobs/decideJobSequence.xml new file mode 100644 index 0000000000..4905586fb9 --- /dev/null +++ b/jee-7/src/main/resources/META-INF/batch-jobs/decideJobSequence.xml @@ -0,0 +1,18 @@ + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/jee-7/src/main/resources/META-INF/batch-jobs/flowJobSequence.xml b/jee-7/src/main/resources/META-INF/batch-jobs/flowJobSequence.xml new file mode 100644 index 0000000000..207a9242b1 --- /dev/null +++ b/jee-7/src/main/resources/META-INF/batch-jobs/flowJobSequence.xml @@ -0,0 +1,21 @@ + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/jee-7/src/main/resources/META-INF/batch-jobs/injectionSimpleBatchLet.xml b/jee-7/src/main/resources/META-INF/batch-jobs/injectionSimpleBatchLet.xml new file mode 100644 index 0000000000..d152f063f6 --- /dev/null +++ b/jee-7/src/main/resources/META-INF/batch-jobs/injectionSimpleBatchLet.xml @@ -0,0 +1,13 @@ + + + + + + + + + + \ No newline at end of file diff --git a/jee-7/src/main/resources/META-INF/batch-jobs/partitionSimpleBatchLet.xml b/jee-7/src/main/resources/META-INF/batch-jobs/partitionSimpleBatchLet.xml new file mode 100644 index 0000000000..76c64ee899 --- /dev/null +++ b/jee-7/src/main/resources/META-INF/batch-jobs/partitionSimpleBatchLet.xml @@ -0,0 +1,24 @@ + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/jee-7/src/main/resources/META-INF/batch-jobs/simpleBatchLet.xml b/jee-7/src/main/resources/META-INF/batch-jobs/simpleBatchLet.xml new file mode 100644 index 0000000000..9c707de8a4 --- /dev/null +++ b/jee-7/src/main/resources/META-INF/batch-jobs/simpleBatchLet.xml @@ -0,0 +1,9 @@ + + + + + + \ No newline at end of file diff --git a/jee-7/src/main/resources/META-INF/batch-jobs/simpleChunk.xml b/jee-7/src/main/resources/META-INF/batch-jobs/simpleChunk.xml new file mode 100644 index 0000000000..b2b15b7bd9 --- /dev/null +++ b/jee-7/src/main/resources/META-INF/batch-jobs/simpleChunk.xml @@ -0,0 +1,11 @@ + + + + + + + + + + \ No newline at end of file diff --git a/jee-7/src/main/resources/META-INF/batch-jobs/simpleErrorChunk.xml b/jee-7/src/main/resources/META-INF/batch-jobs/simpleErrorChunk.xml new file mode 100644 index 0000000000..743fcafd65 --- /dev/null +++ b/jee-7/src/main/resources/META-INF/batch-jobs/simpleErrorChunk.xml @@ -0,0 +1,24 @@ + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/jee-7/src/main/resources/META-INF/batch-jobs/simpleErrorSkipChunk.xml b/jee-7/src/main/resources/META-INF/batch-jobs/simpleErrorSkipChunk.xml new file mode 100644 index 0000000000..2fafb83cc7 --- /dev/null +++ b/jee-7/src/main/resources/META-INF/batch-jobs/simpleErrorSkipChunk.xml @@ -0,0 +1,26 @@ + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/jee-7/src/main/resources/META-INF/batch-jobs/simpleJobSequence.xml b/jee-7/src/main/resources/META-INF/batch-jobs/simpleJobSequence.xml new file mode 100644 index 0000000000..7110db7f23 --- /dev/null +++ b/jee-7/src/main/resources/META-INF/batch-jobs/simpleJobSequence.xml @@ -0,0 +1,17 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/jee-7/src/main/resources/META-INF/batch-jobs/splitJobSequence.xml b/jee-7/src/main/resources/META-INF/batch-jobs/splitJobSequence.xml new file mode 100644 index 0000000000..0a9ea97dc7 --- /dev/null +++ b/jee-7/src/main/resources/META-INF/batch-jobs/splitJobSequence.xml @@ -0,0 +1,22 @@ + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/jee-7/src/main/resources/META-INF/beans.xml b/jee-7/src/main/resources/META-INF/beans.xml new file mode 100644 index 0000000000..ae0f4bf2ee --- /dev/null +++ b/jee-7/src/main/resources/META-INF/beans.xml @@ -0,0 +1,8 @@ + + + \ No newline at end of file diff --git a/jee-7/src/test/java/com/baeldung/batch/understanding/BatchTestHelper.java b/jee-7/src/test/java/com/baeldung/batch/understanding/BatchTestHelper.java new file mode 100644 index 0000000000..5060bd9b91 --- /dev/null +++ b/jee-7/src/test/java/com/baeldung/batch/understanding/BatchTestHelper.java @@ -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 getMetricsMap(Metric[] metrics) { + Map metricsMap = new HashMap<>(); + for (Metric metric : metrics) { + metricsMap.put(metric.getType(), metric.getValue()); + } + return metricsMap; + } + +} diff --git a/jee-7/src/test/java/com/baeldung/batch/understanding/CustomCheckPointUnitTest.java b/jee-7/src/test/java/com/baeldung/batch/understanding/CustomCheckPointUnitTest.java new file mode 100644 index 0000000000..a9488c5c03 --- /dev/null +++ b/jee-7/src/test/java/com/baeldung/batch/understanding/CustomCheckPointUnitTest.java @@ -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 metricsMap = BatchTestHelper.getMetricsMap(stepExecution.getMetrics()); + assertEquals(3L, metricsMap.get(Metric.MetricType.COMMIT_COUNT) + .longValue()); + } + } + assertEquals(jobExecution.getBatchStatus(), BatchStatus.COMPLETED); + } +} diff --git a/jee-7/src/test/java/com/baeldung/batch/understanding/JobSequenceUnitTest.java b/jee-7/src/test/java/com/baeldung/batch/understanding/JobSequenceUnitTest.java new file mode 100644 index 0000000000..7c5e8d0b78 --- /dev/null +++ b/jee-7/src/test/java/com/baeldung/batch/understanding/JobSequenceUnitTest.java @@ -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 stepExecutions = jobOperator.getStepExecutions(executionId); + List 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 stepExecutions = jobOperator.getStepExecutions(executionId); + List 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); + } +} diff --git a/jee-7/src/test/java/com/baeldung/batch/understanding/SimpleBatchLetUnitTest.java b/jee-7/src/test/java/com/baeldung/batch/understanding/SimpleBatchLetUnitTest.java new file mode 100644 index 0000000000..485c997cc6 --- /dev/null +++ b/jee-7/src/test/java/com/baeldung/batch/understanding/SimpleBatchLetUnitTest.java @@ -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); + } +} diff --git a/jee-7/src/test/java/com/baeldung/batch/understanding/SimpleChunkUnitTest.java b/jee-7/src/test/java/com/baeldung/batch/understanding/SimpleChunkUnitTest.java new file mode 100644 index 0000000000..57c794ba00 --- /dev/null +++ b/jee-7/src/test/java/com/baeldung/batch/understanding/SimpleChunkUnitTest.java @@ -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 stepExecutions = jobOperator.getStepExecutions(executionId); + for (StepExecution stepExecution : stepExecutions) { + if (stepExecution.getStepName() + .equals("firstChunkStep")) { + Map 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 stepExecutions = jobOperator.getStepExecutions(executionId); + assertEquals("firstChunkStep", stepExecutions.get(0).getStepName()); + // finding out batch status + assertEquals(BatchStatus.COMPLETED, stepExecutions.get(0).getBatchStatus()); + Map 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); + } +} diff --git a/jee-7/src/test/java/com/baeldung/batch/understanding/SimpleErrorChunkUnitTest.java b/jee-7/src/test/java/com/baeldung/batch/understanding/SimpleErrorChunkUnitTest.java new file mode 100644 index 0000000000..0f6d068888 --- /dev/null +++ b/jee-7/src/test/java/com/baeldung/batch/understanding/SimpleErrorChunkUnitTest.java @@ -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 stepExecutions = jobOperator.getStepExecutions(executionId); + for (StepExecution stepExecution : stepExecutions) { + if (stepExecution.getStepName() + .equals("errorStep")) { + Map 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); + } +}