diff --git a/org.springframework.integration/src/main/java/org/springframework/integration/config/SourcePollingChannelAdapterFactoryBean.java b/org.springframework.integration/src/main/java/org/springframework/integration/config/SourcePollingChannelAdapterFactoryBean.java index 7bdc6e9f87..94d65c3b25 100644 --- a/org.springframework.integration/src/main/java/org/springframework/integration/config/SourcePollingChannelAdapterFactoryBean.java +++ b/org.springframework.integration/src/main/java/org/springframework/integration/config/SourcePollingChannelAdapterFactoryBean.java @@ -23,6 +23,7 @@ import org.springframework.beans.factory.BeanNameAware; import org.springframework.beans.factory.FactoryBean; import org.springframework.beans.factory.InitializingBean; import org.springframework.beans.factory.config.ConfigurableBeanFactory; +import org.springframework.context.Lifecycle; import org.springframework.integration.context.IntegrationContextUtils; import org.springframework.integration.core.MessageChannel; import org.springframework.integration.endpoint.SourcePollingChannelAdapter; @@ -35,7 +36,8 @@ import org.springframework.util.Assert; * * @author Mark Fisher */ -public class SourcePollingChannelAdapterFactoryBean implements FactoryBean, BeanFactoryAware, BeanNameAware, BeanClassLoaderAware, InitializingBean { +public class SourcePollingChannelAdapterFactoryBean + implements FactoryBean, BeanFactoryAware, BeanNameAware, BeanClassLoaderAware, InitializingBean, Lifecycle { private volatile MessageSource source; @@ -138,4 +140,27 @@ public class SourcePollingChannelAdapterFactoryBean implements FactoryBean, Bean } } + /* + * Lifecycle implementation (delegates to the created adapter). + */ + + public boolean isRunning() { + if (this.adapter == null) { + return false; + } + return this.adapter.isRunning(); + } + + public void start() { + if (this.adapter != null) { + this.adapter.start(); + } + } + + public void stop() { + if (this.adapter != null) { + this.adapter.stop(); + } + } + } diff --git a/org.springframework.integration/src/test/java/org/springframework/integration/config/ChannelAdapterParserTests-context.xml b/org.springframework.integration/src/test/java/org/springframework/integration/config/ChannelAdapterParserTests-context.xml index 992980da67..e1d37b6d0c 100644 --- a/org.springframework.integration/src/test/java/org/springframework/integration/config/ChannelAdapterParserTests-context.xml +++ b/org.springframework.integration/src/test/java/org/springframework/integration/config/ChannelAdapterParserTests-context.xml @@ -8,7 +8,7 @@ http://www.springframework.org/schema/integration/spring-integration-1.0.xsd"> - + diff --git a/org.springframework.integration/src/test/java/org/springframework/integration/config/ChannelAdapterParserTests.java b/org.springframework.integration/src/test/java/org/springframework/integration/config/ChannelAdapterParserTests.java index b4fda1ff9d..be29f7a040 100644 --- a/org.springframework.integration/src/test/java/org/springframework/integration/config/ChannelAdapterParserTests.java +++ b/org.springframework.integration/src/test/java/org/springframework/integration/config/ChannelAdapterParserTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2008 the original author or authors. + * Copyright 2002-2009 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. @@ -26,6 +26,7 @@ import org.junit.Before; import org.junit.Test; import org.springframework.context.support.AbstractApplicationContext; +import org.springframework.context.support.ClassPathXmlApplicationContext; import org.springframework.integration.channel.BeanFactoryChannelResolver; import org.springframework.integration.channel.ChannelResolutionException; import org.springframework.integration.channel.DirectChannel; @@ -35,26 +36,45 @@ import org.springframework.integration.core.MessageChannel; import org.springframework.integration.endpoint.EventDrivenConsumer; import org.springframework.integration.endpoint.SourcePollingChannelAdapter; import org.springframework.integration.message.StringMessage; -import org.springframework.test.context.ContextConfiguration; -import org.springframework.test.context.junit4.AbstractJUnit4SpringContextTests; /** * @author Mark Fisher */ -@ContextConfiguration -public class ChannelAdapterParserTests extends AbstractJUnit4SpringContextTests { +public class ChannelAdapterParserTests { + + private AbstractApplicationContext applicationContext; + @Before - public void startContext() { - ((AbstractApplicationContext) this.applicationContext).start(); + public void setUp() { + this.applicationContext = new ClassPathXmlApplicationContext( + "ChannelAdapterParserTests-context.xml", this.getClass()); } @After - public void stopContext() { - ((AbstractApplicationContext) this.applicationContext).stop(); + public void tearDown() { + this.applicationContext.close(); } + @Test + public void methodInvokingSourceStoppedByApplicationContext() { + String beanName = "methodInvokingSource"; + PollableChannel channel = (PollableChannel) this.applicationContext.getBean("queueChannel"); + TestBean testBean = (TestBean) this.applicationContext.getBean("testBean"); + testBean.store("source test"); + Object adapter = this.applicationContext.getBean(beanName); + assertNotNull(adapter); + assertTrue(adapter instanceof SourcePollingChannelAdapter); + this.applicationContext.start(); + Message message = channel.receive(1000); + assertNotNull(message); + assertEquals("source test", testBean.getMessage()); + this.applicationContext.stop(); + message = channel.receive(100); + assertNull(message); + } + @Test public void targetOnly() { String beanName = "outboundWithImplicitChannel"; @@ -93,6 +113,35 @@ public class ChannelAdapterParserTests extends AbstractJUnit4SpringContextTests @Test public void methodInvokingSource() { + String beanName = "methodInvokingSource"; + PollableChannel channel = (PollableChannel) this.applicationContext.getBean("queueChannel"); + TestBean testBean = (TestBean) this.applicationContext.getBean("testBean"); + testBean.store("source test"); + Object adapter = this.applicationContext.getBean(beanName); + assertNotNull(adapter); + assertTrue(adapter instanceof SourcePollingChannelAdapter); + ((SourcePollingChannelAdapter) adapter).start(); + Message message = channel.receive(100); + assertNotNull(message); + assertEquals("source test", testBean.getMessage()); + ((SourcePollingChannelAdapter) adapter).stop(); + } + + @Test + public void methodInvokingSourceNotStarted() { + String beanName = "methodInvokingSource"; + PollableChannel channel = (PollableChannel) this.applicationContext.getBean("queueChannel"); + TestBean testBean = (TestBean) this.applicationContext.getBean("testBean"); + testBean.store("source test"); + Object adapter = this.applicationContext.getBean(beanName); + assertNotNull(adapter); + assertTrue(adapter instanceof SourcePollingChannelAdapter); + Message message = channel.receive(100); + assertNull(message); + } + + @Test + public void methodInvokingSourceStopped() { String beanName = "methodInvokingSource"; PollableChannel channel = (PollableChannel) this.applicationContext.getBean("queueChannel"); TestBean testBean = (TestBean) this.applicationContext.getBean("testBean"); @@ -105,6 +154,24 @@ public class ChannelAdapterParserTests extends AbstractJUnit4SpringContextTests assertNotNull(message); assertEquals("source test", testBean.getMessage()); ((SourcePollingChannelAdapter) adapter).stop(); + message = channel.receive(100); + assertNull(message); + } + + @Test + public void methodInvokingSourceStartedByApplicationContext() { + String beanName = "methodInvokingSource"; + PollableChannel channel = (PollableChannel) this.applicationContext.getBean("queueChannel"); + TestBean testBean = (TestBean) this.applicationContext.getBean("testBean"); + testBean.store("source test"); + Object adapter = this.applicationContext.getBean(beanName); + assertNotNull(adapter); + assertTrue(adapter instanceof SourcePollingChannelAdapter); + this.applicationContext.start(); + Message message = channel.receive(1000); + assertNotNull(message); + assertEquals("source test", testBean.getMessage()); + this.applicationContext.stop(); } @Test(expected = ChannelResolutionException.class)