Skip to content

Commit b050b37

Browse files
committed
Merge pull request #212 from radcortez/master
Fixed Chunk Exception Test.
2 parents 10da618 + 5a2cb46 commit b050b37

File tree

15 files changed

+181
-48
lines changed

15 files changed

+181
-48
lines changed

batch/chunk-exception/pom.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@
1010

1111
<artifactId>chunk-exception</artifactId>
1212
<packaging>war</packaging>
13-
14-
<name>${project.artifactId}</name>
13+
<name>Batch Chunk Exception</name>
14+
<description>Chunk Exception Handling - Retrying and Skipping</description>
1515

1616
<dependencies>
1717
<dependency>

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,5 @@
77
*/
88
public class ChunkExceptionRecorder {
99
public static CountDownLatch chunkExceptionsCountDownLatch = new CountDownLatch(3);
10+
public static int retryReadExecutions = 0;
1011
}

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

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,14 +39,14 @@
3939
*/
4040
package org.javaee7.batch.chunk.exception;
4141

42+
import java.io.Serializable;
43+
4244
/**
4345
* @author Arun Gupta
4446
*/
45-
public class MyInputRecord {
47+
public class MyInputRecord implements Serializable {
4648
private int id;
47-
48-
public MyInputRecord() { }
49-
49+
5050
public MyInputRecord(int id) {
5151
this.id = id;
5252
}
@@ -58,7 +58,22 @@ public int getId() {
5858
public void setId(int id) {
5959
this.id = id;
6060
}
61-
61+
62+
@Override
63+
public boolean equals(Object o) {
64+
if (this == o) return true;
65+
if (o == null || getClass() != o.getClass()) return false;
66+
67+
MyInputRecord that = (MyInputRecord) o;
68+
69+
return id == that.id;
70+
}
71+
72+
@Override
73+
public int hashCode() {
74+
return id;
75+
}
76+
6277
@Override
6378
public String toString() {
6479
return "MyInputRecord: " + id;

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

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -51,11 +51,10 @@ public class MyItemProcessor implements ItemProcessor {
5151
@Override
5252
public Object processItem(Object t) {
5353
System.out.println("MyItemProcessor.processItem: " + t);
54-
55-
if (((MyInputRecord)t).getId() == 6)
54+
55+
if (((MyInputRecord) t).getId() == 6)
5656
throw new NullPointerException();
57-
58-
// return (t.getId() % 2 == 0) ? null : new MyOutputRecord(t.getId() * 2);
59-
return new MyOutputRecord(((MyInputRecord)t).getId() * 2);
57+
58+
return new MyOutputRecord(((MyInputRecord) t).getId());
6059
}
6160
}

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

Lines changed: 32 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -39,33 +39,55 @@
3939
*/
4040
package org.javaee7.batch.chunk.exception;
4141

42-
import java.io.Serializable;
43-
import java.util.StringTokenizer;
4442
import javax.batch.api.chunk.AbstractItemReader;
4543
import javax.inject.Named;
44+
import java.io.Serializable;
45+
import java.util.StringTokenizer;
4646

4747
/**
4848
* @author Arun Gupta
4949
*/
5050
@Named
5151
public class MyItemReader extends AbstractItemReader {
52-
52+
5353
private StringTokenizer tokens;
54-
54+
55+
private MyInputRecord lastElement;
56+
private boolean alreadyFailed;
57+
5558
@Override
56-
public void open(Serializable c) {
59+
public void open(Serializable checkpoint) {
5760
tokens = new StringTokenizer("1,2,3,4,5,6,7,8,9,10", ",");
61+
62+
// This will place the nextToken into the last batch checkpoint. Called on exception retry.
63+
if (checkpoint != null) {
64+
while (!Integer.valueOf(tokens.nextToken()).equals(((MyInputRecord) checkpoint).getId())) {
65+
System.out.println("Skipping already read elements");
66+
}
67+
}
5868
}
59-
69+
6070
@Override
6171
public Object readItem() {
6272
if (tokens.hasMoreTokens()) {
6373
int token = Integer.valueOf(tokens.nextToken());
64-
if (token == 3)
65-
throw new IllegalArgumentException();
66-
67-
return new MyInputRecord(token);
74+
75+
// Simulate a read exception when the token is equal to 5. Do it once only.
76+
if (token == 5 && !alreadyFailed) {
77+
alreadyFailed = true;
78+
throw new IllegalArgumentException("Could not read record");
79+
}
80+
81+
lastElement = new MyInputRecord(token);
82+
System.out.println("MyItemReader.readItem " + lastElement);
83+
return lastElement;
6884
}
6985
return null;
7086
}
87+
88+
@Override
89+
public Serializable checkpointInfo() throws Exception {
90+
// This is used internally by batch to stop the retry. Remember to implement equals on the read elements.
91+
return lastElement;
92+
}
7193
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,9 @@
3939
*/
4040
package org.javaee7.batch.chunk.exception;
4141

42-
import java.util.List;
4342
import javax.batch.api.chunk.AbstractItemWriter;
4443
import javax.inject.Named;
44+
import java.util.List;
4545

4646
/**
4747
* @author Arun Gupta
@@ -51,7 +51,7 @@ public class MyItemWriter extends AbstractItemWriter {
5151

5252
@Override
5353
public void writeItems(List list) {
54-
if (list.contains(new MyOutputRecord(2))) {
54+
if (list.contains(new MyOutputRecord(8))) {
5555
throw new IllegalArgumentException();
5656
}
5757

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

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -39,14 +39,14 @@
3939
*/
4040
package org.javaee7.batch.chunk.exception;
4141

42+
import java.io.Serializable;
43+
4244
/**
4345
* @author Arun Gupta
4446
*/
45-
public class MyOutputRecord {
47+
public class MyOutputRecord implements Serializable {
4648
private int id;
47-
48-
public MyOutputRecord() { }
49-
49+
5050
public MyOutputRecord(int id) {
5151
this.id = id;
5252
}
@@ -58,11 +58,6 @@ public int getId() {
5858
public void setId(int id) {
5959
this.id = id;
6060
}
61-
62-
@Override
63-
public String toString() {
64-
return "MyOutputRecord: " + id;
65-
}
6661

6762
@Override
6863
public boolean equals(Object o) {
@@ -78,4 +73,9 @@ public boolean equals(Object o) {
7873
public int hashCode() {
7974
return id;
8075
}
76+
77+
@Override
78+
public String toString() {
79+
return "MyOutputRecord: " + id;
80+
}
8181
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package org.javaee7.batch.chunk.exception;
2+
3+
import javax.batch.api.chunk.listener.RetryProcessListener;
4+
import javax.inject.Named;
5+
6+
/**
7+
* @author Roberto Cortez
8+
*/
9+
@Named
10+
public class MyRetryProcessorListener implements RetryProcessListener {
11+
@Override
12+
public void onRetryProcessException(Object item, Exception ex) throws Exception {
13+
System.out.println("MyRetryProcessorListener.onRetryProcessException");
14+
}
15+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package org.javaee7.batch.chunk.exception;
2+
3+
import javax.batch.api.chunk.listener.RetryReadListener;
4+
import javax.inject.Named;
5+
6+
/**
7+
* @author Roberto Cortez
8+
*/
9+
@Named
10+
public class MyRetryReadListener implements RetryReadListener {
11+
@Override
12+
public void onRetryReadException(Exception ex) throws Exception {
13+
ChunkExceptionRecorder.retryReadExecutions++;
14+
ChunkExceptionRecorder.chunkExceptionsCountDownLatch.countDown();
15+
System.out.println("MyRetryReadListener.onRetryReadException " + ex.getMessage());
16+
}
17+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package org.javaee7.batch.chunk.exception;
2+
3+
import javax.batch.api.chunk.listener.RetryWriteListener;
4+
import javax.inject.Named;
5+
import java.util.List;
6+
7+
/**
8+
* @author Roberto Cortez
9+
*/
10+
@Named
11+
public class MyRetryWriteListener implements RetryWriteListener {
12+
@Override
13+
public void onRetryWriteException(List<Object> items, Exception ex) throws Exception {
14+
System.out.println("MyRetryWriteListener.onRetryWriteException");
15+
}
16+
}

0 commit comments

Comments
 (0)