Introduce KeyedItemReader to push the getKey() method into a separate place.
This commit is contained in:
@@ -31,7 +31,7 @@ import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
import org.springframework.batch.io.Skippable;
|
||||
import org.springframework.batch.io.support.AbstractTransactionalIoSource;
|
||||
import org.springframework.batch.item.ItemReader;
|
||||
import org.springframework.batch.item.KeyedItemReader;
|
||||
import org.springframework.batch.item.ResourceLifecycle;
|
||||
import org.springframework.batch.restart.GenericRestartData;
|
||||
import org.springframework.batch.restart.RestartData;
|
||||
@@ -117,7 +117,7 @@ import org.springframework.util.StringUtils;
|
||||
* @author Peter Zozom
|
||||
*/
|
||||
public class JdbcCursorItemReader extends AbstractTransactionalIoSource
|
||||
implements ItemReader, ResourceLifecycle, DisposableBean,
|
||||
implements KeyedItemReader, ResourceLifecycle, DisposableBean,
|
||||
InitializingBean, Restartable, StatisticsProvider, Skippable {
|
||||
|
||||
private static Log log = LogFactory.getLog(JdbcCursorItemReader.class);
|
||||
|
||||
@@ -19,7 +19,7 @@ import java.util.Iterator;
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.batch.io.support.AbstractTransactionalIoSource;
|
||||
import org.springframework.batch.item.ItemReader;
|
||||
import org.springframework.batch.item.KeyedItemReader;
|
||||
import org.springframework.batch.item.ResourceLifecycle;
|
||||
import org.springframework.batch.repeat.synch.BatchTransactionSynchronizationManager;
|
||||
import org.springframework.batch.restart.RestartData;
|
||||
@@ -51,7 +51,7 @@ import org.springframework.util.Assert;
|
||||
* @since 1.0
|
||||
*/
|
||||
public class DrivingQueryItemReader extends AbstractTransactionalIoSource
|
||||
implements ItemReader, ResourceLifecycle, InitializingBean,
|
||||
implements KeyedItemReader, ResourceLifecycle, InitializingBean,
|
||||
DisposableBean, Restartable {
|
||||
|
||||
private boolean initialized = false;
|
||||
|
||||
@@ -16,9 +16,10 @@
|
||||
|
||||
package org.springframework.batch.item;
|
||||
|
||||
import org.springframework.batch.item.reader.AbstractItemReader;
|
||||
|
||||
/**
|
||||
* Strategy interface for providing the data for a given batch stage execution.
|
||||
* <br/>
|
||||
* Strategy interface for providing the data. <br/>
|
||||
*
|
||||
* Implementations are expected to be stateful and will be called multiple times
|
||||
* for each batch, with each call to {@link #next} returning a different value
|
||||
@@ -26,8 +27,8 @@ package org.springframework.batch.item;
|
||||
*
|
||||
* Implementations need to be thread safe and clients of a {@link ItemReader}
|
||||
* need to be aware that this is the case. Clients can code to this interface
|
||||
* without worrying about thread safety by using the AbstractItemProvider base
|
||||
* class.<br/>
|
||||
* without worrying about thread safety by using the {@link AbstractItemReader}
|
||||
* base class.<br/>
|
||||
*
|
||||
* A richer interface (e.g. with a look ahead or peek) is not feasible because
|
||||
* we need to support transactions in an asynchronous batch.
|
||||
@@ -50,15 +51,6 @@ public interface ItemReader {
|
||||
*/
|
||||
Object read() throws Exception;
|
||||
|
||||
/**
|
||||
* Get a unique identifier for the item that can be used to cache it between
|
||||
* calls if necessary, and then identify it later.
|
||||
*
|
||||
* @param item the current item.
|
||||
* @return a unique identifier.
|
||||
*/
|
||||
Object getKey(Object item);
|
||||
|
||||
/**
|
||||
* Close the reader, freeing any resources that may have been allocated
|
||||
* since the first call to read().
|
||||
|
||||
@@ -0,0 +1,36 @@
|
||||
/*
|
||||
* 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;
|
||||
|
||||
/**
|
||||
* Extension of the {@link ItemReader} interface that allows items to be
|
||||
* identified and tagged by a unique key.
|
||||
*
|
||||
* @author Dave Syer
|
||||
*
|
||||
*/
|
||||
public interface KeyedItemReader extends ItemReader {
|
||||
|
||||
/**
|
||||
* Get a unique identifier for the item that can be used to cache it between
|
||||
* calls if necessary, and then identify it later.
|
||||
*
|
||||
* @param item the current item.
|
||||
* @return a unique identifier.
|
||||
*/
|
||||
Object getKey(Object item);
|
||||
|
||||
}
|
||||
@@ -24,18 +24,11 @@ import org.springframework.batch.item.ItemReader;
|
||||
*
|
||||
*/
|
||||
public abstract class AbstractItemReader implements ItemReader {
|
||||
|
||||
/**
|
||||
* Simply returns the item itself. Will be adequate for many purposes, but
|
||||
* not (for example) if the item is a message - in which case the identifier
|
||||
* should be used.
|
||||
*
|
||||
* @see org.springframework.batch.item.ItemReader#getKey(java.lang.Object)
|
||||
*/
|
||||
public Object getKey(Object item) {
|
||||
return item;
|
||||
}
|
||||
|
||||
/**
|
||||
* Do nothing.
|
||||
* @see org.springframework.batch.item.ItemReader#close()
|
||||
*/
|
||||
public void close() throws Exception {
|
||||
}
|
||||
|
||||
|
||||
@@ -33,11 +33,11 @@ public class ItemReaderAdapter extends AbstractMethodInvokingDelegator implement
|
||||
return invokeDelegateMethod();
|
||||
}
|
||||
|
||||
//harmless implementation of method required by ItemReader interface
|
||||
public Object getKey(Object item) {
|
||||
return item;
|
||||
}
|
||||
|
||||
/**
|
||||
* Do nothing.
|
||||
*
|
||||
* @see org.springframework.batch.item.ItemReader#close()
|
||||
*/
|
||||
public void close() throws Exception {
|
||||
|
||||
}
|
||||
|
||||
@@ -24,6 +24,7 @@ import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
import org.springframework.batch.item.FailedItemIdentifier;
|
||||
import org.springframework.batch.item.ItemReader;
|
||||
import org.springframework.batch.item.KeyedItemReader;
|
||||
import org.springframework.batch.item.exception.UnexpectedInputException;
|
||||
import org.springframework.jms.JmsException;
|
||||
import org.springframework.jms.core.JmsOperations;
|
||||
@@ -39,7 +40,7 @@ import org.springframework.util.Assert;
|
||||
* @author Dave Syer
|
||||
*
|
||||
*/
|
||||
public class JmsItemReader extends AbstractItemReader implements FailedItemIdentifier {
|
||||
public class JmsItemReader extends AbstractItemReader implements KeyedItemReader, FailedItemIdentifier {
|
||||
|
||||
protected Log logger = LogFactory.getLog(getClass());
|
||||
|
||||
@@ -151,7 +152,7 @@ public class JmsItemReader extends AbstractItemReader implements FailedItemIdent
|
||||
throw new UnexpectedInputException("Could not extract message ID", e);
|
||||
}
|
||||
}
|
||||
return super.getKey(item);
|
||||
return item;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -21,6 +21,7 @@ import org.apache.commons.logging.LogFactory;
|
||||
import org.springframework.batch.item.ItemReader;
|
||||
import org.springframework.batch.item.ItemRecoverer;
|
||||
import org.springframework.batch.item.ItemWriter;
|
||||
import org.springframework.batch.item.KeyedItemReader;
|
||||
import org.springframework.batch.retry.RetryCallback;
|
||||
import org.springframework.batch.retry.RetryContext;
|
||||
import org.springframework.batch.retry.RetryPolicy;
|
||||
@@ -47,13 +48,13 @@ public class ItemReaderRetryCallback implements RetryCallback {
|
||||
public static final String ITEM = ItemReaderRetryCallback.class.getName()
|
||||
+ ".ITEM";
|
||||
|
||||
private ItemReader provider;
|
||||
private KeyedItemReader provider;
|
||||
|
||||
private ItemWriter writer;
|
||||
|
||||
private ItemRecoverer recoverer;
|
||||
|
||||
public ItemReaderRetryCallback(ItemReader provider,
|
||||
public ItemReaderRetryCallback(KeyedItemReader provider,
|
||||
ItemWriter writer) {
|
||||
super();
|
||||
this.provider = provider;
|
||||
@@ -127,7 +128,7 @@ public class ItemReaderRetryCallback implements RetryCallback {
|
||||
*
|
||||
* @return the {@link ItemReader} instance.
|
||||
*/
|
||||
public ItemReader getReader() {
|
||||
public KeyedItemReader getReader() {
|
||||
return provider;
|
||||
}
|
||||
|
||||
|
||||
@@ -19,8 +19,8 @@ package org.springframework.batch.retry.policy;
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
import org.springframework.batch.item.FailedItemIdentifier;
|
||||
import org.springframework.batch.item.ItemReader;
|
||||
import org.springframework.batch.item.ItemRecoverer;
|
||||
import org.springframework.batch.item.KeyedItemReader;
|
||||
import org.springframework.batch.repeat.synch.RepeatSynchronizationManager;
|
||||
import org.springframework.batch.retry.RetryCallback;
|
||||
import org.springframework.batch.retry.RetryContext;
|
||||
@@ -109,7 +109,7 @@ public class ItemReaderRetryPolicy extends AbstractStatefulRetryPolicy {
|
||||
public RetryContext open(RetryCallback callback) {
|
||||
Assert.state(callback instanceof ItemReaderRetryCallback,
|
||||
"Callback must be ItemProviderRetryCallback");
|
||||
ItemProviderRetryContext context = new ItemProviderRetryContext(
|
||||
ItemReaderRetryContext context = new ItemReaderRetryContext(
|
||||
(ItemReaderRetryCallback) callback);
|
||||
context.open(callback);
|
||||
return context;
|
||||
@@ -137,7 +137,7 @@ public class ItemReaderRetryPolicy extends AbstractStatefulRetryPolicy {
|
||||
return ((RetryPolicy) context).handleRetryExhausted(context);
|
||||
}
|
||||
|
||||
private class ItemProviderRetryContext extends RetryContextSupport
|
||||
private class ItemReaderRetryContext extends RetryContextSupport
|
||||
implements RetryPolicy {
|
||||
|
||||
private Object item;
|
||||
@@ -145,11 +145,11 @@ public class ItemReaderRetryPolicy extends AbstractStatefulRetryPolicy {
|
||||
// The delegate context...
|
||||
private RetryContext delegateContext;
|
||||
|
||||
private ItemReader reader;
|
||||
private KeyedItemReader reader;
|
||||
|
||||
private ItemRecoverer recoverer;
|
||||
|
||||
public ItemProviderRetryContext(ItemReaderRetryCallback callback) {
|
||||
public ItemReaderRetryContext(ItemReaderRetryCallback callback) {
|
||||
super(RetrySynchronizationManager.getContext());
|
||||
item = callback.next(this);
|
||||
this.reader = callback.getReader();
|
||||
@@ -239,7 +239,7 @@ public class ItemReaderRetryPolicy extends AbstractStatefulRetryPolicy {
|
||||
* @param item
|
||||
* @return
|
||||
*/
|
||||
protected boolean hasFailed(ItemReader provider, Object item) {
|
||||
protected boolean hasFailed(KeyedItemReader provider, Object item) {
|
||||
if (provider instanceof FailedItemIdentifier) {
|
||||
return ((FailedItemIdentifier) provider).hasFailed(item);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user