OPEN - issue BATCH-378: RepeatListener is confusing and too generic to use for 'intercepting' a step
http://jira.springframework.org/browse/BATCH-378 Fix hibernate aware item writer - no need for listener interface now that ItemWRiter has flush/clear
This commit is contained in:
@@ -0,0 +1,100 @@
|
||||
/*
|
||||
* 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.execution.listener;
|
||||
|
||||
import org.springframework.batch.core.domain.ItemReadListener;
|
||||
import org.springframework.batch.repeat.RepeatContext;
|
||||
import org.springframework.batch.repeat.RepeatListener;
|
||||
import org.springframework.batch.repeat.synch.RepeatSynchronizationManager;
|
||||
import org.springframework.util.ObjectUtils;
|
||||
|
||||
/**
|
||||
* Adapts a {@link RepeatListener} to the {@link ItemReadListener} interface.
|
||||
* The {@link RepeatListener} is assumed to be targeted at the chunk operations
|
||||
* in a step, so after an item corresponds to the after method in
|
||||
* {@link RepeatListener}.<br/>
|
||||
*
|
||||
* The open and close methods on the {@link RepeatListener} are also invoked.
|
||||
* The open method is called on the first call to {@link #beforeRead()}, and
|
||||
* the close is registered as a destruction callback in the
|
||||
* {@link RepeatContext}.<br/>
|
||||
*
|
||||
* A {@link RepeatContext} is obtained as needed from the
|
||||
* {@link RepeatSynchronizationManager}.
|
||||
*
|
||||
* @author Dave Syer
|
||||
*
|
||||
*/
|
||||
public class RepeatListenerItemReadListenerAdapter implements ItemReadListener {
|
||||
|
||||
private RepeatListener delegate;
|
||||
|
||||
/**
|
||||
* @param delegate
|
||||
*/
|
||||
public RepeatListenerItemReadListenerAdapter(RepeatListener delegate) {
|
||||
super();
|
||||
this.delegate = delegate;
|
||||
}
|
||||
|
||||
/**
|
||||
* Does nothing.
|
||||
*
|
||||
* @see org.springframework.batch.core.domain.ItemReadListener#afterRead(java.lang.Object)
|
||||
*/
|
||||
public void afterRead(Object item) {
|
||||
// NO-OP
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Calls the delegate {@link RepeatListener#before}. Also calls
|
||||
* {@link RepeatListener#open} if it hasn't been called yet on this context.
|
||||
*
|
||||
* @see org.springframework.batch.core.domain.ItemReadListener#beforeRead()
|
||||
*/
|
||||
public void beforeRead() {
|
||||
RepeatContext context = RepeatSynchronizationManager.getContext();
|
||||
maybeOpen(context);
|
||||
delegate.before(context);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param context
|
||||
*/
|
||||
private void maybeOpen(final RepeatContext context) {
|
||||
String identity = ObjectUtils.identityToString(delegate);
|
||||
if (!context.hasAttribute(identity)) {
|
||||
context.setAttribute(identity, Boolean.TRUE);
|
||||
delegate.open(context);
|
||||
context.registerDestructionCallback(identity, new Runnable() {
|
||||
public void run() {
|
||||
delegate.close(context);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Calls the delegate {@link RepeatListener#onError}.
|
||||
*
|
||||
* @see org.springframework.batch.core.domain.ItemReadListener#onReadError(java.lang.Exception)
|
||||
*/
|
||||
public void onReadError(Exception ex) {
|
||||
delegate.onError(RepeatSynchronizationManager.getContext(), ex);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,77 @@
|
||||
/*
|
||||
* 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.execution.listener;
|
||||
|
||||
import org.springframework.batch.core.domain.ItemWriteListener;
|
||||
import org.springframework.batch.repeat.ExitStatus;
|
||||
import org.springframework.batch.repeat.RepeatContext;
|
||||
import org.springframework.batch.repeat.RepeatListener;
|
||||
import org.springframework.batch.repeat.synch.RepeatSynchronizationManager;
|
||||
|
||||
/**
|
||||
* Adapts a {@link RepeatListener} to the {@link ItemWriteListener} interface.
|
||||
* The {@link RepeatListener} is assumed to be targeted at the chunk operations
|
||||
* in a step, so after an item corresponds to the after method in
|
||||
* {@link RepeatListener}.<br/>
|
||||
*
|
||||
* A {@link RepeatContext} is obtained as needed from the
|
||||
* {@link RepeatSynchronizationManager}.
|
||||
*
|
||||
* @author Dave Syer
|
||||
*
|
||||
*/
|
||||
public class RepeatListenerItemWriteListenerAdapter implements ItemWriteListener {
|
||||
|
||||
private RepeatListener delegate;
|
||||
|
||||
/**
|
||||
* @param delegate
|
||||
*/
|
||||
public RepeatListenerItemWriteListenerAdapter(RepeatListener delegate) {
|
||||
super();
|
||||
this.delegate = delegate;
|
||||
}
|
||||
|
||||
/**
|
||||
* Calls the delegate {@link RepeatListener#after} with
|
||||
* {@link ExitStatus#CONTINUABLE}.
|
||||
*
|
||||
* @see org.springframework.batch.core.domain.ItemWriteListener#afterWrite()
|
||||
*/
|
||||
public void afterWrite() {
|
||||
delegate.after(RepeatSynchronizationManager.getContext(), ExitStatus.CONTINUABLE);
|
||||
}
|
||||
|
||||
/**
|
||||
* Does nothing.
|
||||
*
|
||||
* @see org.springframework.batch.core.domain.ItemWriteListener#beforeWrite(java.lang.Object)
|
||||
*/
|
||||
public void beforeWrite(Object item) {
|
||||
// NO-OP
|
||||
}
|
||||
|
||||
/**
|
||||
* Calls the delegate {@link RepeatListener#onError} ignoring the item.
|
||||
*
|
||||
* @see org.springframework.batch.core.domain.ItemWriteListener#onWriteError(java.lang.Exception,
|
||||
* java.lang.Object)
|
||||
*/
|
||||
public void onWriteError(Exception ex, Object item) {
|
||||
delegate.onError(RepeatSynchronizationManager.getContext(), ex);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -295,7 +295,6 @@ public class ItemOrientedStep extends AbstractStep implements InitializingBean {
|
||||
|
||||
listener.update(stepExecution.getExecutionContext());
|
||||
try {
|
||||
stepExecution.setStatus(BatchStatus.COMPLETED);
|
||||
jobRepository.saveOrUpdateExecutionContext(stepExecution);
|
||||
}
|
||||
catch (Exception e) {
|
||||
|
||||
@@ -25,11 +25,15 @@ import org.springframework.batch.execution.listener.CompositeChunkListener;
|
||||
import org.springframework.batch.execution.listener.CompositeItemReadListener;
|
||||
import org.springframework.batch.execution.listener.CompositeItemWriteListener;
|
||||
import org.springframework.batch.execution.listener.CompositeStepListener;
|
||||
import org.springframework.batch.execution.listener.RepeatListenerItemReadListenerAdapter;
|
||||
import org.springframework.batch.execution.listener.RepeatListenerItemWriteListenerAdapter;
|
||||
import org.springframework.batch.item.ExecutionContext;
|
||||
import org.springframework.batch.item.ItemStream;
|
||||
import org.springframework.batch.item.exception.StreamException;
|
||||
import org.springframework.batch.item.stream.CompositeItemStream;
|
||||
import org.springframework.batch.repeat.ExitStatus;
|
||||
import org.springframework.batch.repeat.RepeatListener;
|
||||
import org.springframework.batch.repeat.interceptor.CompositeRepeatListener;
|
||||
|
||||
/**
|
||||
* @author Dave Syer
|
||||
@@ -48,6 +52,17 @@ public class ListenerMulticaster implements ItemStream, StepListener, ChunkListe
|
||||
|
||||
private CompositeItemWriteListener itemWriteListener = new CompositeItemWriteListener();
|
||||
|
||||
private CompositeRepeatListener repeatListener = new CompositeRepeatListener();
|
||||
|
||||
/**
|
||||
* Initialise the listener instance.
|
||||
*/
|
||||
public ListenerMulticaster() {
|
||||
super();
|
||||
itemWriteListener.register(new RepeatListenerItemWriteListenerAdapter(repeatListener));
|
||||
itemReadListener.register(new RepeatListenerItemReadListenerAdapter(repeatListener));
|
||||
}
|
||||
|
||||
/**
|
||||
* Register each of the objects as listeners. Once registered, calls to the
|
||||
* {@link ListenerMulticaster} broadcast to the individual listeners.
|
||||
@@ -82,6 +97,9 @@ public class ListenerMulticaster implements ItemStream, StepListener, ChunkListe
|
||||
if (listener instanceof ItemWriteListener) {
|
||||
this.itemWriteListener.register((ItemWriteListener) listener);
|
||||
}
|
||||
if (listener instanceof RepeatListener) {
|
||||
this.repeatListener.register((RepeatListener) listener);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user