MessageBus now manages the subscription of a MessageEndpoint to a SynchronousChannel if that endpoint's Subscription object contains a NULL Schedule.

This commit is contained in:
Mark Fisher
2008-04-04 20:56:12 +00:00
parent 5821ac23a2
commit 8ea5f012f3
7 changed files with 203 additions and 37 deletions

View File

@@ -0,0 +1,71 @@
/*
* 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.bus;
import static org.junit.Assert.assertEquals;
import org.junit.Before;
import org.junit.Test;
import org.springframework.integration.channel.MessageChannel;
import org.springframework.integration.dispatcher.SynchronousChannel;
import org.springframework.integration.endpoint.DefaultMessageEndpoint;
import org.springframework.integration.handler.MessageHandler;
import org.springframework.integration.message.Message;
import org.springframework.integration.message.StringMessage;
import org.springframework.integration.scheduling.Subscription;
/**
* @author Mark Fisher
*/
public class SynchronousChannelSubscriptionTests {
private MessageBus bus = new MessageBus();
private MessageChannel sourceChannel = new SynchronousChannel();
private MessageChannel targetChannel = new SynchronousChannel();
@Before
public void setupChannels() {
bus.registerChannel("sourceChannel", sourceChannel);
bus.registerChannel("targetChannel", targetChannel);
}
@Test
public void testSendAndReceive() throws InterruptedException {
DefaultMessageEndpoint endpoint = new DefaultMessageEndpoint(new TestHandler());
endpoint.setSubscription(new Subscription("sourceChannel"));
endpoint.setDefaultOutputChannelName("targetChannel");
bus.registerEndpoint("testEndpoint", endpoint);
bus.start();
this.sourceChannel.send(new StringMessage("foo"));
Message<?> response = this.targetChannel.receive();
assertEquals("foo!", response.getPayload());
}
private static class TestHandler implements MessageHandler {
public Message<?> handle(Message<?> message) {
return new StringMessage(message.getPayload() + "!");
}
}
}

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.
@@ -25,7 +25,6 @@ import org.springframework.beans.DirectFieldAccessor;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.integration.bus.MessageBus;
import org.springframework.integration.endpoint.ConcurrentHandler;
import org.springframework.integration.endpoint.DefaultMessageEndpoint;
import org.springframework.integration.handler.MessageHandlerChain;
import org.springframework.integration.router.AggregatingMessageHandler;
@@ -85,10 +84,7 @@ public class AggregatorAnnotationTests {
final String endpointName) {
MessageBus messageBus = getMessageBus(context);
DefaultMessageEndpoint endpoint = (DefaultMessageEndpoint) messageBus.lookupEndpoint(endpointName + "-endpoint");
ConcurrentHandler handler = (ConcurrentHandler) endpoint.getHandler();
DirectFieldAccessor concurrentHandlerAccessor = new DirectFieldAccessor(handler);
MessageHandlerChain messageHandlerChain = (MessageHandlerChain) concurrentHandlerAccessor
.getPropertyValue("handler");
MessageHandlerChain messageHandlerChain = (MessageHandlerChain) endpoint.getHandler();
AggregatingMessageHandler aggregatingMessageHandler = (AggregatingMessageHandler) ((List) new DirectFieldAccessor(
messageHandlerChain).getPropertyValue("handlers")).get(0);
DirectFieldAccessor aggregatingMessageHandlerAccessor = new DirectFieldAccessor(aggregatingMessageHandler);

View File

@@ -17,13 +17,13 @@
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.List;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.SynchronousQueue;
import java.util.concurrent.TimeUnit;
@@ -54,10 +54,30 @@ public class SynchronousChannelTests {
}
@Test
public void testSendWithNoHandler() {
public void testSendAndReceiveWithNoHandler() {
SynchronousChannel channel = new SynchronousChannel();
StringMessage message = new StringMessage("test");
assertFalse(channel.send(message));
assertNull(channel.receive());
assertTrue(channel.send(message));
Message<?> response = channel.receive();
assertNotNull(response);
assertEquals(response, message);
assertNull(channel.receive());
}
@Test
public void testSendAndClearWithNoHandler() {
SynchronousChannel channel = new SynchronousChannel();
StringMessage message1 = new StringMessage("test1");
StringMessage message2 = new StringMessage("test2");
assertNull(channel.receive());
assertTrue(channel.send(message1));
assertTrue(channel.send(message2));
List<Message<?>> clearedMessages = channel.clear();
assertEquals(2, clearedMessages.size());
assertEquals(message1, clearedMessages.get(0));
assertEquals(message2, clearedMessages.get(1));
assertNull(channel.receive());
}
@Test
@@ -78,8 +98,7 @@ public class SynchronousChannelTests {
@Test
public void testReceive() {
SynchronousChannel channel = new SynchronousChannel();
channel.setSource(new PollableSource<String>() {
SynchronousChannel channel = new SynchronousChannel(new PollableSource<String>() {
public Collection<String> poll(int limit) {
return Collections.singleton("foo");
}
@@ -93,8 +112,7 @@ public class SynchronousChannelTests {
@Test
public void testReceiveWithMessageResult() {
SynchronousChannel channel = new SynchronousChannel();
channel.setSource(new MessageReturningTestSource("foo"));
SynchronousChannel channel = new SynchronousChannel(new MessageReturningTestSource("foo"));
Message<?> message = channel.receive();
assertNotNull(message);
assertNotNull(message.getPayload());
@@ -106,8 +124,7 @@ public class SynchronousChannelTests {
@Test
public void testReceiveInSeparateThread() throws InterruptedException {
final SynchronousChannel channel = new SynchronousChannel();
channel.setSource(new MessageReturningTestSource("foo"));
final SynchronousChannel channel = new SynchronousChannel(new MessageReturningTestSource("foo"));
final SynchronousQueue<Message<?>> messageHolder = new SynchronousQueue<Message<?>>();
new Thread(new Runnable() {
public void run() {