Added SynchronousChannel (INT-170).

This commit is contained in:
Mark Fisher
2008-04-04 16:58:57 +00:00
parent 11d934c4a8
commit 5821ac23a2
3 changed files with 272 additions and 2 deletions

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.
@@ -78,7 +78,7 @@ public class DefaultMessageDistributor implements MessageDistributor {
Iterator<MessageHandler> iter = targets.iterator();
if (!iter.hasNext()) {
if (logger.isWarnEnabled()) {
logger.warn("dispatcher has no active handlers");
logger.warn("no active handlers");
}
return false;
}

View File

@@ -0,0 +1,91 @@
/*
* 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.integration.dispatcher;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import org.springframework.integration.adapter.PollableSource;
import org.springframework.integration.channel.AbstractMessageChannel;
import org.springframework.integration.channel.DispatcherPolicy;
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 any subscribed {@link MessageHandler handler(s)} in a
* sender's thread. If a {@link PollableSource} is provided, then that source
* will likewise be polled within a receiver's thread. If no source is provided,
* then receive() will always return null.
*
* @author Mark Fisher
*/
public class SynchronousChannel extends AbstractMessageChannel {
private final MessageDistributor distributor;
private volatile PollableSource<?> source;
public SynchronousChannel() {
this(null);
}
public SynchronousChannel(DispatcherPolicy dispatcherPolicy) {
super(dispatcherPolicy != null ? dispatcherPolicy : new DispatcherPolicy());
this.distributor = new DefaultMessageDistributor(this.getDispatcherPolicy());
}
public void setSource(PollableSource<?> source) {
this.source = source;
}
public void addHandler(MessageHandler handler) {
this.distributor.addHandler(handler);
}
@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();
return (result instanceof Message<?>) ? (Message<?>) result :
new SimplePayloadMessageMapper<Object>().toMessage(result);
}
}
return null;
}
@Override
protected boolean doSend(Message<?> message, long timeout) {
return this.distributor.distribute(message);
}
public List<Message<?>> clear() {
return new ArrayList<Message<?>>();
}
public List<Message<?>> purge(MessageSelector selector) {
return new ArrayList<Message<?>>();
}
}

View File

@@ -0,0 +1,179 @@
/*
* 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.integration.dispatcher;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
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.concurrent.CountDownLatch;
import java.util.concurrent.SynchronousQueue;
import java.util.concurrent.TimeUnit;
import org.junit.Test;
import org.springframework.integration.adapter.PollableSource;
import org.springframework.integration.handler.MessageHandler;
import org.springframework.integration.message.Message;
import org.springframework.integration.message.StringMessage;
/**
* @author Mark Fisher
*/
public class SynchronousChannelTests {
private static final String HANDLER_THREAD = "handler-thread";
@Test
public void testSend() {
SynchronousChannel channel = new SynchronousChannel();
channel.addHandler(new ThreadNameSettingTestHandler());
StringMessage message = new StringMessage("test");
assertTrue(channel.send(message));
String handlerThreadName = message.getHeader().getProperty(HANDLER_THREAD);
assertEquals(Thread.currentThread().getName(), handlerThreadName);
}
@Test
public void testSendWithNoHandler() {
SynchronousChannel channel = new SynchronousChannel();
StringMessage message = new StringMessage("test");
assertFalse(channel.send(message));
}
@Test
public void testSendInSeparateThread() throws InterruptedException {
CountDownLatch latch = new CountDownLatch(1);
final SynchronousChannel channel = new SynchronousChannel();
channel.addHandler(new ThreadNameSettingTestHandler(latch));
final StringMessage message = new StringMessage("test");
new Thread(new Runnable() {
public void run() {
channel.send(message);
}
}, "test-thread").start();
latch.await(1000, TimeUnit.MILLISECONDS);
String handlerThreadName = message.getHeader().getProperty(HANDLER_THREAD);
assertEquals("test-thread", handlerThreadName);
}
@Test
public void testReceive() {
SynchronousChannel channel = new SynchronousChannel();
channel.setSource(new PollableSource<String>() {
public Collection<String> poll(int limit) {
return Collections.singleton("foo");
}
});
Message<?> message = channel.receive();
assertNotNull(message);
assertNotNull(message.getPayload());
assertEquals(String.class, message.getPayload().getClass());
assertEquals("foo", message.getPayload());
}
@Test
public void testReceiveWithMessageResult() {
SynchronousChannel channel = new SynchronousChannel();
channel.setSource(new MessageReturningTestSource("foo"));
Message<?> message = channel.receive();
assertNotNull(message);
assertNotNull(message.getPayload());
assertEquals(String.class, message.getPayload().getClass());
assertEquals("foo", message.getPayload());
String handlerThreadName = message.getHeader().getProperty(HANDLER_THREAD);
assertEquals(Thread.currentThread().getName(), handlerThreadName);
}
@Test
public void testReceiveInSeparateThread() throws InterruptedException {
final SynchronousChannel channel = new SynchronousChannel();
channel.setSource(new MessageReturningTestSource("foo"));
final SynchronousQueue<Message<?>> messageHolder = new SynchronousQueue<Message<?>>();
new Thread(new Runnable() {
public void run() {
Message<?> message = channel.receive();
assertNotNull(message);
try {
messageHolder.put(message);
}
catch (InterruptedException e) {
// will fail after timeout below
}
}
}, "test-thread").start();
Message<?> message = messageHolder.poll(1000, TimeUnit.MILLISECONDS);
assertNotNull(message);
assertNotNull(message.getPayload());
assertEquals(String.class, message.getPayload().getClass());
assertEquals("foo", message.getPayload());
String handlerThreadName = message.getHeader().getProperty(HANDLER_THREAD);
assertEquals("test-thread", handlerThreadName);
}
@Test
public void testReceiveWithNoSource() {
SynchronousChannel channel = new SynchronousChannel();
assertNull(channel.receive());
}
private static class ThreadNameSettingTestHandler implements MessageHandler {
private final CountDownLatch latch;
ThreadNameSettingTestHandler() {
this(null);
}
ThreadNameSettingTestHandler(CountDownLatch latch) {
this.latch = latch;
}
public Message<?> handle(Message<?> message) {
message.getHeader().setProperty(HANDLER_THREAD, Thread.currentThread().getName());
if (this.latch != null) {
this.latch.countDown();
}
return null;
}
}
private static class MessageReturningTestSource implements PollableSource<StringMessage> {
private final String messageText;
MessageReturningTestSource(String messageText) {
this.messageText = messageText;
}
public Collection<StringMessage> poll(int limit) {
StringMessage message = new StringMessage(messageText);
message.getHeader().setProperty(HANDLER_THREAD, Thread.currentThread().getName());
return Collections.singleton(message);
}
}
}