Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions batch/chunk-mapper/pom.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.javaee7.batch</groupId>
Expand All @@ -8,11 +9,15 @@
<relativePath>../pom.xml</relativePath>
</parent>

<groupId>org.javaee7.batch</groupId>
<artifactId>chunk-mapper</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>

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

<dependencies>
<dependency>
<groupId>org.javaee7</groupId>
<artifactId>util-samples</artifactId>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,9 @@
package org.javaee7.batch.sample.chunk.mapper;

import java.io.Serializable;
import java.util.Properties;
import java.util.StringTokenizer;
import javax.batch.api.BatchProperty;
import javax.batch.api.chunk.AbstractItemReader;
import javax.batch.runtime.BatchRuntime;
import javax.batch.runtime.context.JobContext;
import javax.inject.Inject;
import javax.inject.Named;
Expand All @@ -54,6 +52,8 @@
*/
@Named
public class MyItemReader extends AbstractItemReader {
public static int totalReaders = 0;
private int readerId;

private StringTokenizer tokens;

Expand All @@ -66,7 +66,7 @@ public class MyItemReader extends AbstractItemReader {
private String endProp;

@Inject
JobContext context;
private JobContext context;

@Override
public void open(Serializable e) {
Expand All @@ -81,14 +81,16 @@ public void open(Serializable e) {
if (i < end)
builder.append(",");
}

readerId = ++totalReaders;
tokens = new StringTokenizer(builder.toString(), ",");
}

@Override
public MyInputRecord readItem() {
if (tokens.hasMoreTokens()) {
int token = Integer.valueOf(tokens.nextToken());
System.out.format("readItem (%d): %d", context.getExecutionId(), token);
System.out.format("readItem (%d): %d\n", readerId, token);
return new MyInputRecord(token);
}
return null;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package org.javaee7.batch.sample.chunk.mapper;

import org.javaee7.util.BatchTestHelper;
import org.jboss.arquillian.container.test.api.Deployment;
import org.jboss.arquillian.junit.Arquillian;
import org.jboss.shrinkwrap.api.ArchivePaths;
import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.api.asset.EmptyAsset;
import org.jboss.shrinkwrap.api.spec.WebArchive;
import org.junit.Test;
import org.junit.runner.RunWith;

import javax.batch.operations.JobOperator;
import javax.batch.runtime.*;
import java.util.List;
import java.util.Map;
import java.util.Properties;

import static org.junit.Assert.assertEquals;

/**
* @author Roberto Cortez
*/
@RunWith(Arquillian.class)
public class BatchChunkMapperTest {
@Deployment
public static WebArchive createDeployment() {
WebArchive war = ShrinkWrap.create(WebArchive.class)
.addClass(BatchTestHelper.class)
.addPackage("org.javaee7.batch.sample.chunk.mapper")
.addAsWebInfResource(EmptyAsset.INSTANCE, ArchivePaths.create("beans.xml"))
.addAsResource("META-INF/batch-jobs/myJob.xml");
System.out.println(war.toString(true));
return war;
}

@Test
public void testBatchChunkMapper() throws Exception {
JobOperator jobOperator = BatchRuntime.getJobOperator();
Long executionId = jobOperator.start("myJob", new Properties());
JobExecution jobExecution = jobOperator.getJobExecution(executionId);

BatchTestHelper.keepTestAlive(jobExecution);

List<StepExecution> stepExecutions = jobOperator.getStepExecutions(executionId);
for (StepExecution stepExecution : stepExecutions) {
if (stepExecution.getStepName().equals("myStep")) {
Map<Metric.MetricType, Long> metricsMap = BatchTestHelper.getMetricsMap(stepExecution.getMetrics());

assertEquals(20L, metricsMap.get(Metric.MetricType.READ_COUNT).longValue());
assertEquals(10L, metricsMap.get(Metric.MetricType.WRITE_COUNT).longValue());
// Number of elements by the item count value on myJob.xml, plus an additional transaction for the
// remaining elements by each partition.
long commitCount = 20L / 3 + (20 % 3 > 0 ? 1 : 0) * 2;
assertEquals(commitCount, metricsMap.get(Metric.MetricType.COMMIT_COUNT).longValue());
}
}

assertEquals(2L, MyItemReader.totalReaders);
assertEquals(BatchStatus.COMPLETED, jobExecution.getBatchStatus());
}
}