Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * 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.support;
 
Abstract superclass for org.springframework.batch.item.ItemReaders that supports restart by storing item count in the org.springframework.batch.item.ExecutionContext (therefore requires item ordering to be preserved between runs). Subclasses are inherently *not* thread-safe.

Author(s):
Robert Kasanicky
 
 public abstract class AbstractItemCountingItemStreamItemReader<T> implements ItemReader<T>, ItemStream {
 
 	private static final String READ_COUNT = "read.count";
 
 	private static final String READ_COUNT_MAX = "read.count.max";
 
 	private int currentItemCount = 0;
 
 	private int maxItemCount = .;
 
 
 	private boolean saveState = true;

Read next item from input.

Returns:
item
Throws:
java.lang.Exception
 
 	protected abstract T doRead() throws Exception;

Open resources necessary to start reading input.
 
 	protected abstract void doOpen() throws Exception;

Close the resources opened in doOpen().
 
 	protected abstract void doClose() throws Exception;

Move to the given item index. Subclasses should override this method if there is a more efficient way of moving to given index than re-reading the input using doRead().
 
 	protected void jumpToItem(int itemIndexthrows Exception {
 		for (int i = 0; i < itemIndexi++) {
 			read();
 		}
 	}
 
 	public final T read() throws ExceptionUnexpectedInputExceptionParseException {
 			return null;
 		}
 		return doRead();
 	}
 
 	protected int getCurrentItemCount() {
 		return ;
 	}

The index of the item to start reading from. If the org.springframework.batch.item.ExecutionContext contains a key [name].read.count (where [name] is the name of this component) the value from the org.springframework.batch.item.ExecutionContext will be used in preference.

Parameters:
count the value of the current item count
See also:
setName(java.lang.String)
	public void setCurrentItemCount(int count) {
		this. = count;
	}

The maximum index of the items to be read. If the org.springframework.batch.item.ExecutionContext contains a key [name].read.count.max (where [name] is the name of this component) the value from the org.springframework.batch.item.ExecutionContext will be used in preference.

Parameters:
count the value of the maximum item count
See also:
setName(java.lang.String)
	public void setMaxItemCount(int count) {
		this. = count;
	}
	public void close() throws ItemStreamException {
		try {
		}
		catch (Exception e) {
			throw new ItemStreamException("Error while closing item reader"e);
		}
	}
	public void open(ExecutionContext executionContextthrows ItemStreamException {
		try {
		}
		catch (Exception e) {
			throw new ItemStreamException("Failed to initialize the reader"e);
		}
		if (!isSaveState()) {
			return;
		}
		if (executionContext.containsKey(.getKey())) {
		}
		if (executionContext.containsKey(.getKey())) {
			int itemCount = executionContext.getInt(.getKey());
			if (itemCount < ) {
				try {
					jumpToItem(itemCount);
				}
				catch (Exception e) {
					throw new ItemStreamException("Could not move to stored position on restart"e);
				}
			}
			 = itemCount;
		}
	}
	public void update(ExecutionContext executionContextthrows ItemStreamException {
		if () {
			Assert.notNull(executionContext"ExecutionContext must not be null");
			}
		}
	}
		return ;
	}

The name of the component which will be used as a stem for keys in the org.springframework.batch.item.ExecutionContext. Subclasses should provide a default value, e.g. the short form of the class name.

Parameters:
name the name for the component
	public void setName(String name) {
	}

Set the flag that determines whether to save internal data for org.springframework.batch.item.ExecutionContext. Only switch this to false if you don't want to save any state from this stream, and you don't need it to be restartable. Always set it to false if the reader is being used in a concurrent environment.

Parameters:
saveState flag value (default true).
	public void setSaveState(boolean saveState) {
		this. = saveState;
	}

The flag that determines whether to save internal state for restarts.

Returns:
true if the flag was set
	public boolean isSaveState() {
		return ;
	}
New to GrepCode? Check out our FAQ X