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

@@ -28,6 +28,11 @@ import org.springframework.batch.core.repository.JobRepository;
import org.springframework.batch.core.tasklet.Tasklet;
import org.springframework.batch.io.exception.BatchCriticalException;
import org.springframework.batch.repeat.ExitStatus;
import org.springframework.batch.repeat.RepeatCallback;
import org.springframework.batch.repeat.RepeatContext;
import org.springframework.batch.repeat.RepeatListener;
import org.springframework.batch.repeat.policy.SimpleCompletionPolicy;
import org.springframework.batch.repeat.support.RepeatTemplate;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.util.Assert;
@@ -46,6 +51,16 @@ public class TaskletStep extends StepSupport implements InitializingBean {
private JobRepository jobRepository;
private RepeatListener[] listeners = new RepeatListener[] {};
public void setListeners(RepeatListener[] listeners) {
this.listeners = listeners;
}
public void setListener(RepeatListener listener) {
listeners = new RepeatListener[] { listener };
}
/**
* Check mandatory properties.
* @see org.springframework.beans.factory.InitializingBean#afterPropertiesSet()
@@ -97,8 +112,21 @@ public class TaskletStep extends StepSupport implements InitializingBean {
ExitStatus exitStatus = ExitStatus.FAILED;
try {
exitStatus = tasklet.execute();
// We are using the RepeatTemplate as a vehicle for the listener
// so it can be set up cheaply here with standard properties.
RepeatTemplate template = new RepeatTemplate();
template.setCompletionPolicy(new SimpleCompletionPolicy(1));
template.setListeners(listeners);
exitStatus =template.iterate(new RepeatCallback() {
public ExitStatus doInIteration(RepeatContext context) throws Exception {
return tasklet.execute();
}
});
updateStatus(stepExecution, BatchStatus.COMPLETED);
}
catch (Exception e) {
logger.error("Encountered an error running the tasklet");

View File

@@ -30,7 +30,7 @@ import org.springframework.batch.item.ItemWriter;
import org.springframework.batch.item.reader.ItemReaderAdapter;
import org.springframework.batch.item.writer.ItemWriterAdapter;
import org.springframework.batch.repeat.RepeatContext;
import org.springframework.batch.repeat.interceptor.RepeatInterceptorAdapter;
import org.springframework.batch.repeat.interceptor.RepeatListenerAdapter;
import org.springframework.batch.repeat.policy.SimpleCompletionPolicy;
import org.springframework.batch.repeat.support.RepeatTemplate;
import org.springframework.batch.support.transaction.ResourcelessTransactionManager;
@@ -75,7 +75,7 @@ public class RepeatOperationsStepTests extends TestCase {
public void testSuccessfulRepeatOperationsHolder() throws Exception {
RepeatTemplate repeatTemplate = new RepeatTemplate();
final List list = new ArrayList();
repeatTemplate.setInterceptor(new RepeatInterceptorAdapter() {
repeatTemplate.setListener(new RepeatListenerAdapter() {
public void onError(RepeatContext context, Throwable e) {
list.add(e);
}
@@ -107,7 +107,7 @@ public class RepeatOperationsStepTests extends TestCase {
public void testSuccessfulRepeatOperationsHolderWithStepOperations() throws Exception {
RepeatTemplate chunkTemplate = new RepeatTemplate();
final List list = new ArrayList();
chunkTemplate.setInterceptor(new RepeatInterceptorAdapter() {
chunkTemplate.setListener(new RepeatListenerAdapter() {
public void before(RepeatContext context) {
list.add(context);
}
@@ -115,7 +115,7 @@ public class RepeatOperationsStepTests extends TestCase {
chunkTemplate.setCompletionPolicy(new SimpleCompletionPolicy(2));
RepeatTemplate stepTemplate = new RepeatTemplate();
final List steps = new ArrayList();
stepTemplate.setInterceptor(new RepeatInterceptorAdapter() {
stepTemplate.setListener(new RepeatListenerAdapter() {
public void before(RepeatContext context) {
steps.add(context);
}

View File

@@ -51,7 +51,7 @@ import org.springframework.batch.repeat.ExitStatus;
import org.springframework.batch.repeat.RepeatContext;
import org.springframework.batch.repeat.exception.handler.DefaultExceptionHandler;
import org.springframework.batch.repeat.exception.handler.ExceptionHandler;
import org.springframework.batch.repeat.interceptor.RepeatInterceptorAdapter;
import org.springframework.batch.repeat.interceptor.RepeatListenerAdapter;
import org.springframework.batch.repeat.policy.SimpleCompletionPolicy;
import org.springframework.batch.repeat.support.RepeatTemplate;
import org.springframework.batch.support.PropertiesConverter;
@@ -186,7 +186,7 @@ public class SimpleStepExecutorTests extends TestCase {
jobExecution.setId(new Long(1));
final StepExecution stepExecution = new StepExecution(step, jobExecution);
template.setInterceptor(new RepeatInterceptorAdapter() {
template.setListener(new RepeatListenerAdapter() {
public void open(RepeatContext context) {
assertNotNull(StepSynchronizationManager.getContext().getStepExecution());
assertEquals(stepExecution, StepSynchronizationManager.getContext().getStepExecution());

View File

@@ -1,5 +1,8 @@
package org.springframework.batch.execution.step.tasklet;
import java.util.ArrayList;
import java.util.List;
import junit.framework.TestCase;
import org.springframework.batch.core.domain.JobExecution;
@@ -12,11 +15,15 @@ import org.springframework.batch.core.tasklet.Tasklet;
import org.springframework.batch.execution.step.simple.JobRepositorySupport;
import org.springframework.batch.io.exception.BatchCriticalException;
import org.springframework.batch.repeat.ExitStatus;
import org.springframework.batch.repeat.RepeatContext;
import org.springframework.batch.repeat.interceptor.RepeatListenerAdapter;
public class TaskletStepTests extends TestCase {
private StepExecution stepExecution;
private List list = new ArrayList();
protected void setUp() throws Exception {
stepExecution = new StepExecution(new StepInstance(new Long(11)), new JobExecution(new JobInstance(
new Long(0L), new JobParameters()), new Long(12)));
@@ -45,22 +52,37 @@ public class TaskletStepTests extends TestCase {
}
}
public void testSuccessfulExecution() throws StepInterruptedException, BatchCriticalException {
public void testSuccessfulExecution() throws Exception {
TaskletStep step = new TaskletStep(new StubTasklet(false, false), new JobRepositorySupport());
step.execute(stepExecution);
assertNotNull(stepExecution.getStartTime());
assertSame(ExitStatus.FINISHED, stepExecution.getExitStatus());
assertEquals(ExitStatus.FINISHED, stepExecution.getExitStatus());
assertNotNull(stepExecution.getEndTime());
}
public void testFailureExecution() throws StepInterruptedException, BatchCriticalException {
public void testFailureExecution() throws Exception {
TaskletStep step = new TaskletStep(new StubTasklet(true, false), new JobRepositorySupport());
step.execute(stepExecution);
assertNotNull(stepExecution.getStartTime());
assertSame(ExitStatus.FAILED, stepExecution.getExitStatus());
assertEquals(ExitStatus.FAILED, stepExecution.getExitStatus());
assertNotNull(stepExecution.getEndTime());
}
public void testSuccessfulExecutionWithListener() throws Exception {
TaskletStep step = new TaskletStep(new StubTasklet(false, false), new JobRepositorySupport());
step.setListener(new RepeatListenerAdapter() {
public void open(RepeatContext context) {
list.add("open");
}
public void close(RepeatContext context) {
list.add("close");
}
});
step.execute(stepExecution);
System.err.println(list);
assertEquals(2, list.size());
}
public void testExceptionExecution() throws StepInterruptedException, BatchCriticalException {
TaskletStep step = new TaskletStep(new StubTasklet(false, true), new JobRepositorySupport());
try {
@@ -69,7 +91,7 @@ public class TaskletStepTests extends TestCase {
}
catch (BatchCriticalException e) {
assertNotNull(stepExecution.getStartTime());
assertSame(ExitStatus.FAILED, stepExecution.getExitStatus());
assertEquals(ExitStatus.FAILED, stepExecution.getExitStatus());
assertNotNull(stepExecution.getEndTime());
}
}

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);
}
}

View File

@@ -25,7 +25,7 @@ import junit.framework.TestCase;
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.batch.repeat.context.RepeatContextSupport;
import org.springframework.dao.DataAccessException;
import org.springframework.orm.hibernate3.HibernateTemplate;
@@ -46,7 +46,7 @@ public class HibernateAwareItemWriterTests extends TestCase {
};
}
private class StubItemWriter implements ItemWriter, RepeatInterceptor {
private class StubItemWriter implements ItemWriter, RepeatListener {
public void write(Object item) {
list.add(item);
}

View File

@@ -30,9 +30,9 @@ import junit.framework.TestCase;
* @author Dave Syer
*
*/
public class ApplicationEventPublisherRepeatInterceptorTests extends TestCase {
public class ApplicationEventPublisherRepeatListenerTests extends TestCase {
private ApplicationEventPublisherRepeatInterceptor interceptor = new ApplicationEventPublisherRepeatInterceptor();
private ApplicationEventPublisherRepeatListener interceptor = new ApplicationEventPublisherRepeatListener();
private List list = new ArrayList();
@@ -51,7 +51,7 @@ public class ApplicationEventPublisherRepeatInterceptorTests extends TestCase {
}
/**
* Test method for {@link org.springframework.batch.repeat.interceptor.ApplicationEventPublisherRepeatInterceptor#after(org.springframework.batch.repeat.RepeatContext, ExitStatus)}.
* Test method for {@link org.springframework.batch.repeat.interceptor.ApplicationEventPublisherRepeatListener#after(org.springframework.batch.repeat.RepeatContext, ExitStatus)}.
*/
public void testAfter() {
interceptor.after(context, ExitStatus.CONTINUABLE);
@@ -62,7 +62,7 @@ public class ApplicationEventPublisherRepeatInterceptorTests extends TestCase {
}
/**
* Test method for {@link org.springframework.batch.repeat.interceptor.ApplicationEventPublisherRepeatInterceptor#before(org.springframework.batch.repeat.RepeatContext)}.
* Test method for {@link org.springframework.batch.repeat.interceptor.ApplicationEventPublisherRepeatListener#before(org.springframework.batch.repeat.RepeatContext)}.
*/
public void testBefore() {
interceptor.before(context);
@@ -73,7 +73,7 @@ public class ApplicationEventPublisherRepeatInterceptorTests extends TestCase {
}
/**
* Test method for {@link org.springframework.batch.repeat.interceptor.ApplicationEventPublisherRepeatInterceptor#close(org.springframework.batch.repeat.RepeatContext)}.
* Test method for {@link org.springframework.batch.repeat.interceptor.ApplicationEventPublisherRepeatListener#close(org.springframework.batch.repeat.RepeatContext)}.
*/
public void testClose() {
interceptor.close(context);
@@ -84,7 +84,7 @@ public class ApplicationEventPublisherRepeatInterceptorTests extends TestCase {
}
/**
* Test method for {@link org.springframework.batch.repeat.interceptor.ApplicationEventPublisherRepeatInterceptor#onError(org.springframework.batch.repeat.RepeatContext, java.lang.Throwable)}.
* Test method for {@link org.springframework.batch.repeat.interceptor.ApplicationEventPublisherRepeatListener#onError(org.springframework.batch.repeat.RepeatContext, java.lang.Throwable)}.
*/
public void testOnError() {
interceptor.onError(context, new RuntimeException("foo"));
@@ -95,7 +95,7 @@ public class ApplicationEventPublisherRepeatInterceptorTests extends TestCase {
}
/**
* Test method for {@link org.springframework.batch.repeat.interceptor.ApplicationEventPublisherRepeatInterceptor#open(org.springframework.batch.repeat.RepeatContext)}.
* Test method for {@link org.springframework.batch.repeat.interceptor.ApplicationEventPublisherRepeatListener#open(org.springframework.batch.repeat.RepeatContext)}.
*/
public void testOpen() {
interceptor.open(context);

View File

@@ -24,23 +24,23 @@ import junit.framework.TestCase;
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.support.RepeatTemplate;
import org.springframework.batch.repeat.support.TaskExecutorRepeatTemplate;
import org.springframework.core.task.SimpleAsyncTaskExecutor;
public class RepeatInterceptorTests extends TestCase {
public class RepeatListenerTests extends TestCase {
int count = 0;
public void testBeforeInterceptors() throws Exception {
RepeatTemplate template = new RepeatTemplate();
final List calls = new ArrayList();
template.setInterceptors(new RepeatInterceptor[] { new RepeatInterceptorAdapter() {
template.setListeners(new RepeatListener[] { new RepeatListenerAdapter() {
public void before(RepeatContext context) {
calls.add("1");
}
}, new RepeatInterceptorAdapter() {
}, new RepeatListenerAdapter() {
public void before(RepeatContext context) {
calls.add("2");
}
@@ -62,7 +62,7 @@ public class RepeatInterceptorTests extends TestCase {
public void testBeforeInterceptorCanVeto() throws Exception {
RepeatTemplate template = new RepeatTemplate();
final List calls = new ArrayList();
template.setInterceptor(new RepeatInterceptorAdapter() {
template.setListener(new RepeatListenerAdapter() {
public void before(RepeatContext context) {
calls.add("1");
context.setCompleteOnly();
@@ -82,11 +82,11 @@ public class RepeatInterceptorTests extends TestCase {
public void testAfterInterceptors() throws Exception {
RepeatTemplate template = new RepeatTemplate();
final List calls = new ArrayList();
template.setInterceptors(new RepeatInterceptor[] { new RepeatInterceptorAdapter() {
template.setListeners(new RepeatListener[] { new RepeatListenerAdapter() {
public void after(RepeatContext context, ExitStatus result) {
calls.add("1");
}
}, new RepeatInterceptorAdapter() {
}, new RepeatListenerAdapter() {
public void after(RepeatContext context, ExitStatus result) {
calls.add("2");
}
@@ -106,11 +106,11 @@ public class RepeatInterceptorTests extends TestCase {
public void testOpenInterceptors() throws Exception {
RepeatTemplate template = new RepeatTemplate();
final List calls = new ArrayList();
template.setInterceptors(new RepeatInterceptor[] { new RepeatInterceptorAdapter() {
template.setListeners(new RepeatListener[] { new RepeatListenerAdapter() {
public void open(RepeatContext context) {
calls.add("1");
}
}, new RepeatInterceptorAdapter() {
}, new RepeatListenerAdapter() {
public void open(RepeatContext context) {
calls.add("2");
context.setCompleteOnly();
@@ -129,7 +129,7 @@ public class RepeatInterceptorTests extends TestCase {
public void testSingleOpenInterceptor() throws Exception {
RepeatTemplate template = new RepeatTemplate();
final List calls = new ArrayList();
template.setInterceptor(new RepeatInterceptorAdapter() {
template.setListener(new RepeatListenerAdapter() {
public void open(RepeatContext context) {
calls.add("1");
}
@@ -148,11 +148,11 @@ public class RepeatInterceptorTests extends TestCase {
public void testCloseInterceptors() throws Exception {
RepeatTemplate template = new RepeatTemplate();
final List calls = new ArrayList();
template.setInterceptors(new RepeatInterceptor[] { new RepeatInterceptorAdapter() {
template.setListeners(new RepeatListener[] { new RepeatListenerAdapter() {
public void close(RepeatContext context) {
calls.add("1");
}
}, new RepeatInterceptorAdapter() {
}, new RepeatListenerAdapter() {
public void close(RepeatContext context) {
calls.add("2");
}
@@ -173,11 +173,11 @@ public class RepeatInterceptorTests extends TestCase {
public void testOnErrorInterceptors() throws Exception {
RepeatTemplate template = new RepeatTemplate();
final List calls = new ArrayList();
template.setInterceptors(new RepeatInterceptor[] { new RepeatInterceptorAdapter() {
template.setListeners(new RepeatListener[] { new RepeatListenerAdapter() {
public void onError(RepeatContext context, Throwable t) {
calls.add("1");
}
}, new RepeatInterceptorAdapter() {
}, new RepeatListenerAdapter() {
public void onError(RepeatContext context, Throwable t) {
calls.add("2");
}
@@ -200,11 +200,11 @@ public class RepeatInterceptorTests extends TestCase {
public void testOnErrorInterceptorsPrecedence() throws Exception {
RepeatTemplate template = new RepeatTemplate();
final List calls = new ArrayList();
template.setInterceptors(new RepeatInterceptor[] { new RepeatInterceptorAdapter() {
template.setListeners(new RepeatListener[] { new RepeatListenerAdapter() {
public void after(RepeatContext context, ExitStatus result) {
calls.add("1");
}
}, new RepeatInterceptorAdapter() {
}, new RepeatListenerAdapter() {
public void onError(RepeatContext context, Throwable t) {
calls.add("2");
}
@@ -230,11 +230,11 @@ public class RepeatInterceptorTests extends TestCase {
template.setTaskExecutor(new SimpleAsyncTaskExecutor());
final List calls = new ArrayList();
final List fails = new ArrayList();
template.setInterceptors(new RepeatInterceptor[] { new RepeatInterceptorAdapter() {
template.setListeners(new RepeatListener[] { new RepeatListenerAdapter() {
public void after(RepeatContext context, ExitStatus result) {
calls.add("1");
}
}, new RepeatInterceptorAdapter() {
}, new RepeatListenerAdapter() {
public void onError(RepeatContext context, Throwable t) {
calls.add("2");
fails.add("2");

View File

@@ -18,10 +18,10 @@ package org.springframework.batch.retry.interceptor;
import junit.framework.TestCase;
public class RetryInterceptorSupportTests extends TestCase {
public class RetryListenerSupportTests extends TestCase {
public void testClose() {
RetryInterceptorSupport support = new RetryInterceptorSupport();
RetryListenerAdapter support = new RetryListenerAdapter();
try {
support.close(null, null, null);
}
@@ -31,7 +31,7 @@ public class RetryInterceptorSupportTests extends TestCase {
}
public void testOnError() {
RetryInterceptorSupport support = new RetryInterceptorSupport();
RetryListenerAdapter support = new RetryListenerAdapter();
try {
support.onError(null, null, null);
}
@@ -41,7 +41,7 @@ public class RetryInterceptorSupportTests extends TestCase {
}
public void testOpen() {
RetryInterceptorSupport support = new RetryInterceptorSupport();
RetryListenerAdapter support = new RetryListenerAdapter();
assertTrue(support.open(null, null));
}

View File

@@ -23,12 +23,12 @@ import junit.framework.TestCase;
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.exception.TerminatedRetryException;
import org.springframework.batch.retry.policy.NeverRetryPolicy;
import org.springframework.batch.retry.support.RetryTemplate;
public class RetryInterceptorTests extends TestCase {
public class RetryListenerTests extends TestCase {
RetryTemplate template = new RetryTemplate();
@@ -37,13 +37,13 @@ public class RetryInterceptorTests extends TestCase {
List list = new ArrayList();
public void testOpenInterceptors() throws Exception {
template.setInterceptors(new RetryInterceptor[] { new RetryInterceptorSupport() {
template.setListeners(new RetryListener[] { new RetryListenerAdapter() {
public boolean open(RetryContext context, RetryCallback callback) {
count++;
list.add("1:" + count);
return true;
}
}, new RetryInterceptorSupport() {
}, new RetryListenerAdapter() {
public boolean open(RetryContext context, RetryCallback callback) {
count++;
list.add("2:" + count);
@@ -61,7 +61,7 @@ public class RetryInterceptorTests extends TestCase {
}
public void testOpenCanVetoRetry() throws Exception {
template.setInterceptor(new RetryInterceptorSupport() {
template.setListener(new RetryListenerAdapter() {
public boolean open(RetryContext context, RetryCallback callback) {
list.add("1");
return false;
@@ -85,12 +85,12 @@ public class RetryInterceptorTests extends TestCase {
}
public void testCloseInterceptors() throws Exception {
template.setInterceptors(new RetryInterceptor[] { new RetryInterceptorSupport() {
template.setListeners(new RetryListener[] { new RetryListenerAdapter() {
public void close(RetryContext context, RetryCallback callback, Throwable t) {
count++;
list.add("1:" + count);
}
}, new RetryInterceptorSupport() {
}, new RetryListenerAdapter() {
public void close(RetryContext context, RetryCallback callback, Throwable t) {
count++;
list.add("2:" + count);
@@ -109,11 +109,11 @@ public class RetryInterceptorTests extends TestCase {
public void testOnError() throws Exception {
template.setRetryPolicy(new NeverRetryPolicy());
template.setInterceptors(new RetryInterceptor[] { new RetryInterceptorSupport() {
template.setListeners(new RetryListener[] { new RetryListenerAdapter() {
public void onError(RetryContext context, RetryCallback callback, Throwable throwable) {
list.add("1");
}
}, new RetryInterceptorSupport() {
}, new RetryListenerAdapter() {
public void onError(RetryContext context, RetryCallback callback, Throwable throwable) {
list.add("2");
}
@@ -139,7 +139,7 @@ public class RetryInterceptorTests extends TestCase {
}
public void testCloseInterceptorsAfterRetry() throws Exception {
template.setInterceptor(new RetryInterceptorSupport() {
template.setListener(new RetryListenerAdapter() {
public void close(RetryContext context, RetryCallback callback, Throwable t) {
list.add("" + count);
// The last attempt should have been successful:

View File

@@ -20,7 +20,7 @@ import java.util.List;
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.batch.sample.domain.CustomerCredit;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
@@ -30,7 +30,7 @@ import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
*
*/
public class HibernateCreditWriter extends HibernateDaoSupport implements
CustomerCreditDao, RepeatInterceptor {
CustomerCreditDao, RepeatListener {
private int failOnFlush = -1;
private List errors = new ArrayList();

View File

@@ -15,7 +15,7 @@
<bean id="loopJob" parent="simpleJob">
<property name="steps">
<bean id="step1" parent="repeatOperationsStep">
<bean id="step1" parent="taskletStep">
<property name="tasklet">
<bean id="module"
class="org.springframework.batch.sample.tasklet.InfiniteLoopTasklet"
@@ -23,27 +23,10 @@
<aop:scoped-proxy />
</bean>
</property>
<property name="chunkOperations">
<property name="listener">
<bean
class="org.springframework.batch.repeat.support.RepeatTemplate">
<property name="completionPolicy">
<bean
class="org.springframework.batch.repeat.policy.SimpleCompletionPolicy">
<property name="chunkSize" value="2"/>
</bean>
</property>
</bean>
class="org.springframework.batch.repeat.interceptor.ApplicationEventPublisherRepeatListener" />
</property>
<property name="stepOperations">
<bean
class="org.springframework.batch.repeat.support.RepeatTemplate">
<property name="interceptor">
<bean
class="org.springframework.batch.repeat.interceptor.ApplicationEventPublisherRepeatInterceptor" />
</property>
</bean>
</property>
</bean>
</property>
</bean>

View File

@@ -25,7 +25,7 @@
<property name="chunkOperations">
<bean
class="org.springframework.batch.repeat.support.RepeatTemplate">
<property name="interceptors"
<property name="listeners"
ref="hibernateOutputSource" />
<property name="completionPolicy">
<bean