From b2533c0788e3fcec8df451baaa06f7ee2cc757a2 Mon Sep 17 00:00:00 2001 From: Oleg Zhurakousky Date: Wed, 15 Sep 2010 13:50:16 -0400 Subject: [PATCH] INT-1431, added SimpleMessageProducingHandlerMonitor to maintain semantics of some MessageHandlers also being MessageProducers --- .../aop/MessagePublishingInterceptor.java | 12 +++++- .../monitor/IntegrationMBeanExporter.java | 9 ++++- .../monitor/SimpleMessageHandlerMonitor.java | 16 +++++++- .../SimpleMessageProducingHandlerMonitor.java | 38 ++++++++++++++++++ ...hMessageProducingHandlersTests-context.xml | 33 +++++++++++++++ ...hainWithMessageProducingHandlersTests.java | 40 +++++++++++++++++++ 6 files changed, 144 insertions(+), 4 deletions(-) create mode 100644 spring-integration-jmx/src/main/java/org/springframework/integration/monitor/SimpleMessageProducingHandlerMonitor.java create mode 100644 spring-integration-jmx/src/test/java/org/springframework/integration/monitor/ChainWithMessageProducingHandlersTests-context.xml create mode 100644 spring-integration-jmx/src/test/java/org/springframework/integration/monitor/ChainWithMessageProducingHandlersTests.java diff --git a/spring-integration-core/src/main/java/org/springframework/integration/aop/MessagePublishingInterceptor.java b/spring-integration-core/src/main/java/org/springframework/integration/aop/MessagePublishingInterceptor.java index c3f46675a7..9bafb47abf 100644 --- a/spring-integration-core/src/main/java/org/springframework/integration/aop/MessagePublishingInterceptor.java +++ b/spring-integration-core/src/main/java/org/springframework/integration/aop/MessagePublishingInterceptor.java @@ -88,7 +88,7 @@ public class MessagePublishingInterceptor implements MethodInterceptor { final StandardEvaluationContext context = new StandardEvaluationContext(); context.addPropertyAccessor(new MapAccessor()); Class targetClass = AopUtils.getTargetClass(invocation.getThis()); - Method method = AopUtils.getMostSpecificMethod(invocation.getMethod(), targetClass); + final Method method = AopUtils.getMostSpecificMethod(invocation.getMethod(), targetClass); String[] argumentNames = this.resolveArgumentNames(method); context.setVariable(PublisherMetadataSource.METHOD_NAME_VARIABLE_NAME, method.getName()); if (invocation.getArguments().length > 0 && argumentNames != null) { @@ -114,6 +114,16 @@ public class MessagePublishingInterceptor implements MethodInterceptor { } finally { publishMessage(method, context); +// Thread t = new Thread(new Runnable() { +// public void run() { +// try { +// publishMessage(method, context); +// } catch (Exception e) { +// e.printStackTrace(); } +// +// } +// }); +// t.start(); } } diff --git a/spring-integration-jmx/src/main/java/org/springframework/integration/monitor/IntegrationMBeanExporter.java b/spring-integration-jmx/src/main/java/org/springframework/integration/monitor/IntegrationMBeanExporter.java index 1d9a9681de..ef5644f7aa 100644 --- a/spring-integration-jmx/src/main/java/org/springframework/integration/monitor/IntegrationMBeanExporter.java +++ b/spring-integration-jmx/src/main/java/org/springframework/integration/monitor/IntegrationMBeanExporter.java @@ -41,6 +41,7 @@ import org.springframework.context.SmartLifecycle; import org.springframework.integration.MessageChannel; import org.springframework.integration.channel.QueueChannel; import org.springframework.integration.core.MessageHandler; +import org.springframework.integration.core.MessageProducer; import org.springframework.integration.core.PollableChannel; import org.springframework.integration.endpoint.AbstractEndpoint; import org.springframework.jmx.export.MBeanExporter; @@ -78,6 +79,7 @@ import org.springframework.util.ReflectionUtils; * * @author Dave Syer * @author Helena Edelson + * @author Oleg Zhurakousky */ @ManagedResource public class IntegrationMBeanExporter extends MBeanExporter implements BeanPostProcessor, BeanFactoryAware, @@ -162,7 +164,12 @@ public class IntegrationMBeanExporter extends MBeanExporter implements BeanPostP public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException { if (bean instanceof MessageHandler) { - SimpleMessageHandlerMonitor monitor = new SimpleMessageHandlerMonitor((MessageHandler) bean); + SimpleMessageHandlerMonitor monitor = null; + if (bean instanceof MessageProducer){ // we need to maintain semantics of the handler also being a producer see INT-1431 + monitor = new SimpleMessageProducingHandlerMonitor((MessageHandler) bean); + } else { + monitor = new SimpleMessageHandlerMonitor((MessageHandler) bean); + } handlers.add(monitor); return monitor; } diff --git a/spring-integration-jmx/src/main/java/org/springframework/integration/monitor/SimpleMessageHandlerMonitor.java b/spring-integration-jmx/src/main/java/org/springframework/integration/monitor/SimpleMessageHandlerMonitor.java index 485243bf2a..ab6fa1c9a8 100644 --- a/spring-integration-jmx/src/main/java/org/springframework/integration/monitor/SimpleMessageHandlerMonitor.java +++ b/spring-integration-jmx/src/main/java/org/springframework/integration/monitor/SimpleMessageHandlerMonitor.java @@ -1,5 +1,17 @@ -/** - * +/* + * 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. + * 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.monitor; diff --git a/spring-integration-jmx/src/main/java/org/springframework/integration/monitor/SimpleMessageProducingHandlerMonitor.java b/spring-integration-jmx/src/main/java/org/springframework/integration/monitor/SimpleMessageProducingHandlerMonitor.java new file mode 100644 index 0000000000..baee4af7e2 --- /dev/null +++ b/spring-integration-jmx/src/main/java/org/springframework/integration/monitor/SimpleMessageProducingHandlerMonitor.java @@ -0,0 +1,38 @@ +/* + * 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. + * 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.monitor; + +import org.springframework.integration.MessageChannel; +import org.springframework.integration.core.MessageHandler; +import org.springframework.integration.core.MessageProducer; +import org.springframework.jmx.export.annotation.ManagedResource; + +/** + * @author Oleg Zhurakousky + * @since 2.0 + * + */ +@ManagedResource +public class SimpleMessageProducingHandlerMonitor extends SimpleMessageHandlerMonitor implements MessageProducer { + + public SimpleMessageProducingHandlerMonitor(MessageHandler handler) { + super(handler); + } + + public void setOutputChannel(MessageChannel outputChannel) { + ((MessageProducer)this.getMessageHandler()).setOutputChannel(outputChannel); + } +} \ No newline at end of file diff --git a/spring-integration-jmx/src/test/java/org/springframework/integration/monitor/ChainWithMessageProducingHandlersTests-context.xml b/spring-integration-jmx/src/test/java/org/springframework/integration/monitor/ChainWithMessageProducingHandlersTests-context.xml new file mode 100644 index 0000000000..3bc26ede32 --- /dev/null +++ b/spring-integration-jmx/src/test/java/org/springframework/integration/monitor/ChainWithMessageProducingHandlersTests-context.xml @@ -0,0 +1,33 @@ + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/spring-integration-jmx/src/test/java/org/springframework/integration/monitor/ChainWithMessageProducingHandlersTests.java b/spring-integration-jmx/src/test/java/org/springframework/integration/monitor/ChainWithMessageProducingHandlersTests.java new file mode 100644 index 0000000000..cb2fa19198 --- /dev/null +++ b/spring-integration-jmx/src/test/java/org/springframework/integration/monitor/ChainWithMessageProducingHandlersTests.java @@ -0,0 +1,40 @@ +/** + * + */ +package org.springframework.integration.monitor; + +import static junit.framework.Assert.assertNotNull; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.ApplicationContext; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; + +/** + * @author Oleg Zhurakousky + * + */ +@ContextConfiguration +@RunWith(SpringJUnit4ClassRunner.class) +public class ChainWithMessageProducingHandlersTests { + + @Autowired + private ApplicationContext applicationContext; + + @Test + public void testSuccessfullApplicationContext(){ + // this is all we need to do. Until INT-1431 was solved initialization of this AC would fail. + assertNotNull(applicationContext); + } + + public static class SampleProducer{ + public String echo(String value){ + return value; + } + } + public static class SampleService{ + public void echo(String value){} + } +}