OPEN - issue BATCH-787: Add write(List) to ItemWriter

Make samples and cli archetype compile again
This commit is contained in:
dsyer
2008-08-19 14:31:41 +00:00
parent 34f7cd8519
commit a5080b772e
31 changed files with 270 additions and 207 deletions

View File

@@ -64,10 +64,12 @@ public class ChunkMessageChannelItemWriter<T> extends StepExecutionListenerSuppo
this.target = target;
}
public void write(T item) throws Exception {
public void write(List<? extends T> items) throws Exception {
bindTransactionResources();
getProcessed().add(item);
logger.debug("Added item to chunk: " + item);
for (T item : items) {
getProcessed().add(item);
logger.debug("Added item to chunk: " + item);
}
}
/**

View File

@@ -1,5 +1,7 @@
package org.springframework.batch.integration.chunk;
import java.util.Collections;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.batch.core.SkipListener;
@@ -55,7 +57,7 @@ public class ItemWriterChunkHandler<T> implements ChunkHandler<T> {
try {
for (T item : chunk.getItems()) {
try {
itemWriter.write(item);
itemWriter.write(Collections.singletonList(item));
}
catch (Exception e) {
if (itemSkipPolicy.shouldSkip(e, parentSkipCount + skipCount)) {

View File

@@ -15,6 +15,8 @@
*/
package org.springframework.batch.integration.item;
import java.util.List;
import org.springframework.batch.item.support.AbstractItemWriter;
import org.springframework.beans.factory.annotation.Required;
import org.springframework.integration.channel.MessageChannel;
@@ -39,10 +41,13 @@ public class MessageChannelItemWriter<T> extends AbstractItemWriter<T> {
/*
* (non-Javadoc)
*
* @see org.springframework.batch.item.ItemWriter#write(java.lang.Object)
*/
public void write(T item) throws Exception {
channel.send(new GenericMessage<T>(item));
public void write(List<? extends T> items) throws Exception {
for (T item : items) {
channel.send(new GenericMessage<T>(item));
}
}
}

View File

@@ -2,13 +2,12 @@ package org.springframework.batch.integration.chunk;
import static org.junit.Assert.assertEquals;
import java.util.List;
import org.junit.Test;
import org.springframework.batch.core.SkipListener;
import org.springframework.batch.core.listener.SkipListenerSupport;
import org.springframework.batch.core.step.skip.AlwaysSkipItemSkipPolicy;
import org.springframework.batch.integration.chunk.ChunkRequest;
import org.springframework.batch.integration.chunk.ChunkResponse;
import org.springframework.batch.integration.chunk.ItemWriterChunkHandler;
import org.springframework.batch.item.support.AbstractItemWriter;
import org.springframework.batch.repeat.ExitStatus;
import org.springframework.util.StringUtils;
@@ -29,9 +28,9 @@ public class ItemWriterChunkHandlerTests {
@SuppressWarnings("unchecked")
@Test
public void testVanillaHandleChunk() {
handler.setItemWriter(new AbstractItemWriter() {
public void write(Object item) throws Exception {
count++;
handler.setItemWriter(new AbstractItemWriter<Object>() {
public void write(List<? extends Object> items) throws Exception {
count+=items.size();
}
});
ChunkResponse response = handler.handleChunk(new ChunkRequest(StringUtils.commaDelimitedListToSet("foo,bar"),
@@ -45,9 +44,9 @@ public class ItemWriterChunkHandlerTests {
@SuppressWarnings("unchecked")
@Test
public void testSetItemSkipPolicy() {
handler.setItemWriter(new AbstractItemWriter() {
public void write(Object item) throws Exception {
count++;
handler.setItemWriter(new AbstractItemWriter<Object>() {
public void write(List<? extends Object> items) throws Exception {
count+=items.size();
throw new RuntimeException("Planned failure");
}
});
@@ -63,9 +62,9 @@ public class ItemWriterChunkHandlerTests {
@SuppressWarnings("unchecked")
@Test
public void testRegisterSkipListener() {
handler.setItemWriter(new AbstractItemWriter() {
public void write(Object item) throws Exception {
count++;
handler.setItemWriter(new AbstractItemWriter<Object>() {
public void write(List<? extends Object> items) throws Exception {
count+=items.size();
throw new RuntimeException("Planned failure");
}
});

View File

@@ -1,5 +1,7 @@
package org.springframework.batch.integration.chunk;
import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.batch.item.support.AbstractItemWriter;
@@ -25,23 +27,28 @@ public class TestItemWriter<T> extends AbstractItemWriter<T> {
*/
public static final String WAIT_ON = "wait";
public void write(T item) throws Exception {
count++;
logger.debug("Writing: "+item);
public void write(List<? extends T> items) throws Exception {
if (item.equals(WAIT_ON)) {
try {
Thread.sleep(200);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
throw new RuntimeException("Unexpected interruption.", e);
for (T item : items) {
count++;
logger.debug("Writing: " + item);
if (item.equals(WAIT_ON)) {
try {
Thread.sleep(200);
}
catch (InterruptedException e) {
Thread.currentThread().interrupt();
throw new RuntimeException("Unexpected interruption.", e);
}
}
if (item.equals(FAIL_ON)) {
throw new IllegalStateException("Planned failure on: " + FAIL_ON);
}
}
if (item.equals(FAIL_ON)) {
throw new IllegalStateException("Planned failure on: " + FAIL_ON);
}
}

View File

@@ -18,6 +18,8 @@ package org.springframework.batch.integration.item;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import java.util.Collections;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.batch.item.ItemWriter;
@@ -43,7 +45,7 @@ public class MessageChannelItemWriterIntegrationTests {
@Test
public void testSend() throws Exception {
itemWriter.write("foo");
itemWriter.write(Collections.singletonList("foo"));
Message<?> message = channel.receive(10);
assertNotNull(message);
assertEquals("foo", message.getPayload());

View File

@@ -21,6 +21,7 @@ import static org.junit.Assert.fail;
import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.util.Collections;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Required;
@@ -63,7 +64,7 @@ public class MessageChannelItemWriterTests {
channel.subscribe(receiver);
MessageChannelItemWriter<String> writer = new MessageChannelItemWriter<String>();
writer.setChannel(channel);
writer.write("foo");
writer.write(Collections.singletonList("foo"));
Message<?> message = receiver.receive(10);
assertNotNull(message);
assertEquals("foo", message.getPayload());
@@ -84,7 +85,7 @@ public class MessageChannelItemWriterTests {
MessageChannelItemWriter<String> writer = new MessageChannelItemWriter<String>();
writer.setChannel(channel);
try {
writer.write("foo");
writer.write(Collections.singletonList("foo"));
fail("Expected RuntimeException");
}
catch (RuntimeException e) {
@@ -108,7 +109,7 @@ public class MessageChannelItemWriterTests {
MessageChannelItemWriter<String> writer = new MessageChannelItemWriter<String>();
writer.setChannel(channel);
try {
writer.write("foo");
writer.write(Collections.singletonList("foo"));
fail("Expected RuntimeException");
}
catch (RuntimeException e) {