From b0c70ab519c9600eda47f79d5974d67b95688086 Mon Sep 17 00:00:00 2001 From: Gary Russell Date: Fri, 20 Dec 2013 14:07:56 -0500 Subject: [PATCH] INT-3248 Suppress HeaderChannelRegistry Warning Remove warning from BFCR if there is no HeaderChannelRegistry in the bean factory. Change the log to DEBUG and remove the stack trace. Instead, update the Exception message when a channel name can't be resolved to a channel to indicate that there is no registry. JIRA: https://jira.springsource.org/browse/INT-3248 --- .../channel/BeanFactoryChannelResolver.java | 9 ++- .../registry/HeaderChannelRegistryTests.java | 58 +++++++++++++++++++ 2 files changed, 65 insertions(+), 2 deletions(-) diff --git a/spring-integration-core/src/main/java/org/springframework/integration/support/channel/BeanFactoryChannelResolver.java b/spring-integration-core/src/main/java/org/springframework/integration/support/channel/BeanFactoryChannelResolver.java index d3ef03bd46..8585e94685 100644 --- a/spring-integration-core/src/main/java/org/springframework/integration/support/channel/BeanFactoryChannelResolver.java +++ b/spring-integration-core/src/main/java/org/springframework/integration/support/channel/BeanFactoryChannelResolver.java @@ -32,7 +32,11 @@ import org.springframework.util.Assert; *

Will lookup Spring managed beans identified by bean name, * expecting them to be of type {@link MessageChannel}. * + * Consults a {@link HeaderChannelRegistry}, if available, if the bean is not found. + * * @author Mark Fisher + * @author Gary Russell + * * @see org.springframework.beans.factory.BeanFactory */ public class BeanFactoryChannelResolver implements ChannelResolver, BeanFactoryAware { @@ -82,7 +86,7 @@ public class BeanFactoryChannelResolver implements ChannelResolver, BeanFactoryA HeaderChannelRegistry.class); } catch (Exception e) { - logger.warn("No HeaderChannelRegistry found", e); + logger.debug("No HeaderChannelRegistry found"); } } @@ -100,7 +104,8 @@ public class BeanFactoryChannelResolver implements ChannelResolver, BeanFactoryA } } throw new ChannelResolutionException( - "failed to look up MessageChannel bean with name '" + name + "'", e); + "failed to look up MessageChannel with name '" + name + "' in the BeanFactory" + + (this.replyChannelRegistry == null ? " (and there is no HeaderChannelRegistry present)." : "."), e); } } diff --git a/spring-integration-core/src/test/java/org/springframework/integration/channel/registry/HeaderChannelRegistryTests.java b/spring-integration-core/src/test/java/org/springframework/integration/channel/registry/HeaderChannelRegistryTests.java index e5d9091b66..dc6e4cd4c9 100644 --- a/spring-integration-core/src/test/java/org/springframework/integration/channel/registry/HeaderChannelRegistryTests.java +++ b/spring-integration-core/src/test/java/org/springframework/integration/channel/registry/HeaderChannelRegistryTests.java @@ -22,11 +22,18 @@ import static org.junit.Assert.assertNull; import static org.junit.Assert.assertThat; import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; +import static org.mockito.Mockito.doAnswer; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; import org.hamcrest.Matchers; import org.junit.Test; import org.junit.runner.RunWith; +import org.mockito.invocation.InvocationOnMock; +import org.mockito.stubbing.Answer; +import org.springframework.beans.factory.BeanFactory; +import org.springframework.beans.factory.NoSuchBeanDefinitionException; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.integration.Message; import org.springframework.integration.MessageChannel; @@ -34,11 +41,15 @@ import org.springframework.integration.channel.DefaultHeaderChannelRegistry; import org.springframework.integration.channel.DirectChannel; import org.springframework.integration.channel.MessagePublishingErrorHandler; import org.springframework.integration.channel.QueueChannel; +import org.springframework.integration.context.IntegrationContextUtils; import org.springframework.integration.core.MessagingTemplate; import org.springframework.integration.handler.AbstractReplyProducingMessageHandler; import org.springframework.integration.message.ErrorMessage; import org.springframework.integration.message.GenericMessage; import org.springframework.integration.support.MessageBuilder; +import org.springframework.integration.support.channel.BeanFactoryChannelResolver; +import org.springframework.integration.support.channel.ChannelResolutionException; +import org.springframework.integration.support.channel.HeaderChannelRegistry; import org.springframework.scheduling.TaskScheduler; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; @@ -143,6 +154,53 @@ public class HeaderChannelRegistryTests { registry.stop(); } + @Test + public void testBFCRWithRegistry() { + BeanFactoryChannelResolver resolver = new BeanFactoryChannelResolver(); + BeanFactory beanFactory = mock(BeanFactory.class); + when(beanFactory.getBean(IntegrationContextUtils.INTEGRATION_HEADER_CHANNEL_REGISTRY_BEAN_NAME, + HeaderChannelRegistry.class)) + .thenReturn(mock(HeaderChannelRegistry.class)); + doAnswer(new Answer(){ + + @Override + public Object answer(InvocationOnMock invocation) throws Throwable { + throw new NoSuchBeanDefinitionException("bar"); + } + }).when(beanFactory).getBean("foo", MessageChannel.class); + resolver.setBeanFactory(beanFactory); + try { + resolver.resolveChannelName("foo"); + fail("Expected exception"); + } + catch (ChannelResolutionException e){ + assertThat(e.getMessage(), + Matchers.equalTo("failed to look up MessageChannel with name 'foo' in the BeanFactory.")); + } + } + + @Test + public void testBFCRNoRegistry() { + BeanFactoryChannelResolver resolver = new BeanFactoryChannelResolver(); + BeanFactory beanFactory = mock(BeanFactory.class); + doAnswer(new Answer(){ + + @Override + public Object answer(InvocationOnMock invocation) throws Throwable { + throw new NoSuchBeanDefinitionException("bar"); + } + }).when(beanFactory).getBean("foo", MessageChannel.class); + resolver.setBeanFactory(beanFactory); + try { + resolver.resolveChannelName("foo"); + fail("Expected exception"); + } + catch (ChannelResolutionException e){ + assertThat(e.getMessage(), + Matchers.equalTo("failed to look up MessageChannel with name 'foo' in the BeanFactory (and there is no HeaderChannelRegistry present).")); + } + } + public static class Foo extends AbstractReplyProducingMessageHandler { @Override