PollableSource no longer accepts a "limit" argument in its poll() method (INT-181).

This commit is contained in:
Mark Fisher
2008-04-09 16:33:16 +00:00
parent fec6dec258
commit 6e30173509
14 changed files with 123 additions and 177 deletions

View File

@@ -17,8 +17,6 @@
package org.springframework.integration.adapter;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.Collection;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.integration.ConfigurationException;
@@ -56,11 +54,11 @@ public class MethodInvokingSource<T> implements PollableSource<Object>, Initiali
this.invoker.setMethodValidator(new MessageReceivingMethodValidator());
}
public Collection<Object> poll(int limit) {
public Object poll() {
if (this.invoker == null) {
this.afterPropertiesSet();
}
return Arrays.asList(this.invoker.invokeMethod(new Object[] {}));
return this.invoker.invokeMethod(new Object[] {});
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2007 the original author or authors.
* Copyright 2002-2008 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.
@@ -16,8 +16,6 @@
package org.springframework.integration.adapter;
import java.util.Collection;
/**
* Interface for any external data source that can be polled.
*
@@ -25,6 +23,6 @@ import java.util.Collection;
*/
public interface PollableSource<T> {
Collection<T> poll(int limit);
T poll();
}

View File

@@ -16,14 +16,12 @@
package org.springframework.integration.adapter;
import java.util.Collection;
import java.util.concurrent.Executors;
import org.springframework.context.Lifecycle;
import org.springframework.integration.ConfigurationException;
import org.springframework.integration.channel.MessageChannel;
import org.springframework.integration.message.MessageMapper;
import org.springframework.integration.message.MessagingException;
import org.springframework.integration.scheduling.MessagingTask;
import org.springframework.integration.scheduling.MessagingTaskScheduler;
import org.springframework.integration.scheduling.MessagingTaskSchedulerAware;
@@ -142,16 +140,14 @@ public class PollingSourceAdapter<T> extends AbstractSourceAdapter<T> implements
}
int messagesProcessed = 0;
int limit = this.maxMessagesPerTask;
Collection<T> results = this.source.poll(limit);
if (results != null) {
if (results.size() > limit) {
throw new MessagingException("source returned too many results, the limit is " + limit);
while (messagesProcessed < limit) {
T result = this.source.poll();
if (result != null && this.sendToChannel(result)) {
messagesProcessed++;
this.onSend(result);
}
for (T next : results) {
if (this.sendToChannel(next)) {
messagesProcessed++;
this.onSend(next);
}
else {
return messagesProcessed;
}
}
return messagesProcessed;

View File

@@ -17,7 +17,6 @@
package org.springframework.integration.dispatcher;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Queue;
import java.util.concurrent.LinkedBlockingQueue;
@@ -30,7 +29,6 @@ import org.springframework.integration.handler.MessageHandler;
import org.springframework.integration.message.Message;
import org.springframework.integration.message.SimplePayloadMessageMapper;
import org.springframework.integration.message.selector.MessageSelector;
import org.springframework.util.CollectionUtils;
/**
* A channel that invokes the subscribed {@link MessageHandler handler(s)} in a
@@ -86,9 +84,8 @@ public class SynchronousChannel extends AbstractMessageChannel {
@Override
protected Message<?> doReceive(long timeout) {
if (this.source != null) {
Collection<?> results = this.source.poll(1);
if (!CollectionUtils.isEmpty(results)) {
Object result = results.iterator().next();
Object result = this.source.poll();
if (result != null) {
return (result instanceof Message<?>) ? (Message<?>) result :
new SimplePayloadMessageMapper<Object>().toMessage(result);
}

View File

@@ -19,8 +19,6 @@ package org.springframework.integration.adapter;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import java.util.Collection;
import org.junit.Test;
import org.springframework.integration.message.MessagingException;
@@ -35,9 +33,9 @@ public class MethodInvokingSourceTests {
MethodInvokingSource<TestBean> source = new MethodInvokingSource<TestBean>();
source.setObject(new TestBean());
source.setMethod("validMethod");
Collection<Object> result = source.poll(5);
Object result = source.poll();
assertNotNull(result);
assertEquals("valid", result.iterator().next());
assertEquals("valid", result);
}
@Test(expected=MessagingException.class)
@@ -45,7 +43,7 @@ public class MethodInvokingSourceTests {
MethodInvokingSource<TestBean> source = new MethodInvokingSource<TestBean>();
source.setObject(new TestBean());
source.setMethod("noMatchingMethod");
source.poll(5);
source.poll();
}
@Test(expected=MessagingException.class)
@@ -53,7 +51,7 @@ public class MethodInvokingSourceTests {
MethodInvokingSource<TestBean> source = new MethodInvokingSource<TestBean>();
source.setObject(new TestBean());
source.setMethod("invalidMethodWithArg");
source.poll(5);
source.poll();
}
@Test(expected=MessagingException.class)
@@ -61,7 +59,7 @@ public class MethodInvokingSourceTests {
MethodInvokingSource<TestBean> source = new MethodInvokingSource<TestBean>();
source.setObject(new TestBean());
source.setMethod("invalidMethodWithNoReturnValue");
source.poll(5);
source.poll();
}

View File

@@ -20,16 +20,12 @@ import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
import org.junit.Test;
import org.springframework.integration.channel.SimpleChannel;
import org.springframework.integration.message.Message;
import org.springframework.integration.message.MessagingException;
/**
* @author Mark Fisher
@@ -66,11 +62,12 @@ public class PollingSourceAdapterTests {
assertEquals("testing.1", message1.getPayload());
Message<?> message2 = channel.receive(0);
assertNull("second message should be null", message2);
source.resetCounter();
adapter.start();
adapter.processMessages();
Message<?> message3 = channel.receive(100);
assertNotNull("third message should not be null", message3);
assertEquals("testing.3", message3.getPayload());
assertEquals("testing.1", message3.getPayload());
}
@Test
@@ -96,38 +93,29 @@ public class PollingSourceAdapterTests {
assertNull("message should be null", message4);
}
@Test(expected=MessagingException.class)
public void testResultSizeExceedsLimit() {
TestSource source = new TestSource("testing", 3);
SimpleChannel channel = new SimpleChannel();
PollingSourceAdapter<String> adapter = new PollingSourceAdapter<String>(source);
adapter.setChannel(channel);
adapter.setPeriod(1000);
adapter.setMaxMessagesPerTask(2);
adapter.start();
adapter.processMessages();
}
private static class TestSource implements PollableSource<String> {
private String message;
private int messagesPerPoll;
private int limit;
private AtomicInteger count = new AtomicInteger();
public TestSource(String message, int messagesPerPoll) {
public TestSource(String message, int limit) {
this.message = message;
this.messagesPerPoll = messagesPerPoll;
this.limit = limit;
}
public Collection<String> poll(int limit) {
List<String> results = new ArrayList<String>(this.messagesPerPoll);
for (int i = 0; i < this.messagesPerPoll; i++) {
results.add(message + "." + count.incrementAndGet());
public void resetCounter() {
this.count.set(0);
}
public String poll() {
if (count.get() >= limit) {
return null;
}
return results;
return message + "." + count.incrementAndGet();
}
}

View File

@@ -269,7 +269,7 @@ public class MessageBusTests {
this.latch = latch;
}
public Collection<Object> poll(int limit) {
public Collection<Object> poll() {
latch.countDown();
throw new RuntimeException("intentional test failure");
}

View File

@@ -21,8 +21,6 @@ import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.SynchronousQueue;
@@ -99,8 +97,8 @@ public class SynchronousChannelTests {
@Test
public void testReceive() {
SynchronousChannel channel = new SynchronousChannel(new PollableSource<String>() {
public Collection<String> poll(int limit) {
return Collections.singleton("foo");
public String poll() {
return "foo";
}
});
Message<?> message = channel.receive();
@@ -186,10 +184,10 @@ public class SynchronousChannelTests {
this.messageText = messageText;
}
public Collection<StringMessage> poll(int limit) {
public StringMessage poll() {
StringMessage message = new StringMessage(messageText);
message.getHeader().setProperty(HANDLER_THREAD, Thread.currentThread().getName());
return Collections.singleton(message);
return message;
}
}