From d592ceac7a4f3ffcc95679b8cce0231c1e7832cb Mon Sep 17 00:00:00 2001 From: Mark Fisher Date: Mon, 26 Apr 2010 00:25:41 +0000 Subject: [PATCH] INT-1094 removed getName() from MessageChannel interface --- .../integration/core/MessageChannel.java | 7 +- .../gateway/AbstractMessagingGateway.java | 4 ++ .../channel/TestChannelResolver.java | 8 +-- .../ServiceActivatorEndpointTests.java | 8 ++- .../gateway/SimpleMessagingGatewayTests.java | 24 ++++--- .../router/MethodInvokingRouterTests.java | 66 +++++++------------ .../router/MultiChannelRouterTests.java | 6 +- .../router/SingleChannelRouterTests.java | 3 +- .../integration/test/util/TestUtils.java | 14 ++-- 9 files changed, 63 insertions(+), 77 deletions(-) diff --git a/org.springframework.integration/src/main/java/org/springframework/integration/core/MessageChannel.java b/org.springframework.integration/src/main/java/org/springframework/integration/core/MessageChannel.java index ece40d5951..72c6a1115d 100644 --- a/org.springframework.integration/src/main/java/org/springframework/integration/core/MessageChannel.java +++ b/org.springframework.integration/src/main/java/org/springframework/integration/core/MessageChannel.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2008 the original author or authors. + * Copyright 2002-2010 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. @@ -23,11 +23,6 @@ package org.springframework.integration.core; */ public interface MessageChannel { - /** - * Return the name of this channel. - */ - String getName(); - /** * Send a {@link Message} to this channel. May throw a RuntimeException for * non-recoverable errors. Otherwise, if the Message cannot be sent for a diff --git a/org.springframework.integration/src/main/java/org/springframework/integration/gateway/AbstractMessagingGateway.java b/org.springframework.integration/src/main/java/org/springframework/integration/gateway/AbstractMessagingGateway.java index b25dd506d4..4babbb4955 100644 --- a/org.springframework.integration/src/main/java/org/springframework/integration/gateway/AbstractMessagingGateway.java +++ b/org.springframework.integration/src/main/java/org/springframework/integration/gateway/AbstractMessagingGateway.java @@ -46,6 +46,8 @@ public abstract class AbstractMessagingGateway extends AbstractEndpoint implemen private volatile MessageChannel replyChannel; + private volatile long replyTimeout = 1000; + private final MessageChannelTemplate channelTemplate = new MessageChannelTemplate(); private volatile boolean shouldThrowErrors = true; @@ -93,6 +95,7 @@ public abstract class AbstractMessagingGateway extends AbstractEndpoint implemen * @param replyTimeout the timeout value in milliseconds */ public void setReplyTimeout(long replyTimeout) { + this.replyTimeout = replyTimeout; this.channelTemplate.setReceiveTimeout(replyTimeout); } @@ -198,6 +201,7 @@ public abstract class AbstractMessagingGateway extends AbstractEndpoint implemen (PollableChannel) this.replyChannel, handler); endpoint.setTrigger(new PeriodicTrigger(10)); endpoint.setBeanFactory(this.getBeanFactory()); + endpoint.setReceiveTimeout(this.replyTimeout); endpoint.afterPropertiesSet(); correlator = endpoint; } diff --git a/org.springframework.integration/src/test/java/org/springframework/integration/channel/TestChannelResolver.java b/org.springframework.integration/src/test/java/org/springframework/integration/channel/TestChannelResolver.java index e54421ab18..1cfdfdcdb9 100644 --- a/org.springframework.integration/src/test/java/org/springframework/integration/channel/TestChannelResolver.java +++ b/org.springframework.integration/src/test/java/org/springframework/integration/channel/TestChannelResolver.java @@ -40,10 +40,10 @@ public class TestChannelResolver implements ChannelResolver { this.channels = channels; } - public void addChannel(MessageChannel channel) { - Assert.notNull(channel, "'channel' must not be null"); - Assert.notNull(channel.getName(), "channel name must not be null"); - this.channels.put(channel.getName(), channel); + public void addChannel(String name, MessageChannel channel) { + Assert.notNull(name, "name must not be null"); + Assert.notNull(channel, "channel must not be null"); + this.channels.put(name, channel); } } diff --git a/org.springframework.integration/src/test/java/org/springframework/integration/endpoint/ServiceActivatorEndpointTests.java b/org.springframework.integration/src/test/java/org/springframework/integration/endpoint/ServiceActivatorEndpointTests.java index e2e49bbe28..ed2216c0fd 100644 --- a/org.springframework.integration/src/test/java/org/springframework/integration/endpoint/ServiceActivatorEndpointTests.java +++ b/org.springframework.integration/src/test/java/org/springframework/integration/endpoint/ServiceActivatorEndpointTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2008 the original author or authors. + * Copyright 2002-2010 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. @@ -81,7 +81,7 @@ public class ServiceActivatorEndpointTests { QueueChannel channel = new QueueChannel(1); channel.setBeanName("testChannel"); TestChannelResolver channelResolver = new TestChannelResolver(); - channelResolver.addChannel(channel); + channelResolver.addChannel("testChannel", channel); ServiceActivatingHandler endpoint = this.createEndpoint(); endpoint.setChannelResolver(channelResolver); Message message = MessageBuilder.withPayload("foo") @@ -105,7 +105,7 @@ public class ServiceActivatorEndpointTests { }; ServiceActivatingHandler endpoint = new ServiceActivatingHandler(handler, "handle"); TestChannelResolver channelResolver = new TestChannelResolver(); - channelResolver.addChannel(replyChannel2); + channelResolver.addChannel("replyChannel2", replyChannel2); endpoint.setChannelResolver(channelResolver); Message testMessage1 = MessageBuilder.withPayload("bar") .setReplyChannel(replyChannel1).build(); @@ -208,6 +208,7 @@ public class ServiceActivatorEndpointTests { private static class TestBean { + @SuppressWarnings("unused") public Message handle(Message message) { return new StringMessage(message.getPayload().toString().toUpperCase()); } @@ -216,6 +217,7 @@ public class ServiceActivatorEndpointTests { private static class TestNullReplyBean { + @SuppressWarnings("unused") public Message handle(Message message) { return null; } diff --git a/org.springframework.integration/src/test/java/org/springframework/integration/gateway/SimpleMessagingGatewayTests.java b/org.springframework.integration/src/test/java/org/springframework/integration/gateway/SimpleMessagingGatewayTests.java index f4a42a7ce9..4ad3ddb478 100644 --- a/org.springframework.integration/src/test/java/org/springframework/integration/gateway/SimpleMessagingGatewayTests.java +++ b/org.springframework.integration/src/test/java/org/springframework/integration/gateway/SimpleMessagingGatewayTests.java @@ -16,7 +16,13 @@ package org.springframework.integration.gateway; -import static org.easymock.EasyMock.*; +import static org.easymock.EasyMock.createMock; +import static org.easymock.EasyMock.expect; +import static org.easymock.EasyMock.getCurrentArguments; +import static org.easymock.EasyMock.isA; +import static org.easymock.EasyMock.replay; +import static org.easymock.EasyMock.reset; +import static org.easymock.EasyMock.verify; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNull; @@ -57,6 +63,8 @@ public class SimpleMessagingGatewayTests { this.simpleMessagingGateway.setRequestChannel(requestChannel); this.simpleMessagingGateway.setReplyChannel(replyChannel); this.simpleMessagingGateway.setBeanFactory(TestUtils.createTestApplicationContext()); + this.simpleMessagingGateway.afterPropertiesSet(); + this.simpleMessagingGateway.start(); reset(allmocks); } @@ -139,7 +147,7 @@ public class SimpleMessagingGatewayTests { @Test public void sendObjectAndReceiveObject() { - expect(replyChannel.getName()).andReturn("replyChannel").anyTimes(); + expect(replyChannel.receive(0)).andReturn(messageMock); expect(requestChannel.send(isA(Message.class))).andReturn(true); replay(allmocks); this.simpleMessagingGateway.setReplyTimeout(0); @@ -153,7 +161,7 @@ public class SimpleMessagingGatewayTests { // setup local mocks MessageHeaders messageHeadersMock = createMock(MessageHeaders.class); //set expectations - expect(replyChannel.getName()).andReturn("replyChannel").anyTimes(); + expect(replyChannel.receive(0)).andReturn(messageMock); expect(messageMock.getHeaders()).andReturn(messageHeadersMock); expect(requestChannel.send(messageMock)).andReturn(true); expect(messageHeadersMock.getId()).andReturn(UUID.randomUUID()); @@ -161,6 +169,7 @@ public class SimpleMessagingGatewayTests { //play scenario replay(allmocks); replay(messageHeadersMock); + this.simpleMessagingGateway.setReplyTimeout(0); this.simpleMessagingGateway.sendAndReceive(messageMock); verify(allmocks); verify(messageHeadersMock); @@ -168,7 +177,6 @@ public class SimpleMessagingGatewayTests { @Test(expected = IllegalArgumentException.class) public void sendNullAndReceiveObject() { - expect(replyChannel.getName()).andReturn("replyChannel").anyTimes(); replay(allmocks); try { this.simpleMessagingGateway.sendAndReceive(null); @@ -180,10 +188,11 @@ public class SimpleMessagingGatewayTests { @Test public void sendObjectAndReceiveMessage() { - expect(replyChannel.getName()).andReturn("replyChannel").anyTimes(); + expect(replyChannel.receive(100)).andReturn(messageMock); expect(requestChannel.send(isA(Message.class))).andReturn(true); replay(allmocks); - this.simpleMessagingGateway.setReplyTimeout(0); + // TODO: commenting the next line causes the test to hang + this.simpleMessagingGateway.setReplyTimeout(100); this.simpleMessagingGateway.sendAndReceiveMessage("test"); verify(allmocks); } @@ -194,7 +203,7 @@ public class SimpleMessagingGatewayTests { // setup local mocks MessageHeaders messageHeadersMock = createMock(MessageHeaders.class); //set expectations - expect(replyChannel.getName()).andReturn("replyChannel").anyTimes(); + expect(replyChannel.receive(0)).andReturn(messageMock); expect(messageMock.getHeaders()).andReturn(messageHeadersMock); expect(messageHeadersMock.getReplyChannel()).andReturn(replyChannel); expect(requestChannel.send(messageMock)).andReturn(true); @@ -207,7 +216,6 @@ public class SimpleMessagingGatewayTests { @Test(expected = IllegalArgumentException.class) public void sendNullAndReceiveMessage() { - expect(replyChannel.getName()).andReturn("replyChannel").anyTimes(); replay(allmocks); try { this.simpleMessagingGateway.sendAndReceiveMessage(null); diff --git a/org.springframework.integration/src/test/java/org/springframework/integration/router/MethodInvokingRouterTests.java b/org.springframework.integration/src/test/java/org/springframework/integration/router/MethodInvokingRouterTests.java index b3cf501e5a..5ddf1a138f 100644 --- a/org.springframework.integration/src/test/java/org/springframework/integration/router/MethodInvokingRouterTests.java +++ b/org.springframework.integration/src/test/java/org/springframework/integration/router/MethodInvokingRouterTests.java @@ -44,9 +44,8 @@ public class MethodInvokingRouterTests { @Test public void channelNameResolutionByPayloadConfiguredByMethodReference() throws Exception { QueueChannel barChannel = new QueueChannel(); - barChannel.setBeanName("bar-channel"); TestChannelResolver channelResolver = new TestChannelResolver(); - channelResolver.addChannel(barChannel); + channelResolver.addChannel("bar-channel", barChannel); SingleChannelNameRoutingTestBean testBean = new SingleChannelNameRoutingTestBean(); Method routingMethod = testBean.getClass().getMethod("routePayload", String.class); MethodInvokingRouter router = new MethodInvokingRouter(testBean, routingMethod); @@ -61,9 +60,8 @@ public class MethodInvokingRouterTests { @Test public void channelNameResolutionByPayloadConfiguredByMethodName() { QueueChannel barChannel = new QueueChannel(); - barChannel.setBeanName("bar-channel"); TestChannelResolver channelResolver = new TestChannelResolver(); - channelResolver.addChannel(barChannel); + channelResolver.addChannel("bar-channel", barChannel); SingleChannelNameRoutingTestBean testBean = new SingleChannelNameRoutingTestBean(); MethodInvokingRouter router = new MethodInvokingRouter(testBean, "routePayload"); router.setChannelResolver(channelResolver); @@ -78,11 +76,9 @@ public class MethodInvokingRouterTests { public void channelNameResolutionByHeader() throws Exception { QueueChannel fooChannel = new QueueChannel(); QueueChannel barChannel = new QueueChannel(); - fooChannel.setBeanName("foo-channel"); - barChannel.setBeanName("bar-channel"); TestChannelResolver channelResolver = new TestChannelResolver(); - channelResolver.addChannel(fooChannel); - channelResolver.addChannel(barChannel); + channelResolver.addChannel("foo-channel", fooChannel); + channelResolver.addChannel("bar-channel", barChannel); SingleChannelNameRoutingTestBean testBean = new SingleChannelNameRoutingTestBean(); Method routingMethod = testBean.getClass().getMethod("routeByHeader", String.class); MethodInvokingRouter router = new MethodInvokingRouter(testBean, routingMethod); @@ -123,11 +119,9 @@ public class MethodInvokingRouterTests { private void doTestChannelNameResolutionByMessage(MethodInvokingRouter router) { QueueChannel fooChannel = new QueueChannel(); QueueChannel barChannel = new QueueChannel(); - fooChannel.setBeanName("foo-channel"); - barChannel.setBeanName("bar-channel"); TestChannelResolver channelResolver = new TestChannelResolver(); - channelResolver.addChannel(fooChannel); - channelResolver.addChannel(barChannel); + channelResolver.addChannel("foo-channel", fooChannel); + channelResolver.addChannel("bar-channel", barChannel); router.setChannelResolver(channelResolver); Message fooMessage = new StringMessage("foo"); Message barMessage = new StringMessage("bar"); @@ -166,10 +160,8 @@ public class MethodInvokingRouterTests { Message badMessage = new StringMessage("bad"); QueueChannel fooChannel = new QueueChannel(); QueueChannel barChannel = new QueueChannel(); - fooChannel.setBeanName("foo-channel"); - barChannel.setBeanName("bar-channel"); - channelResolver.addChannel(fooChannel); - channelResolver.addChannel(barChannel); + channelResolver.addChannel("foo-channel", fooChannel); + channelResolver.addChannel("bar-channel", barChannel); router.setChannelResolver(channelResolver); router.handleMessage(fooMessage); Message result1 = fooChannel.receive(0); @@ -202,10 +194,8 @@ public class MethodInvokingRouterTests { private void doTestChannelInstanceResolutionByMessage(MethodInvokingRouter router, TestChannelResolver channelResolver) { QueueChannel fooChannel = new QueueChannel(); QueueChannel barChannel = new QueueChannel(); - fooChannel.setBeanName("foo-channel"); - barChannel.setBeanName("bar-channel"); - channelResolver.addChannel(fooChannel); - channelResolver.addChannel(barChannel); + channelResolver.addChannel("foo-channel", fooChannel); + channelResolver.addChannel("bar-channel", barChannel); router.setChannelResolver(channelResolver); Message fooMessage = new StringMessage("foo"); Message barMessage = new StringMessage("bar"); @@ -241,10 +231,8 @@ public class MethodInvokingRouterTests { private void doTestMultiChannelNameResolutionByPayload(MethodInvokingRouter router, TestChannelResolver channelResolver) { QueueChannel fooChannel = new QueueChannel(); QueueChannel barChannel = new QueueChannel(); - fooChannel.setBeanName("foo-channel"); - barChannel.setBeanName("bar-channel"); - channelResolver.addChannel(fooChannel); - channelResolver.addChannel(barChannel); + channelResolver.addChannel("foo-channel", fooChannel); + channelResolver.addChannel("bar-channel", barChannel); router.setChannelResolver(channelResolver); Message fooMessage = new StringMessage("foo"); Message barMessage = new StringMessage("bar"); @@ -286,10 +274,8 @@ public class MethodInvokingRouterTests { private void doTestMultiChannelNameResolutionByMessage(MethodInvokingRouter router, TestChannelResolver channelResolver) { QueueChannel fooChannel = new QueueChannel(); QueueChannel barChannel = new QueueChannel(); - fooChannel.setBeanName("foo-channel"); - barChannel.setBeanName("bar-channel"); - channelResolver.addChannel(fooChannel); - channelResolver.addChannel(barChannel); + channelResolver.addChannel("foo-channel", fooChannel); + channelResolver.addChannel("bar-channel", barChannel); router.setChannelResolver(channelResolver); Message fooMessage = new StringMessage("foo"); Message barMessage = new StringMessage("bar"); @@ -331,10 +317,8 @@ public class MethodInvokingRouterTests { private void doTestMultiChannelNameArrayResolutionByMessage(MethodInvokingRouter router, TestChannelResolver channelResolver) { QueueChannel fooChannel = new QueueChannel(); QueueChannel barChannel = new QueueChannel(); - fooChannel.setBeanName("foo-channel"); - barChannel.setBeanName("bar-channel"); - channelResolver.addChannel(fooChannel); - channelResolver.addChannel(barChannel); + channelResolver.addChannel("foo-channel", fooChannel); + channelResolver.addChannel("bar-channel", barChannel); router.setChannelResolver(channelResolver); Message fooMessage = new StringMessage("foo"); Message barMessage = new StringMessage("bar"); @@ -376,10 +360,8 @@ public class MethodInvokingRouterTests { private void doTestMultiChannelListResolutionByPayload(MethodInvokingRouter router, TestChannelResolver channelResolver) { QueueChannel fooChannel = new QueueChannel(); QueueChannel barChannel = new QueueChannel(); - fooChannel.setBeanName("foo-channel"); - barChannel.setBeanName("bar-channel"); - channelResolver.addChannel(fooChannel); - channelResolver.addChannel(barChannel); + channelResolver.addChannel("foo-channel", fooChannel); + channelResolver.addChannel("bar-channel", barChannel); router.setChannelResolver(channelResolver); Message fooMessage = new StringMessage("foo"); Message barMessage = new StringMessage("bar"); @@ -421,10 +403,8 @@ public class MethodInvokingRouterTests { private void doTestMultiChannelListResolutionByMessage(MethodInvokingRouter router, TestChannelResolver channelResolver) { QueueChannel fooChannel = new QueueChannel(); QueueChannel barChannel = new QueueChannel(); - fooChannel.setBeanName("foo-channel"); - barChannel.setBeanName("bar-channel"); - channelResolver.addChannel(fooChannel); - channelResolver.addChannel(barChannel); + channelResolver.addChannel("foo-channel", fooChannel); + channelResolver.addChannel("bar-channel", barChannel); router.setChannelResolver(channelResolver); Message fooMessage = new StringMessage("foo"); Message barMessage = new StringMessage("bar"); @@ -466,10 +446,8 @@ public class MethodInvokingRouterTests { private void doTestMultiChannelArrayResolutionByMessage(MethodInvokingRouter router, TestChannelResolver channelResolver) { QueueChannel fooChannel = new QueueChannel(); QueueChannel barChannel = new QueueChannel(); - fooChannel.setBeanName("foo-channel"); - barChannel.setBeanName("bar-channel"); - channelResolver.addChannel(fooChannel); - channelResolver.addChannel(barChannel); + channelResolver.addChannel("foo-channel", fooChannel); + channelResolver.addChannel("bar-channel", barChannel); router.setChannelResolver(channelResolver); Message fooMessage = new StringMessage("foo"); Message barMessage = new StringMessage("bar"); diff --git a/org.springframework.integration/src/test/java/org/springframework/integration/router/MultiChannelRouterTests.java b/org.springframework.integration/src/test/java/org/springframework/integration/router/MultiChannelRouterTests.java index 82aca926e8..d66bcee2f9 100644 --- a/org.springframework.integration/src/test/java/org/springframework/integration/router/MultiChannelRouterTests.java +++ b/org.springframework.integration/src/test/java/org/springframework/integration/router/MultiChannelRouterTests.java @@ -44,11 +44,9 @@ public class MultiChannelRouterTests { }; QueueChannel channel1 = new QueueChannel(); QueueChannel channel2 = new QueueChannel(); - channel1.setBeanName("channel1"); - channel2.setBeanName("channel2"); TestChannelResolver channelResolver = new TestChannelResolver(); - channelResolver.addChannel(channel1); - channelResolver.addChannel(channel2); + channelResolver.addChannel("channel1", channel1); + channelResolver.addChannel("channel2", channel2); router.setChannelResolver(channelResolver); Message message = new StringMessage("test"); router.handleMessage(message); diff --git a/org.springframework.integration/src/test/java/org/springframework/integration/router/SingleChannelRouterTests.java b/org.springframework.integration/src/test/java/org/springframework/integration/router/SingleChannelRouterTests.java index 7505382f85..ab0b327be6 100644 --- a/org.springframework.integration/src/test/java/org/springframework/integration/router/SingleChannelRouterTests.java +++ b/org.springframework.integration/src/test/java/org/springframework/integration/router/SingleChannelRouterTests.java @@ -56,9 +56,8 @@ public class SingleChannelRouterTests { } }; QueueChannel channel = new QueueChannel(); - channel.setBeanName("testChannel"); TestChannelResolver channelResolver = new TestChannelResolver(); - channelResolver.addChannel(channel); + channelResolver.addChannel("testChannel", channel); router.setChannelResolver(channelResolver); Message message = new StringMessage("test"); router.handleMessage(message); diff --git a/org.springframework.integration/src/test/java/org/springframework/integration/test/util/TestUtils.java b/org.springframework.integration/src/test/java/org/springframework/integration/test/util/TestUtils.java index b80ae5ee88..9c2d4a594e 100644 --- a/org.springframework.integration/src/test/java/org/springframework/integration/test/util/TestUtils.java +++ b/org.springframework.integration/src/test/java/org/springframework/integration/test/util/TestUtils.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2009 the original author or authors. + * Copyright 2002-2010 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. @@ -31,6 +31,7 @@ import org.springframework.beans.factory.BeanNameAware; import org.springframework.beans.factory.InitializingBean; import org.springframework.beans.factory.config.ConfigurableListableBeanFactory; import org.springframework.context.support.GenericApplicationContext; +import org.springframework.integration.channel.AbstractMessageChannel; import org.springframework.integration.channel.BeanFactoryChannelResolver; import org.springframework.integration.channel.MessagePublishingErrorHandler; import org.springframework.integration.context.IntegrationContextUtils; @@ -128,12 +129,12 @@ public abstract class TestUtils { } public void registerChannel(String channelName, MessageChannel channel) { - if (channel.getName() != null) { + if (channel instanceof AbstractMessageChannel && ((AbstractMessageChannel) channel).getName() != null) { if (channelName == null) { - Assert.notNull(channel.getName(), "channel name must not be null"); - channelName = channel.getName(); - } else { - Assert.isTrue(channel.getName().equals(channelName), + channelName = ((AbstractMessageChannel) channel).getName(); + } + else { + Assert.isTrue(((AbstractMessageChannel) channel).getName().equals(channelName), "channel name has already been set with a conflicting value"); } } @@ -151,6 +152,7 @@ public abstract class TestUtils { } } + @SuppressWarnings("unchecked") public static MessageHandler handlerExpecting(final Matcher messageMatcher) { return new MessageHandler() { public void handleMessage(Message message) throws MessageRejectedException, MessageHandlingException, MessageDeliveryException {