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()); + } +}