Unwrap ExecutionException in the AsyncItemWriter
When using the `AsyncItemProcessor` and `AsyncItemWriter`, business exceptions that occur during the process phase are hidden by an `ExecutionException` that is returned wrapping the originally thrown exception in the `AsyncItemWriter`. In this commit, we now unwrap any `ExecutionException` that is returned and throw the cause. Debug logging is also added to allow the logging of the original exception as well. Resolves BATCH-2386
This commit is contained in:
@@ -15,6 +15,14 @@
|
||||
*/
|
||||
package org.springframework.batch.integration.async;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.ExecutionException;
|
||||
import java.util.concurrent.Future;
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
|
||||
import org.springframework.batch.item.ExecutionContext;
|
||||
import org.springframework.batch.item.ItemStream;
|
||||
import org.springframework.batch.item.ItemStreamException;
|
||||
@@ -23,12 +31,10 @@ import org.springframework.batch.item.ItemWriter;
|
||||
import org.springframework.beans.factory.InitializingBean;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.Future;
|
||||
|
||||
public class AsyncItemWriter<T> implements ItemStreamWriter<Future<T>>, InitializingBean {
|
||||
|
||||
private static final Log logger = LogFactory.getLog(AsyncItemWriter.class);
|
||||
|
||||
private ItemWriter<T> delegate;
|
||||
|
||||
public void afterPropertiesSet() throws Exception {
|
||||
@@ -45,7 +51,9 @@ public class AsyncItemWriter<T> implements ItemStreamWriter<Future<T>>, Initiali
|
||||
/**
|
||||
* In the processing of the {@link java.util.concurrent.Future}s passed, nulls are <em>not</em> passed to the
|
||||
* delegate since they are considered filtered out by the {@link org.springframework.batch.integration.async.AsyncItemProcessor}'s
|
||||
* delegated {@link org.springframework.batch.item.ItemProcessor}.
|
||||
* delegated {@link org.springframework.batch.item.ItemProcessor}. If the unwrapping
|
||||
* of the {@link Future} results in an {@link ExecutionException}, that will be
|
||||
* unwrapped and the cause will be thrown.
|
||||
*
|
||||
* @param items {@link java.util.concurrent.Future}s to be upwrapped and passed to the delegate
|
||||
* @throws Exception
|
||||
@@ -53,12 +61,27 @@ public class AsyncItemWriter<T> implements ItemStreamWriter<Future<T>>, Initiali
|
||||
public void write(List<? extends Future<T>> items) throws Exception {
|
||||
List<T> list = new ArrayList<T>();
|
||||
for (Future<T> future : items) {
|
||||
T item = future.get();
|
||||
try {
|
||||
T item = future.get();
|
||||
|
||||
if(item != null) {
|
||||
list.add(future.get());
|
||||
if(item != null) {
|
||||
list.add(future.get());
|
||||
}
|
||||
}
|
||||
catch (ExecutionException e) {
|
||||
Throwable cause = e.getCause();
|
||||
|
||||
if(cause != null && cause instanceof Exception) {
|
||||
logger.debug("An exception was thrown while processing an item", e);
|
||||
|
||||
throw (Exception) cause;
|
||||
}
|
||||
else {
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
delegate.write(list);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user