PollableSource now returns a Message rather than an Object from poll(). Now PollableSource implementations may use a MessageMapper, but the "source adapter" no longer does.

This commit is contained in:
Mark Fisher
2008-04-10 21:40:41 +00:00
parent 63680cd570
commit 069e7672e3
24 changed files with 242 additions and 175 deletions

View File

@@ -23,8 +23,6 @@ import org.springframework.beans.factory.InitializingBean;
import org.springframework.integration.ConfigurationException;
import org.springframework.integration.channel.MessageChannel;
import org.springframework.integration.message.Message;
import org.springframework.integration.message.MessageMapper;
import org.springframework.integration.message.SimplePayloadMessageMapper;
import org.springframework.util.Assert;
/**
@@ -38,8 +36,6 @@ public abstract class AbstractSourceAdapter<T> implements SourceAdapter, Initial
private MessageChannel channel;
private MessageMapper<?,T> mapper = new SimplePayloadMessageMapper<T>();
private long sendTimeout = -1;
private volatile boolean initialized = false;
@@ -58,15 +54,6 @@ public abstract class AbstractSourceAdapter<T> implements SourceAdapter, Initial
this.sendTimeout = sendTimeout;
}
public void setMessageMapper(MessageMapper<?,T> mapper) {
Assert.notNull(mapper, "'mapper' must not be null");
this.mapper = mapper;
}
protected MessageMapper<?,T> getMessageMapper() {
return this.mapper;
}
public final void afterPropertiesSet() {
if (this.channel == null) {
throw new ConfigurationException("'channel' is required");
@@ -85,29 +72,16 @@ public abstract class AbstractSourceAdapter<T> implements SourceAdapter, Initial
protected void initialize() {
}
protected boolean sendToChannel(T object) {
protected boolean sendToChannel(Message<T> message) {
if (!this.initialized) {
this.afterPropertiesSet();
}
if (object == null) {
if (message == null) {
if (logger.isDebugEnabled()) {
logger.debug("adapter attempted to send a null object");
}
return false;
}
Message<?> message = null;
if (object instanceof Message<?>) {
message = (Message<?>) object;
}
else {
message = this.mapper.toMessage(object);
}
if (message == null) {
if (logger.isWarnEnabled()) {
logger.warn("unable to create Message from source object: " + object);
}
return false;
}
if (this.sendTimeout < 0) {
return this.channel.send(message);
}

View File

@@ -21,6 +21,8 @@ import java.lang.reflect.Method;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.integration.ConfigurationException;
import org.springframework.integration.handler.HandlerMethodInvoker;
import org.springframework.integration.message.GenericMessage;
import org.springframework.integration.message.Message;
import org.springframework.integration.util.MethodValidator;
import org.springframework.util.Assert;
@@ -54,11 +56,11 @@ public class MethodInvokingSource<T> implements PollableSource<Object>, Initiali
this.invoker.setMethodValidator(new MessageReceivingMethodValidator());
}
public Object poll() {
public Message<Object> poll() {
if (this.invoker == null) {
this.afterPropertiesSet();
}
return this.invoker.invokeMethod(new Object[] {});
return new GenericMessage<Object>(this.invoker.invokeMethod(new Object[] {}));
}

View File

@@ -16,13 +16,15 @@
package org.springframework.integration.adapter;
import org.springframework.integration.message.Message;
/**
* Interface for any external data source that can be polled.
* Interface for any external message source that can be polled.
*
* @author Mark Fisher
*/
public interface PollableSource<T> {
T poll();
Message<T> poll();
}

View File

@@ -21,6 +21,7 @@ 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.Message;
import org.springframework.integration.message.MessageMapper;
import org.springframework.integration.scheduling.MessagingTask;
import org.springframework.integration.scheduling.MessagingTaskScheduler;
@@ -141,7 +142,7 @@ public class PollingSourceAdapter<T> extends AbstractSourceAdapter<T> implements
int messagesProcessed = 0;
int limit = this.maxMessagesPerTask;
while (messagesProcessed < limit) {
T result = this.source.poll();
Message<T> result = this.source.poll();
if (result != null && this.sendToChannel(result)) {
messagesProcessed++;
this.onSend(result);
@@ -154,11 +155,11 @@ public class PollingSourceAdapter<T> extends AbstractSourceAdapter<T> implements
}
/**
* Callback method invoked after an item is sent to the channel.
* Callback method invoked after a message is sent to the channel.
* <p>
* Subclasses may override. The default implementation does nothing.
*/
protected void onSend(T sentItem) {
protected void onSend(Message<T> sentMessage) {
}

View File

@@ -21,6 +21,7 @@ import static org.junit.Assert.assertNotNull;
import org.junit.Test;
import org.springframework.integration.message.Message;
import org.springframework.integration.message.MessagingException;
/**
@@ -33,9 +34,10 @@ public class MethodInvokingSourceTests {
MethodInvokingSource<TestBean> source = new MethodInvokingSource<TestBean>();
source.setObject(new TestBean());
source.setMethod("validMethod");
Object result = source.poll();
Message<?> result = source.poll();
assertNotNull(result);
assertEquals("valid", result);
assertNotNull(result.getPayload());
assertEquals("valid", result.getPayload());
}
@Test(expected=MessagingException.class)

View File

@@ -25,6 +25,7 @@ import java.util.concurrent.atomic.AtomicInteger;
import org.junit.Test;
import org.springframework.integration.channel.SimpleChannel;
import org.springframework.integration.message.GenericMessage;
import org.springframework.integration.message.Message;
/**
@@ -40,7 +41,7 @@ public class PollingSourceAdapterTests {
adapter.setChannel(channel);
adapter.setPeriod(100);
adapter.start();
Message<?> message = channel.receive();
Message<?> message = channel.receive(1000);
assertNotNull("message should not be null", message);
assertEquals("testing.1", message.getPayload());
}
@@ -57,7 +58,7 @@ public class PollingSourceAdapterTests {
adapter.processMessages();
adapter.processMessages();
adapter.stop();
Message<?> message1 = channel.receive();
Message<?> message1 = channel.receive(1000);
assertNotNull("message should not be null", message1);
assertEquals("testing.1", message1.getPayload());
Message<?> message2 = channel.receive(0);
@@ -111,11 +112,11 @@ public class PollingSourceAdapterTests {
this.count.set(0);
}
public String poll() {
public Message<String> poll() {
if (count.get() >= limit) {
return null;
}
return message + "." + count.incrementAndGet();
return new GenericMessage<String>(message + "." + count.incrementAndGet());
}
}

View File

@@ -21,7 +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.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
@@ -269,7 +268,7 @@ public class MessageBusTests {
this.latch = latch;
}
public Collection<Object> poll() {
public Message<Object> poll() {
latch.countDown();
throw new RuntimeException("intentional test failure");
}

View File

@@ -97,8 +97,8 @@ public class SynchronousChannelTests {
@Test
public void testReceive() {
SynchronousChannel channel = new SynchronousChannel(new PollableSource<String>() {
public String poll() {
return "foo";
public Message<String> poll() {
return new StringMessage("foo");
}
});
Message<?> message = channel.receive();
@@ -175,7 +175,7 @@ public class SynchronousChannelTests {
}
private static class MessageReturningTestSource implements PollableSource<StringMessage> {
private static class MessageReturningTestSource implements PollableSource<String> {
private final String messageText;