RESOLVED - issue BATCH-789: Remove mark/reset from ItemReader

Removed unneeded exception classes
This commit is contained in:
dsyer
2008-08-21 12:50:11 +00:00
parent c52b277358
commit a1030c2609
13 changed files with 35 additions and 259 deletions

View File

@@ -1,45 +0,0 @@
/*
* 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.item;
/**
* An exception class thrown when an {@link ItemReader} fails to mark its current state for future retry.
*
* @author Dave Syer
* @author Ben Hale
*/
public class MarkFailedException extends ItemReaderException {
/**
* Create a new {@link MarkFailedException} based on a message.
*
* @param message the message for this exception
*/
public MarkFailedException(String message) {
super(message);
}
/**
* Create a new {@link MarkFailedException} based on a message and another exception.
*
* @param msg the message for this exception
* @param nested the other exception
*/
public MarkFailedException(String msg, Throwable nested) {
super(msg, nested);
}
}

View File

@@ -1,45 +0,0 @@
/*
* 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.item;
/**
* An exception class thrown when an {@link ItemReader} fails to reset its state based on the previous mark.
*
* @author Dave Syer
* @author Ben Hale
*/
public class ResetFailedException extends ItemReaderException {
/**
* Create a new {@link ResetFailedException} based on a message.
*
* @param message the message for this exception
*/
public ResetFailedException(String message) {
super(message);
}
/**
* Create a new {@link ResetFailedException} based on a message and another exception.
*
* @param msg the message for this exception
* @param nested the other exception
*/
public ResetFailedException(String msg, Throwable nested) {
super(msg, nested);
}
}

View File

@@ -32,8 +32,6 @@ import org.springframework.batch.item.FlushFailedException;
import org.springframework.batch.item.ItemStream;
import org.springframework.batch.item.ItemStreamException;
import org.springframework.batch.item.ItemWriter;
import org.springframework.batch.item.MarkFailedException;
import org.springframework.batch.item.ResetFailedException;
import org.springframework.batch.item.WriterNotOpenException;
import org.springframework.batch.item.file.mapping.FieldSet;
import org.springframework.batch.item.file.transform.LineAggregator;
@@ -187,14 +185,12 @@ public class FlatFileItemWriter<T> extends ExecutionContextUserSupport implement
String line = lineAggregator.aggregate(item) + lineSeparator;
try {
state.write(line);
} catch (IOException e) {
throw new FlushFailedException(
"Could not write data. The file may be corrupt.", e);
}
catch (IOException e) {
throw new FlushFailedException("Could not write data. The file may be corrupt.", e);
}
}
state.mark();
}
/**
@@ -240,8 +236,7 @@ public class FlatFileItemWriter<T> extends ExecutionContextUserSupport implement
}
}
catch (IOException e) {
throw new FlushFailedException(
"Could not write headers. The file may be corrupt.", e);
throw new FlushFailedException("Could not write headers. The file may be corrupt.", e);
}
}
}
@@ -395,18 +390,6 @@ public class FlatFileItemWriter<T> extends ExecutionContextUserSupport implement
fileChannel.position(lastMarkedByteOffsetPosition);
}
/**
* Mark the current position.
*/
public void mark() {
try {
lastMarkedByteOffsetPosition = this.position();
}
catch (IOException e) {
throw new MarkFailedException("Unable to get position for mark", e);
}
}
/**
* Creates the buffered writer for the output file channel based on
* configuration information.
@@ -424,7 +407,8 @@ public class FlatFileItemWriter<T> extends ExecutionContextUserSupport implement
// in case of restarting reset position to last committed point
if (restarted) {
this.reset();
checkFileSize();
truncate();
}
initialized = true;
@@ -450,43 +434,21 @@ public class FlatFileItemWriter<T> extends ExecutionContextUserSupport implement
}
}
/**
* Resets the file writer's current position to the point stored in the
* last marked byte offset position variable. It first checks to make
* sure the current size of the file is not less than the byte position
* to be moved to (if it is, throws an environment exception), then it
* truncates the file to that reset position, and set the cursor to
* start writing at that point.
*/
public void reset() throws ResetFailedException {
checkFileSize();
try {
getOutputState().truncate();
}
catch (IOException e) {
throw new ResetFailedException("Unable to truncate file", e);
}
}
/**
* Checks (on setState) to make sure that the current output file's size
* is not smaller than the last saved commit point. If it is, then the
* file has been damaged in some way and whole task must be started over
* again from the beginning.
* @throws IOException if there is an IO problem
*/
private void checkFileSize() {
private void checkFileSize() throws IOException {
long size = -1;
try {
outputBufferedWriter.flush();
size = fileChannel.size();
}
catch (IOException e) {
throw new ResetFailedException("An Error occured while checking file size", e);
}
outputBufferedWriter.flush();
size = fileChannel.size();
if (size < lastMarkedByteOffsetPosition) {
throw new ResetFailedException("Current file size is smaller than size at last commit");
throw new ItemStreamException("Current file size is smaller than size at last commit");
}
}