Replaced the @Handler annotation with @ServiceActivator.

This commit is contained in:
Mark Fisher
2008-09-03 19:32:31 +00:00
parent e4c3ead89f
commit 1e5ace02dd
22 changed files with 75 additions and 100 deletions

View File

@@ -21,8 +21,8 @@ import static org.junit.Assert.assertEquals;
import org.junit.Before;
import org.junit.Test;
import org.springframework.integration.annotation.Handler;
import org.springframework.integration.annotation.MessageEndpoint;
import org.springframework.integration.annotation.ServiceActivator;
import org.springframework.integration.channel.ChannelRegistry;
import org.springframework.integration.channel.DirectChannel;
import org.springframework.integration.channel.QueueChannel;
@@ -125,7 +125,7 @@ public class DirectChannelSubscriptionTests {
@MessageEndpoint
public static class TestEndpoint {
@Handler(inputChannel="sourceChannel", outputChannel="targetChannel")
@ServiceActivator(inputChannel="sourceChannel", outputChannel="targetChannel")
public Message<?> handle(Message<?> message) {
return new StringMessage(message.getPayload() + "-from-annotated-endpoint");
}
@@ -135,7 +135,7 @@ public class DirectChannelSubscriptionTests {
@MessageEndpoint
public static class FailingTestEndpoint {
@Handler(inputChannel="sourceChannel", outputChannel="targetChannel")
@ServiceActivator(inputChannel="sourceChannel", outputChannel="targetChannel")
public Message<?> handle(Message<?> message) {
throw new RuntimeException("intentional test failure");
}

View File

@@ -27,8 +27,8 @@ import org.junit.Test;
import org.springframework.beans.factory.config.RuntimeBeanReference;
import org.springframework.beans.factory.support.RootBeanDefinition;
import org.springframework.context.support.GenericApplicationContext;
import org.springframework.integration.annotation.Handler;
import org.springframework.integration.annotation.MessageEndpoint;
import org.springframework.integration.annotation.ServiceActivator;
import org.springframework.integration.bus.DefaultMessageBus;
import org.springframework.integration.channel.MessageChannel;
import org.springframework.integration.channel.QueueChannel;
@@ -38,16 +38,16 @@ import org.springframework.integration.message.StringMessage;
/**
* @author Mark Fisher
*/
public class SubscriberAnnotationPostProcessorTests {
public class ServiceActivatorAnnotationPostProcessorTests {
@Test
public void testAnnotatedSubscriber() throws InterruptedException {
public void testAnnotatedMethod() throws InterruptedException {
CountDownLatch latch = new CountDownLatch(1);
GenericApplicationContext context = new GenericApplicationContext();
context.registerBeanDefinition("testChannel", new RootBeanDefinition(QueueChannel.class));
RootBeanDefinition subscriberDef = new RootBeanDefinition(SubscriberAnnotationTestBean.class);
subscriberDef.getConstructorArgumentValues().addGenericArgumentValue(latch);
context.registerBeanDefinition("testBean", subscriberDef);
RootBeanDefinition beanDefinition = new RootBeanDefinition(SimpleServiceActivatorAnnotationTestBean.class);
beanDefinition.getConstructorArgumentValues().addGenericArgumentValue(latch);
context.registerBeanDefinition("testBean", beanDefinition);
String busBeanName = MessageBusParser.MESSAGE_BUS_BEAN_NAME;
context.registerBeanDefinition(busBeanName, new RootBeanDefinition(DefaultMessageBus.class));
RootBeanDefinition postProcessorDef = new RootBeanDefinition(MessagingAnnotationPostProcessor.class);
@@ -55,7 +55,7 @@ public class SubscriberAnnotationPostProcessorTests {
context.registerBeanDefinition("postProcessor", postProcessorDef);
context.refresh();
context.start();
SubscriberAnnotationTestBean testBean = (SubscriberAnnotationTestBean) context.getBean("testBean");
SimpleServiceActivatorAnnotationTestBean testBean = (SimpleServiceActivatorAnnotationTestBean) context.getBean("testBean");
assertEquals(1, latch.getCount());
assertNull(testBean.getMessageText());
MessageChannel testChannel = (MessageChannel) context.getBean("testChannel");
@@ -67,13 +67,13 @@ public class SubscriberAnnotationPostProcessorTests {
}
public static class AbstractSubscriberAnnotationTestBean {
public static class AbstractServiceActivatorAnnotationTestBean {
protected String messageText;
private CountDownLatch latch;
public AbstractSubscriberAnnotationTestBean(CountDownLatch latch) {
public AbstractServiceActivatorAnnotationTestBean(CountDownLatch latch) {
this.latch = latch;
}
@@ -87,14 +87,14 @@ public class SubscriberAnnotationPostProcessorTests {
}
@MessageEndpoint
public static class SubscriberAnnotationTestBean extends SubscriberAnnotationPostProcessorTests.AbstractSubscriberAnnotationTestBean {
@MessageEndpoint
public static class SimpleServiceActivatorAnnotationTestBean extends AbstractServiceActivatorAnnotationTestBean {
public SubscriberAnnotationTestBean(CountDownLatch latch) {
public SimpleServiceActivatorAnnotationTestBean(CountDownLatch latch) {
super(latch);
}
@Handler(inputChannel="testChannel")
@ServiceActivator(inputChannel="testChannel")
public void testMethod(String messageText) {
this.messageText = messageText;
this.countDown();

View File

@@ -32,9 +32,9 @@ import org.springframework.beans.DirectFieldAccessor;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.integration.annotation.ChannelAdapter;
import org.springframework.integration.annotation.Handler;
import org.springframework.integration.annotation.MessageEndpoint;
import org.springframework.integration.annotation.Poller;
import org.springframework.integration.annotation.ServiceActivator;
import org.springframework.integration.annotation.Transformer;
import org.springframework.integration.bus.DefaultMessageBus;
import org.springframework.integration.bus.MessageBus;
@@ -71,8 +71,8 @@ public class MessagingAnnotationPostProcessorTests {
@Test
public void testSimpleHandlerWithContext() {
AbstractApplicationContext context = new ClassPathXmlApplicationContext(
"handlerAnnotationPostProcessorTests.xml", this.getClass());
ServiceInvoker invoker = (ServiceInvoker) context.getBean("simpleHandler");
"serviceActivatorAnnotationPostProcessorTests.xml", this.getClass());
ServiceInvoker invoker = (ServiceInvoker) context.getBean("testBean");
String reply = (String) invoker.invoke(new StringMessage("world"));
assertEquals("hello world", reply);
context.stop();
@@ -81,7 +81,7 @@ public class MessagingAnnotationPostProcessorTests {
@Test
public void testSimpleHandlerEndpointWithContext() {
AbstractApplicationContext context = new ClassPathXmlApplicationContext(
"handlerAnnotationPostProcessorTests.xml", this.getClass());
"serviceActivatorAnnotationPostProcessorTests.xml", this.getClass());
MessageChannel inputChannel = (MessageChannel) context.getBean("inputChannel");
PollableChannel outputChannel = (PollableChannel) context.getBean("outputChannel");
inputChannel.send(new StringMessage("foo"));
@@ -294,7 +294,7 @@ public class MessagingAnnotationPostProcessorTests {
postProcessor.afterPropertiesSet();
AnnotatedEndpointWithPolledAnnotation endpoint = new AnnotatedEndpointWithPolledAnnotation();
postProcessor.postProcessAfterInitialization(endpoint, "testBean");
ServiceActivatorEndpoint processedEndpoint = (ServiceActivatorEndpoint) messageBus.lookupEndpoint("testBean.handler");
ServiceActivatorEndpoint processedEndpoint = (ServiceActivatorEndpoint) messageBus.lookupEndpoint("testBean.serviceActivator");
DirectFieldAccessor accessor = new DirectFieldAccessor(processedEndpoint);
MessageSource<?> source = (MessageSource<?>) accessor.getPropertyValue("source");
assertTrue(source instanceof SubscribableSource);
@@ -382,7 +382,7 @@ public class MessagingAnnotationPostProcessorTests {
return this.channelRegistry;
}
@Handler(inputChannel="inputChannel")
@ServiceActivator(inputChannel="inputChannel")
public Message<?> handle(Message<?> message) {
return null;
}
@@ -401,7 +401,7 @@ public class MessagingAnnotationPostProcessorTests {
private static class SimpleAnnotatedEndpointImplementation implements SimpleAnnotatedEndpointInterface {
@Handler(inputChannel="inputChannel", outputChannel="outputChannel")
@ServiceActivator(inputChannel="inputChannel", outputChannel="outputChannel")
public String test(String input) {
return "test-" + input;
}
@@ -411,7 +411,7 @@ public class MessagingAnnotationPostProcessorTests {
@MessageEndpoint
private static class AnnotatedEndpointWithPolledAnnotation {
@Handler(inputChannel="testChannel")
@ServiceActivator(inputChannel="testChannel")
@Poller(period=1234, initialDelay=5678, fixedRate=true, timeUnit=TimeUnit.SECONDS)
public String prependFoo(String s) {
return "foo" + s;
@@ -422,7 +422,7 @@ public class MessagingAnnotationPostProcessorTests {
@MessageEndpoint
private static class HandlerAnnotatedBean {
@Handler
@ServiceActivator
public String test(String s) {
return s + s;
}

View File

@@ -16,8 +16,8 @@
package org.springframework.integration.config.annotation;
import org.springframework.integration.annotation.Handler;
import org.springframework.integration.annotation.MessageEndpoint;
import org.springframework.integration.annotation.ServiceActivator;
import org.springframework.integration.endpoint.annotation.ITestEndpoint;
/**
@@ -26,7 +26,7 @@ import org.springframework.integration.endpoint.annotation.ITestEndpoint;
@MessageEndpoint
public class SimpleAnnotatedEndpoint implements ITestEndpoint {
@Handler(inputChannel="inputChannel", outputChannel="outputChannel")
@ServiceActivator(inputChannel="inputChannel", outputChannel="outputChannel")
public String sayHello(String name) {
return "hello " + name;
}

View File

@@ -16,16 +16,16 @@
package org.springframework.integration.config.annotation;
import org.springframework.integration.annotation.Handler;
import org.springframework.integration.annotation.MessageEndpoint;
import org.springframework.integration.annotation.ServiceActivator;
/**
* @author Mark Fisher
*/
@MessageEndpoint
public class SimpleHandlerTestBean {
public class SimpleServiceActivatorTestBean {
@Handler
@ServiceActivator
public String sayHello(String input) {
return "hello " + input;
}

View File

@@ -16,8 +16,8 @@
package org.springframework.integration.config.annotation;
import org.springframework.integration.annotation.Handler;
import org.springframework.integration.annotation.MessageEndpoint;
import org.springframework.integration.annotation.ServiceActivator;
/**
* @author Mark Fisher
@@ -25,7 +25,7 @@ import org.springframework.integration.annotation.MessageEndpoint;
@MessageEndpoint
public class TypeConvertingTestEndpoint {
@Handler(inputChannel="inputChannel", outputChannel="outputChannel")
@ServiceActivator(inputChannel="inputChannel", outputChannel="outputChannel")
public int multiplyByTwo(int number) {
return number * 2;
}

View File

@@ -8,6 +8,7 @@
http://www.springframework.org/schema/integration/spring-integration-core-1.0.xsd">
<message-bus/>
<annotation-driven/>
<channel id="inputChannel"/>
@@ -15,12 +16,8 @@
<queue capacity="5"/>
</channel>
<service-activator input-channel="inputChannel" ref="simpleHandler" output-channel="outputChannel"/>
<service-activator input-channel="inputChannel" ref="testBean" output-channel="outputChannel"/>
<beans:bean class="org.springframework.integration.config.annotation.MessagingAnnotationPostProcessor">
<beans:constructor-arg ref="internal.MessageBus"/>
</beans:bean>
<beans:bean id="simpleHandler" class="org.springframework.integration.config.annotation.SimpleHandlerTestBean"/>
<beans:bean id="testBean" class="org.springframework.integration.config.annotation.SimpleServiceActivatorTestBean"/>
</beans:beans>

View File

@@ -16,8 +16,8 @@
package org.springframework.integration.endpoint.annotation;
import org.springframework.integration.annotation.Handler;
import org.springframework.integration.annotation.MessageEndpoint;
import org.springframework.integration.annotation.ServiceActivator;
import org.springframework.integration.message.Message;
import org.springframework.integration.message.StringMessage;
@@ -27,7 +27,7 @@ import org.springframework.integration.message.StringMessage;
@MessageEndpoint
public class MessageParameterAnnotatedEndpoint {
@Handler(inputChannel="inputChannel", outputChannel="outputChannel")
@ServiceActivator(inputChannel="inputChannel", outputChannel="outputChannel")
public StringMessage sayHello(Message<?> message) {
return new StringMessage("hello " + message.getPayload());
}

View File

@@ -24,7 +24,6 @@ import java.util.Properties;
import org.junit.Test;
import org.springframework.integration.annotation.Handler;
import org.springframework.integration.annotation.Header;
import org.springframework.integration.message.GenericMessage;
import org.springframework.integration.message.Message;
@@ -111,7 +110,7 @@ public class DefaultMessageHandlerTests {
@Test
public void messageOnlyWithAnnotatedMethod() throws Exception {
AnnotatedTestHandler handler = new AnnotatedTestHandler();
AnnotatedTestService handler = new AnnotatedTestService();
Method method = handler.getClass().getMethod("messageOnly", Message.class);
DefaultMessageHandler adapter = new DefaultMessageHandler();
adapter.setObject(handler);
@@ -122,7 +121,7 @@ public class DefaultMessageHandlerTests {
@Test
public void payloadWithAnnotatedMethod() throws Exception {
AnnotatedTestHandler handler = new AnnotatedTestHandler();
AnnotatedTestService handler = new AnnotatedTestService();
Method method = handler.getClass().getMethod("integerMethod", Integer.class);
DefaultMessageHandler adapter = new DefaultMessageHandler();
adapter.setObject(handler);
@@ -133,7 +132,7 @@ public class DefaultMessageHandlerTests {
@Test
public void convertedPayloadWithAnnotatedMethod() throws Exception {
AnnotatedTestHandler handler = new AnnotatedTestHandler();
AnnotatedTestService handler = new AnnotatedTestService();
Method method = handler.getClass().getMethod("integerMethod", Integer.class);
DefaultMessageHandler adapter = new DefaultMessageHandler();
adapter.setObject(handler);
@@ -144,7 +143,7 @@ public class DefaultMessageHandlerTests {
@Test(expected = MessagingException.class)
public void conversionFailureWithAnnotatedMethod() throws Exception {
AnnotatedTestHandler handler = new AnnotatedTestHandler();
AnnotatedTestService handler = new AnnotatedTestService();
Method method = handler.getClass().getMethod("integerMethod", Integer.class);
DefaultMessageHandler adapter = new DefaultMessageHandler();
adapter.setObject(handler);
@@ -155,7 +154,7 @@ public class DefaultMessageHandlerTests {
@Test
public void messageAndHeaderWithAnnotatedMethod() throws Exception {
AnnotatedTestHandler handler = new AnnotatedTestHandler();
AnnotatedTestService handler = new AnnotatedTestService();
Method method = handler.getClass().getMethod("messageAndHeader", Message.class, Integer.class);
DefaultMessageHandler adapter = new DefaultMessageHandler();
adapter.setObject(handler);
@@ -168,7 +167,7 @@ public class DefaultMessageHandlerTests {
@Test
public void multipleHeadersWithAnnotatedMethod() throws Exception {
AnnotatedTestHandler handler = new AnnotatedTestHandler();
AnnotatedTestService handler = new AnnotatedTestService();
Method method = handler.getClass().getMethod("twoHeaders", String.class, Integer.class);
DefaultMessageHandler adapter = new DefaultMessageHandler();
adapter.setObject(handler);
@@ -212,50 +211,41 @@ public class DefaultMessageHandlerTests {
}
}
private static class AnnotatedTestHandler {
private static class AnnotatedTestService {
@Handler
public String messageOnly(Message<?> message) {
return (String) message.getPayload();
}
@Handler
public String messageAndHeader(Message<?> message, @Header("number") Integer num) {
return (String) message.getPayload() + "-" + num.toString();
}
@Handler
public String twoHeaders(@Header String prop, @Header("number") Integer num) {
return prop + "-" + num.toString();
}
@Handler
public Integer optionalHeader(@Header(required=false) Integer num) {
return num;
}
@Handler
public Integer requiredHeader(@Header(value="num", required=true) Integer num) {
return num;
}
@Handler
public String optionalAndRequiredHeader(@Header(required=false) String prop, @Header(value="num", required=true) Integer num) {
return prop + num;
}
@Handler
public Properties propertiesMethod(Properties properties) {
return properties;
}
@Handler
@SuppressWarnings("unchecked")
public Map mapMethod(Map map) {
return map;
}
@Handler
public Integer integerMethod(Integer i) {
return i;
}

View File

@@ -26,7 +26,6 @@ import java.util.Properties;
import org.junit.Test;
import org.springframework.integration.annotation.Handler;
import org.springframework.integration.annotation.Header;
import org.springframework.integration.message.Message;
import org.springframework.integration.message.MessageBuilder;
@@ -40,7 +39,7 @@ public class MethodArgumentMessageMapperTests {
@Test
public void testOptionalHeader() throws Exception {
Method method = TestHandler.class.getMethod("optionalHeader", Integer.class);
Method method = TestService.class.getMethod("optionalHeader", Integer.class);
MethodArgumentMessageMapper<String> mapper = new MethodArgumentMessageMapper<String>(method);
Object[] args = (Object[]) mapper.mapMessage(new StringMessage("foo"));
assertEquals(1, args.length);
@@ -49,14 +48,14 @@ public class MethodArgumentMessageMapperTests {
@Test(expected=MessageHandlingException.class)
public void testRequiredHeaderNotProvided() throws Exception {
Method method = TestHandler.class.getMethod("requiredHeader", Integer.class);
Method method = TestService.class.getMethod("requiredHeader", Integer.class);
MethodArgumentMessageMapper<String> mapper = new MethodArgumentMessageMapper<String>(method);
mapper.mapMessage(new StringMessage("foo"));
}
@Test
public void testRequiredHeaderProvided() throws Exception {
Method method = TestHandler.class.getMethod("requiredHeader", Integer.class);
Method method = TestService.class.getMethod("requiredHeader", Integer.class);
MethodArgumentMessageMapper<String> mapper = new MethodArgumentMessageMapper<String>(method);
Message<String> message = MessageBuilder.fromPayload("foo")
.setHeader("num", new Integer(123)).build();
@@ -67,7 +66,7 @@ public class MethodArgumentMessageMapperTests {
@Test(expected=MessageHandlingException.class)
public void testOptionalAndRequiredHeaderWithOnlyOptionalHeaderProvided() throws Exception {
Method method = TestHandler.class.getMethod("optionalAndRequiredHeader", String.class, Integer.class);
Method method = TestService.class.getMethod("optionalAndRequiredHeader", String.class, Integer.class);
MethodArgumentMessageMapper<String> mapper = new MethodArgumentMessageMapper<String>(method);
Message<String> message = MessageBuilder.fromPayload("foo")
.setHeader("prop", "bar").build();
@@ -76,7 +75,7 @@ public class MethodArgumentMessageMapperTests {
@Test
public void testOptionalAndRequiredHeaderWithOnlyRequiredHeaderProvided() throws Exception {
Method method = TestHandler.class.getMethod("optionalAndRequiredHeader", String.class, Integer.class);
Method method = TestService.class.getMethod("optionalAndRequiredHeader", String.class, Integer.class);
MethodArgumentMessageMapper<String> mapper = new MethodArgumentMessageMapper<String>(method);
Message<String> message = MessageBuilder.fromPayload("foo")
.setHeader("num", new Integer(123)).build();
@@ -88,7 +87,7 @@ public class MethodArgumentMessageMapperTests {
@Test
public void testOptionalAndRequiredHeaderWithBothHeadersProvided() throws Exception {
Method method = TestHandler.class.getMethod("optionalAndRequiredHeader", String.class, Integer.class);
Method method = TestService.class.getMethod("optionalAndRequiredHeader", String.class, Integer.class);
MethodArgumentMessageMapper<String> mapper = new MethodArgumentMessageMapper<String>(method);
Message<String> message = MessageBuilder.fromPayload("foo")
.setHeader("num", new Integer(123))
@@ -102,7 +101,7 @@ public class MethodArgumentMessageMapperTests {
@Test
public void testPropertiesMethodWithNonPropertiesPayload() throws Exception {
Method method = TestHandler.class.getMethod("propertiesMethod", Properties.class);
Method method = TestService.class.getMethod("propertiesMethod", Properties.class);
MethodArgumentMessageMapper<String> mapper = new MethodArgumentMessageMapper<String>(method);
Message<String> message = MessageBuilder.fromPayload("test")
.setHeader("prop1", "foo").setHeader("prop2", "bar").build();
@@ -115,7 +114,7 @@ public class MethodArgumentMessageMapperTests {
@Test
public void testPropertiesMethodWithPropertiesPayload() throws Exception {
Method method = TestHandler.class.getMethod("propertiesMethod", Properties.class);
Method method = TestService.class.getMethod("propertiesMethod", Properties.class);
MethodArgumentMessageMapper<Properties> mapper = new MethodArgumentMessageMapper<Properties>(method);
Properties payload = new Properties();
payload.setProperty("prop1", "foo");
@@ -132,7 +131,7 @@ public class MethodArgumentMessageMapperTests {
@Test
@SuppressWarnings("unchecked")
public void testMapMethodWithNonMapPayload() throws Exception {
Method method = TestHandler.class.getMethod("mapMethod", Map.class);
Method method = TestService.class.getMethod("mapMethod", Map.class);
MethodArgumentMessageMapper<String> mapper = new MethodArgumentMessageMapper<String>(method);
Message<String> message = MessageBuilder.fromPayload("test")
.setHeader("attrib1", new Integer(123))
@@ -146,7 +145,7 @@ public class MethodArgumentMessageMapperTests {
@Test
@SuppressWarnings("unchecked")
public void testMapMethodWithMapPayload() throws Exception {
Method method = TestHandler.class.getMethod("mapMethod", Map.class);
Method method = TestService.class.getMethod("mapMethod", Map.class);
MethodArgumentMessageMapper<Map<String,Integer>> mapper = new MethodArgumentMessageMapper<Map<String,Integer>>(method);
Map<String, Integer> payload = new HashMap<String, Integer>();
payload.put("attrib1", new Integer(123));
@@ -162,50 +161,41 @@ public class MethodArgumentMessageMapperTests {
}
private static class TestHandler {
private static class TestService {
@Handler
public String messageOnly(Message<?> message) {
return (String) message.getPayload();
}
@Handler
public String messageAndHeader(Message<?> message, @Header("number") Integer num) {
return (String) message.getPayload() + "-" + num.toString();
}
@Handler
public String twoHeaders(@Header String prop, @Header("number") Integer num) {
return prop + "-" + num.toString();
}
@Handler
public Integer optionalHeader(@Header(required=false) Integer num) {
return num;
}
@Handler
public Integer requiredHeader(@Header(value="num", required=true) Integer num) {
return num;
}
@Handler
public String optionalAndRequiredHeader(@Header(required=false) String prop, @Header(value="num", required=true) Integer num) {
return prop + num;
}
@Handler
public Properties propertiesMethod(Properties properties) {
return properties;
}
@Handler
@SuppressWarnings("unchecked")
public Map mapMethod(Map map) {
return map;
}
@Handler
public Integer integerMethod(Integer i) {
return i;
}