INT-3565 Inject Advice Chain to Direct Handlers
JIRA: https://jira.spring.io/browse/INT-3565 Cast to `IntegrationObjectSupport` fails if the handler is already proxied in `AbstractSimpleMessageHandlerFactoryBean`. Conflicts: spring-integration-core/src/main/java/org/springframework/integration/config/AbstractSimpleMessageHandlerFactoryBean.java Resolved. INT-3565 Polishing - Fix test. - enhance test to show that directly bound message handler beans are not advised if already proxied.
This commit is contained in:
@@ -19,6 +19,7 @@ import org.aopalliance.aop.Advice;
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
|
||||
import org.springframework.aop.framework.Advised;
|
||||
import org.springframework.beans.BeansException;
|
||||
import org.springframework.beans.factory.BeanFactory;
|
||||
import org.springframework.beans.factory.BeanFactoryAware;
|
||||
@@ -29,6 +30,7 @@ import org.springframework.integration.context.IntegrationObjectSupport;
|
||||
import org.springframework.integration.context.Orderable;
|
||||
import org.springframework.integration.core.MessageProducer;
|
||||
import org.springframework.integration.handler.AbstractReplyProducingMessageHandler;
|
||||
import org.springframework.integration.support.context.NamedComponent;
|
||||
import org.springframework.messaging.MessageChannel;
|
||||
import org.springframework.messaging.MessageHandler;
|
||||
import org.springframework.util.Assert;
|
||||
@@ -117,12 +119,25 @@ public abstract class AbstractSimpleMessageHandlerFactoryBean<H extends MessageH
|
||||
if (this.handler instanceof MessageProducer && this.outputChannel != null) {
|
||||
((MessageProducer) this.handler).setOutputChannel(this.outputChannel);
|
||||
}
|
||||
if (this.handler instanceof IntegrationObjectSupport && this.componentName != null) {
|
||||
((IntegrationObjectSupport) this.handler).setComponentName(this.componentName);
|
||||
Object actualHandler = extractTarget(this.handler);
|
||||
if (actualHandler == null) {
|
||||
actualHandler = this.handler;
|
||||
}
|
||||
if (!CollectionUtils.isEmpty(this.adviceChain) &&
|
||||
this.handler instanceof AbstractReplyProducingMessageHandler) {
|
||||
((AbstractReplyProducingMessageHandler) this.handler).setAdviceChain(this.adviceChain);
|
||||
if (actualHandler instanceof IntegrationObjectSupport && this.componentName != null) {
|
||||
((IntegrationObjectSupport) actualHandler).setComponentName(this.componentName);
|
||||
}
|
||||
if (!CollectionUtils.isEmpty(this.adviceChain)) {
|
||||
if (actualHandler instanceof AbstractReplyProducingMessageHandler) {
|
||||
((AbstractReplyProducingMessageHandler) actualHandler).setAdviceChain(this.adviceChain);
|
||||
}
|
||||
else if (logger.isDebugEnabled()) {
|
||||
String name = this.componentName;
|
||||
if (name == null && actualHandler instanceof NamedComponent) {
|
||||
name = ((NamedComponent) actualHandler).getComponentName();
|
||||
}
|
||||
logger.debug("adviceChain can only be set on an AbstractReplyProducingMessageHandler"
|
||||
+ (name == null ? "" : (", " + name)) + ".");
|
||||
}
|
||||
}
|
||||
if (this.handler instanceof Orderable && this.order != null) {
|
||||
((Orderable) this.handler).setOrder(this.order);
|
||||
@@ -155,4 +170,21 @@ public abstract class AbstractSimpleMessageHandlerFactoryBean<H extends MessageH
|
||||
return true;
|
||||
}
|
||||
|
||||
private Object extractTarget(Object object) {
|
||||
if (!(object instanceof Advised)) {
|
||||
return object;
|
||||
}
|
||||
Advised advised = (Advised) object;
|
||||
if (advised.getTargetSource() == null) {
|
||||
return null;
|
||||
}
|
||||
try {
|
||||
return extractTarget(advised.getTargetSource().getTarget());
|
||||
}
|
||||
catch (Exception e) {
|
||||
logger.error("Could not extract target", e);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -31,6 +31,8 @@ import org.springframework.messaging.PollableChannel;
|
||||
import org.springframework.messaging.support.GenericMessage;
|
||||
|
||||
/**
|
||||
* Also in JMX - changes here should be reflected there.
|
||||
*
|
||||
* @author Mark Fisher
|
||||
* @author Gary Russell
|
||||
*/
|
||||
|
||||
@@ -0,0 +1,107 @@
|
||||
/*
|
||||
* Copyright 2002-2014 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 static org.junit.Assert.assertEquals;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import org.springframework.context.ConfigurableApplicationContext;
|
||||
import org.springframework.context.support.ClassPathXmlApplicationContext;
|
||||
import org.springframework.integration.handler.AbstractReplyProducingMessageHandler;
|
||||
import org.springframework.integration.handler.advice.AbstractRequestHandlerAdvice;
|
||||
import org.springframework.integration.support.MessageBuilder;
|
||||
import org.springframework.messaging.Message;
|
||||
import org.springframework.messaging.MessageChannel;
|
||||
import org.springframework.messaging.PollableChannel;
|
||||
import org.springframework.messaging.support.GenericMessage;
|
||||
|
||||
/**
|
||||
* @author Mark Fisher
|
||||
* @author Gary Russell
|
||||
*/
|
||||
public class TransformerContextTests {
|
||||
|
||||
private static volatile int adviceCalled;
|
||||
|
||||
private static volatile int bazCalled;
|
||||
|
||||
@Test
|
||||
public void methodInvokingTransformer() {
|
||||
ConfigurableApplicationContext context = new ClassPathXmlApplicationContext(
|
||||
"transformerContextTests.xml", this.getClass());
|
||||
MessageChannel input = context.getBean("input", MessageChannel.class);
|
||||
PollableChannel output = context.getBean("output", PollableChannel.class);
|
||||
input.send(new GenericMessage<String>("foo"));
|
||||
Message<?> reply = output.receive(0);
|
||||
assertEquals("FOO", reply.getPayload());
|
||||
assertEquals(1, adviceCalled);
|
||||
|
||||
input = context.getBean("direct", MessageChannel.class);
|
||||
input.send(new GenericMessage<String>("foo"));
|
||||
reply = output.receive(0);
|
||||
assertEquals("FOO", reply.getPayload());
|
||||
|
||||
input = context.getBean("directRef", MessageChannel.class);
|
||||
input.send(new GenericMessage<String>("foo"));
|
||||
reply = output.receive(0);
|
||||
assertEquals("FOO", reply.getPayload());
|
||||
assertEquals(2, adviceCalled);
|
||||
|
||||
input = context.getBean("service", MessageChannel.class);
|
||||
input.send(new GenericMessage<String>("foo"));
|
||||
assertEquals(1, bazCalled);
|
||||
assertEquals(3, adviceCalled);
|
||||
|
||||
context.close();
|
||||
}
|
||||
|
||||
public static class FooAdvice extends AbstractRequestHandlerAdvice {
|
||||
|
||||
@Override
|
||||
protected Object doInvoke(ExecutionCallback callback, Object target, Message<?> message) throws Exception {
|
||||
adviceCalled++;
|
||||
return callback.execute();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public static class Bar extends AbstractReplyProducingMessageHandler {
|
||||
|
||||
@Override
|
||||
protected Object handleRequestMessage(Message<?> requestMessage) {
|
||||
Exception e = new RuntimeException();
|
||||
StackTraceElement[] st = e.getStackTrace();
|
||||
return MessageBuilder.withPayload(requestMessage.getPayload().toString().toUpperCase())
|
||||
.setHeader("callStack", st);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public static class BazService {
|
||||
|
||||
public void qux() {
|
||||
bazCalled++;
|
||||
}
|
||||
|
||||
public String upperCase(String input) {
|
||||
return input.toUpperCase();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<beans:beans xmlns="http://www.springframework.org/schema/integration"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xmlns:beans="http://www.springframework.org/schema/beans"
|
||||
xmlns:int-jmx="http://www.springframework.org/schema/integration/jmx"
|
||||
xmlns:context="http://www.springframework.org/schema/context"
|
||||
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
|
||||
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
|
||||
http://www.springframework.org/schema/integration/jmx http://www.springframework.org/schema/integration/jmx/spring-integration-jmx.xsd
|
||||
http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration.xsd">
|
||||
|
||||
<context:mbean-server/>
|
||||
|
||||
<int-jmx:mbean-export default-domain="transformers" />
|
||||
|
||||
<channel id="input"/>
|
||||
|
||||
<recipient-list-router input-channel="input"> <!-- INT-3565 ClassCastException with DEBUG -->
|
||||
<recipient channel="input1" selector-expression="true" />
|
||||
</recipient-list-router>
|
||||
|
||||
<channel id="output">
|
||||
<queue capacity="50"/>
|
||||
</channel>
|
||||
|
||||
<transformer input-channel="input1" ref="testBean" method="upperCase" output-channel="output">
|
||||
<request-handler-advice-chain>
|
||||
<beans:bean class="org.springframework.integration.monitor.TransformerContextTests$FooAdvice" />
|
||||
</request-handler-advice-chain>
|
||||
</transformer>
|
||||
|
||||
<beans:bean id="testBean" class="org.springframework.integration.monitor.TransformerContextTests$BazService"/>
|
||||
|
||||
<transformer input-channel="direct" output-channel="output">
|
||||
<beans:bean class="org.springframework.integration.monitor.TransformerContextTests$Bar"/>
|
||||
</transformer>
|
||||
|
||||
<transformer input-channel="directRef" output-channel="output" ref="trans" method="handleMessage">
|
||||
<request-handler-advice-chain>
|
||||
<beans:bean class="org.springframework.integration.monitor.TransformerContextTests$FooAdvice" />
|
||||
</request-handler-advice-chain>
|
||||
</transformer>
|
||||
|
||||
<beans:bean id="trans" class="org.springframework.integration.monitor.TransformerContextTests$Bar"/>
|
||||
|
||||
<service-activator input-channel="service" method="qux">
|
||||
<beans:bean class="org.springframework.integration.monitor.TransformerContextTests$BazService" />
|
||||
<request-handler-advice-chain>
|
||||
<beans:bean class="org.springframework.integration.monitor.TransformerContextTests$FooAdvice" />
|
||||
</request-handler-advice-chain>
|
||||
</service-activator>
|
||||
|
||||
</beans:beans>
|
||||
Reference in New Issue
Block a user