From 3839db543301bc656435af36fe0d3f315f614204 Mon Sep 17 00:00:00 2001 From: Oleg Zhurakousky Date: Mon, 7 Mar 2011 18:14:45 -0500 Subject: [PATCH] INT-1829 added support for MessageHistory to outbound-channel-adapter --- spring-integration-core/.springBeans | 3 +- .../endpoint/EventDrivenConsumer.java | 1 - .../handler/MethodInvokingMessageHandler.java | 30 ++++---- .../integration/history/AnnotatedAdapter.java | 45 ++++++++++++ .../integration/history/AnotatedTests.java | 69 +++++++++++++++++++ .../integration/history/annotated-config.xml | 18 +++++ 6 files changed, 146 insertions(+), 20 deletions(-) create mode 100644 spring-integration-core/src/test/java/org/springframework/integration/history/AnnotatedAdapter.java create mode 100644 spring-integration-core/src/test/java/org/springframework/integration/history/AnotatedTests.java create mode 100644 spring-integration-core/src/test/java/org/springframework/integration/history/annotated-config.xml diff --git a/spring-integration-core/.springBeans b/spring-integration-core/.springBeans index af61d5c04c..5505ed4aa2 100644 --- a/spring-integration-core/.springBeans +++ b/spring-integration-core/.springBeans @@ -1,7 +1,7 @@ 1 - + @@ -9,6 +9,7 @@ src/test/java/org/springframework/integration/gateway/GatewayInterfaceTest-context.xml src/test/java/org/springframework/integration/gateway/InnerGatewayWithChainTests-context.xml + src/test/java/org/springframework/integration/history/annotated-config.xml diff --git a/spring-integration-core/src/main/java/org/springframework/integration/endpoint/EventDrivenConsumer.java b/spring-integration-core/src/main/java/org/springframework/integration/endpoint/EventDrivenConsumer.java index 2266491660..5778c21897 100644 --- a/spring-integration-core/src/main/java/org/springframework/integration/endpoint/EventDrivenConsumer.java +++ b/spring-integration-core/src/main/java/org/springframework/integration/endpoint/EventDrivenConsumer.java @@ -16,7 +16,6 @@ package org.springframework.integration.endpoint; -import org.springframework.integration.channel.AbstractSubscribableChannel; import org.springframework.integration.context.IntegrationObjectSupport; import org.springframework.integration.core.MessageHandler; import org.springframework.integration.core.SubscribableChannel; diff --git a/spring-integration-core/src/main/java/org/springframework/integration/handler/MethodInvokingMessageHandler.java b/spring-integration-core/src/main/java/org/springframework/integration/handler/MethodInvokingMessageHandler.java index 029963d8d6..8eff4b1682 100644 --- a/spring-integration-core/src/main/java/org/springframework/integration/handler/MethodInvokingMessageHandler.java +++ b/spring-integration-core/src/main/java/org/springframework/integration/handler/MethodInvokingMessageHandler.java @@ -18,7 +18,6 @@ package org.springframework.integration.handler; import java.lang.reflect.Method; -import org.springframework.core.Ordered; import org.springframework.integration.Message; import org.springframework.integration.MessagingException; import org.springframework.integration.core.MessageHandler; @@ -28,37 +27,32 @@ import org.springframework.util.Assert; * A {@link MessageHandler} that invokes the specified method on the provided object. * * @author Mark Fisher + * @author Oleg Zhurakousky */ -public class MethodInvokingMessageHandler extends MethodInvokingMessageProcessor implements MessageHandler, Ordered { - - private volatile int order = Ordered.LOWEST_PRECEDENCE; +public class MethodInvokingMessageHandler extends AbstractMessageHandler { + private volatile MethodInvokingMessageProcessor processor; public MethodInvokingMessageHandler(Object object, Method method) { - super(object, method); Assert.isTrue(method.getReturnType().equals(void.class), "MethodInvokingMessageHandler requires a void-returning method"); + processor = new MethodInvokingMessageProcessor(object, method); } public MethodInvokingMessageHandler(Object object, String methodName) { - super(object, methodName); + processor = new MethodInvokingMessageProcessor(object, methodName); } - - public void setOrder(int order) { - this.order = order; - } - - public int getOrder() { - return this.order; - } - - public void handleMessage(Message message) { - Object result = this.processMessage(message); + @Override + protected void handleMessageInternal(Message message) throws Exception { + Object result = processor.processMessage(message); if (result != null) { throw new MessagingException(message, "the MethodInvokingMessageHandler method must " + "have a void return, but '" + this + "' received a value: [" + result + "]"); } } - + + public String getComponentType() { + return "outbound-channel-adapter"; + } } diff --git a/spring-integration-core/src/test/java/org/springframework/integration/history/AnnotatedAdapter.java b/spring-integration-core/src/test/java/org/springframework/integration/history/AnnotatedAdapter.java new file mode 100644 index 0000000000..7a0add65e7 --- /dev/null +++ b/spring-integration-core/src/test/java/org/springframework/integration/history/AnnotatedAdapter.java @@ -0,0 +1,45 @@ +/* + * Copyright 2002-2011 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.history; + +import org.springframework.beans.BeansException; +import org.springframework.context.ApplicationContext; +import org.springframework.context.ApplicationContextAware; +import org.springframework.context.ApplicationEvent; +import org.springframework.integration.Message; +import org.springframework.integration.annotation.MessageEndpoint; + +/** + * @author Oleg Zhurakousky + * + */ +@MessageEndpoint(value = "outputAdapter") +public class AnnotatedAdapter implements ApplicationContextAware{ + private volatile ApplicationContext applicationContext; + + @SuppressWarnings("serial") + public void handle(Message message) { + System.out.println("Message: " + message); + applicationContext.publishEvent(new ApplicationEvent(message) {}); + } + + public void setApplicationContext(ApplicationContext applicationContext) + throws BeansException { + this.applicationContext = applicationContext; + } + +} + diff --git a/spring-integration-core/src/test/java/org/springframework/integration/history/AnotatedTests.java b/spring-integration-core/src/test/java/org/springframework/integration/history/AnotatedTests.java new file mode 100644 index 0000000000..a1b953c4b1 --- /dev/null +++ b/spring-integration-core/src/test/java/org/springframework/integration/history/AnotatedTests.java @@ -0,0 +1,69 @@ +/* + * Copyright 2002-2011 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.history; + +import static junit.framework.Assert.assertEquals; +import static org.mockito.Mockito.spy; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; + +import java.lang.reflect.Field; +import java.util.Properties; + +import org.junit.Test; +import org.mockito.Mockito; + +import org.springframework.context.ApplicationEvent; +import org.springframework.context.ApplicationListener; +import org.springframework.context.support.ClassPathXmlApplicationContext; +import org.springframework.integration.Message; +import org.springframework.integration.MessageChannel; +import org.springframework.integration.core.MessageHandler; +import org.springframework.integration.endpoint.EventDrivenConsumer; +import org.springframework.integration.message.GenericMessage; +import org.springframework.integration.test.util.TestUtils; + +/** + * @author Oleg Zhurakousky + * + */ +public class AnotatedTests { + + @Test + public void testHistoryWithAnnotatedComponents() throws Exception{ + ClassPathXmlApplicationContext ac = new ClassPathXmlApplicationContext("annotated-config.xml", this.getClass()); + ApplicationListener listener = new ApplicationListener() { + + public void onApplicationEvent(ApplicationEvent event) { + MessageHistory history = MessageHistory.read((Message) event.getSource()); + Properties adapterHistory = history.get(1); + assertEquals("myAdapter", adapterHistory.get("name")); + assertEquals("outbound-channel-adapter", adapterHistory.get("type")); + } + }; + listener = spy(listener); + ac.addApplicationListener(listener); + + MessageChannel channel = ac.getBean("inputChannel", MessageChannel.class); + EventDrivenConsumer consumer = ac.getBean("myAdapter", EventDrivenConsumer.class); + MessageHandler handler = (MessageHandler) TestUtils.getPropertyValue(consumer, "handler"); + Field handlerField = consumer.getClass().getDeclaredField("handler"); + handlerField.setAccessible(true); + handlerField.set(consumer, handler); + channel.send(new GenericMessage("hello")); + verify(listener, times(1)).onApplicationEvent((ApplicationEvent) Mockito.any()); + } +} diff --git a/spring-integration-core/src/test/java/org/springframework/integration/history/annotated-config.xml b/spring-integration-core/src/test/java/org/springframework/integration/history/annotated-config.xml new file mode 100644 index 0000000000..4167bd87d6 --- /dev/null +++ b/spring-integration-core/src/test/java/org/springframework/integration/history/annotated-config.xml @@ -0,0 +1,18 @@ + + + + + + + + + + + +