From 7a2cdadf8c61b656bbe185eb1d8907dd20abcd09 Mon Sep 17 00:00:00 2001 From: lucasward Date: Mon, 11 Feb 2008 03:17:58 +0000 Subject: [PATCH] BATCH-220: Committed the read part of chunking, without adding it to the StepExecutor. It should be noted that I used 'Chunker' instead of 'ChunkReader', since there's already precedence in the java world, and it isn't reading chunk so much as making a chunk out of items. I have also added skipCount to StepExecution, since it's reasonable that this type of data A) should be persisted and B) the domain object is the most logical place for this type of state. --- .../batch/core/domain/Chunk.java | 73 +++++++++++++++++++ .../batch/core/domain/ReadFailurePolicy.java | 37 ++++++++++ .../batch/core/domain/StepExecution.java | 24 ++++++ .../batch/core/domain/ChunkTests.java | 54 ++++++++++++++ 4 files changed, 188 insertions(+) create mode 100644 spring-batch-core/src/main/java/org/springframework/batch/core/domain/Chunk.java create mode 100644 spring-batch-core/src/main/java/org/springframework/batch/core/domain/ReadFailurePolicy.java create mode 100644 spring-batch-core/src/test/java/org/springframework/batch/core/domain/ChunkTests.java diff --git a/spring-batch-core/src/main/java/org/springframework/batch/core/domain/Chunk.java b/spring-batch-core/src/main/java/org/springframework/batch/core/domain/Chunk.java new file mode 100644 index 000000000..a87a44d55 --- /dev/null +++ b/spring-batch-core/src/main/java/org/springframework/batch/core/domain/Chunk.java @@ -0,0 +1,73 @@ +/* + * Copyright 2006-2008 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.batch.core.domain; + +import java.io.Serializable; +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; + +import org.springframework.util.Assert; + +/** + * A 'chunk' of items, that will be committed together. It is expected that a + * chunk may be serialized, especially if using a queue to dispatch chunks to + * various {@link ChunkProcessor}s. + * + * @author Ben Hale + * @author Lucas Ward + */ +public class Chunk implements Serializable { + + private final Long id; + + private final List items; + + public Chunk(Long id, List items) { + validateSerializable(items); + this.items = items; + this.id = id; + } + + /** + * Get the list of items for this chunk. + * + * @return items. + */ + public List getItems() { + return new ArrayList(items); + } + + /** + * Get the chunk id. + * + * @return id of this chunk. + */ + public Long getId() { + return id; + } + + /** + * In order for the whole chunk to be serializable, every item in the chunk + * must be serializable. + */ + private void validateSerializable(List items) { + for (Iterator i = items.iterator(); i.hasNext();) { + Assert.isInstanceOf(Serializable.class, i.next(), "All items in a chunk must be serialiable"); + } + } + +} diff --git a/spring-batch-core/src/main/java/org/springframework/batch/core/domain/ReadFailurePolicy.java b/spring-batch-core/src/main/java/org/springframework/batch/core/domain/ReadFailurePolicy.java new file mode 100644 index 000000000..19ab021a4 --- /dev/null +++ b/spring-batch-core/src/main/java/org/springframework/batch/core/domain/ReadFailurePolicy.java @@ -0,0 +1,37 @@ +/* + * Copyright 2006-2007 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.batch.core.domain; + +import org.springframework.batch.core.domain.StepExecution; + +/** + * Policy for determining whether or not reading should continue. + * + * @author Lucas Ward + */ +public interface ReadFailurePolicy { + + /** + * Returns true or false, indicating whether or not reading should + * continue for the current step execution with the given throwable. + * + * @param ex throwable encountered while reading + * @param stepExecution currently running execution + * @return true if reading should continue, false otherwise. + * @throws IllegalArgumentException if t or stepExecution is null + */ + boolean shouldContinue(Exception ex, StepExecution stepExecution); +} diff --git a/spring-batch-core/src/main/java/org/springframework/batch/core/domain/StepExecution.java b/spring-batch-core/src/main/java/org/springframework/batch/core/domain/StepExecution.java index b809b136a..e0afdd78f 100644 --- a/spring-batch-core/src/main/java/org/springframework/batch/core/domain/StepExecution.java +++ b/spring-batch-core/src/main/java/org/springframework/batch/core/domain/StepExecution.java @@ -46,6 +46,10 @@ public class StepExecution extends Entity { private int commitCount = 0; private int rollbackCount = 0; + + private int skipCount = 0; + + private int retryCount = 0; private Date startTime = new Date(System.currentTimeMillis()); @@ -368,5 +372,25 @@ public class StepExecution extends Entity { public void setTerminateOnly() { this.terminateOnly = true; } + + public void setSkipCount(int skipCount) { + this.skipCount = skipCount; + } + + public int getSkipCount() { + return skipCount; + } + + public void incrementSkipCount(){ + skipCount++; + } + + public void setRetryCount(int retryCount) { + this.retryCount = retryCount; + } + + public int getRetryCount() { + return retryCount; + } } diff --git a/spring-batch-core/src/test/java/org/springframework/batch/core/domain/ChunkTests.java b/spring-batch-core/src/test/java/org/springframework/batch/core/domain/ChunkTests.java new file mode 100644 index 000000000..0bc2b85c2 --- /dev/null +++ b/spring-batch-core/src/test/java/org/springframework/batch/core/domain/ChunkTests.java @@ -0,0 +1,54 @@ +/* + * Copyright 2006-2008 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.batch.core.domain; + +import java.util.ArrayList; +import java.util.List; + +import junit.framework.TestCase; + +/** + * @author Lucas Ward + * + */ +public class ChunkTests extends TestCase { + + List items; + Chunk chunk; + + protected void setUp() throws Exception { + super.setUp(); + + items = new ArrayList(); + items.add("1"); + items.add("2"); + } + + + public void testCreateChunk(){ + + chunk = new Chunk(new Long(1), items); + assertEquals(items, chunk.getItems()); + } + + public void testImmutability(){ + + chunk = new Chunk(new Long(1), items); + List testItems = chunk.getItems(); + testItems.add("3"); + assertEquals(2, chunk.getItems().size()); + } +}