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.file;
 
 
Reads items from multiple resources sequentially - resource list is given by setResources(org.springframework.core.io.Resource[]), the actual reading is delegated to setDelegate(org.springframework.batch.item.file.ResourceAwareItemReaderItemStream). Input resources are ordered using setComparator(java.util.Comparator) to make sure resource ordering is preserved between job runs in restart scenario.

Author(s):
Robert Kasanicky
Lucas Ward
 
 public class MultiResourceItemReader<T> implements ItemReader<T>, ItemStream {
 
 	private static final Log logger = LogFactory.getLog(MultiResourceItemReader.class);
 
 	private static final String RESOURCE_KEY = "resourceIndex";
 
 
 	private ResourceAwareItemReaderItemStream<? extends T> delegate;
 
 	private Resource[] resources;
 
 	private boolean saveState = true;
 
 	private int currentResource = -1;
 
 	// signals there are no resources to read -> just return null on first read
 	private boolean noInput;
 
 	private Comparator<Resourcecomparator = new Comparator<Resource>() {

Compares resource filenames.
 
 		public int compare(Resource r1Resource r2) {
 			return r1.getFilename().compareTo(r2.getFilename());
 		}
 
 	};
 
 	}

Reads the next item, jumping to next resource if necessary.
 
 
 		if () {
 			return null;
 		}
 
 		// If there is no resource, then this is the first item, set the current
 		// resource to 0 and open the first delegate.
 		if ( == -1) {
 		}
 
 		T item;
 		item = readNextItem();
		return item;
	}

Use the delegate to read the next item, jump to next resource if current one is exhausted. Items are appended to the buffer.

Returns:
next item from input
	private T readNextItem() throws Exception {
item = .read();
		while (item == null) {
				return null;
			}
			item = .read();
		}
		return item;
	}

Close the setDelegate(org.springframework.batch.item.file.ResourceAwareItemReaderItemStream) reader and reset instance variable values.
	public void close() throws ItemStreamException {
		 = false;
	}

Figure out which resource to start with in case of restart, open the delegate and restore delegate's position in the resource.
	public void open(ExecutionContext executionContextthrows ItemStreamException {
		Assert.notNull("Resources must be set");
		 = false;
		if (. == 0) {
			.warn("No resources to read");
			 = true;
			return;
		}
			.open(executionContext);
		}
		else {
		}
	}

Store the current resource index and position in the resource.
	public void update(ExecutionContext executionContextthrows ItemStreamException {
		if () {
			.update(executionContext);
		}
	}

Parameters:
delegate reads items from single org.springframework.core.io.Resource.
	public void setDelegate(ResourceAwareItemReaderItemStream<? extends T> delegate) {
		this. = delegate;
	}

Set the boolean indicating whether or not state should be saved in the provided org.springframework.batch.item.ExecutionContext during the org.springframework.batch.item.ItemStream call to update.

Parameters:
saveState
	public void setSaveState(boolean saveState) {
		this. = saveState;
	}

Parameters:
comparator used to order the injected resources, by default compares org.springframework.core.io.Resource.getFilename() values.
	public void setComparator(Comparator<Resourcecomparator) {
		this. = comparator;
	}

Parameters:
resources input resources
	public void setResources(Resource[] resources) {
		Assert.notNull(resources"The resources must not be null");
		this. = Arrays.asList(resources).toArray(new Resource[resources.length]);
	}
			return null;
		}
	}
New to GrepCode? Check out our FAQ X