BATCH-2312 Implement ItemStream in AsyncItemWriter

This commit adds the feature of allowing a delegate ItemWriter to be an
ItemStreamWriter and have Spring Batch respect the ItemStream lifecycle
events without explicitly configuring the delegate as a stream.
This commit is contained in:
Amer Aljovic
2015-06-24 13:46:15 +02:00
committed by Michael Minella
parent ff2b6a8cee
commit 716c9baff3
2 changed files with 106 additions and 6 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2006-2007 the original author or authors.
* Copyright 2006-2015 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.
@@ -15,6 +15,10 @@
*/
package org.springframework.batch.integration.async;
import org.springframework.batch.item.ExecutionContext;
import org.springframework.batch.item.ItemStream;
import org.springframework.batch.item.ItemStreamException;
import org.springframework.batch.item.ItemStreamWriter;
import org.springframework.batch.item.ItemWriter;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.util.Assert;
@@ -23,14 +27,14 @@ import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Future;
public class AsyncItemWriter<T> implements ItemWriter<Future<T>>, InitializingBean {
public class AsyncItemWriter<T> implements ItemStreamWriter<Future<T>>, InitializingBean {
private ItemWriter<T> delegate;
public void afterPropertiesSet() throws Exception {
Assert.notNull(delegate, "A delegate ItemWriter must be provided.");
}
/**
* @param delegate ItemWriter that does the actual writing of the Future results
*/
@@ -57,4 +61,25 @@ public class AsyncItemWriter<T> implements ItemWriter<Future<T>>, InitializingBe
}
delegate.write(list);
}
@Override
public void open(ExecutionContext executionContext) throws ItemStreamException {
if (delegate instanceof ItemStream) {
((ItemStream) delegate).open(executionContext);
}
}
@Override
public void update(ExecutionContext executionContext) throws ItemStreamException {
if (delegate instanceof ItemStream) {
((ItemStream) delegate).update(executionContext);
}
}
@Override
public void close() throws ItemStreamException {
if (delegate instanceof ItemStream) {
((ItemStream) delegate).close();
}
}
}