diff --git a/spring-integration-core/src/main/java/org/springframework/integration/handler/MessageHandlerChain.java b/spring-integration-core/src/main/java/org/springframework/integration/handler/MessageHandlerChain.java index 28cdc10306..f0ed463804 100644 --- a/spring-integration-core/src/main/java/org/springframework/integration/handler/MessageHandlerChain.java +++ b/spring-integration-core/src/main/java/org/springframework/integration/handler/MessageHandlerChain.java @@ -24,6 +24,7 @@ import org.springframework.core.Ordered; import org.springframework.integration.Message; import org.springframework.integration.MessageChannel; import org.springframework.integration.MessageHandlingException; +import org.springframework.integration.context.IntegrationObjectSupport; import org.springframework.integration.core.MessageHandler; import org.springframework.integration.core.MessageProducer; import org.springframework.integration.filter.MessageFilter; @@ -165,6 +166,19 @@ public class MessageHandlerChain extends AbstractMessageHandler implements Messa } } + @Override + public void setComponentName(String componentName) { + super.setComponentName(componentName); + int i = 0; + if (this.handlers != null) { + for (MessageHandler messageHandler : this.handlers) { + if (messageHandler instanceof IntegrationObjectSupport) { + ((IntegrationObjectSupport) messageHandler).setComponentName(componentName + "#handler#" + i); + } + i++; // increment, regardless of whether we assigned a component name + } + } + } private class ReplyForwardingMessageChannel implements MessageChannel { diff --git a/spring-integration-core/src/test/java/org/springframework/integration/handler/MessageHandlerChainTests.java b/spring-integration-core/src/test/java/org/springframework/integration/handler/MessageHandlerChainTests.java index 8040b6d8dd..58c0807798 100644 --- a/spring-integration-core/src/test/java/org/springframework/integration/handler/MessageHandlerChainTests.java +++ b/spring-integration-core/src/test/java/org/springframework/integration/handler/MessageHandlerChainTests.java @@ -17,6 +17,7 @@ package org.springframework.integration.handler; import static org.easymock.EasyMock.*; +import static org.junit.Assert.assertEquals; import java.util.ArrayList; import java.util.List; @@ -26,6 +27,7 @@ import org.junit.Test; import org.springframework.beans.factory.support.DefaultListableBeanFactory; import org.springframework.integration.Message; import org.springframework.integration.MessageChannel; +import org.springframework.integration.context.IntegrationObjectSupport; import org.springframework.integration.core.MessageHandler; import org.springframework.integration.core.MessageProducer; import org.springframework.integration.support.MessageBuilder; @@ -169,8 +171,22 @@ public class MessageHandlerChainTests { chain.afterPropertiesSet(); } + @Test + public void componentNaming() { + List handlers = new ArrayList(); + handlers.add(producer1); + handlers.add(handler1); // this one won't be named + handlers.add(producer2); + handlers.add(producer3); + MessageHandlerChain chain = new MessageHandlerChain(); + chain.setHandlers(handlers); + chain.setComponentName("testChain"); + assertEquals("testChain#handler#0", producer1.getComponentName()); + assertEquals("testChain#handler#2", producer2.getComponentName()); + assertEquals("testChain#handler#3", producer3.getComponentName()); + } - private static class ProducingHandlerStub implements MessageHandler, MessageProducer { + private static class ProducingHandlerStub extends IntegrationObjectSupport implements MessageHandler, MessageProducer { private volatile MessageChannel output;