MessageEndpointAnnotationPostProcessor now handles proxies and also detects inherited @MessageEndpoint annotations (INT-160).

This commit is contained in:
Mark Fisher
2008-04-03 20:31:24 +00:00
parent 70e365f4dd
commit 07edd8f423
6 changed files with 171 additions and 16 deletions

View File

@@ -0,0 +1,26 @@
/*
* 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.endpoint.annotation;
/**
* @author Mark Fisher
*/
public interface ITestEndpoint {
String sayHello(String name);
}

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,10 +25,12 @@ import java.util.concurrent.TimeUnit;
import org.junit.Test;
import org.springframework.aop.framework.ProxyFactory;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.integration.annotation.Concurrency;
import org.springframework.integration.annotation.DefaultOutput;
import org.springframework.integration.annotation.Handler;
import org.springframework.integration.annotation.MessageEndpoint;
import org.springframework.integration.annotation.Polled;
import org.springframework.integration.bus.MessageBus;
@@ -149,6 +151,110 @@ public class MessageEndpointAnnotationPostProcessorTests {
assertEquals(messageBus, channelRegistry);
}
@Test
public void testProxiedMessageEndpointAnnotation() {
MessageBus messageBus = new MessageBus();
messageBus.setAutoCreateChannels(true);
MessageEndpointAnnotationPostProcessor postProcessor =
new MessageEndpointAnnotationPostProcessor(messageBus);
postProcessor.afterPropertiesSet();
ProxyFactory proxyFactory = new ProxyFactory(new SimpleAnnotatedEndpoint());
Object proxy = proxyFactory.getProxy();
postProcessor.postProcessAfterInitialization(proxy, "proxy");
messageBus.start();
MessageChannel inputChannel = messageBus.lookupChannel("inputChannel");
MessageChannel outputChannel = messageBus.lookupChannel("outputChannel");
inputChannel.send(new StringMessage("world"));
Message<?> message = outputChannel.receive(1000);
assertEquals("hello world", message.getPayload());
}
@Test
public void testMessageEndpointAnnotationInherited() {
MessageBus messageBus = new MessageBus();
messageBus.setAutoCreateChannels(true);
MessageEndpointAnnotationPostProcessor postProcessor =
new MessageEndpointAnnotationPostProcessor(messageBus);
postProcessor.afterPropertiesSet();
postProcessor.postProcessAfterInitialization(new SimpleAnnotatedEndpointSubclass(), "subclass");
messageBus.start();
MessageChannel inputChannel = messageBus.lookupChannel("inputChannel");
MessageChannel outputChannel = messageBus.lookupChannel("outputChannel");
inputChannel.send(new StringMessage("world"));
Message<?> message = outputChannel.receive(1000);
assertEquals("hello world", message.getPayload());
}
@Test
public void testMessageEndpointAnnotationInheritedWithProxy() {
MessageBus messageBus = new MessageBus();
messageBus.setAutoCreateChannels(true);
MessageEndpointAnnotationPostProcessor postProcessor =
new MessageEndpointAnnotationPostProcessor(messageBus);
postProcessor.afterPropertiesSet();
ProxyFactory proxyFactory = new ProxyFactory(new SimpleAnnotatedEndpointSubclass());
Object proxy = proxyFactory.getProxy();
postProcessor.postProcessAfterInitialization(proxy, "proxy");
messageBus.start();
MessageChannel inputChannel = messageBus.lookupChannel("inputChannel");
MessageChannel outputChannel = messageBus.lookupChannel("outputChannel");
inputChannel.send(new StringMessage("world"));
Message<?> message = outputChannel.receive(1000);
assertEquals("hello world", message.getPayload());
}
@Test
public void testMessageEndpointAnnotationInheritedFromInterface() {
MessageBus messageBus = new MessageBus();
MessageChannel inputChannel = new SimpleChannel();
MessageChannel outputChannel = new SimpleChannel();
messageBus.registerChannel("inputChannel", inputChannel);
messageBus.registerChannel("outputChannel", outputChannel);
MessageEndpointAnnotationPostProcessor postProcessor =
new MessageEndpointAnnotationPostProcessor(messageBus);
postProcessor.afterPropertiesSet();
postProcessor.postProcessAfterInitialization(new SimpleAnnotatedEndpointImplementation(), "impl");
messageBus.start();
inputChannel.send(new StringMessage("ABC"));
Message<?> message = outputChannel.receive(1000);
assertEquals("test-ABC", message.getPayload());
}
@Test
public void testMessageEndpointAnnotationInheritedFromInterfaceWithAutoCreatedChannels() {
MessageBus messageBus = new MessageBus();
messageBus.setAutoCreateChannels(true);
MessageEndpointAnnotationPostProcessor postProcessor =
new MessageEndpointAnnotationPostProcessor(messageBus);
postProcessor.afterPropertiesSet();
postProcessor.postProcessAfterInitialization(new SimpleAnnotatedEndpointImplementation(), "impl");
messageBus.start();
MessageChannel inputChannel = messageBus.lookupChannel("inputChannel");
MessageChannel outputChannel = messageBus.lookupChannel("outputChannel");
inputChannel.send(new StringMessage("ABC"));
Message<?> message = outputChannel.receive(1000);
assertEquals("test-ABC", message.getPayload());
}
@Test
public void testMessageEndpointAnnotationInheritedFromInterfaceWithProxy() {
MessageBus messageBus = new MessageBus();
MessageChannel inputChannel = new SimpleChannel();
MessageChannel outputChannel = new SimpleChannel();
messageBus.registerChannel("inputChannel", inputChannel);
messageBus.registerChannel("outputChannel", outputChannel);
MessageEndpointAnnotationPostProcessor postProcessor =
new MessageEndpointAnnotationPostProcessor(messageBus);
postProcessor.afterPropertiesSet();
ProxyFactory proxyFactory = new ProxyFactory(new SimpleAnnotatedEndpointImplementation());
Object proxy = proxyFactory.getProxy();
postProcessor.postProcessAfterInitialization(proxy, "proxy");
messageBus.start();
inputChannel.send(new StringMessage("ABC"));
Message<?> message = outputChannel.receive(1000);
assertEquals("test-ABC", message.getPayload());
}
@MessageEndpoint(defaultOutput="testChannel")
private static class PolledAnnotationTestBean {
@@ -204,4 +310,23 @@ public class MessageEndpointAnnotationPostProcessorTests {
}
}
private static class SimpleAnnotatedEndpointSubclass extends SimpleAnnotatedEndpoint {
}
@MessageEndpoint(input="inputChannel", defaultOutput="outputChannel", pollPeriod=25)
private static interface SimpleAnnotatedEndpointInterface {
String test(String input);
}
private static class SimpleAnnotatedEndpointImplementation implements SimpleAnnotatedEndpointInterface {
@Handler
public String test(String input) {
return "test-" + input;
}
}
}

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.
@@ -23,7 +23,7 @@ import org.springframework.integration.annotation.MessageEndpoint;
* @author Mark Fisher
*/
@MessageEndpoint(input="inputChannel", defaultOutput="outputChannel", pollPeriod=10)
public class SimpleAnnotatedEndpoint {
public class SimpleAnnotatedEndpoint implements ITestEndpoint {
@Handler
public String sayHello(String name) {