BAEL-2168 Java EE 7 batch processing (#5861)
* review changes * Fixed public static count * review comments
This commit is contained in:
committed by
Josh Cummings
parent
547f99925f
commit
b0f62f4eca
@@ -1,12 +1,22 @@
|
||||
package com.baeldung.batch.understanding;
|
||||
|
||||
import javax.batch.api.chunk.AbstractCheckpointAlgorithm;
|
||||
import javax.batch.runtime.context.JobContext;
|
||||
import javax.inject.Inject;
|
||||
import javax.inject.Named;
|
||||
|
||||
@Named
|
||||
public class CustomCheckPoint extends AbstractCheckpointAlgorithm {
|
||||
|
||||
@Inject
|
||||
JobContext jobContext;
|
||||
|
||||
private Integer counterRead = 0;
|
||||
|
||||
@Override
|
||||
public boolean isReadyToCheckpoint() throws Exception {
|
||||
return SimpleChunkItemReader.COUNT % 5 == 0;
|
||||
counterRead = (Integer)jobContext.getTransientUserData();
|
||||
System.out.println("counterRead : " + counterRead);
|
||||
return counterRead % 5 == 0;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,20 +1,38 @@
|
||||
package com.baeldung.batch.understanding;
|
||||
|
||||
import java.util.Properties;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
import javax.batch.api.AbstractBatchlet;
|
||||
import javax.batch.api.BatchProperty;
|
||||
import javax.batch.runtime.BatchStatus;
|
||||
import javax.batch.runtime.context.JobContext;
|
||||
import javax.batch.runtime.context.StepContext;
|
||||
import javax.inject.Inject;
|
||||
import javax.inject.Named;
|
||||
|
||||
@Named
|
||||
public class InjectSimpleBatchLet extends AbstractBatchlet {
|
||||
Logger logger = Logger.getLogger(InjectSimpleBatchLet.class.getName());
|
||||
|
||||
@Inject
|
||||
@BatchProperty(name = "name")
|
||||
private String nameString;
|
||||
|
||||
@Inject
|
||||
StepContext stepContext;
|
||||
private Properties stepProperties;
|
||||
@Inject
|
||||
JobContext jobContext;
|
||||
private Properties jobProperties;
|
||||
|
||||
@Override
|
||||
public String process() throws Exception {
|
||||
System.out.println("Value passed in = " + nameString);
|
||||
logger.info("BatchProperty : " + nameString);
|
||||
stepProperties = stepContext.getProperties();
|
||||
jobProperties = jobContext.getProperties();
|
||||
logger.info("Step property : "+ stepProperties.getProperty("stepProp1"));
|
||||
logger.info("job property : "+jobProperties.getProperty("jobProp1"));
|
||||
return BatchStatus.COMPLETED.toString();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,20 +1,28 @@
|
||||
package com.baeldung.batch.understanding;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.Properties;
|
||||
import java.util.StringTokenizer;
|
||||
import javax.batch.api.chunk.AbstractItemReader;
|
||||
import javax.batch.runtime.BatchStatus;
|
||||
import javax.batch.runtime.context.JobContext;
|
||||
import javax.inject.Inject;
|
||||
import javax.inject.Named;
|
||||
|
||||
@Named
|
||||
public class SimpleChunkItemReader extends AbstractItemReader {
|
||||
private StringTokenizer tokens;
|
||||
public static int COUNT = 0;
|
||||
private Integer count=0;
|
||||
|
||||
@Inject
|
||||
JobContext jobContext;
|
||||
|
||||
@Override
|
||||
public Integer readItem() throws Exception {
|
||||
if (tokens.hasMoreTokens()) {
|
||||
COUNT++;
|
||||
this.count++;
|
||||
String tempTokenize = tokens.nextToken();
|
||||
jobContext.setTransientUserData(count);
|
||||
return Integer.valueOf(tempTokenize);
|
||||
}
|
||||
return null;
|
||||
@@ -24,4 +32,5 @@ public class SimpleChunkItemReader extends AbstractItemReader {
|
||||
public void open(Serializable checkpoint) throws Exception {
|
||||
tokens = new StringTokenizer("1,2,3,4,5,6,7,8,9,10", ",");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
+7
-2
@@ -4,17 +4,22 @@ import java.io.Serializable;
|
||||
import java.util.StringTokenizer;
|
||||
|
||||
import javax.batch.api.chunk.AbstractItemReader;
|
||||
import javax.batch.runtime.context.JobContext;
|
||||
import javax.inject.Inject;
|
||||
import javax.inject.Named;
|
||||
|
||||
@Named
|
||||
public class SimpleChunkItemReaderError extends AbstractItemReader {
|
||||
@Inject
|
||||
JobContext jobContext;
|
||||
private StringTokenizer tokens;
|
||||
public static int COUNT = 0;
|
||||
private Integer count = 0;
|
||||
|
||||
@Override
|
||||
public Integer readItem() throws Exception {
|
||||
if (tokens.hasMoreTokens()) {
|
||||
COUNT++;
|
||||
count++;
|
||||
jobContext.setTransientUserData(count);
|
||||
int token = Integer.valueOf(tokens.nextToken());
|
||||
if (token == 3) {
|
||||
throw new RuntimeException("Something happened");
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package com.baeldung.batch.understanding;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import javax.batch.api.chunk.AbstractItemWriter;
|
||||
@@ -7,7 +8,9 @@ import javax.inject.Named;
|
||||
|
||||
@Named
|
||||
public class SimpleChunkWriter extends AbstractItemWriter {
|
||||
List<Integer> processed = new ArrayList<>();
|
||||
@Override
|
||||
public void writeItems(List<Object> items) throws Exception {
|
||||
items.stream().map(Integer.class::cast).forEach(this.processed::add);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user