OPEN - issue BATCH-347: aop / template interceptor class names ambiguous

http://jira.springframework.org/browse/BATCH-347

Rename Rpeat/RetryInterceptor as *Listener (and get rid of last error in samples).
This commit is contained in:
dsyer
2008-02-10 16:27:08 +00:00
parent 31818eed98
commit ad71b2f56e
22 changed files with 187 additions and 164 deletions

View File

@@ -22,7 +22,7 @@ import org.hibernate.SessionFactory;
import org.springframework.batch.item.ItemWriter;
import org.springframework.batch.repeat.ExitStatus;
import org.springframework.batch.repeat.RepeatContext;
import org.springframework.batch.repeat.RepeatInterceptor;
import org.springframework.batch.repeat.RepeatListener;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.core.AttributeAccessor;
import org.springframework.orm.hibernate3.HibernateOperations;
@@ -35,14 +35,14 @@ import org.springframework.util.Assert;
* boundaries away from a less smart {@link ItemWriter} (the delegate). A delegate is required, and will be used to do
* the actual writing of the item.<br/>
*
* This class implements {@link RepeatInterceptor} and it will only work if properly registered. If the delegate is also
* a {@link RepeatInterceptor} then it does not need to be separately registered as we make the callbacks here in the
* This class implements {@link RepeatListener} and it will only work if properly registered. If the delegate is also
* a {@link RepeatListener} then it does not need to be separately registered as we make the callbacks here in the
* right places.
*
* @author Dave Syer
*
*/
public class HibernateAwareItemWriter implements ItemWriter, RepeatInterceptor, InitializingBean {
public class HibernateAwareItemWriter implements ItemWriter, RepeatListener, InitializingBean {
/**
* Key for items processed in the current transaction {@link RepeatContext}.
@@ -114,41 +114,41 @@ public class HibernateAwareItemWriter implements ItemWriter, RepeatInterceptor,
}
/**
* Does nothing unless the delegate is also a {@link RepeatInterceptor} in which case pass on the call to him.
* Does nothing unless the delegate is also a {@link RepeatListener} in which case pass on the call to him.
*
* @see org.springframework.batch.repeat.RepeatInterceptor#before(org.springframework.batch.repeat.RepeatContext)
* @see org.springframework.batch.repeat.RepeatListener#before(org.springframework.batch.repeat.RepeatContext)
*/
public void before(RepeatContext context) {
if (delegate instanceof RepeatInterceptor) {
RepeatInterceptor interceptor = (RepeatInterceptor) delegate;
if (delegate instanceof RepeatListener) {
RepeatListener interceptor = (RepeatListener) delegate;
interceptor.before(context);
}
}
/**
* Does nothing unless the delegate is also a {@link RepeatInterceptor} in which case pass on the call to him.
* Does nothing unless the delegate is also a {@link RepeatListener} in which case pass on the call to him.
*
* @see org.springframework.batch.repeat.RepeatInterceptor#after(org.springframework.batch.repeat.RepeatContext,
* @see org.springframework.batch.repeat.RepeatListener#after(org.springframework.batch.repeat.RepeatContext,
* org.springframework.batch.repeat.ExitStatus)
*/
public void after(RepeatContext context, ExitStatus result) {
if (delegate instanceof RepeatInterceptor) {
RepeatInterceptor interceptor = (RepeatInterceptor) delegate;
if (delegate instanceof RepeatListener) {
RepeatListener interceptor = (RepeatListener) delegate;
interceptor.after(context, result);
}
}
/**
* Flush the Hibernate session so that any batch exceptions are within the RepeatContext. If the delegate is also a
* {@link RepeatInterceptor} then it will be given the call before flushing.
* {@link RepeatListener} then it will be given the call before flushing.
*
*
* @see org.springframework.batch.repeat.RepeatInterceptor#close(org.springframework.batch.repeat.RepeatContext)
* @see org.springframework.batch.repeat.RepeatListener#close(org.springframework.batch.repeat.RepeatContext)
*/
public void close(RepeatContext context) {
try {
if (delegate instanceof RepeatInterceptor) {
RepeatInterceptor interceptor = (RepeatInterceptor) delegate;
if (delegate instanceof RepeatListener) {
RepeatListener interceptor = (RepeatListener) delegate;
interceptor.close(context);
}
flush();
@@ -175,30 +175,30 @@ public class HibernateAwareItemWriter implements ItemWriter, RepeatInterceptor,
}
/**
* Does nothing unless the delegate is also a {@link RepeatInterceptor} in which case pass on the call to him.
* Does nothing unless the delegate is also a {@link RepeatListener} in which case pass on the call to him.
*
* @see org.springframework.batch.repeat.RepeatInterceptor#onError(org.springframework.batch.repeat.RepeatContext,
* @see org.springframework.batch.repeat.RepeatListener#onError(org.springframework.batch.repeat.RepeatContext,
* java.lang.Throwable)
*/
public void onError(RepeatContext context, Throwable e) {
if (delegate instanceof RepeatInterceptor) {
RepeatInterceptor interceptor = (RepeatInterceptor) delegate;
if (delegate instanceof RepeatListener) {
RepeatListener interceptor = (RepeatListener) delegate;
interceptor.onError(context, e);
}
}
/**
* Sets up the context as a transaction resource so that we can store state and refer back to it in the
* {@link #write(Object)} method. If the delegate is also a {@link RepeatInterceptor} then it will be given the call
* {@link #write(Object)} method. If the delegate is also a {@link RepeatListener} then it will be given the call
* afterwards.
*
* @see org.springframework.batch.repeat.RepeatInterceptor#open(org.springframework.batch.repeat.RepeatContext)
* @see org.springframework.batch.repeat.RepeatListener#open(org.springframework.batch.repeat.RepeatContext)
*/
public void open(RepeatContext context) {
this.setContext(context);
getProcessed().clear();
if (delegate instanceof RepeatInterceptor) {
RepeatInterceptor interceptor = (RepeatInterceptor) delegate;
if (delegate instanceof RepeatListener) {
RepeatListener interceptor = (RepeatListener) delegate;
interceptor.open(context);
}
}

View File

@@ -17,20 +17,19 @@
package org.springframework.batch.repeat;
/**
* Interface for interceptors in the batch process. Implementers can provide
* Interface for listeners to the batch process. Implementers can provide
* enhance the behaviour of a batch in small cross-cutting modules. The
* framework provides callbacks at key points in the processing.
*
* @author Dave Syer
*
*/
public interface RepeatInterceptor {
public interface RepeatListener {
/**
* Called by the framework before each batch item. Implementers can halt a
* batch by setting the complete flag on the context.
*
* @param context
* the current batch context.
* @param context the current batch context.
*/
void before(RepeatContext context);
@@ -39,10 +38,8 @@ public interface RepeatInterceptor {
* item processing results in an exception. This method is called as soon as
* the result is known.
*
* @param context
* the current batch context
* @param result
* the result of the callback
* @param context the current batch context
* @param result the result of the callback
*/
void after(RepeatContext context, ExitStatus result);
@@ -54,8 +51,7 @@ public interface RepeatInterceptor {
* enclosing batches (the whole job), the would need to use the parent
* context (recursively).
*
* @param context
* the current batch context
* @param context the current batch context
*/
void open(RepeatContext context);
@@ -67,10 +63,8 @@ public interface RepeatInterceptor {
* There is no need to re-throw the exception here - that will be done by
* the enclosing framework.
*
* @param context
* the current batch context
* @param e
* the error that was encountered in an item callback.
* @param context the current batch context
* @param e the error that was encountered in an item callback.
*/
void onError(RepeatContext context, Throwable e);
@@ -79,8 +73,7 @@ public interface RepeatInterceptor {
* completion (i.e. even after an exception). Implementers can use this
* method to clean up any resources.
*
* @param context
* the current batch context.
* @param context the current batch context.
*/
void close(RepeatContext context);
}

View File

@@ -38,7 +38,6 @@ import org.springframework.util.Assert;
* not-null, representing the {@link Void#TYPE}).
*
* @author Dave Syer
* @since 2.1
*/
public class RepeatOperationsInterceptor implements MethodInterceptor {

View File

@@ -17,7 +17,7 @@ package org.springframework.batch.repeat.interceptor;
import org.springframework.batch.repeat.ExitStatus;
import org.springframework.batch.repeat.RepeatContext;
import org.springframework.batch.repeat.RepeatInterceptor;
import org.springframework.batch.repeat.RepeatListener;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.context.ApplicationEventPublisherAware;
@@ -25,7 +25,7 @@ import org.springframework.context.ApplicationEventPublisherAware;
* @author Dave Syer
*
*/
public class ApplicationEventPublisherRepeatInterceptor implements ApplicationEventPublisherAware, RepeatInterceptor {
public class ApplicationEventPublisherRepeatListener implements ApplicationEventPublisherAware, RepeatListener {
private ApplicationEventPublisher applicationEventPublisher;

View File

@@ -18,9 +18,9 @@ package org.springframework.batch.repeat.interceptor;
import org.springframework.batch.repeat.ExitStatus;
import org.springframework.batch.repeat.RepeatContext;
import org.springframework.batch.repeat.RepeatInterceptor;
import org.springframework.batch.repeat.RepeatListener;
public class RepeatInterceptorAdapter implements RepeatInterceptor {
public class RepeatListenerAdapter implements RepeatListener {
public void before(RepeatContext context) {
}

View File

@@ -24,7 +24,7 @@ import org.springframework.batch.repeat.CompletionPolicy;
import org.springframework.batch.repeat.ExitStatus;
import org.springframework.batch.repeat.RepeatCallback;
import org.springframework.batch.repeat.RepeatContext;
import org.springframework.batch.repeat.RepeatInterceptor;
import org.springframework.batch.repeat.RepeatListener;
import org.springframework.batch.repeat.RepeatOperations;
import org.springframework.batch.repeat.exception.RepeatException;
import org.springframework.batch.repeat.exception.handler.DefaultExceptionHandler;
@@ -48,9 +48,9 @@ import org.springframework.util.Assert;
*
* Clients that want to take some business action when an exception is thrown by
* the {@link RepeatCallback} can consider using a custom
* {@link RepeatInterceptor} instead of trying to customise the
* {@link RepeatListener} instead of trying to customise the
* {@link CompletionPolicy}. This is generally a friendlier interface to
* implement, and the {@link RepeatInterceptor#after(RepeatContext, ExitStatus)}
* implement, and the {@link RepeatListener#after(RepeatContext, ExitStatus)}
* method is passed in the result of the callback, which would be an instance of
* {@link Throwable} if the business processing had thrown an exception. If the
* exception is not to be propagated to the caller, then a non-default
@@ -64,18 +64,18 @@ public class RepeatTemplate implements RepeatOperations {
protected Log logger = LogFactory.getLog(getClass());
private RepeatInterceptor[] interceptors = new RepeatInterceptor[] {};
private RepeatListener[] listeners = new RepeatListener[] {};
private CompletionPolicy completionPolicy = new DefaultResultCompletionPolicy();
private ExceptionHandler exceptionHandler = new DefaultExceptionHandler();
public void setInterceptors(RepeatInterceptor[] interceptors) {
this.interceptors = interceptors;
public void setListeners(RepeatListener[] listeners) {
this.listeners = listeners;
}
public void setInterceptor(RepeatInterceptor interceptor) {
interceptors = new RepeatInterceptor[] { interceptor };
public void setListener(RepeatListener listener) {
listeners = new RepeatListener[] { listener };
}
/**
@@ -159,8 +159,8 @@ public class RepeatTemplate implements RepeatOperations {
// processing takes place.
boolean running = !isMarkedComplete(context);
for (int i = 0; i < interceptors.length; i++) {
RepeatInterceptor interceptor = interceptors[i];
for (int i = 0; i < listeners.length; i++) {
RepeatListener interceptor = listeners[i];
interceptor.open(context);
running = running && !isMarkedComplete(context);
if (!running)
@@ -182,8 +182,8 @@ public class RepeatTemplate implements RepeatOperations {
* that they all happen in the same thread - it's easier for
* tracking batch status, amongst other things.
*/
for (int i = 0; i < interceptors.length; i++) {
RepeatInterceptor interceptor = interceptors[i];
for (int i = 0; i < listeners.length; i++) {
RepeatListener interceptor = listeners[i];
interceptor.before(context);
// Allow before interceptors to veto the batch by setting
// flag.
@@ -208,14 +208,14 @@ public class RepeatTemplate implements RepeatOperations {
try {
for (int i = interceptors.length; i-- > 0;) {
RepeatInterceptor interceptor = interceptors[i];
for (int i = listeners.length; i-- > 0;) {
RepeatListener interceptor = listeners[i];
interceptor.onError(context, throwable);
// This is not an error - only log at debug
// level.
logger.debug("Exception intercepted ("
+ (i + 1) + " of "
+ interceptors.length + ")", throwable);
+ listeners.length + ")", throwable);
}
exceptionHandler
.handleException(context, throwable);
@@ -257,8 +257,8 @@ public class RepeatTemplate implements RepeatOperations {
} finally {
try {
for (int i = interceptors.length; i-- > 0;) {
RepeatInterceptor interceptor = interceptors[i];
for (int i = listeners.length; i-- > 0;) {
RepeatListener interceptor = listeners[i];
interceptor.close(context);
}
} finally {
@@ -379,8 +379,8 @@ public class RepeatTemplate implements RepeatOperations {
// that...
if (value != null && value.isContinuable()) {
for (int i = interceptors.length; i-- > 0;) {
RepeatInterceptor interceptor = interceptors[i];
for (int i = listeners.length; i-- > 0;) {
RepeatListener interceptor = listeners[i];
interceptor.after(context, value);
}

View File

@@ -19,14 +19,14 @@ package org.springframework.batch.retry;
import org.springframework.batch.retry.exception.TerminatedRetryException;
/**
* Interface for interceptor that can be used to add behaviour to a retry.
* Interface for listener that can be used to add behaviour to a retry.
* Implementations of {@link RetryOperations} can chose to issue callbacks to an
* interceptor during the retry lifecycle.
*
* @author Dave Syer
*
*/
public interface RetryInterceptor {
public interface RetryListener {
/**
* Called before the first attempt in a retry. For instance, implementers

View File

@@ -40,7 +40,6 @@ import org.springframework.util.Assert;
*
* @author Rob Harrop
* @author Dave Syer
* @since 2.1
*/
public class RetryOperationsInterceptor implements MethodInterceptor {

View File

@@ -18,9 +18,9 @@ package org.springframework.batch.retry.interceptor;
import org.springframework.batch.retry.RetryCallback;
import org.springframework.batch.retry.RetryContext;
import org.springframework.batch.retry.RetryInterceptor;
import org.springframework.batch.retry.RetryListener;
public class RetryInterceptorSupport implements RetryInterceptor {
public class RetryListenerAdapter implements RetryListener {
public void close(RetryContext context, RetryCallback callback, Throwable throwable) {
}

View File

@@ -20,7 +20,7 @@ import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.batch.retry.RetryCallback;
import org.springframework.batch.retry.RetryContext;
import org.springframework.batch.retry.RetryInterceptor;
import org.springframework.batch.retry.RetryListener;
import org.springframework.batch.retry.RetryOperations;
import org.springframework.batch.retry.RetryPolicy;
import org.springframework.batch.retry.backoff.BackOffContext;
@@ -57,7 +57,6 @@ import org.springframework.batch.retry.synch.RetrySynchronizationManager;
*
* @author Rob Harrop
* @author Dave Syer
* @since 2.1
*/
public class RetryTemplate implements RetryOperations {
@@ -67,26 +66,26 @@ public class RetryTemplate implements RetryOperations {
private volatile RetryPolicy retryPolicy = new SimpleRetryPolicy();
private volatile RetryInterceptor[] interceptors = new RetryInterceptor[0];
private volatile RetryListener[] listeners = new RetryListener[0];
/**
* Setter for interceptors. The interceptors are executed before and after a
* Setter for listeners. The listeners are executed before and after a
* retry block (i.e. before and after all the attempts), and on an error
* (every attempt).
* @param interceptors
* @see RetryInterceptor
* @param listeners
* @see RetryListener
*/
public void setInterceptors(RetryInterceptor[] interceptors) {
this.interceptors = interceptors;
public void setListeners(RetryListener[] listeners) {
this.listeners = listeners;
}
/**
* Setter for single interceptor if there is only one.
* @param interceptor
* @see #setInterceptors(RetryInterceptor[])
* Setter for single listener if there is only one.
* @param listener
* @see #setListeners(RetryListener[])
*/
public void setInterceptor(RetryInterceptor interceptor) {
this.interceptors = new RetryInterceptor[] { interceptor };
public void setListener(RetryListener listener) {
this.listeners = new RetryListener[] { listener };
}
/**
@@ -220,8 +219,8 @@ public class RetryTemplate implements RetryOperations {
boolean result = true;
for (int i = 0; i < interceptors.length; i++) {
result = result && interceptors[i].open(context, callback);
for (int i = 0; i < listeners.length; i++) {
result = result && listeners[i].open(context, callback);
}
return result;
@@ -229,14 +228,14 @@ public class RetryTemplate implements RetryOperations {
}
private void doCloseInterceptors(RetryCallback callback, RetryContext context, Throwable lastException) {
for (int i = interceptors.length; i-- > 0;) {
interceptors[i].close(context, callback, lastException);
for (int i = listeners.length; i-- > 0;) {
listeners[i].close(context, callback, lastException);
}
}
private void doOnErrorInterceptors(RetryCallback callback, RetryContext context, Throwable throwable) {
for (int i = interceptors.length; i-- > 0;) {
interceptors[i].onError(context, callback, throwable);
for (int i = listeners.length; i-- > 0;) {
listeners[i].onError(context, callback, throwable);
}
}