Skip to content

Commit 2013686

Browse files
committed
Merge branch 'master' of github.com:javaee-samples/javaee7-samples
2 parents 8cc7c7f + 7be2ecf commit 2013686

39 files changed

+25082
-10
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ bin/
44
target/
55
libs/
66
tmp/
7+
node_modules/
78

89
# OS Files #
910
.DS_Store

batch/chunk-exception/pom.xml

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,16 @@
77
<version>1.0-SNAPSHOT</version>
88
<relativePath>../pom.xml</relativePath>
99
</parent>
10-
11-
<groupId>org.javaee7.batch</groupId>
10+
1211
<artifactId>chunk-exception</artifactId>
13-
<version>1.0-SNAPSHOT</version>
1412
<packaging>war</packaging>
1513

1614
<name>${project.artifactId}</name>
1715

16+
<dependencies>
17+
<dependency>
18+
<groupId>org.javaee7</groupId>
19+
<artifactId>util-samples</artifactId>
20+
</dependency>
21+
</dependencies>
1822
</project>
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package org.javaee7.batch.chunk.exception;
2+
3+
import java.util.concurrent.CountDownLatch;
4+
5+
/**
6+
* @author Roberto Cortez
7+
*/
8+
public class ChunkExceptionRecorder {
9+
public static CountDownLatch chunkExceptionsCountDownLatch = new CountDownLatch(3);
10+
}

batch/chunk-exception/src/main/java/org/javaee7/batch/chunk/exception/MyItemWriter.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,10 @@ public class MyItemWriter extends AbstractItemWriter {
5151

5252
@Override
5353
public void writeItems(List list) {
54+
if (list.contains(new MyOutputRecord(2))) {
55+
throw new IllegalArgumentException();
56+
}
57+
5458
System.out.println("MyItemWriter.writeItems: " + list);
5559
}
5660
}

batch/chunk-exception/src/main/java/org/javaee7/batch/chunk/exception/MyOutputRecord.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,4 +63,19 @@ public void setId(int id) {
6363
public String toString() {
6464
return "MyOutputRecord: " + id;
6565
}
66+
67+
@Override
68+
public boolean equals(Object o) {
69+
if (this == o) return true;
70+
if (o == null || getClass() != o.getClass()) return false;
71+
72+
MyOutputRecord that = (MyOutputRecord) o;
73+
74+
return id == that.id;
75+
}
76+
77+
@Override
78+
public int hashCode() {
79+
return id;
80+
}
6681
}

batch/chunk-exception/src/main/java/org/javaee7/batch/chunk/exception/MySkipProcessorListener.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ public class MySkipProcessorListener implements SkipProcessListener {
5151

5252
@Override
5353
public void onSkipProcessItem(Object t, Exception e) throws Exception {
54+
ChunkExceptionRecorder.chunkExceptionsCountDownLatch.countDown();
5455
System.err.println("MySkipProcessorListener.onSkipProcessItem: " + ((MyInputRecord)t).getId() + ", " + e.getMessage());
5556
}
5657

batch/chunk-exception/src/main/java/org/javaee7/batch/chunk/exception/MySkipReadListener.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ public class MySkipReadListener implements SkipReadListener {
5151

5252
@Override
5353
public void onSkipReadItem(Exception e) throws Exception {
54+
ChunkExceptionRecorder.chunkExceptionsCountDownLatch.countDown();
5455
System.err.println("MySkipReadListener.onSkipReadItem: " + e.getMessage());
5556
}
5657

batch/chunk-exception/src/main/java/org/javaee7/batch/chunk/exception/MySkipWriteListener.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,9 @@ public class MySkipWriteListener implements SkipWriteListener {
5252

5353
@Override
5454
public void onSkipWriteItem(List list, Exception e) throws Exception {
55+
ChunkExceptionRecorder.chunkExceptionsCountDownLatch.countDown();
5556
System.err.println("MySkipWriteListener.onSkipWriteItem: " + list.size() + ", " + e.getMessage());
57+
list.remove(new MyOutputRecord(2));
5658
}
5759

5860
}

batch/chunk-exception/src/main/resources/META-INF/batch-jobs/myJob.xml

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -48,17 +48,17 @@
4848
<listener ref="mySkipWriteListener"/>
4949
</listeners>
5050
<chunk item-count="5" skip-limit="5">
51-
<reader ref="myItemReader"></reader>
52-
<processor ref="myItemProcessor"></processor>
53-
<writer ref="myItemWriter"></writer>
51+
<reader ref="myItemReader"/>
52+
<processor ref="myItemProcessor"/>
53+
<writer ref="myItemWriter"/>
5454
<skippable-exception-classes>
5555
<include class="java.lang.RuntimeException"/>
5656
<exclude class="java.io.IOException"/>
5757
</skippable-exception-classes>
58-
<!-- <retryable-exception-classes>
58+
<retryable-exception-classes>
5959
<include class="java.lang.IllegalArgumentException"/>
60-
</retryable-exception-classes>-->
61-
</chunk>
60+
</retryable-exception-classes>
61+
</chunk>
6262
</step>
6363
</job>
6464

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package org.javaee7.batch.chunk.exception;
2+
3+
import org.javaee7.util.BatchTestHelper;
4+
import org.jboss.arquillian.container.test.api.Deployment;
5+
import org.jboss.arquillian.junit.Arquillian;
6+
import org.jboss.shrinkwrap.api.ArchivePaths;
7+
import org.jboss.shrinkwrap.api.ShrinkWrap;
8+
import org.jboss.shrinkwrap.api.asset.EmptyAsset;
9+
import org.jboss.shrinkwrap.api.spec.WebArchive;
10+
import org.junit.Test;
11+
import org.junit.runner.RunWith;
12+
13+
import javax.batch.operations.JobOperator;
14+
import javax.batch.runtime.*;
15+
import java.util.List;
16+
import java.util.Map;
17+
import java.util.Properties;
18+
import java.util.concurrent.TimeUnit;
19+
20+
import static org.junit.Assert.assertEquals;
21+
import static org.junit.Assert.assertTrue;
22+
23+
/**
24+
* @author Roberto Cortez
25+
*/
26+
@RunWith(Arquillian.class)
27+
public class BatchChunkExceptionTest {
28+
@Deployment
29+
public static WebArchive createDeployment() {
30+
WebArchive war = ShrinkWrap.create(WebArchive.class)
31+
.addClass(BatchTestHelper.class)
32+
.addPackage("org.javaee7.batch.chunk.exception")
33+
.addAsWebInfResource(EmptyAsset.INSTANCE, ArchivePaths.create("beans.xml"))
34+
.addAsResource("META-INF/batch-jobs/myJob.xml");
35+
System.out.println(war.toString(true));
36+
return war;
37+
}
38+
39+
@Test
40+
public void testBatchChunkException() throws Exception {
41+
JobOperator jobOperator = BatchRuntime.getJobOperator();
42+
Long executionId = jobOperator.start("myJob", new Properties());
43+
JobExecution jobExecution = jobOperator.getJobExecution(executionId);
44+
45+
BatchTestHelper.keepTestAlive(jobExecution);
46+
47+
List<StepExecution> stepExecutions = jobOperator.getStepExecutions(executionId);
48+
for (StepExecution stepExecution : stepExecutions) {
49+
if (stepExecution.getStepName().equals("myStep")) {
50+
Map<Metric.MetricType, Long> metricsMap = BatchTestHelper.getMetricsMap(stepExecution.getMetrics());
51+
52+
assertEquals(1L, (long) metricsMap.get(Metric.MetricType.READ_SKIP_COUNT));
53+
assertEquals(1L, (long) metricsMap.get(Metric.MetricType.PROCESS_SKIP_COUNT));
54+
assertEquals(1L, (long) metricsMap.get(Metric.MetricType.WRITE_SKIP_COUNT));
55+
}
56+
}
57+
58+
assertTrue(ChunkExceptionRecorder.chunkExceptionsCountDownLatch.await(0, TimeUnit.SECONDS));
59+
assertEquals(BatchStatus.COMPLETED, jobExecution.getBatchStatus());
60+
}
61+
}

0 commit comments

Comments
 (0)