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;
 
 import java.io.File;
 import java.util.List;
 
Wraps a ResourceAwareItemWriterItemStream and creates a new output resource when the count of items written in current resource exceeds setItemCountLimitPerResource(int). Suffix creation can be customized with setResourceSuffixCreator(org.springframework.batch.item.file.ResourceSuffixCreator). Note that new resources are created only at chunk boundaries i.e. the number of items written into one resource is between the limit set by setItemCountLimitPerResource(int) and (limit + chunk size).

Parameters:
<T> item type
Author(s):
Robert Kasanicky
 
 public class MultiResourceItemWriter<T> extends ExecutionContextUserSupport implements ItemWriter<T>, ItemStream {
 
 	final static private String RESOURCE_INDEX_KEY = "resource.index";
 
 	final static private String CURRENT_RESOURCE_ITEM_COUNT = "resource.item.count";
 
 	private Resource resource;
 
 
 
 	private int currentResourceItemCount = 0;
 
 	private int resourceIndex = 1;
 
 
 	private boolean saveState = true;
 
 	private boolean opened = false;
 
 	}
 
 	public void write(List<? extends T> itemsthrows Exception {
 		if (!) {
 			 = true;
 		}
 		.write(items);
 			 = false;
 		}
 	}

Allows customization of the suffix of the created resources based on the index.
 
 	public void setResourceSuffixCreator(ResourceSuffixCreator suffixCreator) {
 		this. = suffixCreator;
 	}

After this limit is exceeded the next chunk will be written into newly created resource.
	public void setItemCountLimitPerResource(int itemCountLimitPerResource) {
		this. = itemCountLimitPerResource;
	}

Delegate used for actual writing of the output.
	public void setDelegate(ResourceAwareItemWriterItemStream<? super T> delegate) {
		this. = delegate;
	}

Prototype for output resources. Actual output files will be created in the same directory and use the same name as this prototype with appended suffix (according to setResourceSuffixCreator(org.springframework.batch.item.file.ResourceSuffixCreator).
	public void setResource(Resource resource) {
		this. = resource;
	}
	public void setSaveState(boolean saveState) {
		this. = saveState;
	}
	public void close() throws ItemStreamException {
		if () {
		}
	}
	public void open(ExecutionContext executionContextthrows ItemStreamException {
		 = executionContext.getInt(getKey(), 1);
		try {
		}
		catch (IOException e) {
			throw new ItemStreamException("Couldn't assign resource"e);
		}
			// It's a restart
			.open(executionContext);
		}
		else {
			 = false;
		}
	}
	public void update(ExecutionContext executionContextthrows ItemStreamException {
		if () {
			if () {
				.update(executionContext);
			}
		}
	}

Create output resource (if necessary) and point the delegate to it.
	private void setResourceToDelegate() throws IOException {
		File file = new File(path);
		Assert.state(file.canWrite(), "Output resource " + path + " must be writable");
	}
New to GrepCode? Check out our FAQ X